U
    /er                     @  sv   d dl mZ d dlZd dlmZ d dlmZ d dlZd dlZd dl	m
Z
 d dlmZ d dlmZmZ G dd	 d	ZdS )
    )annotationsN)deque)Any)WINDOWS)nvml)	monotonictimec                   @  s   e Zd ZU ded< ded< ded< ded< d	ed
< ded< ded< ded< ded< ded< ded< ded< ded< d+ddddddZddddZddd d!Zddd"d#Zd$dd%d&Zdd'd(d)d*Z	dS ),SystemMonitorzpsutil.Processprocz
int | Nonemaxlenintcountfloat	last_timezdict[str, deque[float]]
quantitiesboolmonitor_net_iomonitor_disk_iomonitor_host_cpur   _last_net_io_counters_last_disk_io_counters_last_host_cpu_countersz
str | Nonegpu_namegpu_memory_total   Nzbool | None)r   r   r   c                 C  s  t  | _d| _|| _t | _t|dt|dt|dd| _zt 	 | _
W n tk
rf   d| _Y n(X d| _t|d| jd< t|d| jd< |d krtjd}|rzt  }W n tk
r   d}Y n6X |d krd}n&|| _t|d| jd	< t|d| jd
< || _|d krtjd}|| _|rZt   | _}|jD ]}t|d| jd| < q>tspt|d| jd< t dkrt }|d | _|d | _t|d| jd< t|d| jd< nd | _d| _|   d S )Nr   )r   cpumemoryr   FThost_net_io.read_bpshost_net_io.write_bpsz%distributed.admin.system-monitor.diskhost_disk_io.read_bpshost_disk_io.write_bpsz)distributed.admin.system-monitor.host-cpu	host_cpu.num_fdsnamezmemory-totalgpu_utilizationgpu_memory_used)psutilProcessr
   r   r   r   r   r   r   net_io_countersr   	Exceptionr   daskconfiggetdisk_io_countersr   r   r   	cpu_timesr   _fieldsr   r   Zdevice_get_countZone_timer   r   update)selfr   r   r   disk_iocZ	hostcpu_ckZ	gpu_extra r6   >/tmp/pip-unpacked-wheel-g426oqom/distributed/system_monitor.py__init__#   s`    





zSystemMonitor.__init__zdict[str, Any])returnc                 C  s   dd | j  D S )Nc                 S  s   i | ]\}}||d  qS )r'   r6   ).0r5   vr6   r6   r7   
<dictcomp>l   s      z(SystemMonitor.recent.<locals>.<dictcomp>)r   itemsr3   r6   r6   r7   recentk   s    zSystemMonitor.recentc                 C  s   | j  jS )zSample process memory, as reported by the OS.
        This one-liner function exists so that it can be easily mocked in unit tests,
        as the OS allocating and releasing memory is highly volatile and a constant
        source of flakiness.
        )r
   Zmemory_infoZrssr>   r6   r6   r7   get_process_memoryn   s    z SystemMonitor.get_process_memoryc              	   C  s  t  }t }|| j pd}|| _|  jd7  _| j  | j |  |d}W 5 Q R X | jrt	
 }| j}|j|j | |d< |j|j | |d< || _| jrt	 }|d k	st| j}|j|j | |d< |j|j | |d< || _| jr>t	 }	| j}
|	jD ]0}t|	|t|
| }t|| d|d	| < q|	| _tjd
krX| j |d< | jrt }|d |d< |d |d< |  D ]$\}}|dkr| j!| "| q|S )NgMbP?   r   r   r   r    r!      r"   win32r#   Zutilizationr%   zmemory-usedr&   r   )#r   r   r   r   r
   ZoneshotZcpu_percentr@   r   r(   r*   r   Z
bytes_recvZ
bytes_sentr   r/   AssertionErrorr   
read_byteswrite_bytesr   r0   r   r1   getattrroundsysplatformr#   r   r   Z	real_timer=   r   append)r3   nowZnow_monodurationresultZnet_ioclastr4   Z	last_diskZhost_cpuZlast_cpur5   deltaZgpu_metricsr$   r;   r6   r6   r7   r2   v   s`    





zSystemMonitor.updatestrc                 C  s8   d| j d d | j d d d tr&dn| j d d f S )Nz.<SystemMonitor: cpu: %d memory: %d MB fds: %s>r   r'   r   g    .AzN/Ar#   )r   r   r>   r6   r6   r7   __repr__   s
    zSystemMonitor.__repr__zdict[str, list])startr9   c                   sT   || j krdd | jD S tdtt| jd  || j    fdd| j D S )Nc                 S  s   i | ]
}|g qS r6   r6   r:   r5   r6   r6   r7   r<      s      z-SystemMonitor.range_query.<locals>.<dictcomp>r'   r   c                   s*   i | ]"\} | fd dt dD qS )c                   s&   g | ]}| t  kr | nd qS )N)len)r:   ir;   r6   r7   
<listcomp>   s     z8SystemMonitor.range_query.<locals>.<dictcomp>.<listcomp>r   )rangerT   istartrW   r7   r<      s    )r   r   minmaxrU   r=   )r3   rS   r6   rZ   r7   range_query   s    
"
zSystemMonitor.range_query)r   NN)
__name__
__module____qualname____annotations__r8   r?   r@   r2   rR   r^   r6   r6   r6   r7   r	      s,   
   H>r	   )
__future__r   rI   collectionsr   typingr   r(   r,   Zdistributed.compatibilityr   Zdistributed.diagnosticsr   Zdistributed.metricsr   r   r	   r6   r6   r6   r7   <module>   s   