
    Yf<                   |   d dl mZ 	 	 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	Z	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Z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Z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& d dl'm(Z( d dl)m*Z* d d	l+m,Z, d d
l-m.Z. d dl/m0Z0 d dl1m2Z2m3Z3 d dl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z? d dl@ZAd dlBmCZC d dlDmEZE d dlFmGZG d dlHmIZI d dlJmKZK d dlLmMZM d dlNmOZO d dlPmQZQ d dlRmSZSmTZTmUZUmVZVmWZW d dlX ej"                  dk7  rd dlYZY ej                         dk(  rd dl[m\Z] nd dl^m]Z] d dl_m`Z`maZa d dlbmcZc  ed      Zdd*d Zeeeed_f        d!Zgd"Zhd+d#Zi eiegehf       d,d$Zjeje_k        d-d%Zld-d&Zm G d' d(      Zneod)k(  renj                          yy# e$ r Y w xY w).    )annotationsN)Console)find_browser)_get_module_details)	get_timesTimeInfo)redirect_python)defaultdict)	dataclass)SourceLoader)
ModuleSpec)CodeType	FrameType)AnyCallableDict	GeneratorListOptionalSetTupleTypeUnioncast)ScaleneArguments)ScaleneClientTimer)ScaleneFuncUtils)ScaleneJSON)ScaleneMapFile)ScaleneOutput)ScalenePreload)ScaleneSignals)AddressByteCodeIndexFilename
LineNumberScaleneStatistics)*win32Darwin)ScaleneAppleGPU)
ScaleneGPU)ScaleneParseArgsStopJupyterExecution)ScaleneSigQueuezwhite on blue)stylec                      y N argss    X/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/scalene/scalene_profiler.pynadar7   m   s              c                R    t         j                  | k\  sJ d| d    d| d    d       y )Nz Scalene requires Python version r   .   z
 or above.)sysversion_info)versions    r6   require_pythonrA   v   s9    G#N	)'!*Qwqzl*MN#r8   c                ,    t         j                  |       S )zHandle @profile decorators.

    If Scalene encounters any functions decorated by @profile, it will
    only report stats for those functions.

    )Scaleneprofilefuncs    r6   scalene_redirect_profilerG      s     ??4  r8   c                 ,    t         j                          y)zStart profiling.N)rC   startr3   r8   r6   rI   rI      s    MMOr8   c                 ,    t         j                          y)zStop profiling.N)rC   stopr3   r8   r6   rK   rK      s    LLNr8   c                  	   e Zd ZU dZded<   	  e ej                  d            ZdZdZdZdZdZdZd	ed
<    ed       ed       ed      gZej,                  Zedrd       ZdZdZ ed      Z ed      ZdZ ej@                         Z!ded<   dZ"dZ#dZ$dZ% e&       Z'ded<    e(e&      Z)ded<   e*jV                  jX                  Z-e*j\                  Z/ e0       Z1 e2       Z3 e4       Z5 e6       Z7 e8       Z9 e:       Z;e;jy                         e7_=        e;jy                         e9_=        g Z>ded<   ded<   ded <   edsd!       Z?ed"        Z@ eA       ZB ed#      ZC ed#      ZDd$ed%<    eEd&      ZFd'ed(<   dZGded)<   d*ed+<   d*ed,<    ed#      ZH e(eI      ZJd-ed.<    e&       ZKd/ed0<   d1ed2<   d1ed3<   d4ed5<    eL       ZMd6ed7<   eNj                  ZOej                  ZQeNj                  ZSdZTej                  ZVej                  d8k7  reNj                  ZYeNj                  Z[edtd9       Z\edud:       Z]ed;        Z^edvd<       Z_edwd=       Z`edxd>       Zaedvd?       Zbe	 	 	 	 	 	 dyd@       ZcedxdA       ZdedxdB       Zee	 	 	 	 	 	 	 	 dzdC       ZfegdxdD       Zhegd{dE       Ziegd{dF       Zjed|dG       Zked}dH       Zled~dI       Zmed~dJ       ZndKZoedxdL       ZpedxdM       ZqedxdN       Zre	 	 	 	 	 	 dydO       Zse	 	 	 	 	 	 dydP       Zte	 	 	 	 	 	 dydQ       Zue	 	 	 	 	 	 dydR       ZvedxdS       ZwedxdT       Zx	 d	 	 	 	 	 ddVZye	 	 	 	 	 	 dydW       ZzedddX       Z{e e|j                  dU      	 	 	 	 ddY              Z~eddZ       Zedxd[       Ze	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd\       Zedd]       Ze	 	 	 	 	 	 dd^       Zedd_       Zedxd`       Zedda       Zedxdb       Ze	 	 	 	 	 	 ddc       Ze e|j                  dU      ddd              ZdZedxde       Zedxdf       Zedvdg       Ze	 	 	 	 	 	 ddh       Ze	 	 	 	 	 	 ddi       Zedddj       Zedxdk       Z	 	 	 	 	 	 	 	 	 	 ddlZeddm       Zedxdn       Zedxdo       Zedxdp       Ze	 d	 	 	 	 	 	 	 ddq       ZyU# e	$ r  ej                         Z
e
re
ndZY w xY w)rC   zThe Scalene profiler itself.int_Scalene__availableCPUsr   r=   F   bool_Scalene__initializedNADAc                 b    t        t        t        t        t        f   t
        j                        S )z2Helper function to type last profiled information.)r   r   r%   r&   r$   rC   _Scalene__last_profiledr3   r8   r6   last_profiled_tuplezScalene.last_profiled_tuple   s(     (J568O8O
 	
r8   zscalene-guizprofile.jsonzprofile.htmlzError in program being profiledzqueue.Queue[Any]_Scalene__windows_queuei   MFfzSet[Filename]_Scalene__files_to_profilezDict[Filename, Set[Any]]_Scalene__functions_to_profilez!List[Tuple[Filename, LineNumber]]_Scalene__invalidate_queuethreading.Lock_Scalene__invalidate_mutexstr_Scalene__profiler_basec                 *    t         j                         S )z;Return the true lock, which we shim in replacement_lock.py.)rC   _Scalene__original_lockr3   r8   r6   get_original_lockzScalene.get_original_lock   s     &&((r8   c                 "    t         j                  S r2   )rC   _Scalene__signalsr3   r8   r6   get_signalszScalene.get_signals   s       r8    zpathlib.Path_Scalene__python_alias_dirinffloat_Scalene__next_output_time_Scalene__pidr   _Scalene__malloc_mapfile_Scalene__memcpy_mapfileDict[int, bool]_Scalene__is_thread_sleepingSet[int]
child_pidszScaleneSigQueue[Any]_Scalene__alloc_sigq_Scalene__memcpy_sigqzList[ScaleneSigQueue[Any]]_Scalene__sigqueuesr   client_timerr)   c                 P    t        t        j                  j                               S )zReturn the set of all signals currently set.

        Used by replacement_signal_fns.py to shim signals used by the client program.
        )setrC   rf   get_all_signalsr3   r8   r6   get_all_signals_setzScalene.get_all_signals_set  s     7$$44677r8   c                 >    t         j                  j                         S r2   )rC   rf   get_lifecycle_signalsr3   r8   r6   r}   zScalene.get_lifecycle_signals  s      6688r8   c                     dt         _        y )NTrC   _Scalene__lifecycle_disabledr3   r8   r6   disable_lifecyclezScalene.disable_lifecycle   s
    '+$r8   c                 "    t         j                  S r2   r   r3   r8   r6   get_lifecycle_disabledzScalene.get_lifecycle_disabled#  s    +++r8   c                 >    t         j                  j                         S )zReturn the set of all TIMER signals currently set.

        Used by replacement_signal_fns.py to shim timers used by the client program.
        )rC   rf   get_timer_signalsr3   r8   r6   r   zScalene.get_timer_signals'  s       2244r8   c                     dt         _        y)z:Tell Scalene that it is running inside a Jupyter notebook.TNrC   _Scalene__in_jupyterr3   r8   r6   set_in_jupyterzScalene.set_in_jupyter/  s      $r8   c                 "    t         j                  S )z<Return whether Scalene is running inside a Jupyter notebook.r   r3   r8   r6   
