+
    BiH                        ^ RI t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RI	H
t
HtHt ^ RIHt ]P                  '       g   ] P                   ! R4      h]P"                  ! RRRRR7      '       d   ] P                   ! R4      hR	 t]! 4       '       g   ] P                   ! R
4      h ! R R] P&                  4      tR tR tRR/R lt ! R R4      t] P2                  ! ]! 4       R4      ] P2                  ! ]! 4       R4       ! R R] P&                  ]4      4       4       tR t] P2                  ! ]! 4       R4      ] P2                  ! ]! ^^4      R4       ! R R] P&                  ]4      4       4       t]R8X  d   ] P<                  ! 4        R# R# )    N)support)make_scriptassert_python_failureassert_python_ok)temp_dirztest module requires subprocessT)addressmemoryubfunctionz,test crash randomly on ASAN/MSAN/UBSAN buildc                  ^    \         P                  ! R 4      p V '       g   R# \        V 4      ^8H  # )PY_HAVE_PERF_TRAMPOLINEF)	sysconfigget_config_varint)perf_trampolines    ./usr/lib/python3.14/test/test_perf_profiler.pysupports_trampoline_profilingr      s)    ../HIO1$$    z'perf trampoline profiling not supportedc                   L  a a ] tR t^%t oV 3R ltV3R lV 3R llt]P                  ! ]P                  ! 4       R4      R 4       t
]P                  ! ]P                  ! 4       R4      R 4       t]P                  ! ]P                  ! 4       R4      R 4       tR tR	 tR
 tRtVtV ;t# )TestPerfTrampolinec                   < \         SV `  4        \        \        P                  ! R 4      P                  R4      4      V n        R# /tmp/z
perf-*.mapNsupersetUpsetpathlibPathglob
perf_filesself	__class__s   &r   r   TestPerfTrampoline.setUp&   ,    gll7388FGr   c                   < V ^8  d   QhRR/#    returnN )format__classdict__s   "r   __annotate__TestPerfTrampoline.__annotate__*         $ r   c                   < \         SV `  4        \        \        P                  ! R 4      P                  R4      4      V P                  ,
          pV F  pVP                  4        K  	  R# r   r   tearDownr   r   r   r    r!   unlinkr#   files_to_deletefiler$   s   &  r   r3   TestPerfTrampoline.tearDown*   M    W%**<89DOOK 	 $DKKM $r   z#fails on BOLT instrumented binariesc           
       a R p\        4       ;_uu_ 4       p\        VRV4      p/ \        P                  CRR/Cp\        P
                  ! \        P                  RV.R\        P                  \        P                  VR7      ;_uu_ 4       pVP                  4       w  rgRRR4       RRR4       V P                  XR4       V P                  XR4       \        P                  ! R	XP                   R
24      pV P                  VP                  4       4       VP!                  4       p	V	P#                  4       p
RX 2RV 2RV 2.pV F  o\%        V3R lV
 4       R4      pV P'                  VRS R24       VP)                  R4      ^ ,          pV P+                  VR4       V P                  \-        V4      P/                  \0        P2                  4      R4       K  	  R#   + '       g   i     ELG; i  + '       g   i     ELS; i)zif 1:
                def foo():
                    pass

                def bar():
                    foo()

                def baz():
                    bar()

                baz()
                perftest
