
    %Vji-                        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j        d          Z ej                    Z ej        d          Ze                    e           e                    e           e                    ej                   d	e_         ed
          Z ed          Z  G d de!          Z"ddee#         de$fdZ%ddee#         de#fdZ&dee$         de$fdZ' G d d          Z(	 ddee(         dedeeee f         geeee          f         f         fdZ)dS )    N)Sequence)Lock)default_timer)AnyCallableOptionalTypeVar)	ParamSpecstrobelight_function_profilerzB%(name)s, line %(lineno)d, %(asctime)s, %(levelname)s: %(message)sF_P_Rc                       e Zd ZdZdS )StrobelightCLIProfilerErrorzC
    Raised when an error happens during strobelight profiling
    N)__name__
__module____qualname____doc__     b/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/_strobelight/cli_function_profiler.pyr   r       s           r   r   pidreturnc                 ~    d}| pt          j                    } t          j        |                    |                     S )zFReturns the link to the process's namespace, example: pid:[4026531836]z/proc/{}/ns/pid)osgetpidreadlinkformat)r   PID_NAMESPACE_PATHs     r   _pid_namespace_linkr   &   s7    *

C;)0055666r   c                     | pt          j                    } t          |           }t          ||                    d          dz   d                   S )z"Returns the process's namespace id[   )r   r   r   intfind)r   links     r   _pid_namespacer'   -   sF    

Cs##DtDIIcNNQ&+,---r   commandc                 ,    d                     |           S )N )join)r(   s    r   _command_to_stringr,   4   s    88Gr   c                   ,   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Zd#dZd$deddfdZd#dZd#dZdeddfdZdefdZdeeef         d ej        d!ej        de
e         f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 requires 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_lenr1   r2   r3   r4   r5   r6   r7   r8   r9   r:   c       
             || _         || _        || _        || _        || _        || _        d | _        d | _        || _        d S N)	r1   r2   r3   r4   r5   r6   current_run_idprofile_resultr8   )selfr1   r2   r3   r4   r5   r6   r7   r8   r9   r:   s              r   __init__z'StrobelightCLIFunctionProfiler.__init__H   sT     +(@%&*,H)-J* .237&r   r   c                    t          j                    }t          |          }ddddddddt          | j                   d	t          | j        d
z             d| d| g}| j        rB|                    d           |                    d                    | j                             t          
                    dt          |                     t          j        |d          }|j                            d          }t          
                    d|           |j        dk    rt#          d|           t%          j        d|          x}r)t          |                    d                    | _        d S t#          d|           )Nstrobeclientrunz
--profilerpyperfz--event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$   r3   r2   r8   appendr+   loggerdebugr,   
subprocessrC   stderrdecode
returncoder   researchgroupr=   )r?   	processId	namespacer(   resultoutputmatchs          r   
_run_asyncz)StrobelightCLIFunctionProfiler._run_asyncb   s   IKK	"9--	4#$$&404788:&&9&&
   	7NN?+++NN388D$455666*,>w,G,GHHH===%%g.._f---!!-UVUU   I5v>>>5 	"%ekk!nn"5"5DF)PPP
 
 	
r   r   counterc                    |dk    rt          d          ddd| j         g}t                              dt	          |                     t          j        |d          }|j                            d	          }t                              d
|           |j	        dk    rt          d|           t          j        d|          x}rd|                    d          }|dk    rd S |dk    r.t          j        d           |                     |dz              d S t          d| d          t          d| d          )N   z*wait_for_running called more than 20 timesrB   getRunStatus--run-idrH   TrI   rK   rL   r   zAfailed to start strobelight profiling, error in wait_for_running:zProfile run status: (.*)r"   RUNNING	PREPARING
   zunexpected z phaseunexpected output
