
    |h*                        S r SSKJr  SSKrSSKrSSKrSSKJrJr   SSK	r	Sr
\
rSS jrSS jr " S	 S
5      rg! \ a    Sr
 N!f = f)a  
Memory monitoring utilities for measuring memory usage.

Example usage:
    tracker = MemoryTracker("my_function")
    with tracker.monitor():
        my_function()
    # Access data: tracker.rss_delta, tracker.duration, etc.
    # Get formatted string: tracker.get_summary()
    )annotationsN)DictOptionalTFc                 ~    [         (       a"   [        R                  " 5       n U R                  $ g! [         a     gf = f)z
Get current available system memory in bytes.

Used for memory threshold checking in parallel test execution.

Returns:
    int or None: Available memory in bytes, or None if unavailable
N)_HAS_PSUTILpsutilvirtual_memory	available	Exception)sys_mems    P/home/james-whalen/.local/lib/python3.13/site-packages/numba/misc/memoryutils.pyget_available_memoryr      sD     {	++-G$$$   		s    / 
<<c                 z   0 n [         (       am   [        R                  " [        R                  " 5       5      nUR                  5       nUR                  U S'   [        R                  " 5       nUR                  U S'   SU ;  a  SU S'   SU ;  a  SU S'   U $ ! [        R                  [        R                  4 a     N>f = f)a>  
Get memory usage information needed for monitoring.

Returns only RSS and available memory which are the fields
actually used by the MemoryTracker.

Returns:
    dict: Memory usage information including:
        - rss: Current process RSS (physical memory currently used)
        - available: Available system memory
rssr
   N)r   r   Processosgetpidmemory_infor   r	   r
   NoSuchProcessAccessDenied)r   processmem_infor   s       r   get_memory_usager   /   s     K{	nnRYY[1G**,H!)K ++-G'.'8'8K$ K!E+%#'K  $$f&9&9: 		s   A,B $B:9B:c                      \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S
\S'   S
\S'   S\S'   S\S'   SS jr\R                  S 5       r	SS jr
Srg)MemoryTrackerT   z
A simple memory monitor that tracks RSS delta and timing.

Stores monitoring data in instance attributes for later access.
Each instance is typically used for monitoring a single operation.
intpidstrnamezfloat | None
start_timeend_timezDict[str, int | None] | Nonestart_memory
end_memorydurationz
int | None	rss_deltac                    [         R                  " 5       U l        Xl        SU l        SU l        SU l        SU l        SU l        SU l	        g)z6Initialize a MemoryTracker with empty monitoring data.N)
r   r   r   r    r!   r"   r#   r$   r%   r&   )selfr    s     r   __init__MemoryTracker.__init__d   s?    99;	     c              #    #    [         R                   " 5       U l        [        5       U l         U v   [         R                   " 5       U l        [        5       U l        U R                  U R                  -
  U l        U R                  R                  SS5      nU R
                  R                  SS5      nU(       a  U(       a  X!-
  OSU l        g! [         R                   " 5       U l        [        5       U l        U R                  U R                  -
  U l        U R                  R                  SS5      nU R
                  R                  SS5      nU(       a  U(       a
  X!-
  U l        f SU l        f = f7f)a_  
Context manager to monitor memory usage during function execution.

Records start/end memory usage and timing, calculates RSS delta,
and stores all data in instance attributes.

Args:
    name (str): Name/identifier for the function or operation being
                monitored

Yields:
    self: The MemoryTracker instance for accessing stored data
r   r   N)	timer!   r   r#   r"   r$   r%   getr&   )r(   	start_rssend_rsss      r   monitorMemoryTracker.monitoro   s      ))+,.	?J !IIKDM.0DO MMDOO;DM ))--eQ7Ioo))%3G!*w  '2<= N !IIKDM.0DO MMDOO;DM ))--eQ7Ioo))%3G!*w  '2DN<= Ns   *E-C
 BE-
B E**E-c                N   U R                   b  U R                  c  [        S5      eU R                  R                  S5      nSS jn[        R
                  " S[        R                  " U R                  5      5      nU R                   R                  SS5      nU R                  R                  SS5      nSU R                   3SU R                   3S	U 3S
U R                  S S3SU" U5       3SU" U5       3SU" U R                  SS9 3SU" U5       3/nSR                  U5      $ )at  
Return a formatted summary of the memory monitoring data.

Formats the stored monitoring data into a human-readable string
containing name, PID, RSS delta, available memory, duration,
and start time.

Returns:
    str: Formatted summary string with monitoring results

Note:
    Should be called after monitor() context has completed
    to ensure all data is available.
z$Memory monitoring data not availabler
   c                    U c  gU S:X  a  gSnU(       a
  U S:  a  SOSn[        U 5      n S H  nU S:  a  U U S	 S
U 3s  $ U S-  n M     U U S	 S3$ )z&Convert bytes to human readable formatzN/Ar   z0 B -+)BKBMBGBg      @z.2f z TB)abs)	bytes_val	show_signsignunits       r   format_bytes/MemoryTracker.get_summary.<locals>.format_bytes   s{     A~D'!msII/v%"VIc?!D6::V#	 0 VIc?#..r+   z%H:%M:%Sr   r   zName: zPID: zStart: z
Duration: z.3fszStart RSS: z	End RSS: zRSS delta: T)r?   zAvail memory: z | )F)r#   r$   
ValueErrorr.   r-   strftime	localtimer!   r    r   r%   r&   join)r(   current_availablerB   start_tsr/   r0   bufs          r   get_summaryMemoryTracker.get_summary   s%    $(?CDD OO//<	/$ ==T^^DOO-LM%%))%3	//%%eQ/ TYYK DHH:hZ s+1-,y123W-./,t~~FGH\*;<=>	
 zz#r+   )r%   r$   r"   r    r   r&   r#   r!   N)r    r   )returnr   )__name__
__module____qualname____firstlineno____doc____annotations__r)   
contextlibcontextmanagerr1   rL   __static_attributes__ r+   r   r   r   T   s\     
H
I..,,	 ? ?@4r+   r   )rN   zOptional[int])rN   zDict[str, Optional[int]])rS   
__future__r   r   rU   r-   typingr   r   r   r   ImportErrorIS_SUPPORTEDr   r   r   rX   r+   r   <module>r]      s]   	 # 	   !K
 &"Jp p  Ks   ; AA