PYTHON_JIT0-XperfTtextstderrstdoutenvN 
/tmp/perf-.mappy::foo:py::bar:py::baz:c              3   :   <"   T F  pSV9   g   K  Vx  K  	  R # 5iNr+   ).0lineexpected_symbols   & r   	<genexpr>;TestPerfTrampoline.test_trampoline_works.<locals>.<genexpr>Z   s     H*$40G*s   	
zCould not find z in perf file 0xz*Address should contain only hex characters)r   r   osenviron
subprocessPopensys
executablePIPEcommunicateassertEqualr   r   pid
assertTrueexists	read_text
splitlinesnextassertIsNotNonesplitassertNotStartsWithr   issubsetstring	hexdigits)r#   code
script_dirscriptrC   processrB   rA   	perf_fileperf_file_contents
perf_linesexpected_symbols	perf_line	perf_addrrN   s   &             @r   test_trampoline_works(TestPerfTrampoline.test_trampoline_works2   s    ZZ: Z>F3RZZ3s3C!!62!!  !(!4!4!6  	$$LL:gkk]$!?@		((*+&002'224
vhvhvh

  0OH*H$I   __,=]K ",Q/I$$Y5OOI''(8(89<  0+   ZZs$   A4G/
G	G/G,&	G//H 	c           
     n   R p\        4       ;_uu_ 4       p\        VRV4      p/ \        P                  CRR/Cp\        P
                  ! \        P                  RV.R\        P                  \        P                  VR7      ;_uu_ 4       pVP                  4       w  rgRRR4       RRR4       V P                  XP                  ^ 4       V P                  XR4       \        XP                  4       4      p\        P                  ! R	VP                    R
24      p	\        P                  ! R	V R
24      p
V P#                  V	P%                  4       4       V P#                  V
P%                  4       4       V	P'                  4       pV P)                  RX 2V4       V P)                  RV 2V4       V P)                  RV 2V4       V
P'                  4       pV P)                  RV 2V4       V P)                  RV 2V4       V P)                  RV 2V4       R#   + '       g   i     EL|; i  + '       g   i     EL; i)a  if 1:
                import os, sys

                def foo_fork():
                    pass

                def bar_fork():
                    foo_fork()

                def baz_fork():
                    bar_fork()

                def foo():
                    pid = os.fork()
                    if pid == 0:
                        print(os.getpid())
                        baz_fork()
                    else:
                        _, status = os.waitpid(-1, 0)
                        sys.exit(status)

                def bar():
                    foo()

                def baz():
                    bar()

                baz()
                r;   r<   r=   r>   Tr?   NrD   rE   rF   rG   rH   rI   py::foo_fork:py::bar_fork:zpy::baz_fork:)r   r   rS   rT   rU   rV   rW   rX   rY   rZ   r[   
returncoder   stripr   r   r\   r]   r^   r_   assertIn)r#   rh   ri   rj   rC   rk   rB   rA   	child_pidrl   perf_child_filerm   child_perf_file_contentss   &            r    test_trampoline_works_with_forks3TestPerfTrampoline.test_trampoline_works_with_forksf   s   : ZZ: Z>F3RZZ3s3C!!62!!  !(!4!4!6  	++Q/$'	LL:gkk]$!?@	!,,I;d'CD	((*+..01&002)+=>)+=>)+=>#2#<#<#> fX.0HIfX.0HIfX.0HI3   ZZs$   A4H#	H	H#H 	H##H4	c           
     <   R p\        4       ;_uu_ 4       p\        VRV4      p/ \        P                  CRR/Cp\        P
                  ! \        P                  V.R\        P                  \        P                  VR7      ;_uu_ 4       pVP                  4       w  rgRRR4       RRR4       V P                  XR4       V P                  XR4       \        P                  ! RXP                   R	24      pV P                  VP                  4       4       VP!                  4       p	V P#                  R
X 2V	4       V P%                  RV 2V	4       V P%                  RV 2V	4       V P%                  RV 2V	4       R#   + '       g   i     L; i  + '       g   i     L; i)a  if 1:
                import sys
                def foo():
                    pass

                def spam():
                    pass

                def bar():
                    sys.deactivate_stack_trampoline()
                    foo()
                    sys.activate_stack_trampoline("perf")
                    spam()

                def baz():
                    bar()

                sys.activate_stack_trampoline("perf")
                baz()
                r;   r<   r=   Tr?   NrD   rE   rF   rG   z	py::spam:rH   rI   )r   r   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r   r   r\   r]   r^   r_   assertNotInry   )
