o
    Zh-                     @   sn  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZmZmZmZ d dlmZ edZe ZedZee ee eej d	e_ed
ZedZ G dd de!Z"ddee# de$fddZ%ddee# de#fddZ&dee$ de$fddZ'G dd dZ(	ddee( dedeeee f geeee  f f fddZ)dS )    N)Sequence)Lock)default_timer)AnyCallableOptionalTypeVar)	ParamSpecZstrobelight_function_profilerzB%(name)s, line %(lineno)d, %(asctime)s, %(levelname)s: %(message)sF_P_Rc                   @   s   e Zd ZdZdS )StrobelightCLIProfilerErrorzC
    Raised when an error happens during strobelight profiling
    N)__name__
__module____qualname____doc__ r   r   W/var/www/auris/lib/python3.10/site-packages/torch/_strobelight/cli_function_profiler.pyr       s    r   pidreturnc                 C   s    d}| pt  } t || S )zFReturns the link to the process's namespace, example: pid:[4026531836]z/proc/{}/ns/pid)osgetpidreadlinkformat)r   ZPID_NAMESPACE_PATHr   r   r   _pid_namespace_link&   s   r   c                 C   s.   | pt  } t| }t||dd d S )z"Returns the process's namespace id[   )r   r   r   intfind)r   linkr   r   r   _pid_namespace-   s   r    commandc                 C   s
   d | S )N )join)r!   r   r   r   _command_to_string4   s   
r$   c                   @   s   e Zd ZdZe Zddddddddddd	
d
ededede	dedede
ee	  de
ee	  dedefddZd+ddZd,deddfddZd+ddZd+dd Zd!eddfd"d#Zdefd$d%Zd&eeef d'ejd(ejde
e fd)d*ZdS )-StrobelightCLIFunctionProfilera  
    Note: this is a Meta only tool.

    StrobelightCLIFunctionProfiler can be used to profile a python function and
    generate a strobelight link with the results. It works on meta servers but
    does not requries an fbcode target.
    When stop_at_error is false(default), error during profiling does not prevent
    the work function from running.

    Check function_profiler_example.py for an example.
    FiX  g    cAzpytorch-strobelight-ondemand<   N   )
stop_at_errormax_profile_duration_secsample_eachrun_user_nametimeout_wait_for_running_sectimeout_wait_for_finished_secrecorded_env_variablessample_tagsstack_max_lenasync_stack_max_lenr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   c       
         C   s:   || _ || _|| _|| _|| _|| _d | _d | _|| _d S N)	r(   r)   r*   r+   r,   r-   current_run_idprofile_resultr/   )selfr(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r   r   r   __init__H   s   
z'StrobelightCLIFunctionProfiler.__init__r   c                 C   s   t  }t|}ddddddddt| j d	t| jd
  d| d| g}| jr7|d |d| j t	
dt| tj|dd}|jd}t	
d| |jdkr^td| td| }rpt|d| _d S td| )Nstrobeclientrunz
--profilerZpyperfz--eventZcyclesz--asyncz--sample-intervalz--duration-msi  z--pid:z--sample-tags,running command: %sTcapture_outpututf-8output:
{%s}r   z:failed to start strobelight profiling, error in run_async:zINFO Run Id: (-?\d+)r   z9failed to start strobelight profiling, unexpected result )r   r   r    r   r*   r)   r/   appendr#   loggerdebugr$   
subprocessr8   stderrdecode
returncoder   researchgroupr3   )r5   Z	processId	namespacer!   resultoutputmatchr   r   r   
_run_asyncb   sB   


z)StrobelightCLIFunctionProfiler._run_asyncr   counterc                 C   s   |dkrt dddd| j g}tdt| tj|dd}|jd	}td
| |j	dkr7t d| t
d| }rd|d}|dkrJd S |dkr\td | |d  d S t d| dt d| d)N   z*wait_for_running called more than 20 timesr7   getRunStatus--run-idr;   Tr<   r>   r?   r   zAfailed to start strobelight profiling, error in wait_for_running:zProfile run status: (.*)r   RUNNINGZ	PREPARING
   zunexpected z phaseunexpected output
: r"   )r   r3   rA   rB   r$   rC   r8   rD   rE   rF   rG   rH   rI   timesleep_wait_for_running)r5   rO   r!   rK   rL   rM   current_statusr   r   r   rX      s.   