: r*   )r   r=   rN   rO   r,   rP   rC   rQ   rR   rS   rT   rU   rV   timesleep_wait_for_running)r?   r]   r(   rY   rZ   r[   current_statuss          r   rh   z0StrobelightCLIFunctionProfiler._wait_for_running   sb   R<<-<   ">:$BU?WX*,>w,G,GHHH===%%g.._f---!!-\TZ\\   I8&AAA5 		X"[[^^N**;..
2&&w{33312V2V2V2VWWW)*K&*K*K*KLLLr   c                 $   dddt          | j                  g}t                              dt	          |                     t          j        |d          }|j                            d          }t                              d|           |j	        d	k    rt          d
|           t          j        d|          x}r?|                    d          }|                    d          rd S t          d| d          t          d| d          )NrB   stopRunra   rH   TrI   rK   rL   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 resultre   r*   )strr=   rN   rO   r,   rP   rC   rQ   rR   rS   r   rT   rU   rV   __contains__)r?   r(   rY   rZ   r[   ri   s         r   	_stop_runz(StrobelightCLIFunctionProfiler._stop_run   s"   !9j#d>Q:R:RS*,>w,G,GHHH===%%g.._f---!!-WvWW   Iov6665 	"[[^^N**:66 1XXXX   **K&*K*K*KLLLr   c                 $   dddt          | j                  g}t                              dt	          |                     t          j        |d          }|j                            d          }t                              d|           |j	        d	k    rt          d
|           t          j        d|          x}r{|                    d          }|                    d          r*t          j        d           |                                  d S |                    d          st          d|           g | _        t          j        d|          D ]8}| xj        |d	         z  c_        t                              |d	                    9d S )NrB   r`   ra   rH   TrI   rK   rL   r   z<failed to extract profiling results, return code is not 0 : rl   r"   zProfile run status: PROCESSINGrd   z!Profile run finished with SUCCESSz9failed to extract profiling results, unexpected response zF(Total samples(.*)|GraphProfiler(.*)|Icicle view \(python stack\)(.*)))rm   r=   rN   rO   r,   rP   rC   rQ   rR   rS   r   rT   rU   rV   rn   rf   rg   _get_resultsr>   findallinfo)r?   r(   rY   rZ   r[   ri   items          r   rq   z+StrobelightCLIFunctionProfiler._get_results   s   !>:s4CV?W?WX*,>w,G,GHHH===%%g.._f---!!-WvWW   Iov6665 		"[[^^N**+KLL 
2!!####001TUU 1XPVXX   !JU
 
 	! 	!D 47*KKQ    	! 	!r   collect_resultsc                 "   	 |                                   t                              d           t                              d           |sd S |                                  d S # t
          $ r  t                              dd           Y d S w xY w)Nzstrobelight profiling stoppedzcollection stoppedzerror during stop_strobelightTexc_info)ro   rN   rs   rO   rq   	Exceptionwarning)r?   ru   s     r   _stop_strobelight_no_throwz9StrobelightCLIFunctionProfiler._stop_strobelight_no_throw   s    	KNNKK7888LL-..."  	K 	K 	KNN:TNJJJJJJ	Ks   A
A$ A$ $&BBc                 ^   d}	 |                                   d}t                              d| j                   |                                  t                              d           dS # t
          $ r8 t                              dd           |r|                     d           Y dS w xY w)NFTzstrobelight run id is: %szstrobelight profiling runningzerror during start_strobelight:rw   ru   )r\   rN   rs   r=   rh   ry   rz   r{   )r?   strobelight_starteds     r   _start_strobelightz1StrobelightCLIFunctionProfiler._start_strobelight   s    #	OO"&KK3T5HIII""$$$KK78884 	 	 	NN<tNLLL" G///FFF55		s   A$A* *>B,+B,work_functionargskwargsc                    d | _         d | _        t          j                            d          x}r|s8| j        rt          d          t                              d            ||i |S | 	                                }|s\| j        r-t          j        
                                 t          d           ||i |}t          j        
                                 |S 	 t                              d           t                      } ||i |}t                      }||z
  }	t                              d|	           |                     d           t          j        
                                 |S # t          $ rW}
t                              dd	           |                     d           t          j        
                                 |
d }
~
ww xY wd S )
NFzconcurrent runs not supportedz%failed to start strobelight profilingzcollection startedzwork function took %s secondsTr}   zwork function throw exceptionrw   )r=   r>   r.   _lockacquirer1   r   rN   rz   r   releaserO   timerrs   r{   ry   )r?   r   r   r   lockedstartedrY   startend
total_timeerrors              r   profilez&StrobelightCLIFunctionProfiler.profile  s    #"39AA%HHH6 !	 6% W56UVVV>???$}d5f555--//G % 28@@BBB5?   '777.4<<>>>1222&777gg 5[
;ZHHH///EEE.4<<>>>   >NNN///FFF.4<<>>>	
 ts   BE2 2
G<AGG)r   N)r   )r   r   r   r   r   r   boolr$   floatrm   r   listr@   r\   rh   ro   rq   r{   r   r   r   r   r   r   r   r   r   r   r.   r.   8   s       
 
 DFFE
 $(/ ;,.-/6:+/ #&' ' ' ' #&	'
 ' ' '*' (+' !)c 3' d3i(' ' !' ' ' '4'
 '
 '
 '
RM M MT M M M M<M M M M.! ! ! !>KK 
K K K K&D     (%b"f-(68g(IK(	"( ( ( ( ( (r   r.   profilerr   c                       st          di | dt          t          t          f         dt          t          t          t                   f         f fd}|S )Nr   r   c                      t          j                   dt          j        dt          j        dt
          t                   f fd            }|S )Nr   r   r   c                  &     j         g| R i |S r<   )r   )r   r   r   r   s     r   wrapper_functionz@strobelight.<locals>.strobelight_inner.<locals>.wrapper_function;  s&    #8#MCDCCCFCCCr   )	functoolswrapsr   r   r   r   r   )r   r   r   s   ` r   strobelight_innerz&strobelight.<locals>.strobelight_inner8  sm     
	'	'	DBG 	Dry 	DXb\ 	D 	D 	D 	D 	D 	D 
(	'	D  r   r   )r.   r   r   r   r   )r   r   r   s   `  r   strobelightr   2  sm      <1;;F;; B' 	"hrl"	#            r   r<   )*r   loggingr   rT   rP   rf   collections.abcr   	threadingr   timeitr   r   typingr   r   r   r	   typing_extensionsr
   	getLoggerrN   StreamHandlerconsole_handler	Formatter	formattersetFormatter
addHandlersetLevelINFO	propagater   r   ry   r   r$   rm   r   r'   r,   r.   r   r   r   r   <module>r      s        				 				      $ $ $ $ $ $       ) ) ) ) ) ) 3 3 3 3 3 3 3 3 3 3 3 3 ' ' ' ' ' ' 
	:	;	;'''))GH 	   Y ' ' '   / " " "     Yt__WT]]    )   7 7Xc] 7c 7 7 7 7. . . . . . . #    r r r r r r r rv :> 56ILxB (2x|+;"<<=     r   