r#   rh   ri   rj   rC   rk   rB   rA   rl   rm   s
   &         r   test_sys_apiTestPerfTrampoline.test_sys_api   sW   ( ZZ: Z>F3RZZ3s3C!!(!!  !(!4!4!6  	$$LL:gkk]$!?@		((*+&0028F8,.@A	&*,>?)+=>)+=>%  Zs$   A3FE8	F8FFF	c                &    R p\        RVRR7       R# )zif 1:
                import sys
                sys.activate_stack_trampoline("perf")
                sys.activate_stack_trampoline("perf")
                -cr=   r<   Nr   r#   rh   s   & r   %test_sys_api_with_existing_trampoline8TestPerfTrampoline.test_sys_api_with_existing_trampoline   s    
 	t4r   c                l    R p\        RVRR7      w  r#pV P                  RVP                  4       4       R# )zjif 1:
                import sys
                sys.activate_stack_trampoline("invalid")
                r   r=   r   zinvalid backend: invalidN)r   ry   decode)r#   rh   rcouterrs   &    r   $test_sys_api_with_invalid_trampoline7TestPerfTrampoline.test_sys_api_with_invalid_trampoline   s2     -T4CH0#**,?r   c                &    R p\        RVRR7       R# )a  if 1:
                import sys
                sys.activate_stack_trampoline("perf")
                assert sys.is_stack_trampoline_active() is True
                sys.deactivate_stack_trampoline()
                assert sys.is_stack_trampoline_active() is False
                r   r=   r   Nr   r   s   & r   test_sys_api_get_status*TestPerfTrampoline.test_sys_api_get_status   s     	t4r   r!   )__name__