in_jupyterzScalene.in_jupyter4  s     ###r8   c                    t         )z*Handle keyboard interrupts (e.g., Ctrl-C).)KeyboardInterruptsignum
this_frames     r6   interruption_handlerzScalene.interruption_handler9  s
      r8   c                 J    t        t        j                  j                         y)z:Mark a new line by allocating the trigger number of bytes.N)	bytearrayscalenescalene_configNEWLINE_TRIGGER_LENGTHr3   r8   r6   update_linezScalene.update_lineF  s     	'((??@r8   c                     t         j                  5  t         j                         } t         j                  j	                  | d   | d   f       t         j                          d d d        y # 1 sw Y   y xY w)Nr   r=   )rC   r_   rV   r]   appendr   )last_prof_tuples    r6   update_profiledzScalene.update_profiledK  sb    '' 	"%99;O&&-- #_Q%78 !	" 	" 	"s   AA**A3c                b   	 | j                   j                  }| j                  }t        j	                         \  }}}||k(  r||k(  rt        j
                  S d| _        d| _        t        | ||      ryt        j                  d       t        j                          dt        _        t        d      t        d      t        d      gt        _        y# t"        $ r Y yt$        $ rK}t'        t        j(                   d|t        j*                         t-        j.                          Y d}~yd}~ww xY w)z]Mark the last_profiled information as invalid as soon as we execute a different line of code.NFTrS   r   :
file)f_codeco_filenamef_linenorC   rV   invalidate_lines_pythonf_tracef_trace_lineson_stackr>   settracer   #_Scalene__last_profiled_invalidatedr%   r&   r$   rU   AttributeError	Exceptionprint_Scalene__error_messagestderr	traceback	print_exc)	frame_event_argffflfnamelinenolasties	            r6   r   zScalene.invalidate_lines_pythonT  s    
#	))BB%,%@%@%B"UFEe",666 EM"'Euf- LL##%26G/  1a 'G#  	 	W,,-S113::F!	s,   AC C 2AC 	D.D.#AD))D.c                l    | j                   j                          | j                  j                          y)z.Clear the various states for forked processes.N)_Scalene__statsclearrs   )clss    r6   clear_metricszScalene.clear_metrics~  s$     	r8   c                :    | j                   j                  |       y)z7Add this pid to the set of children. Used when forking.N)rs   addr   pids     r6   add_child_pidzScalene.add_child_pid  s     	3r8   c                    t        j                  t              5  | j                  j	                  |       ddd       y# 1 sw Y   yxY w)zJRemove a child once we have joined with it (used by replacement_pjoin.py).N)
contextlibsuppressKeyErrorrs   remover   s     r6   remove_child_pidzScalene.remove_child_pid  s9       * 	'NN!!#&	' 	' 	's	   ?Ac                4   t         j                  j                  | j                  j                         t         j
                  | j                  j                     j                  |        t         j                  j                  rt         j                          | S )zRecord the file and function name.

        Replacement @profile decorator function.  Scalene tracks which
        functions - in which files - have been decorated; if any have,
        it and only reports stats for those.

        )	rC   r[   r   __code__r   r\   _Scalene__argsmemoryregister_files_to_profilerE   s    r6   rD   zScalene.profile  sc     	""&&t}}'@'@A&&t}}'@'@AEEdK>>  --/r8   c                \       t                t        j                         d fd       }|S )aF  Provide a decorator that calls the wrapped function with the
        Scalene variant.

                Wrapped function must be of type (s: Scalene) -> Any.

                This decorator allows for marking a function in a separate
                file as a drop-in replacement for an existing library
                function. The intention is for these functions to replace a
                function that indefinitely blocks (which interferes with
                Scalene) with a function that awakens periodically to allow
                for signals to be delivered.

        c                      | i |S r2   r3   )r5   kwargsrF   s     r6   wrappedzScalene.shim.<locals>.wrapped  s    (((r8   )r5   r   r   r   returnr   )rC   	functoolswraps)rF   r   s   ` r6   shimzScalene.shim  s0     	W
 
		) 
	) r8   c                *    dt         j                  | <   y)zTIndicate the given thread is sleeping.

        Used to attribute CPU time.
        TNrC   rq   tids    r6   set_thread_sleepingzScalene.set_thread_sleeping  s     -1$$S)r8   c                *    dt         j                  | <   y)zOIndicate the given thread is not sleeping.

        Used to attribute CPU time.FNr   r   s    r6   reset_thread_sleepingzScalene.reset_thread_sleeping  s    
 -2$$S)r8   Tc                 x   t         j                  dk(  sJ dt        _        t        j                  rt        j                  j                          t        j                  t        j                  j                         t        j                  t        j                  j                         t        j                  ryy)zHFor Windows, send periodic timer signals; launch as a background thread.r)   TN)r>   platformrC   timer_signalsrW   gettimesleepr   cpu_sampling_rate_Scalene__orig_raise_signalrf   
cpu_signalr3   r8   r6   windows_timer_loopzScalene.windows_timer_loop  st     ||w&&& $####'')JJw~~778''(9(9(D(DE ##r8   c                 N    t         j                  D ]  } | j                           y)z9Start the signal processing queues (i.e., their threads).N)rC   rv   rI   sigqs    r6   start_signal_queueszScalene.start_signal_queues  s"     '' 	DJJL	r8   c                 N    t         j                  D ]  } | j                           y)z8Stop the signal processing queues (i.e., their threads).N)rC   rv   rK   r   s    r6   stop_signal_queueszScalene.stop_signal_queues  s"     '' 	DIIK	r8   c                    t         j                          t         j                          t         j                  t         j                         y)zHandle terminate signals.N)rC   rK   output_profile_Scalene__orig_exit_Scalene__sigterm_exit_coder   s     r6   term_signal_handlerzScalene.term_signal_handler  s-     	 G778r8   c                &   t         j                  j                  syddlm} |r$t         j                  |t         j                         d}|}|r^t         j                  |j                  j                  |j                  j                        x}rnt        t        |j                        }|r^|sy|sJ |j                         }t         j                         \  }}}|s#|r!t!        |||      st         j#                          |j%                          t'        |j                  j                        t)        |j*                        t-        |j.                        gt         _        t         j2                  j5                  dg       |j7                          ~y)zHandle allocation signals.Nr   pywhereF)rC   r   r   r   r   enter_function_metar   should_tracer   r   co_namer   r   f_backget_last_profiled_invalidatedrV   r   r   #set_last_profiled_invalidated_falser%   r&   r   r$   f_lastirU   rt   putenable_settrace)	r   r   r   found_framerZ   invalidatedr   r   r   s	            r6   malloc_signal_handlerzScalene.malloc_signal_handler  sA    ~~$$#''
GOOD%22$$ahh&6&6 {  Y)A  q
 ;;=!(!<!<!>j%8##%335QXX))*qzz"!))$#

 	  !%!r8   c                    |r$t         j                  |t         j                         t         j                  j	                  dg       ~y)zHandle free signals.r   N)rC   r   r   rt   r   r   s     r6   free_signal_handlerzScalene.free_signal_handler$  s4     ''
GOOD  !%r8   c                H    t         j                  j                  | |f       ~y)zHandle memcpy signals.N)rC   ru   r   r   s     r6   memcpy_signal_handlerzScalene.memcpy_signal_handler4  s      	!!6:"67r8   c                    t         j                  dk(  sJ dt        _        t        j	                  t        j
                  j                  t        j                         dt        _        t        j                  t        j                        } | j                          t        j                  j                  d        t        j                          y )Nr)   T)target)r>   r   rC   r   _Scalene__orig_signalrf   r   cpu_signal_handler	threadingThreadr   rI   rW   r   r   )ts    r6   enable_signals_win32zScalene.enable_signals_win32B  s    ||w&&& $((&&	

 !%G$>$>?		##D)##%r8   c                 @   t         j                  dk(  rt        j                          yt        j	                          t        j
                  j                  t        j                  ft        j
                  j                  t        j                  ft        j
                  j                  t        j                  ft        j                  t        j                  ft        j
                  j                  t        j                   ffD ]  \  } }t        j#                  | |        t        j
                  j%                         D ]  }t        j'                  |d        t        j)                  t        j
                  j*                  t        j,                  j.                         y)zeSet up the signal handlers to handle interrupts for profiling and start the
        timer interrupts.r)   NF)r>   r   rC   r  r   rf   malloc_signalr   free_signalr  memcpy_signalr  signalSIGTERMr   r   r  r  rz   _Scalene__orig_siginterrupt_Scalene__orig_setitimercpu_timer_signalr   r   )sighandlerss      r6   enable_signalszScalene.enable_signalsR  s(    <<7"((*##% ,,g.K.KL**G,G,GH,,g.K.KL^^W889))7+E+EF
 	0LC !!#w/	0 ""224 	2A''51	2  ..NN,,	
r8   Nc                   dd l }dd l}dd l}dd l}dd l}dd l}dd l}t        j                  dk7  rdd l	}dd l
}t        t        |      t        _        t        t        j                         t        _        t        t        j$                        t        _        t        j"                  t        j&                  gt        _        t        j+                         t        _        t/        j0                         t        _        t        j                  dk(  r.|j4                  r"t7        dt        j8                         d|_        	 t;        d      t        _        t;        d      t        _        t        jD                  jG                  |jH                         tK        tL        jN                  jQ                  tR                    t        _*        |jV                  rhtL        jX                  d	   j[                  tL        j\                        d   }t_        j`                  |      t        _1        |jV                  t        _2        n4t_        j`                  tg        jh                  d
            t        _1        dt        _2        d}d|v r|jj                  r|dz  }dD ]'  }tm        ||      s|d|jo                  dd       z  }) |dtM        jp                          dz  }ts        jt                  |      }t        j                  dk(  r'djw                  d |jy                         D              }n&djw                  d |jy                         D              }t{        ||t        jb                        t        _>        t        j                  t        j                         |rt        |      t        _C        y y # t@        $ r% |j4                  rt        jB                  d       Y nw xY w)Nr   r)   zIScalene warning: Memory profiling is not currently supported for Windows.r   Fmallocmemcpyr=   PATHr   )prefixrh   offz --off)	use_virtual_timecpugpur   cliwebhtml
no_browserreduced_profilez  --_-z --pid=z ---
c              3  F   K   | ]  \  }}d | dt        |       d  yw)zset =r*  N)r`   .0kvs      r6   	<genexpr>z#Scalene.__init__.<locals>.<genexpr>  s,      $.4q!d1#Qs1vhb)$   ! c              3  Z   K   | ]#  \  }}d j                  |t        |      f       % yw)r,  N)joinr`   r-  s      r6   r1  z#Scalene.__init__.<locals>.<genexpr>  s*      #.4q!CHHaQ[)#s   )+)Dscalene.replacement_exitscalene.replacement_get_contextscalene.replacement_lockscalene.replacement_mp_lockscalene.replacement_pjoinscalene.replacement_signal_fnsscalene.replacement_thread_joinr>   r   scalene.replacement_fork!scalene.replacement_poll_selectorr   r   rC   r   r/   alloc_sigqueue_processorrt   memcpy_sigqueue_processorru   rv   rd   r_   queueQueuerW   r   r   r   r   rn   ro   r   exitrf   set_timer_signalsr   r`   ospathdirname__file__ra   r   environsplitpathseppathlibPathri   rm   tempfilemkdtempr  getattrreplacegetpidr!   get_preload_environr5  itemsr	   _Scalene__orig_pythonatexitregisterexit_handlerr%    _Scalene__program_being_profiled)	self	argumentsprogram_being_profiledr   rG  cmdlineargrI  prefaces	            r6   __init__zScalene.__init__k  s   
 	(. 	(*(-.<<7"+4.	:.,, 
 !0--!
   !!
 &-%>%>%@""'++-<<7"_gjgqgq $)	 
	'5h'?G$'5h'?G$ 	++I,F,FG"%bggooh&?"@==
 jj(..rzz:1=G)0g)>G&%MMGM *1  	2*G& GMG	!imm8#
 > 9c*ckk#s&;%<==G> T22G$88CG||w&)) $8?$  (( #8?#  %4GWgF`F`$aG! 	,,-!/78N/OG, "y  		s   :(N# #*OOc                |   	 t               \  }}t        j                         }t        j                         }t        j
                  j                  dk(  st        j
                  j                  dk(  r|t        j
                  _        |t        j
                  _        |t        j
                  _        |t        j
                  _	        t        j                  dk7  rFt        j                  t        j                  j                  t        j                  j                         	 t        j                  dk(  r t        j                   j#                  d       yyt        j$                  j'                         \  }}t        j)                  | t        j+                         ||||||t        j
                  j                  t        j
                  j                  t        j
                  j                  t        j
                  j                  t        j,                         |t        j
                  j                  z
  }|t        j
                  _        |t        j
                  _        |t        j
                  _        |t        j
                  _	        t        j                  dk7  r8t        j.                  j0                  rt        j.                  j3                  |      \  }	}
|	r#t        j5                  t6        j8                         |
dkD  r%t;        |
t        j                  j                        }n8t        j                  j                  }t        j.                  j=                          t        j                  t        j                  j                  |       nFt        j                  t        j                  j                  t        j                  j                         t        j                  dk(  r t        j                   j#                  d       yy# t        j                  dk(  r t        j                   j#                  d       w w xY w)zHandle CPU signals.r   r)   N)r   r   process_timeperf_counterrC   _Scalene__last_signal_timevirtual	wallclockr>   userr   r  rf   r  r   r   rW   r   _Scalene__gpu	get_statsprocess_cpu_samplecompute_frames_to_recordrq   rw   is_setyield_next_delayr   r  SIGUSR1minreset)r   r   now_sysnow_usernow_virtualnow_wallclockgpu_loadgpu_mem_usedelapsedshould_raiseremaining_timeto_waits               r6   r  zScalene.cpu_signal_handler  s   K	2 )GX++-K --/M**22a7--771< 6A**27D**418**.2:**/<<7*,,))::88 n ||w&''++D1 'k (/}}'>'>'@$X| &&002**22**44**..**//,, $g&@&@&J&JJG1<G&&.3@G&&0-4G&&*.6G&&+||w&''..  ,,==gF$&#33FNNC &)"%*GNN,L,L# #*.."B"B,,224,,))::
 ,,))::88
 ||w&''++D1 's||w&''++D1 's   DP I?P 5P;c                6   t         j                  j                  r8t         j                  j	                  t         j
                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  | t         j                  j                  t         j                  j                  
      }d|v ryt         j                  j                  }|st         j"                  dk(  rd}nd}t%        |d      5 }|j'                  t        j(                  |dd	      d
