
    sh-                     P   S SK r S SKrS SKrS SKrS SKrS SKrS SKJr  S SKJ	r	  S SK
Jr  S SKJrJrJrJr  S SKJr  \R(                  " S5      r\R,                  " 5       r\R0                  " S5      r\R5                  \5        \R7                  \5        \R9                  \R:                  5        S	\l        \" S
5      r\" S5      r  " S S\!5      r"SS\\#   S\$4S jjr%SS\\#   S\#4S jjr&S\\$   S\$4S jr' " S S5      r( SS\\(   S\S\\\\ 4   /\\\\    4   4   4S jjr)g)    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                       \ rS rSrSrSrg)StrobelightCLIProfilerError    z;
Raised when an error happens during strobelight profiling
 N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       |/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/_strobelight/cli_function_profiler.pyr   r       s    r   r   pidreturnc                     SnU =(       d    [         R                  " 5       n [         R                  " UR                  U 5      5      $ )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"   &   s3    *

C;;)00566r   c                     U =(       d    [         R                  " 5       n [        U 5      n[        XR	                  S5      S-   S 5      $ )z"Returns the process's namespace id[   )r   r   r"   intfind)r   links     r   _pid_namespacer*   -   s9    

Cs#DtIIcNQ&,--r   commandc                 $    SR                  U 5      $ )N )join)r+   s    r   _command_to_stringr/   4   s    88Gr   c                   0   \ rS rSrSr\" 5       rSSSSSSSSS	S	S
.
S\S\S\	S\
S\S\S\\\
      S\\\
      S\S\4S jjrS$S jrS%S\SS4S jjrS$S jrS$S jrS\SS4S jrS\4S jrS\\\4   S \R.                  S!\R0                  S\\   4S" jrS#rg)&StrobelightCLIFunctionProfiler8   a  
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_lenr5   r6   r7   r8   r9   r:   r;   r<   r=   r>   c       
         t    Xl         X l        X0l        X@l        XPl        X`l        S U l        S U l        Xl        g N)	r5   r6   r7   r8   r9   r:   current_run_idprofile_resultr<   )selfr5   r6   r7   r8   r9   r:   r;   r<   r=   r>   s              r   __init__'StrobelightCLIFunctionProfiler.__init__H   s@     +(@%&*,H)-J* .237&r   r   c                    [         R                  " 5       n[        U5      nSSSSSSSS[        U R                  5       S	[        U R
                  S
-  5       SU SU 3/nU R                  (       a;  UR                  S5        UR                  SR                  U R                  5      5        [        R                  S[        U5      5        [        R                  " USS9nUR                  R                  S5      n[        R                  SU5        UR                   S:w  a  [#        SU 35      e[$        R&                  " SU5      =n(       a   [        UR)                  S5      5      U l        g [#        SU 35      e)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'   r7   r6   r<   appendr.   loggerdebugr/   
subprocessrH   stderrdecode
returncoder   researchgrouprA   )rC   	processId	namespacer+   resultoutputmatchs          r   
_run_async)StrobelightCLIFunctionProfiler._run_asyncb   sW   IIK	"9-	4##$%4004789k9+&
  NN?+NN388D$4$456*,>w,GH=%%g._f-!-LVHU  II5v>>5>"%ekk!n"5D)GxP
 	
r   counterc                 V   US:  a  [        S5      eSSSU R                   /n[        R                  S[	        U5      5        [
        R                  " USS9nUR                  R                  S	5      n[        R                  S
U5        UR                  S:w  a  [        SU 35      e[        R                  " SU5      =n(       aX  UR                  S5      nUS:X  a  g US:X  a+  [        R                  " S5        U R                  US-   5        g [        SU S35      e[        SU S35      e)N   z*wait_for_running called more than 20 timesrG   getRunStatus--run-idrM   TrN   rP   rQ   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   rA   rS   rT   r/   rU   rH   rV   rW   rX   rY   rZ   r[   timesleep_wait_for_running)rC   rc   r+   r^   r_   r`   current_statuss          r   rn   0StrobelightCLIFunctionProfiler._wait_for_running   s#   R<-<  ">:$BUBUAVX*,>w,GH=%%g._f-!-STZS[\  II8&AA5A"[[^N*;.

2&&w{31K?Ov2VWW),A&*KLLr   c                    SSS[        U R                  5      /n[        R                  S[	        U5      5        [
        R                  " USS9nUR                  R                  S5      n[        R                  SU5        UR                  S	:w  a  [        S
U 35      e[        R                  " SU5      =n(       a7  UR                  S5      nUR                  S5      (       a  g [        SU S35      e[        SU S35      e)NrG   stopRunrg   rM   TrN   rP   rQ   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 resultrk   r-   )strrA   rS   rT   r/   rU   rH   rV   rW   rX   r   rY   rZ   r[   __contains__)rC   r+   r^   r_   r`   ro   s         r   	_stop_run(StrobelightCLIFunctionProfiler._stop_run   s    !9j#d>Q>Q:RS*,>w,GH=%%g._f-!-NvhW  IIov6656"[[^N**:661@@PPWX  *,A&*KLLr   c                    SSS[        U R                  5      /n[        R                  S[	        U5      5        [
        R                  " USS9nUR                  R                  S5      n[        R                  SU5        UR                  S	:w  a  [        S
U 35      e[        R                  " SU5      =n(       ar  UR                  S5      nUR                  S5      (       a'  [        R                   " S5        U R#                  5         g UR                  S5      (       d  [        SU 35      e/ U l        [        R&                  " SU5       H3  nU =R$                  US	   -  sl        [        R)                  US	   5        M5     g )NrG   rf   rg   rM   TrN   rP   rQ   r   z<failed to extract profiling results, return code is not 0 : rs   r%   zProfile run status: PROCESSINGrj   z!Profile run finished with SUCCESSz9failed to extract profiling results, unexpected response zF(Total samples(.*)|GraphProfiler(.*)|Icicle view \(python stack\)(.*)))rt   rA   rS   rT   r/   rU   rH   rV   rW   rX   r   rY   rZ   r[   ru   rl   rm   _get_resultsrB   findallinfo)rC   r+   r^   r_   r`   ro   items          r   ry   +StrobelightCLIFunctionProfiler._get_results   sL   !>:s4CVCV?WX*,>w,GH=%%g._f-!-NvhW  IIov6656"[[^N**+KLL

2!!##001TUU1OPVxX  !JJU
D 47*KKQ 
r   collect_resultsc                      U R                  5         [        R                  S5        [        R                  S5        U(       d  g U R	                  5         g ! [
         a    [        R                  SSS9   g f = f)Nzstrobelight profiling stoppedzcollection stoppedzerror during stop_strobelightTexc_info)rv   rS   r{   rT   ry   	Exceptionwarning)rC   r~   s     r   _stop_strobelight_no_throw9StrobelightCLIFunctionProfiler._stop_strobelight_no_throw   s`    	KNNKK78LL-." 	KNN:TNJ	Ks   AA A A65A6c                 ,   Sn U R                  5         Sn[        R                  SU R                  5        U R	                  5         [        R                  S5        g! [
         a-    [        R                  SSS9  U(       a  U R                  SS9   gf = f)NFTzstrobelight run id is: %szstrobelight profiling runningzerror during start_strobelight:r   r~   )ra   rS   r{   rA   rn   r   r   r   )rC   strobelight_starteds     r   _start_strobelight1StrobelightCLIFunctionProfiler._start_strobelight   s    #	OO"&KK3T5H5HI""$KK78 	NN<tNL"///F		s   AA 4BBwork_functionargskwargsc                    S U l         S U l        [        R                  R	                  S5      =n(       Ga4  U(       d9  U R
                  (       a  [        S5      e[        R                  S5        U" U0 UD6$ U R                  5       nU(       db  U R
                  (       a)  [        R                  R                  5         [        S5      eU" U0 UD6n[        R                  R                  5         U$  [        R                  S5        [        5       nU" U0 UD6n[        5       nX-
  n	[        R                  SU	5        U R                  SS9  [        R                  R                  5         U$ g ! [         aH  n
[        R                  SSS	9  U R                  SS9  [        R                  R                  5         U
eS n
A
ff = f)
NFzconcurrent runs not supportedz%failed to start strobelight profilingzcollection startedzwork function took %s secondsTr   zwork function throw exceptionr   )rA   rB   r1   _lockacquirer5   r   rS   r   r   releaserT   timerr{   r   r   )rC   r   r   r   lockedstartedr^   startend
total_timeerrors              r   profile&StrobelightCLIFunctionProfiler.profile  s    #"399AA%HH6H%%56UVV>?$d5f55--/G%%288@@B5?  '77.44<<>12&77g [
;ZH///E.44<<>   >N///F.44<<>	s   0A9E+ +
F=5AF88F=)	rA   r6   rB   r8   r7   r<   r5   r:   r9   )r   N)r   )r   r   r   r   r   r   r   boolr'   floatrt   r   listrD   ra   rn   rv   ry   r   r   r   r   r   r   r   r   r   r   r   r   r1   r1   8   s2   
 FE
 $(/ ;,.-/6:+/ #&' ' #&	'
 ' ' '*' (+' !)c 3' d3i(' ' !'4'
RM MT M<M.!>KK 
K&D  (%b"f-(68gg(IK(	"(r   r1   profilerr   c                    ^  T (       d  [        S0 UD6m S[        [        [        4   S[        [        [        [           4   4U 4S jjnU$ )Nr   r   c                    >^  [         R                  " T 5      S[        R                  S[        R                  S[
        [           4UU 4S jj5       nU$ )Nr   r   r   c                  0   > TR                   " T/U Q70 UD6$ r@   )r   )r   r   r   r   s     r   wrapper_function@strobelight.<locals>.strobelight_inner.<locals>.wrapper_function;  s    ##MCDCFCCr   )	functoolswrapsr   r   r   r   r   )r   r   r   s   ` r   strobelight_inner&strobelight.<locals>.strobelight_inner8  sN     
	'	DBGG 	Dryy 	DXb\ 	D 
(	D  r   r   )r1   r   r   r   r   )r   r   r   s   `  r   strobelightr   2  sI     1;F; B' 	"hrl"	#  r   r@   )*r   loggingr   rY   rU   rl   collections.abcr   	threadingr   timeitr   r   typingr   r   r   r	   typing_extensionsr
   	getLoggerrS   StreamHandlerconsole_handler	Formatter	formattersetFormatter
addHandlersetLevelINFO	propagater   r   r   r   r'   rt   r"   r*   r/   r1   r   r   r   r   <module>r      sQ     	 	   $  ) 3 3 ' 
		:	;'')H	   Y '   / "   t_T]) 7Xc] 7c 7. . . # r rv :>56ILxB (2x|+;"<<=r   