o
    ni                     @   s   d dl Z d dlmZ d dlmZmZmZ zd dlZW n ey%   dZY nw ddl	m
Z
mZ ddlmZ ddlmZmZmZ erLd dlmZ d d	lmZ G d
d dZG dd dZG dd dZejG dd dZdS )    N)deque)TYPE_CHECKINGListOptional   )aggregate_lastaggregate_mean)asset_registry)	InterfaceMetricMetricsMonitor)Deque)SettingsStaticc                   @   H   e Zd ZdZdZdeddfddZddd	Zdd
dZde	fddZ
dS )ProcessCpuPercentzECPU usage of the process in percent normalized by the number of CPUs.cpupidreturnNc                 C   s   || _ tg | _d | _d S N)r   r   samplesprocessselfr    r   b/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/sdk/internal/system/assets/cpu.py__init__   s   

zProcessCpuPercent.__init__c                 C   s6   | j d u rt| j| _ | j| j  t   d S r   )r   psutilProcessr   r   appendcpu_percent	cpu_countr   r   r   r   sample!   s   
zProcessCpuPercent.samplec                 C      | j   d S r   r   clearr!   r   r   r   r%   .      zProcessCpuPercent.clearc                 C   s   | j si S t| j }| j|iS r   )r   r   name)r   	aggregater   r   r   r(   1   s   

zProcessCpuPercent.aggregater   N__name__
__module____qualname____doc__r'   intr   r"   r%   dictr(   r   r   r   r   r      s    

r   c                   @   sN   e Zd ZdZdZddee ddfddZddd	Zdd
dZ	de
fddZdS )
CpuPercentz,CPU usage of the system in percent per core.zcpu.{i}.cpu_percentNintervalr   c                 C   s   t g | _|| _d S r   )r   r   r2   )r   r2   r   r   r   r   ?   s   

zCpuPercent.__init__c                 C   s   | j tj| jdd d S )NT)r2   percpu)r   r   r   r   r2   r!   r   r   r   r"   C   s   zCpuPercent.samplec                 C   r#   r   r$   r!   r   r   r   r%   F   r&   zCpuPercent.clearc                    sX   | j si S t| j d }i }t|D ] t fdd| j D }||| jj d< q|S )Nr   c                    s   g | ]}|  qS r   r   ).0r"   ir   r   
<listcomp>O   s    z(CpuPercent.aggregate.<locals>.<listcomp>r5   )r   lenranger   r'   format)r   num_cpucpu_metricsaggregate_ir   r5   r   r(   I   s   zCpuPercent.aggregater   r)   )r+   r,   r-   r.   r'   r   floatr   r"   r%   r0   r(   r   r   r   r   r1   :   s    

r1   c                   @   r   )ProcessCpuThreadsz&Number of threads used by the process.zproc.cpu.threadsr   r   Nc                 C   s   t g | _|| _d | _d S r   )r   r   r   r   r   r   r   r   r   Z   s   

zProcessCpuThreads.__init__c                 C   s.   | j d u rt| j| _ | j| j   d S r   )r   r   r   r   r   r   num_threadsr!   r   r   r   r"   _   s   
zProcessCpuThreads.samplec                 C   r#   r   r$   r!   r   r   r   r%   e   r&   zProcessCpuThreads.clearc                 C   s   | j si S | jt| j iS r   )r   r'   r   r!   r   r   r   r(   h   s   zProcessCpuThreads.aggregater)   r*   r   r   r   r   r?   U   s    

r?   c                   @   s\   e Zd Zdddddejddfdd	Zedefd
dZde	fddZ
dddZdddZdS )CPU	interfacer
   settingsr   shutdown_eventr   Nc                 C   sB   | j j | _t|jt t|jg| _t	| j| j|||| _
d S r   )	__class__r+   lowerr'   r   x_stats_pidr1   r?   metricsr   metrics_monitor)r   rB   rC   rD   r   r   r   r   p   s   
zCPU.__init__c                 C   s   t d uS r   )r   )clsr   r   r   is_available   s   zCPU.is_availablec                 C   sr   t jddt jddd}z!t  jt  jt  jd|d< dd t jdd	D |d
< W |S  ty8   Y |S w )NF)logicalT)r    cpu_count_logicalcurrentminmaxcpu_freqc                 S   s   g | ]}|j |j|jd qS )rN   rN   )r4   freqr   r   r   r7      s    zCPU.probe.<locals>.<listcomp>)r3   cpu_freq_per_core)r   r    rR   rO   rP   rQ   	Exception)r   
asset_infor   r   r   probe   s    




z	CPU.probec                 C   r#   r   )rI   startr!   r   r   r   rX      r&   z	CPU.startc                 C   r#   r   )rI   finishr!   r   r   r   rY      r&   z
CPU.finishr)   )r+   r,   r-   	threadingEventr   classmethodboolrK   r0   rW   rX   rY   r   r   r   r   rA   n   s    

rA   )rZ   collectionsr   typingr   r   r   r   ImportErroraggregatorsr   r   r	   
interfacesr
   r   r   r   "wandb.sdk.internal.settings_staticr   r   r1   r?   registerrA   r   r   r   r   <module>   s&    $