z          ddd       |i k7  S t         j                  }t         j                  j*                  }t         j                  j,                  sUt/        j0                  t2              5  dt         j4                  v rd}nddl}|j9                         j:                  }ddd       |j	                  |t         j                  t         j                  t         j                  t         j                  t         j                  | t         j                  j                  t         j                  j                  	      }|S # 1 sw Y   |i k7  S xY w# 1 sw Y   xY w)zMOutput the profile. Returns true iff there was any info reported the profile.)profile_memoryr'  is_childTr)   CONz/dev/stdoutw   )	sort_keysindentr*  N	ipykernel   r   )rC   r   json_Scalene__jsonoutput_profilesrY  r   rm   profile_this_coderi   _Scalene__program_path_Scalene__entrypoint_dirr   r'  _Scalene__outputoutput_filer>   r   openwritedumpscolumn_widthr%  r   r   r   modulesshutilget_terminal_sizecolumns)program_argsjson_outputoutfilerZ   outputr  r  
did_outputs           r6   r   zScalene.output_profile0  s   
 >>!..8800))**&&((&~~44 ' > > 9 K" [(&&22G<<7*#G+Ggs# qJJ{d1EL "$$ %%F">>66L>>&&((3 J"ckk1'*%'-'?'?'A'I'IJ  &55))**&&&~~44 ' > >  6 
 J = "$$J Js   +I?14J?JJc                :    d t         j                  |    D        }|S )Nc              3  F   K   | ]  }t        j                  |        y wr2   )inspectgetsourcelines)r.  fns     r6   r1  z(Scalene.get_line_info.<locals>.<genexpr>v  s#      
 ""2&
