o
    1 iR"                     @   s   d dl Z d dlZd dlmZ d dlmZmZ d dlmZm	Z	m
Z
 d dlZG dd dZdejv Ze Zedd	 d
ddddddddddddZeddG dd dZeddG dd dZd!ddZde	e defdd ZdS )"    N)defaultdict)asdict	dataclass)DictListUnionc                   @   s    e Zd ZdZdd Zdd ZdS )_NullLogSpanz,A log span context manager that does nothingc                 C      d S N )selfr   r   b/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/_private/profiling.py	__enter__      z_NullLogSpan.__enter__c                 C   r	   r
   r   )r   typevaluetbr   r   r   __exit__   r   z_NullLogSpan.__exit__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   
   s    r   ZRAY_PROFILINGc                   C   s   dS )NZgeneric_workr   r   r   r   r   <lambda>   s    r   Zcq_build_abandonedZrail_responseZ	rail_loadZrail_animationZ	rail_idleZdetailed_memory_dumpZgoodZterribleZvsync_highlight_colorZbackground_memory_dump)Zworker_idletaskztask:deserialize_argumentsztask:executeztask:store_outputsZwait_for_functionzray.getzray.putzray.waitZsubmit_taskZfetch_and_run_functionZregister_remote_functionT)initc                   @   sj   e Zd ZU eed< eed< eed< eed< eed< eed< eed< eeeeef f ed< d	Zeed
< dS )ChromeTracingCompleteEventcatnamepidtidtsdurcnameargsXphN)	r   r   r   str__annotations__intr   r   r%   r   r   r   r   r   ,   s   
 r   c                   @   sF   e Zd ZU eed< eeef ed< eed< dZeed< dZeed< dS )ChromeTracingMetadataEventr   r#   r   Nr   Mr%   )	r   r   r   r&   r'   r   r(   r   r%   r   r   r   r   r)   F   s   
 r)   c                 C   s:   t stS tjjj}|jtjjjkrtS |j	| 
d|S )aF  Profile a span of time so that it appears in the timeline visualization.

    Note that this only works in the raylet code path.

    This function can be used as follows (both on the driver or within a task).

    .. testcode::
        import ray._private.profiling as profiling

        with profiling.profile("custom event", extra_data={'key': 'val'}):
            # Do some computation here.
            x = 1 * 2

    Optionally, a dictionary can be passed as the "extra_data" argument, and
    it can have keys "name" and "cname" if you want to override the default
    timeline display text and box color. Other values will appear at the bottom
    of the chrome tracing GUI when you click on the box corresponding to this
    profile span.

    Args:
        event_type: A string describing the type of the event.
        extra_data: This must be a dictionary mapping strings to strings. This
            data will be added to the json objects that are used to populate
            the timeline, so if you want to set a particular color, you can
            simply set the "cname" attribute to an appropriate color.
            Similarly, if you set the "name" attribute, then that will set the
            text displayed on the box in the timeline.

    Returns:
        An object that can profile a span of time via a "with" statement.
    ascii)PROFILING_ENABLEDNULL_LOG_SPANrayZ_privateworkerZglobal_workermodeZ
LOCAL_MODEZcore_workerZprofile_eventencode)Z
event_type
extra_datar/   r   r   r   profileT   s    
r3   tasksreturnc                 C   s  g }i }d}i }d}| D ]}| dg }|r|d }|d }	|d }
|
d |d  }|
dvr/q|	D ]}|d	 }|d
 |d
< |d |d< |d |d< |d |d< |d |d< |d }||vre|||< |d7 }|| |f|vry|||| |f< |d7 }t|d  }|}d|v rt|d	 d  }d|v r|d }t|||| ||| |f |d d |d d |d d  ||d}|t| q1q| D ]\}}|ttd|dd| id q| D ]\}}|ttdd||d d|d id qt|S )zGenerate a chrome/perfetto tracing dump using task events.

    Args:
        tasks: List of tasks generated by a state API list_tasks(detail=True).

    Returns:
        Json serialized dump to create a chrome/perfetto tracing.
    r   profiling_datanode_ip_addresseventscomponent_type:component_id)r/   Zdriverr2   task_idZjob_idattempt_numberZfunc_or_class_nameZactor_id
event_name   r"   r   
start_timeg     @@end_time)r   r   r   r   r    r!   r"   r#   Zprocess_namezNode )r   r   r#   Zthread_namer*   )r   r%   r   r   r#   )	get_default_color_mappingr   appendr   itemsr)   jsondumps)r4   Z
all_eventsZnode_to_indexZnode_idxZworker_to_indexZ
worker_idxr   r6   r7   Zcomponent_eventsr9   r;   eventr2   r>   r"   r   Z	new_eventnodeir/   r   r   r   chrome_tracing_dump|   s   





rK   r
   )rF   oscollectionsr   dataclassesr   r   typingr   r   r   r.   r   environr,   r-   rC   r   r)   r3   dictr&   rK   r   r   r   r   <module>   sF    


(