z0StrobelightCLIFunctionProfiler._wait_for_runningc                 C   s   dddt | jg}tdt| tj|dd}|jd}td| |j	d	kr0t
d
| td| }rL|d}|drDd S t
d| dt
d| d)Nr7   ZstopRunrR   r;   Tr<   r>   r?   r   z<failed to stop strobelight profiling, return code is not 0 :INFO ::1:(.*)r   zSuccess!z*failed to stop strobelight profiling, got z resultrU   r"   )strr3   rA   rB   r$   rC   r8   rD   rE   rF   r   rG   rH   rI   __contains__)r5   r!   rK   rL   rM   rY   r   r   r   	_stop_run   s"   



z(StrobelightCLIFunctionProfiler._stop_runc                 C   s   dddt | jg}tdt| tj|dd}|jd}td| |j	d	kr0t
d
| td| }rY|d}|drMtd |   d S |dsYt
d| g | _td|D ]}|  j|d	 7  _t|d	  qbd S )Nr7   rQ   rR   r;   Tr<   r>   r?   r   z<failed to extract profiling results, return code is not 0 : rZ   r   zProfile run status: PROCESSINGrT   z!Profile run finished with SUCCESSz9failed to extract profiling results, unexpected response zF(Total samples(.*)|GraphProfiler(.*)|Icicle view \(python stack\)(.*)))r[   r3   rA   rB   r$   rC   r8   rD   rE   rF   r   rG   rH   rI   r\   rV   rW   _get_resultsr4   findallinfo)r5   r!   rK   rL   rM   rY   itemr   r   r   r^      s6   




z+StrobelightCLIFunctionProfiler._get_resultscollect_resultsc                 C   sX   z|    td td |sW d S |   W d S  ty+   tjddd Y d S w )Nzstrobelight profiling stoppedzcollection stoppedzerror during stop_strobelightTexc_info)r]   rA   r`   rB   r^   	Exceptionwarning)r5   rb   r   r   r   _stop_strobelight_no_throw   s   

z9StrobelightCLIFunctionProfiler._stop_strobelight_no_throwc                 C   sj   d}z|    d}td| j |   td W dS  ty4   tjddd |r1| jdd Y dS w )NFTzstrobelight run id is: %szstrobelight profiling runningzerror during start_strobelight:rc   rb   )rN   rA   r`   r3   rX   re   rf   rg   )r5   Zstrobelight_startedr   r   r   _start_strobelight   s   
z1StrobelightCLIFunctionProfiler._start_strobelightwork_functionargskwargsc              
   O   s   d | _ d | _tjd }r|s#| jrtdtd ||i |S | 	 }|sC| jr5tj
  td||i |}tj
  |S z*td t }||i |}t }|| }	td|	 | jdd tj
  |W S  ty }
 ztjddd	 | jdd tj
  |
d }
~
ww d S )
NFzconcurrent runs not supportedz%failed to start strobelight profilingzcollection startedzwork function took %s secondsTrh   zwork function throw exceptionrc   )r3   r4   r%   _lockacquirer(   r   rA   rf   ri   releaserB   timerr`   rg   re   )r5   rj   rk   rl   lockedstartedrK   startend
total_timeerrorr   r   r   profile  sH   





z&StrobelightCLIFunctionProfiler.profile)r   N)r   )r   r   r   r   r   rm   boolr   floatr[   r   listr6   rN   rX   r]   r^   rg   ri   r   r
   r   rk   rl   rw   r   r   r   r   r%   8   sj    
	



)



r%   profilerrl   c                    s@    s	t di | dtttf dtttt f f fdd}|S )Nrj   r   c                    s2   t  dtjdtjdtt f fdd}|S )Nrk   rl   r   c                     s    j g| R i |S r2   )rw   )rk   rl   )r{   rj   r   r   wrapper_function;  s   z@strobelight.<locals>.strobelight_inner.<locals>.wrapper_function)	functoolswrapsr
   rk   rl   r   r   )rj   r|   r{   )rj   r   strobelight_inner8  s   &z&strobelight.<locals>.strobelight_innerr   )r%   r   r
   r   r   )r{   rl   r   r   r   r   strobelight2  s   
	r   r2   )*r}   loggingr   rG   rC   rV   collections.abcr   	threadingr   Ztimeitr   rp   typingr   r   r   r   Ztyping_extensionsr	   	getLoggerrA   StreamHandlerZconsole_handler	Formatter	formattersetFormatter
addHandlersetLevelINFO	propagater
   r   re   r   r   r[   r   r    r$   r%   r   r   r   r   r   <module>   sH   


 | 