r2  )rC   r\   )r   	line_infos     r6   get_line_infozScalene.get_line_infoq  s$    

44U;
	 r8   c                    t         j                  sy| t         j                  vryt         j                  |       }t        fd|D              }|S )zIWhen using @profile, only profile files & lines that have been decorated.TFc              3  \   K   | ]#  \  }}|cxk  xr |t        |      z   k  nc  % y wr2   )len)r.  lines
line_startr   s      r6   r1  z,Scalene.profile_this_code.<locals>.<genexpr>  s3      
#
 &::E
#:::
s   ),)rC   r[   r  any)r   r   r  found_functions    `  r6   r  zScalene.profile_this_code|  sQ     ))222))%0	 
'0
 
 r8   c                 J    t        t        j                  j                         y r2   )r   rC   r   stacksr3   r8   r6   print_stackszScalene.print_stacks  s    goo$$%r8   c           	        |t         j                  k\  rt         xj                  t         j                  j                  z  c_        t         j                  }t        j                         5 }t         j                  D cg c]  }|j                  |j                         }}|j                          t         j                          |j                          ddd       |sy||z
  }||	z
  }||z
  }t        |dk  |dk  |dk  g      ryd}|dk7  r||z  }|t         j                  z  }|dkD  rd}|}t        j                   |      rd}|t         j                  j"                  z  }t         j                  xj$                  |z  c_        t         j                  j"                  }||z
  }t'        |d      }||z   }t)        fd|D              }|dk(  rd}||z  }|d   d   }||z  }||z  }||z  }||z   |z  } t         j                  j*                  r6t-        |t         j.                  t         j                  j*                  |||        t         j1                  |t         j                         t3        |j4                  j6                        }!t9        |j:                        }"t=        t>        tA        jB                         jD                        }#|#   s*t         j                  jF                  |!   |"xx   |z  cc<   t         j                  jH                  |!   |"xx   |z  cc<   t         j                  jJ                  |!xx   | z  cc<   t         j                  jL                  |!   |"   jO                  |       t         j                  jP                  |!   |"   jO                  |       t         j                  jR                  |!   |"xx   |z  cc<   t         j                  jT                  |!   |"   jO                  |       |D ]  \  }$}%}&|$|k(  rt-        |$t         j.                  t         j                  j*                  |||        t3        |$j4                  j6                        }!t9        |$j:                        }"t         j1                  |$t         j                         |%   rtW        jX                  |&j4                  t[        |&j\                              r)t         j                  jH                  |!   |"xx   |z  cc<   n(t         j                  jF                  |!   |"xx   |z  cc<   t         j                  jJ                  |!xx   |z  cc<   t         j                  jL                  |!   |"   jO                  |       t         j                  jP                  |!   |"   jO                  |        |dd= ~t         j                  xj^                  |z  c_/        yc c}w # 1 sw Y   xY w)z$Handle interrupts for CPU profiling.Nr           g      ?c              3  2   K   | ]  \  }}}|      y wr2   r3   )r.  r   tident
orig_frameis_thread_sleepings       r6   r1  z-Scalene.process_cpu_sample.<locals>.<genexpr>  s'      
)vz #6**
s   r=   )0rC   rl   r   profile_intervalr   r   	ExitStackrv   enter_contextlock
stop_clockr   start_clockr  rN   mathisnanr   total_gpu_samplesmaxsumr  	add_stackr   r   r%   r   r   r&   r   r   rM   r	  main_threadidentcpu_samples_pythoncpu_samples_ccpu_samplescpu_utilizationpushcore_utilizationgpu_samplesgpu_mem_samplesr   is_call_functionr$   r   total_cpu_samples)'_signum
new_framesrs  rt  rq  rr  ru  rv  prev_virtualprev_wallclock	_prev_sys	prev_userr  statsstackr  r(  elapsed_virtualelapsed_wallclockelapsed_userr  r  gpu_timepython_timec_time
total_timetotal_framesnormalized_timemain_thread_frameaverage_python_timeaverage_c_timeaverage_gpu_timeaverage_cpu_timer   r   main_tidr   r  r  s'               `                          r6   rj  zScalene.process_cpu_sample  s6   4 G666 &&'..*I*II&OOE%%' $5:A:M:MNQU((0NN  "&&(!!#	$ ( &4)N:  )+!#%6%:L1<LMN !*->>O
 +W-D-DDS !O , ::hHgnn>>>))X5)nn66 ;.VQ 6)

  
-7
 

 1L$|3 'qM!,)L8,.#l2'&0L@>>  !$$&&#  	##$5wG*11==>-667 Y224::;!(+OO..u5$%  OO))%08NJ8OO''.2BB.OO++E26:?? OO,,U3F;@@  OO''.v6:JJ6OO++E26:??M ,6 )	'UFJ))e**oo,,)$&( U\\556E/F''w? "&)00!!j001
 --e4V<O< 2259$%  OO''./A.OO++E26:?? OO,,U3F;@@ O)	X qM))Z7)G O$ $s   *W="W6WWW!c                 p   t        j                         D  cg c]}  } | t        j                         k7  rdt        t        t        j                         j                  t        t        | j                        d            t        t        | j                        f }} t        t        t        j                         j                        }|j                  dt        j                         j                  |t        t        d            |f       g }|D ]  \  }}|}|s|j                  j                  }|j                  j                  }|sOt        t        |j                        }	t        |	j                  j                        }|	j                  j                  }t         j#                  ||      sc|rt        t        |j                        }nnE|r,|j                  j                  }|j                  j                  }t         j#                  ||      sc|s|j%                  |||f        |dd= |S c c} w )z9Collect all stack frames that Scalene actually processes.Nr   )r	  	enumerater  r   r   r>   _current_framesr   rM   r  insertr   r   r   r   r%   rC   r   r   )
r  framesr   r  r   r  r  r   rF   backs
             r6   rk  z Scalene.compute_frames_to_recordX  s    ((*
/
 I))++ '')--d3.@$G S!''"
/
 
/
 3	--/556##%))#tIt/DE	
 >@
% 	?OUFJLL,,E<<''D Iu||4 !8!89{{****5$7
  ELL9E!LL44E <<//D **5$7 !!5&*"=>7	?8 1Ii
/
s   BH3c                d   t        | j                  j                        }t        | j                        }| }	 dt        |j                  j
                        v r?t        t        |j                        }|ydt        |j                  j
                        v r?t        j                  |j                  j                  |j                  j
                        syt        |      }|j                  j                  }||j                  |   |<   t        |      |j                  |<   y# t        $ r Y yw xY w)zGUpdate tracking info so we can correctly report line number info later.<N)r%   r   r   r&   r   r   r   r   r   r   rC   r   get_fully_qualified_nameco_firstlinenofunction_mapfirstline_map)r   r  r   r   rZ   fn_name	firstlines          r6   r   zScalene.enter_function_meta  s    
 112ENN+	!(("2"233AHH- 9 !(("2"233 ##AHH$8$8!((:J:JK*1-HH++	,35!&)'1)'<G$  		s   =D# 6!D# #	D/.D/c                   t         j                  }t        j                         }g }t	        j
                  t              5  t         j                  j                         rt         j                  j                         }|j                         dk(  rn|j                  d      \	  }}}}}}	}
}}t        |      t        |      k7  r|j                  t        |      |t        |      t        |      |	t        |
      t!        t        |            t#        t        |            f       t         j                  j                         rddd       |xj$                  t'        |      z  c_        t)        |j*                  d      }|j,                  }d}|D ]P  }|\  }}}}}	}}}|t         j.                  k(  }|t         j0                  z  }|rP|xj*                  |z  c_        |j*                  |j,                  kD  r|j*                  |_        ||_        ||f|_        n~|t         j6                  t         j8                  fv sJ |xj*                  |z  c_        t)        d|j*                        |_        |t         j8                  k(  r|j:                  d   |	k(  r|dz  }t=        j>                         t         j@                  z
  }|jB                  j                  ||j*                  g       S |j*                  }|rk|dk  rA|j:                  \  }}}|dk7  r,|jD                  |   |   \  }}||dz   f|jD                  |   |<   t        d      t!        d      tG        d      f|_        d}t        d      t!        d      tG        d      f} d}!|}"|D ]  }|\  }}}}}	}}}|t         j.                  k(  }|r|tH        jJ                  jL                  dz   k(  rt         jN                  5  t         jP                  jS                  d      \  }#}$ddd       |jT                  #   $xx   dz  cc<   |jV                  |#   |$xx   |jX                  |#   |$   z  cc<   d|jZ                  |#   |$<   d|jX                  |#   |$<   |j\                  |   |   j_                  |       |t         j0                  z  }|rI||z  }|"|z  }"|	}!|j`                  |   |xx   |z  cc<   |jb                  |   |xx   ||z  z  cc<   |jd                  |xx   dz  cc<   |xjf                  |z  c_3        |jZ                  |   |xx   |z  cc<   |jZ                  |   |   |jX                  |   |   kD  r"|jZ                  |   |   |jX                  |   |<   t)        |jX                  |   |   |jZ                  |   |         |jX                  |   |<   t)        |jZ                  |   |   |jh                  |   |         |jh                  |   |<   n|t         j6                  t         j8                  fv sJ |"|z  }"|jj                  |   |xx   |z  cc<   |jl                  |   |xx   dz  cc<   |xjn                  |z  c_7        |jZ                  |   |xx   |z  cc<   t)        d|jZ                  |   |         |jZ                  |   |<   |jp                  |   |   j                  t=        j>                         t         j@                  z
  t)        d|"      g       |dkD  swt        |      t!        |      tG        |!      f}  |jr                  d   ||z
  z   |jr                  d   |z   f|_9        t         jt                  jv                  rZ||j,                  k  rJ|j,                  d	kD  r:| |_        | \  }}}%|jD                  |   |   \  }}|dz   |f|jD                  |   |<   yyyy# 1 sw Y   ~xY w# 1 sw Y   xY w)
