ó
    Eñi+.  ã                   ó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Jr  S SK	J
r
  S SKJr  S SK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)ÚCallableÚSequence)ÚLock)Údefault_timer)ÚAnyÚ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   ó    Úe/home/mande/repo/quber/.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Üˆt—I‘I˜c“N QÑ&¨Ð,Ó-Ð-r   Úcommandc                 ó$   • SR                  U 5      $ )NÚ )Újoin)r+   s    r   Ú_command_to_stringr/   4   s   € Ø�8‰8�GÓÐ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)&ÚStrobelightCLIFunctionProfileré8   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 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_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        X€l        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  € Ü—I’I“Kˆ	Ü" 9Ó-ˆ	àØØØØØØØÜ�4×#Ñ#Ó$Ð%ØÜ�4×0Ñ0°4Ñ7Ó8Ð9ØØˆk˜˜9˜+Ð&ð
ˆð  ××Ø�N‰N˜?Ô+Ø�N‰N˜3Ÿ8™8 D×$4Ñ$4Ó5Ô6ä�‰Ð*Ô,>¸wÓ,GÔHÜ—’ ¸Ñ=ˆØ—‘×%Ñ% gÓ.ˆÜ�‰�_ fÔ-à×Ñ Ó!Ü-ØLÈVÈHÐUóð ô —I’IÐ5°vÓ>Ð>ˆ5Õ>Ü"% e§k¡k°!£nÓ"5ˆDÔØä)Ø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‹<Ü-Ø<óð ð " >°:À$×BUÑBUÐAVÐXˆÜ�‰Ð*Ô,>¸wÓ,GÔHÜ—’ ¸Ñ=ˆØ—‘×%Ñ% gÓ.ˆÜ�‰�_ fÔ-à×Ñ Ó!Ü-ØSÐTZÐS[Ð\óð ô —I’IÐ8¸&ÓAÐAˆ5ÕAØ"Ÿ[™[¨›^ˆNØ Ó*ØØ ;Ó.Ü—
’
˜2”Ø×&Ñ& w°¡{Ô3Øä1°KÀÐ?OÈvÐ2VÓ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ò   € Ø! 9¨j¼#¸d×>QÑ>QÓ:RÐSˆÜ�‰Ð*Ô,>¸wÓ,GÔHÜ—’ ¸Ñ=ˆØ—‘×%Ñ% gÓ.ˆÜ�‰�_ fÔ-à×Ñ Ó!Ü-ØNÈvÈhÐWóð ô —I’I˜o¨vÓ6Ð6ˆ5Õ6Ø"Ÿ[™[¨›^ˆNØ×*Ñ*¨:×6Ñ6Øä1Ø@ÀÐ@PÐPWÐ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  € Ø! >°:¼sÀ4×CVÑCVÓ?WÐXˆÜ�‰Ð*Ô,>¸wÓ,GÔHÜ—’ ¸Ñ=ˆØ—‘×%Ñ% gÓ.ˆÜ�‰�_ fÔ-à×Ñ Ó!Ü-ØNÈvÈhÐWóð ô —I’I˜o¨vÓ6Ð6ˆ5Õ6Ø"Ÿ[™[¨›^ˆNØ×*Ñ*Ð+K×LÑLÜ—
’
˜2”Ø×!Ñ!Ô#ØØ#×0Ñ0Ð1T×UÑUÜ1ØOÐPVÈxÐXóð ð !ˆÔÜ—J’JØUØö
ˆDð ×Ò 4¨¡7Ñ*ÕÜ�K‰K˜˜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_throwÚ9StrobelightCLIFunctionProfiler._stop_strobelight_no_throwß   s`   € ð	Kà�N‰NÔÜ�K‰KÐ7Ô8ä�L‰LÐ-Ô.æ"Øà×ÑÕøÜó 	KÜ�N‰NÐ:ÀTˆNÓ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_strobelightÚ1StrobelightCLIFunctionProfiler._start_strobelightò   s…   € Ø#Ðð	Ø�O‰OÔØ"&ÐÜ�K‰KÐ3°T×5HÑ5HÔIØ×"Ñ"Ô$Ü�K‰KÐ7Ô8Øøäó 	Ü�N‰NÐ<ÀtˆNÑ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ÐHˆ6ÖHÞØ×%×%Ü5Ð6UÓVÐVä—‘Ð>Ô?Ù$ dÐ5¨fÑ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  † ñ
ñ ‹F€Eð
 $Ø(/Ø Ø;Ø,.Ø-/Ø6:Ø+/Ø Ø#&ò'ð ð'ð #&ð	'ð
 ð'ð ð'ð '*ð'ð (+ð'ð !)¨¨c©Ñ 3ð'ð ˜d 3™iÑ(ð'ð ð'ð !õ'ô4'
ñRM¨ð M°Tõ Mô<Mô.!ð>KàðKð 
ôKð& Dô ð (Ø% b¨" fÑ-ð(Ø68·g±gð(Ø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ÐC°DÒC¸FÑCÐCr   )Ú	functoolsÚwrapsr   rŒ   r�   r   r   )r‹   r¢   rž   s   ` €r   Ústrobelight_innerÚ&strobelight.<locals>.strobelight_inner8  sN   ù€ ô 
�Š˜Ó	'ð	D¤B§G¡Gð 	D´r·y±yð 	DÄXÌbÁ\÷ 	Dó 
(ð	Dð  Ðr   r   )r1   r   r   r   r   )rž   r�   r¦   s   `  r   Ústrobelightr¨   2  sI   ø€ ö Ü1Ñ;°FÑ;ˆð Ü¤¤B Ñ'ð ä	”"”hœr‘lÐ"Ñ	#÷ ð Ðr   r@   )*r¤   Úloggingr   rY   rU   rl   Úcollections.abcr   r   Ú	threadingr   Útimeitr   r’   Útypingr   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  ðó Û Û 	Û 	Û Û ß .Ý Ý )ß )Ñ )Ý 'ð 
×	Ò	Ð:Ó	;€à×'Ò'Ó)€Ø×ÒØHó€	ð × Ñ ˜YÔ 'à × Ñ �/Ô "Ø ‡��—‘Ô Ø€Ô áˆtƒ_€ÙˆTƒ]€ô )ô ñ7˜X c™]ð 7°cõ 7ñ.˜ ™ð .°õ .ð ¨¡ð °#ô ÷rñ rðv :>ñØÐ5Ñ6ðØILðàˆx˜˜B˜ÑÐ  (¨2¨x¸©|Ð+;Ñ"<Ð<Ñ=ör   