__module____qualname____firstlineno__r   r3   unittestskipIfr   check_bolt_optimizedrr   r}   r   r   r   r   __static_attributes____classdictcell____classcell__r$   r-   s   @@r   r   r   %   s     H  __W1135Z[1 \1f __W1135Z[:J \:Jx __W1135Z[*? \*?X5@5 5r   r   c                  L    \         P                  ! R 4      p V '       g   R# RV 9   # )PY_CORE_CFLAGSFzno-omit-frame-pointer)r   r   )cflagss    r   )is_unwinding_reliable_with_frame_pointersr      s$    %%&67F"f,,r   c                  6    R R.p \         P                  ! V RR7      pRT9  d   R# \	        4       ;_uu_ 4       p TR,           pR RRR	R
RRTR\
        P                  RR3p / \        P                  CRR/Cp\         P                  ! YR\         P                  TR7      pRT9  d    RRR4       R#  RRR4       R#   \         P                  \        3 d     R# i ; i  \         P                  \        3 d     RRR4       R# i ; i  + '       g   i     R# ; i)perfz--helpTr@   Fz	perf.data/perf_output.perfrecord--no-buildid--no-buildid-cache-g--call-graph=fp-o--r   zprint("hello")r<   r=   )cwdr@   rA   rC   Nhello)
rU   check_outputSubprocessErrorOSErrorr   rW   rX   rS   rT   STDOUT)cmdrB   ri   output_filerC   s        r   perf_command_worksr      s%   x ((48 &  
z	$'::K$! C 4RZZ3s3C,,$z7H7HcF & 3 
0 !1 
6 I &&0 < **G4 	- 
*	+ 
6 s@   B; DA#CD;CCD8DDDD	use_jitFc                   \         P                  P                  4       pV'       d   VP                  V4       R VR&   V R,           pV'       g   RRRRRRR	VR
3	pMRRRRRRRRR	VR
3p\        P
                  ! Wb,           \        P                  \        P                  VRR7      pVP                  '       d>   \        VP                  \        P                  R7       \        RVP                   24      hV'       d   V R,           pRRRRVR	V3p	\        P
                  ! V	\        P                  \        P                  VRR7      pVP                  '       d>   \        VP                  \        P                  R7       \        RVP                   24      h\         P                  ! W4       Rp\        P
                  ! RRRV3\        P                  \        P                  VRRR7      pVP                  VP                  3# )r=   r<   r   r   r   r   r   r   r   r   r   z--call-graph=dwarf,65528z-F99z-k1T)rB   rA   rC   r@   )r7   zPerf failed with return code z/jit_output.dumpinjectz-jz-i)rA   rB   rC   r@   rj   )rB   rA   rC   checkr@   )r   rj   )rS   rT   copyupdaterU   runrY   rw   printrA   rW   
ValueErrorrenamerB   )
r   r   argsenv_varsrC   r   base_cmdprocjit_output_filecommands
   &$*,      r   run_perfr     s   
**//
C

8C++K +	
  &
 >>D dkk

+88IJKK 228T4dOT~~JOOJOOSW
 ???$++CJJ/<T__<MNOO
		//!H>>	4-D ;;##r   c                   2   a  ] tR tRt o R tR tR tRtV tR# )TestPerfProfilerMixini_  c                    \        4       hrK   )NotImplementedError)r#   ri   	perf_moderj   s   &&&&r   r   TestPerfProfilerMixin.run_perf`  s    !##r   c                J   \        4       ;_uu_ 4       pR p\        VRV4      pV P                  W4      w  rEV P                  VR4       V P	                  RV 2V4       V P	                  RV 2V4       V P	                  RV 2V4       RRR4       R#   + '       g   i     R# ; i)!  if 1:
                def foo(n):
                    x = 0
                    for i in range(n):
                        x += i

                def bar(n):
                    foo(n)

                def baz(n):
                    bar(n)

                baz(10000000)
                r;   rD   rG   rH   rI   N)r   r   r   r[   ry   r#   ri   rh   rj   rB   rA   s   &     r   7test_python_calls_appear_in_the_stack_if_perf_activatedMTestPerfProfilerMixin.test_python_calls_appear_in_the_stack_if_perf_activatedc  s    ZZ:D !Z>F!]]:>NFVR(MMHVH-v6MMHVH-v6MMHVH-v6+ ZZZs   A4BB"	c                N   \        4       ;_uu_ 4       pR p\        VRV4      pV P                  WRR7      w  rEV P                  VR4       V P	                  RV 2V4       V P	                  RV 2V4       V P	                  RV 2V4       RRR4       R#   + '       g   i     R# ; i)	r   r;   F)activate_trampolinerD   rG   rH   rI   N)r   r   r   r[   r   r   s   &     r   @test_python_calls_do_not_appear_in_the_stack_if_perf_deactivatedVTestPerfProfilerMixin.test_python_calls_do_not_appear_in_the_stack_if_perf_deactivated{  s    ZZ:D !Z>F!]] + NF VR(xx0&9xx0&9xx0&9/ ZZZs   A6BB$	r+   N)	r   r   r   r   r   r   r   r   r   )r-   s   @r   r   r   _  s     $70: :r   r   zperf command doesn't workz+Unwinding is unreliable with frame pointersc                   Z   a a ] tR tRt oRR ltV 3R ltV3R lV 3R lltR tRtVt	V ;t
# )	TestPerfProfileri  c                    V'       d   \        V\        P                  R V4      # \        V\        P                  V4      # )r>   r   rW   rX   r#   ri   rj   r   s   &&&&r   r   TestPerfProfiler.run_perf  s-    J&II
CNNF;;r   c                   < \         SV `  4        \        \        P                  ! R 4      P                  R4      4      V n        R# r   r   r"   s   &r   r   TestPerfProfiler.setUp  r&   r   c                   < V ^8  d   QhRR/# r(   r+   )r,   r-   s   "r   r.   TestPerfProfiler.__annotate__  r0   r   c                   < \         SV `  4        \        \        P                  ! R 4      P                  R4      4      V P                  ,
          pV F  pVP                  4        K  	  R# r   r2   r5   s   &  r   r3   TestPerfProfiler.tearDown  r9   r   c           
        R p\        4       ;_uu_ 4       p\        VRV4      p/ \        P                  CRR/Cp\        P
                  ! \        P                  RV.R\        P                  \        P                  VR7      ;_uu_ 4       pVP                  4       w  rgRRR4       RRR4       V P                  XP                  ^ 4       V P                  RX4       \        XP                  4       4      p\        P                   ! R	VP"                   R
24      p	\        P                   ! R	V R
24      p
V P%                  V	P'                  4       4       V P%                  V
P'                  4       4       V	P)                  4       pV P+                  RX 2V4       V P+                  RV 2V4       V P+                  RV 2V4       V P+                  RV 2V4       V
P)                  4       pV P+                  RV 2V4       V P+                  RV 2V4       VP-                  R4      pV F*  pRV 2V9   g   RV 2V9   g   K  V P+                  W4       K,  	  R#   + '       g   i     EL; i  + '       g   i     EL; i)a  if 1:
                import sys
                import os
                import sysconfig
                from _testinternalcapi import (
                    compile_perf_trampoline_entry,
                    perf_trampoline_set_persist_after_fork,
                )

                def foo_fork():
                    pass

                def bar_fork():
                    foo_fork()

                def foo():
                    import time; time.sleep(1)

                def bar():
                    foo()

                def compile_trampolines_for_all_functions():
                    perf_trampoline_set_persist_after_fork(1)
                    for _, obj in globals().items():
                        if callable(obj) and hasattr(obj, '__code__'):
                            compile_perf_trampoline_entry(obj.__code__)

                if __name__ == "__main__":
                    compile_trampolines_for_all_functions()
                    pid = os.fork()
                    if pid == 0:
                        print(os.getpid())
                        bar_fork()
                    else:
                        bar()
                r;   r<   r=   r>   T)universal_newlinesrA   rB   rC   NzError:rE   rF   rG   rH   ru   rv   
)r   r   rS   rT   rU   rV   rW   rX   rY   rZ   r[   rw   r   r   rx   r   r   r\   r]   r^   r_   ry   rc   )r#   rh   ri   rj   rC   rk   rB   rA   rz   rl   r{   rm   r|   perf_file_linesrM   s   &              r   test_pre_fork_compile&TestPerfProfiler.test_pre_fork_compile  s!   #J ZZ: Z>F3RZZ3s3C!!62#'!!  !(!4!4!6  	++Q/6*'	LL:gkk]$!?@	!,,I;d'CD	((*+..01&002)+=>)+=>fX.0BCfX.0BC#2#<#<#> fX.0HIfX.0HI -2248#Dvh'4/]6(3Kt3Sd= $=   ZZs$   A4I$	I	I$I!	I$$I5	r   T)r   r   r   r   r   r   r3   r   r   r   r   r   s   @@r   r   r     s&     <
H I> I>r   r   c                 R    \         P                  ! R R.RR7      pTP                  4       ^,          pTP                  R4      ^ ,          pTP                  R4      p\        \        \        TR,          4      4      pY0T38  #   \         P                  \        \        3 d     R# i ; i)r   z	--versionTr   F-.:Nr)   N)	rU   r   CalledProcessErrorFileNotFoundErrorPermissionErrorrc   tuplemapr   )majorminoroutputversions   &&  r   _is_perf_version_at_leastr     s    ((&+)>TJ llnQGmmC #GmmC GCWR[)*Gen$$ ))+<oN s   B  B&%B&z+perf command may not work due to a perf bugc                   T   a a ] tR tRt oRR ltV 3R ltV3R lV 3R lltRtVtV ;t	# )TestPerfProfilerWithDwarfi  c                    V'       d   \        V\        P                  R VRR7      # \        V\        P                  VRR7      # )z
-Xperf_jitT)r   r   r   s   &&&&r   r   "TestPerfProfilerWithDwarf.run_perf  s8    CNNL&$  
CNNFDIIr   c                  < \         SV `  4        \        \        P                  ! R 4      P                  R4      4      V n        V ;P                  \        \        P                  ! R 4      P                  R4      4      ,          un        R# r   z	jit*.dumpzjitted-*.soNr   r"   s   &r   r   TestPerfProfilerWithDwarf.setUp  sS    gll7388EF3w||G499-HIIr   c                   < V ^8  d   QhRR/# r(   r+   )r,   r-   s   "r   r.   &TestPerfProfilerWithDwarf.__annotate__  r0   r   c                @  < \         SV `  4        \        \        P                  ! R 4      P                  R4      4      pV\        \        P                  ! R 4      P                  R4      4      ,          pWP                  ,
          pV F  pVP                  4        K  	  R# r   r2   r5   s   &  r   r3   "TestPerfProfilerWithDwarf.tearDown  sn    gll7388EF3w||G499-HII)OO;#DKKM $r   r   r   )
r   r   r   r   r   r   r3   r   r   r   r   s   @@r   r   r     s      
JJ
  r   r   __main__)r   rf   rU   rW   r   rS   r   testr   test.support.script_helperr   r   r   test.support.os_helperr   has_subprocess_supportSkipTestcheck_sanitizerr   TestCaser   r   r   r   r   
skipUnlessr   r   r   r   mainr+   r   r   <module>r     s      
  	   
 , %%%


=
>>
4MM 

J
KK% %&&


E
FFC5** C5L-(V?$ ?$D4: 4:n 
')+FG	-/1[>x((*? [>	 H
[>|%$ 
')+FG	a#%R 1 13H  H, zMMO r   