z;Handle interrupts for memory profiling (mallocs and frees).rh   ,Nr      r=   0x0r  d   )<rC   r   rE  rR  r   r   FileNotFoundErrorrn   readget_strstriprJ  rM   r   rk   r%   r&   r$   alloc_samplesr  r  current_footprintmax_footprintMALLOC_ACTIONBYTES_PER_MBmax_footprint_python_fractionmax_footprint_locFREE_ACTIONFREE_ACTION_SAMPLEDlast_malloc_triggeredr   monotonic_ns_Scalene__start_timememory_footprint_samples
leak_scorer#   r   r   r   r_   r]   popmemory_malloc_countmemory_aggregate_footprintmemory_current_highwater_markmemory_current_footprint	bytei_mapr   memory_malloc_samplesmemory_python_samplesmalloc_samplestotal_memory_malloc_samplesmemory_max_footprintmemory_free_samplesmemory_free_counttotal_memory_free_samplesper_line_footprint_samplesallocation_velocityr   memory_leak_detector)&xr  curr_pidarr	count_stractionalloc_time_strpython_fraction_strr   pointerreported_fnamereported_lineno	bytei_strbeforeprevmaxfreed_last_triggeritem_alloc_timecountpython_fractionr   r   bytei	is_malloc	timestampafterthis_fnthis_ln	_this_ptrmallocsfreesallocslast_mallocmalloc_pointercurr	last_file	last_liner(  s&                                         r6   r?  z Scalene.alloc_sigqueue_processor  s.    99;  	   !23 	**//1#44<<>	??$* OOC(
"'"#x=CH,

N+i(12 0"3#78%c)n5	# **//1	> 	s3x'
 U,,a0%% (	D 	'"7"77IW)))E''50'**U-@-@@*/*A*AE':IE7/4foE+''//"    ''50' +.a1H1H*I'g99933A6'A&!+&))+g.B.BBI**11++G(	R ''!Q&.3.I.I+)a<%*%5%5g%>w%GNGU	:E$$W-g6
 1+E' |Z]GENC  O	D 	 '"7"77IUg&<&<&S&SVW&WW// M+2+E+E+I+I!+L(IyM )))4Y?1D?00;88CINO  HI..y9)DLM33I>yI OOE"6*..u5W)))E%!(++E26:eC:++E26:#e+: $$U+q0+11U:1..u5f=F=2259&A99%@HI
 66u=fE 77> FI77>vF2259&AF33E:6B =@2259&A..u5f==**51&9
 ''//"    ))%08EA8''.v6!;6//58/..u5f=F=@Cu55e<VDA..u5f= ,,U3F;BB""$w';';;SD\J zUOv&N+WO	b %%a(EFN;%%a(61%
!
 NN//%---##c)*5E'*E61"--e4V<NGU/6{E.BEU#F+ * . 0{	 	lM Ms    D_ #_ _
_	c                 ,    t         j                          y)ztThe parent process should invoke this function just before a fork.

        Invoked by replacement_fork.py.
        N)rC   r   r3   r8   r6   before_forkzScalene.before_fork  s     	""$r8   c                V    t         j                  |        t         j                          y)znThe parent process should invoke this function after a fork.

        Invoked by replacement_fork.py.
        N)rC   r   r   )	child_pids    r6   after_fork_in_parentzScalene.after_fork_in_parent  s     	i(##%r8   c                 n   dt         _        t         j                          t         j                  j	                         rt         j                  j                          t         j                  t         _        dt         j                  vst         j                  j                  st         j                          yy)z
        Executed by a child process after a fork; mutates the
        current profiler into a child.

        Invoked by replacement_fork.py.
        Tr  N)rC   _Scalene__is_childr   rh  has_gpunvml_reinit_Scalene__parent_pidrm   r   r  r  r3   r8   r6   after_fork_in_childzScalene.after_fork_in_child  so     "==  "MM%%',,&gnn.@.@""$ /Ar8   c           
     V   t        j                         }g }t        j                  t              5  t
        j                  j                         rt
        j                  j                         }|j                  d      \  }}}}}	}
t        |      t        |      k7  rl|j                  |t        |	      t        |
      t        |      t        |      f       t
        j                  j                         rddd       |j                          |D ]  }|\  }}}}}t        |      }t        |      }t        |      }t
        j                   j"                  |   |   j%                  |       t
        j                   j&                  |   |xx   t        |      z  cc<    y# 1 sw Y   xY w)z3Process memcpy signals (used in a ScaleneSigQueue).r  N)rE  rR  r   r   
ValueErrorrC   ro   r  r  rJ  rM   r   sortr%   r&   r$   r   r
  r   memcpy_samples)r  r   r  r  r  memcpy_time_str
count_str2r   filenamer   r(  r$  linenum	byteindex_memcpy_timer&  r   line_nobyteidxs                      r6   r@  z!Scalene.memcpy_sigqueue_processor  si    99;46  , 	**//1#44<<>	 OOC(#x=CH,

 FE
O,J **//1	, 	
 	ID@D=Hgy,X&E )G#I.GOO%%e,W599'BOO**51':c%jH:	I1	 	s   CFF(c                     syt         j                   v ryt         j                  rK t         j                  v r8|t         j                      D ch c]  }|j                  j                   c}v ryy	 t        t        j                         j                               }t         j                  j                  s{t        j                         D ]d  }t        j                         D ]K  }t        j                  ||      }t        t        j                  |      j                               }||v sJ  y f t         j                  j                   j#                  d      }t%         fd|D              ry j'                  d      rvddl}	t+        j,                  d       x}
rZ|	j/                         j0                  j2                  t5        |
d	            }t7         d
      5 }|j9                  |       ddd       yt;        t         j                  j<                  j#                  d            }|rt?         fd|D              ry d   dk(  r	 d   dk(  ryt         j                  j                  rytA        tB        jD                  jG                  tB        jD                  jI                  t         jJ                                      t         jJ                   v S c c}w # t        $ r Y yw xY w# 1 sw Y   yxY w)z:Return true if we should trace this filename and function.FTr  c              3  2   K   | ]  }|d k7  s	|v   yw)rh   Nr3   r.  profrH  s     r6   r1  z'Scalene.should_trace.<locals>.<genexpr>  s!      
!%DH
s   
	z_ipython-input-r   Nz_ipython-input-([0-9]+)-.*r=   zw+c              3  &   K   | ]  }|v 
 y wr2   r3   rP  s     r6   r1  z'Scalene.should_trace.<locals>.<genexpr>  s      $
%)D $
s   r  rP   >)&rC   ra   r\   r   r   r`   rL  rM  resolveOSErrorr   profile_all	sysconfigget_scheme_namesget_path_namesget_pathprofile_excluderJ  r  
startswithIPythonrematchget_ipythonhistory_managerinput_hist_rawrM   r  r  ry   profile_onlyallr%   rE  rF  normpathr5  r  )rH  rF   r  resolved_filenamenpthe_pathlibdirprofile_exclude_listr]  resultcell_contentsrZ   profile_only_sets   `             r6   r   zScalene.should_trace  s    ""h.))7999%<<XF KK''   	 #GLL$:$B$B$D E ~~))//1 %"113 %A(11!Q7H X.668F !22$%%  '~~==CCCH 
)=
 
 01 "?JJvJ '')99HHF1I 
 (D) +QGGM*+ w~~::@@EF $
-=$
 !
 A;#(2,#"5 >>%% GGRWW\\'*@*@(KL
 %%11  		@+s$   K4,K K)	K&%K&)K2c                    t         j                  s0t        dt        j                         t        j
                  d       t         j                  j                          t         j                          t        j                         t         _        dt         _        t         j                  j                  rddlm}  | j#                          yy)zInitiate profiling.zERROR: Do not try to invoke `start` if you have not called Scalene using one of the methods
in https://github.com/plasma-umass/scalene#using-scalene
(The most likely issue is that you need to run your code with `scalene`, not `python`).r   r=   Fr   r   N)rC   rR   r   r>   r   rC  r   r  r  r   r  r  _Scalene__doner   r   r   r   set_scalene_done_falser   s    r6   rI   zScalene.start$  s     $$j ZZ	 HHQK##% #002>>  '**, !r8   c                    dt         _        t         j                  j                  rddlm}  | j                          t         j                          t         j                  j                          t         j                  j                  rt        j                  j                  t        j                  j                  t         j                  j                        t        j                  j                  t         j                               t         _        t         j                  j"                  rt         j                  j$                  st         j&                  s	 t)               sdt         j                  _        nMdt         j                  _        dt         j,                  _        t         j                   t         j,                  _        t         j                  j"                  rct         j5                         rNdt         j                  _        dt         j,                  _        t         j                   t         j,                  _        yyyyyy# t2        $ r dt         j                  _        Y w xY w)zComplete profiling.Tr   r   FN)rC   rp  r   r   r   r   set_scalene_done_truedisable_signalsr   r  r  rE  rF  r5  rG  basename_Scalene__profile_filenamer$  r#  r=  r   r  r  r%  r  r   r   r   s    r6   rK   zScalene.stop9  s    >>  '))+ 	!""$>>!!)+ 6 67  !;!;<*G& NNNN&&&&+#~ */GNN& +/GNN',1G$$)3:3M3MG$$0 ~~!!g&8&8&:&*#(-  %/6/I/I  ,	 ';!% ' '   +%*"+s   A-I I&%I&c                 "    t         j                  S )z-Return true if Scalene has stopped profiling.)rC   rp  r3   r8   r6   is_donezScalene.is_donef  s     ~~r8   c                    t         j                  D ]0  }t         j                  |t         j                  j                         2 t         j                          y)zdRespond to a signal to start or resume profiling (--on).

        See scalene_parseargs.py.
        N)rC   rs   _Scalene__orig_killrf   start_profiling_signalrI   r  _this_framer   s      r6   start_signal_handlerzScalene.start_signal_handlerk  s@     %% 	OCW%6%6%M%MN	Or8   c                .   t         j                  D ]0  }t         j                  |t         j                  j                         2 t         j                          t         j                  j                  r$t         j                  t        j                         yy)z]Respond to a signal to suspend profiling (--off).

        See scalene_parseargs.py.
        N)rC   rs   rz  rf   stop_profiling_signalrK   r  r  r   r>   argvr|  s      r6   stop_signal_handlerzScalene.stop_signal_handler}  sf     %% 	NCW%6%6%L%LM	N''""388, (r8   c                   t         j                  dk(  rdt        _        y	 t        j	                  t        j
                  j                  d       t        j
                  j                  t        j
                  j                  t        j
                  j                  fD ]&  }t        j                  |t        j                         ( t        j                          y# t        $ r | rt        j                  d       Y yY yw xY w)zTurn off the profiling signals.r)   FNr   )retry)r>   r   rC   r   r  rf   r  r  r  r  r  r  SIG_IGNr   r   rt  )r  r  s     r6   rt  zScalene.disable_signals  s     <<7"$)G!	5$$W%6%6%G%GK!!//!!--!!// ;
 %%c6>>:; &&( 	5''e'4 	5s   B8C !D ?D c                    t         j                          t        j                  t              5  t         j
                  st         j                  j                          ddd       t        j                  t              5  t        j                  dt        j                                 ddd       y# 1 sw Y   VxY w# 1 sw Y   yxY w)z"When we exit, disable all signals.Nz/tmp/scalene-malloc-lock)rC   rt  r   r   r   rm   ri   cleanuprE  r   rR  r3   r8   r6   rX  zScalene.exit_handler  s     	!  + 	5==**224	5   + 	@II0>?	@ 	@	5 	5	@ 	@s   /B2>+B>2B;>Cc           
        t         j                  j                  rddlm} |j                          dt         j                  vst         j                  j                  s| j                          d}	 t        |||       | j+                          t         j                  j                  r j-                          |j/                          t         j0                  }	t         j3                         \  }
}}|	j4                  |
   |xx   dz  cc<   |	j6                  |
   |xx   |	j8                  |
   |   z  cc<   t         j;                  |      }|st        d	t        j                          t         j                  j<                  s7t        d
t        j                          t        dt        j                          nkt         j                  j>                  st         j                  j@                  rt        dt        j                          nt        dt        j                          |rDt         j                  jB                  r*t         j                  jD                  st         jF                  r|S tI        t         jJ                  t         j                  jL                  rt         j                  jL                  nt         jN                         t         jQ                         rYddl)m*} |jW                  dd      }|st        dt        j                          |S |jY                  |t         jN                         |S t         j                  jZ                  s}t\        j^                  ja                  dd      }t\        j^                  ja                  dd      }t         j                  jL                  rt         j                  jL                  }n6t]        jb                          t\        jd                   t         jN                   }t         jf                  dk(  rt\        jh                  jk                  tl              }to        jp                  t         jr                  | t\        jd                   d|tu        t        jv                  jx                        gtn        jz                  tn        jz                         t\        j^                  j}                  ||d       |S # t        $ r5}t        |j                        t        k(  r|j                  nd}Y d}~d}~wt        $ r t        dt        j                          Y t"        $ rN}t        t         j$                   d|t        j                          t'        j(                          d}Y d}~d}~ww xY w# | j+                          t         j                  j                  r j-                          |j/                          t         j0                  }	t         j3                         \  }
}}|	j4                  |
   |xx   dz  cc<   |	j6                  |
   |xx   |	j8                  |
   |   z  cc<   t         j;                  |      }|st        d	t        j                          t         j                  j<                  s7t        d
t        j                          t        dt        j                          nkt         j                  j>                  st         j                  j@                  rt        dt        j                          nt        dt        j                          |rDt         j                  jB                  r*t         j                  jD                  st         jF                  r|c cY S tI        t         jJ                  t         j                  jL                  rt         j                  jL                  nt         jN                         t         jQ                         rWddl)m*} |jW                  dd      }|st        dt        j                          w |jY                  |t         jN                         w t         j                  jZ                  s~t\        j^                  ja                  dd      }t\        j^                  ja                  dd      }t         j                  jL                  rt         j                  jL                  }n6t]        jb                          t\        jd                   t         jN                   }t         jf                  dk(  rt\        jh                  jk                  tl              }to        jp                  t         jr                  | t\        jd                   d|tu        t        jv                  jx                        gtn        jz                  tn        jz                         t\        j^                  j}                  ||d       w w xY w)z!Initiate execution and profiling.r   r   r  r=   NzScalene execution interrupted.r   r   zCScalene: The specified code did not run for long enough to profile.zSBy default, Scalene only profiles code in the file executed and its subdirectories.zETo track the time spent in all files, use the `--profile-all` option.zaThe patterns used in `--profile-only` or `--profile-exclude` may be filtering out too many files.z[Scalene can only profile code that runs for at least one second or allocates at least 10MB.)profile_fnameoutput_fname)ScaleneJupyteri  i(#  z0Scalene error: could not find an available port.DYLD_INSERT_LIBRARIESrh   
LD_PRELOADzlaunchbrowser.py)stdoutr   )r  r  )?rC   r   r   r   r   populate_structr  rI   exec
SystemExittypecoderM   r   r   r>   r   r   r   r   r   rK   disable_settracedepopulate_structr   rV   r  r  r  r   rV  rc  r[  r$  r#  r=  generate_htmlrv  r  _Scalene__profiler_htmlr   scalene.scalene_jupyterr  find_available_portdisplay_profiler&  rE  rI  r  getcwdseprm   rF  rG  rH  
subprocessPopenrU  r`   r   SCALENE_PORTDEVNULLupdate)rZ  r  the_globals
the_localsleftr   exit_statusser   r  r5  r6  r(  r  r  portold_dyldold_ldr  dirs                       r6   profile_codezScalene.profile_code  s    >>  '##%&gnn.@.@JJLw	{J/ IIK~~$$((*))+ OOE(/(C(C(E%Y	1%%i0;q@;,,Y744Y?	JK  !//5JY
 ~~11
 m ZZ _ ZZ ^^00GNN4R4R }"zz w"zz NN&&****""%88>>)) %^^33,,	 !!#B%99$EL"zz+L G #22g55F ? ~~00  "zz~~.ErJHZZ^^L"=F~~--'.~~'='=  "yy{mBFF8G4K4K3LM % }})
 !ggooh7"(('*?*?-0E"&&9I*J*6*-g.D.D.Q.Q*R*T 1;0B0B0:0B0BD JJ%%5=*0 o  	A%)"'']c%9"''qK  	E2DD 	W,,-S113::F!K	
 IIK~~$$((*))+ OOE(/(C(C(E%Y	1%%i0;q@;,,Y744Y?	JK  !//5JY
 ~~11
 m ZZ _ ZZ ^^00GNN4R4R }"zz w"zz NN&&****""%88>>)) %^^33,,	 !!#B%99$EL"zz+ #22g55 ~~00  "zz~~.ErJHZZ^^L"=F~~--'.~~'='=  "yy{mBFF8G4K4K3LM % }})
 !ggooh7"(('*?*?-0E"&&9I*J*6*-g.D.D.Q.Q*R*T 1;0B0B0:0B0BD JJ%%5=*01 1sQ   0S 	V*T V	 'V-V	 0V8AV;V	 VV	 	G/g:I"gc                   t        t        |       t        _        t	        j
                         t        j                  j                  z   t        _        | j                  t        j                  _        | j                  rYt        j                  j                  t        j                  j                  | j                              t        j                  _        | j                   dk7  t        _        t        j"                  r| j                   nt        j$                         t        _        t        j                  j(                  s+dt        j                  _        dt        j*                  _        yy)zProcess all arguments.r   FN)r   r   rC   r   r   rc  r  rl   r%  r  r  rE  rF  abspath
expanduserr  r   r=  rR  r@  r"  r  r4   s    r6   process_argszScalene.process_args=  s     .5'.."A"AA 	" !%		<<+-77??""4<<0,G( "XX]+2+=+=txx299;~~!!#(G !&GNN "r8   c                     dt         _        y)zKIndicate that Scalene has been initialized and is ready to begin profiling.TN)rC   rR   r3   r8   r6   set_initializedzScalene.set_initializedQ  s     !%r8   c                     t        j                         \  } }t        j                          t        j	                  | |       y)zInitialize and profile.N)r-   
parse_argsrC   r  run_profiler)r5   r  s     r6   mainzScalene.mainV  s7     '')	
!T4(r8   c                    ddl m}  t        j                  j                  j                  d      }| j                  t        t        j                        |z   t        j                  t        j                  j                         y)zFTells the pywhere module, which tracks memory, which files to profile.r   r   r  N)r   r   rC   r   rc  rJ  r   listr[   r  rV  )r   profile_only_lists     r6   r   z!Scalene.register_files_to_profile`  sZ     	$#NN77==cB))++,/@@""NN&&	
r8   c                   |rt         j                          t         j                  s0t        dt        j
                         t	        j                  d       t        j                  dk7  rt         j                  j                  t         j                  ft         j                  j                  t         j                  ffD ]1  \  }}t         j                  ||       t         j                  |d       3 t         j                  t        j                   t         j"                         |rdnt%        j&                  |       }|st)        j*                  t,              5  t/        j0                         t/        j2                  t        j4                  j7                               k7  rt        dt/        j8                          t        j
                         t        dt/        j8                          t        j
                         t        dt/        j8                          t        j
                         d	d	d	       t         j:                  j=                          |t        _        t)        j*                  t,              5  |stA        jB                  d
       d	d	d	       d	}	 t         jE                  |        d	}d}	 tG        t        j>                        dk\  rHt        j>                  d   dk(  r2	 tI        t        j>                  d          t	        j                  d       tG        t        j>                        dk\  rt        j>                  d   dk(  rjd}	t        j>                  ^}
}t        _        tQ        |      \  }
}}
|jR                  stT        t        j>                  jW                  d|jR                         nd}	t        j>                  D cg c]  }tY        jZ                  d|      s| }}t)        j*                  t,              5  |j]                  t        j>                  d   t^        f       d	d	d	       |stT        t.        j`                  jc                  t.        j`                  je                  |d               }tg        |dd      5 }d}	 ti        |jk                         |d      }tm        t.        j`                  jo                  |            }|	s+t        j`                  jW                  d|       |t         _8        tG        | jr                        dkD  r<tm        t.        j`                  jc                  | jr                              t         _:        n|t         _:        t         jv                  jx                  rt         j{                          dd	l>}|j~                  }|j~                  }||d<   d	|d<   |#|j                  }|j                  d      d   |d<   t        j                          t        | tm        |            }	 |j                  ||||      }|st	        j                  |       d	d	d	       t)        j*                  t,              5  t         j                  t         j                  fD ]3  }|j                          t         j                  r$|j                          5 	 d	d	d	       |st	        j                         y	y	# 1 sw Y   xY w# 1 sw Y   DxY w# tJ        $ r- tM        jN                          t	        j                  d       Y w xY wc c}w # 1 sw Y   xY w# tJ        $ r- tM        jN                          t	        j                  d       Y w xY w# t        $ r Y Dt        $ r  t,        $ r}d}|j                  t        |      j                  |j                        }t        |t        j
                         t        tM        j                         t        j
                         Y d	}~d	}~ww xY w# 1 sw Y   xY w# tT        t        f$ rU |rt        d t        j
                         nt        dt        j
                         t	        j                  d       Y Dw xY w# t        $ r5}t        |j                        t        k(  r|j                  nd}Y d	}~d	}~wt        $ r Y t,        $ rI t        dtM        j                         z   t        j
                         t	        j                  d       Y w xY w# 1 sw Y   uxY w# t)        j*                  t,              5  t         j                  t         j                  fD ]3  }|j                          t         j                  r$|j                          5 	 d	d	d	       n# 1 sw Y   nxY w|st	        j                         w w xY w)zSet up and initiate profiling.zERROR: Do not try to manually invoke `run_profiler`.
To invoke Scalene programmatically, see the usage noted in https://github.com/plasma-umass/scalene#using-scalener   r=   r)   FzScalene now profiling process z?  to disable profiling: python3 -m scalene.profile --off --pid z?  to resume profiling:  python3 -m scalene.profile --on  --pid Nforkr   r  z-cz-mTz.*\.py$rzutf-8)encodingrh   r  rH  __spec__r<   __package__z<Scalene: An exception of type {0} occurred. Arguments:
{1!r}z#Scalene: could not find input file z!Scalene: no input file specified.zScalene failed to initialize.
)UrC   r   rR   r   r>   r   rC  r   rf   r{  r~  r  r  r  r  r  SIGINTr   r!   setup_preloadr   r   r   rE  getpgrp	tcgetpgrpr  filenorR  r   	clear_allr  multiprocessingset_start_methodr  r  r  SyntaxErrorr   r   r   originr  r  r^  r_  extendrH  rF  r  r  r  compiler  r%   rG  r  program_pathr  r   r   r   __main____dict__namerJ  gccollectr  r.   r   formatr  __name__r5   
format_excIOErrorr  r  rM   rn   ro   closer=  r  )r5   r  
is_jupyterr  r  did_preloadspecprogsr  moduler(  mod_namer  	prog_nameprog_being_profiledr  r  r  r  r  r  profilerextemplatemessager   mapfiles                              r6   r  zScalene.run_profilern  s\    ""$$$CZZ
 HHQK<<7" %%<<00
 %%;;//	! 8W %%c73++C78 	fmmW-I-IJE^%A%A$%G 	 $$Y/ ::<2<<

0A0A0C#DD:299;-HszzZYZ\ZcZcZeYfg ZZ YZ\ZcZcZeYfg ZZ 	!!#  + 	9008	9 L	&  &EKusxx=A%#((1+*=$SXXa[) HHQKsxx=A%#((1+*=!F .1XX*Ax#( "5X!>JAtQ;;//HHOOAt{{3"F %(HHHqQ0GHH((3 :LL#((1+x!89:++GGOOBGG,>,>uQx,HI	sW LG( "D$&/446%"  $,BGGOOI,F#GL!<8 4@04,,-119GGOOD,=,=>2.
 2>.~~,,99;#!)!2!2J"*"3"3K.7K
+ /3K
+'#yy59ZZ_Q5GM2JJL&tXi-@AHG&.&;&; *k4'  *HH[1CLGx $$Y/ *,,,,  *G MMO"--)** % } 	9 	9 ' $!++-$, I: :" ' $!++-$n 0 ) $ G#b"*//$r(2C2CRWW"MgCJJ7i2243::FF	GQLG LGZ &w/ ?	{KRUR\R\]=CJJO  	?$(LC$7!&&QK# 	 	3i6J6J6LLSVS]S]^HHQK	* *$$Y/ *,,,,  *G MMO"--)** * * % sk  C[9[a< 62` )[  C` 
\&\*` )\.A` `\+*E`+]$,` Add[[ 2\` \` \(#` +2]!` ]!!`$	`-`0`A9` :` ```` A a95a< 8a99a< <	d*b5/d% 5dd% Add% dd% d"%g?Af f 	g f)% g)r   z*Tuple[Filename, LineNumber, ByteCodeIndex])r   r^   )r   rr   )r   z%Tuple[signal.Signals, signal.Signals])r   rQ   )r   zTuple[int, signal.Signals]r   None)r   NUnion[Callable[[signal.Signals, FrameType], None], int, signal.Handlers, None]r   Optional[FrameType]r   r  )r   r   r   r`   r   r`   r   r   )r   rM   r   r  rF   r   r   r   )rF   zCallable[[Any], Any]r   r   )r   rM   r   r  r2   )r[  argparse.Namespacer\  zOptional[Filename]r   r  )r  zOptional[List[str]]r   rQ   )r   r%   r   z,Generator[Tuple[list[str], int], None, None])r   r%   r   r&   r   rQ   )r  r  r  &List[Tuple[FrameType, int, FrameType]]rs  rk   rt  rk   rq  rk   rr  rk   ru  rk   rv  rk   r  rk   r  rk   r  rk   r  rk   r  rp   r   r  )r   r  )r   r   r  r'   r   r  )r  zOptional[List[int]]r   r  )r:  rM   r   r  )r  r  r   r   r   r  )rH  r%   rF   r`   r   rQ   )r  r  r}  r  r   r  )T)r  rQ   r   r  )
r  r`   r  Dict[str, str]r  r  r  	List[str]r   rM   )r5   r  r   r  )F)r5   r  r  r  r  rQ   r   r  )r  
__module____qualname____doc____annotations__r  rE  sched_getaffinityrN   r   	cpu_countr   r  r   r=  r@  rR   r%   r&   r$   rU   r>   
executablerU  staticmethodrV   r   _Scalene__gui_dirrv  r  r   rA  rB  rW   r  r  r  r  ry   r[   r
   r\   r	  r
  r5  _Scalene__original_thread_joinLockrc   r   r   r"   rf   r'   r   r    r  r   r  r,   rh  r>  r"  r]   rd   rg   r   rd  r  r  rk   rl   rm   rY  rQ   rq   rs   r   rw   r  r  _exitr   raise_signalr   r   killrz  r   	setitimerr  siginterruptr  r{   r}   r   r   r   r   r   r   r   r   r   classmethodr   r   r   rD   r   r   r   r   r   r   r   r   r   r  r  r  r  r`  r  r   r   	lru_cacher  r  r  rj  rk  r   r?  r8  r;  rA  r@  r   rp  rI   rK   rx  r~  r  rt  rX  r  r  r  r  r   r  r3   r8   r6   rC   rC      s	   & 82b22156
 LLJLM4'Aa8HIONNM
 
 #(I!.1~.O7O 	    LMK ),-7B37G4G '--22  nnOF I!GH]FLE==?HLFJ<>9>&&) ) ! ! " b\N| %$
 !&e,E3N$$$$  (| -8-/  	   
 '&''++'9';L$;MMM((K -- ''K
||w!++$118 8 9 9 , ,, , 5 5 $ $ $ $ 
 

  (
  

  
  A A " " ''"%'-0'	' 'R  
     ' '
    2 1 1 2 2 MF F  
  
 9
9 (9 
9 9 1
1 (1 
1 1f 
 ( 
  
 ( 
    
 
6 6:kP%kP !3kP 
	kPZ U2
U2 (U2 
U2 U2n > >@ Y	5     & & D8
D8 ;D8 D8 D8 D8 D8 D8 D8 D8 D8  !D8" #D8$ ,%D8& 
'D8 D8N 6 6p ==!2=	= =4 UC UCn % % & & % %" ,I
,I ,I 
,I ,I\ YH2  H2T F- -( *J *JX   
 ) 
 " -
- )- 
- -( 5 5& @ @JJ $J #	J
 J 
JX ' '& % % ) ) 

 

 FK& &(1&?C&	& &_6  8 BLLN	'0)a8s   R* *S
	S
rC   r  )r5   r   r   r  )r@   zTuple[int, int]r   r  r  r  )q
__future__r   	cysignalsModuleNotFoundErrorargparserV  builtinsr   r   r  	importlibr  r  r  r  rE  rL  r   rA  r^  r  statr  r>   rW  rN  r	  r   r   
webbrowserrich.consoler   scalene.find_browserr   scalene.get_module_detailsr   scalene.time_infor   r   scalene.redirect_pythonr	   collectionsr
   dataclassesr   importlib.abcr   importlib.machineryr   typesr   r   typingr   r   r   r   r   r   r   r   r   r   r   scalene.scalene_configr   scalene.scalene_argumentsr   scalene.scalene_client_timerr   scalene.scalene_funcutilsr   scalene.scalene_jsonr   scalene.scalene_mapfiler   scalene.scalene_outputr    scalene.scalene_preloadr!   scalene.scalene_signalsr"   scalene.scalene_statisticsr#   r$   r%   r&   r'   scalene.scalene_utilityresourcesystemscalene.scalene_apple_gpur+   r,   scalene.scalene_gpuscalene.scalene_parseargsr-   r.   scalene.scalene_sigqueuer/   consoler7   logMINIMUM_PYTHON_VERSION_MAJORMINIMUM_PYTHON_VERSION_MINORrA   rG   rD   rI   rK   rC   r  r  r3   r8   r6   <module>r&     s   " 	      	      	    	    
       ! - : 1 3 # ! & * %     6 ; 6 , 2 0 2 2  &<<78?? G. L 4

(	     N ,.JK L! , 

U& U&p< zLLN y@  		s   F2 2F;:F;