o
    va0                  
   @   s   d Z ddlmZmZ ddlmZ ddlmZ zddlm	Z
 W n ey: Z zdZede W Y dZ[ndZ[ww dZd	d
ddddddgZG dd deZdd Zdd Zdd Zdd Zdd ZdS )z(GPU plugin (limited to NVIDIA chipsets).    )	nativestrto_fahrenheit)logger)GlancesPluginNTz6Missing Python Lib ({}), Nvidia GPU plugin is disabledFproczGPU processor%)namedescriptionZy_unitmemzMemory consumptionc                       sp   e Zd ZdZd fdd	Zdd Zdd Zejej	d	d
 Z
 fddZdddZdd Z fddZ  ZS )PluginzqGlances GPU plugin (limited to NVIDIA chipsets).

    stats is a list of dictionaries with one entry per GPU
    Nc                    s(   t t| j||g d |   d| _dS )zInit the plugin.)argsconfigZstats_init_valueTN)superr   __init__init_nvidiaZdisplay_curse)selfr   r   	__class__ =/usr/lib/python3/dist-packages/glances/plugins/glances_gpu.pyr   4   s   
zPlugin.__init__c                 C   sP   t rd| _zt  t | _d| _W | jS  ty'   td d| _Y | jS w )zInit the NVIDIA API.FTz pynvml could not be initialized.)	import_error_tag
nvml_readypynvmlZnvmlInitget_device_handlesdevice_handles	Exceptionr   debugr   r   r   r   r   @   s   
zPlugin.init_nvidiac                 C   s   dS )zReturn the key of the list.gpu_idr   r   r   r   r   get_keyO   s   zPlugin.get_keyc                 C   s@   |   }| js
| jS | jdkr|  }n| jdkr	 || _| jS )zUpdate the GPU stats.ZlocalZsnmp)Zget_init_valuer   statsZinput_methodget_device_stats)r   r    r   r   r   updateS   s   !


zPlugin.updatec                    s   t t|   | jD ]]}i i i d| j||   < d|v r3| j|d dd}|| j||    d d< d|v rM| j|d dd}|| j||    d d< d|v rg| j|d dd}|| j||    d d< q
dS )zUpdate stats views.)r   r
   temperaturer   )header
decorationr
   r#   T)r   r   update_viewsr    Zviewsr   Z	get_alert)r   iZalertr   r   r   r&      s"   
zPlugin.update_viewsc              
      s  g } j r j g ks  r|S t fdd j D } j d }d}t j dkr4|dt j 7 }|rA|dd|d	 7 }n|d
d7 }|dd }| |d t j dksb|jr~|   zt	dd  j D t j  }W n t
y   dd}	Y nw d|}	t j dkrdd}ndd}| | | |	 j|   ddd |   zt	dd  j D t j  }
W n t
y   dd}Y nw d|
}t j dkrdd}ndd}| | | | j|   ddd |   zt	dd  j D t j  }W n t
y;   dd}Y nw d}|jrHt|}d}d||}t j dkr\dd }ndd!}| | | | j|   d"dd |S  j D ]V}|   d
|d# }z	d|d }W n tt
fy   dd}Y nw z	d|d }W n tt
fy   dd}Y nw d$|||}| | q|S )%z2Return the dict to display in the curse interface.c                 3   s&    | ]}|d   j d d  kV  qdS )r   r   N)r    .0sr   r   r   	<genexpr>   s   $ z#Plugin.msg_curse.<locals>.<genexpr>r       z{} z{} {}ZGPUr   z{}N   ZTITLEc                 s        | ]}|d ur|d V  qd S )Nr   r   r(   r   r   r   r+          z{:>4}zN/Az	{:>3.0f}%z{:13}z
proc mean:zproc:r   r%   )itemkeyoptionc                 s   r/   )Nr
   r   r(   r   r   r   r+      r0   z	mem mean:zmem:r
   c                 s   r/   )Nr#   r   r(   r   r   r   r+      r0   CFz
{:>3.0f}{}z
temp mean:ztemperature:r#   r   z{}: {} mem: {})r    Zis_disabledalllenformatappendZcurse_add_lineZmeangpuZcurse_new_linesum	TypeErrorZ	get_viewsr   Z
fahrenheitr   
ValueError)r   r   Z	max_widthretZ	same_nameZ	gpu_statsr$   msgZ	mean_procZmean_proc_msgZmean_memZmean_mem_msgZmean_temperatureZmean_temperature_msgZunitZid_msgZproc_msgZmem_msgr   r   r   	msg_curse   s   
"

"

"

zPlugin.msg_cursec                 C   sp   g }t | jD ].\}}t }|  |d< ||d< t||d< t||d< t||d< t||d< || q|S )zGet GPU stats.r2   r   r   r
   r   r#   )		enumerater   dictr   get_device_nameget_memget_procget_temperaturer9   )r   r    indexdevice_handleZdevice_statsr   r   r   r!     s   zPlugin.get_device_statsc              
      sZ   | j r$zt  W n ty# } ztd| W Y d}~nd}~ww tt| 	  dS )z/Overwrite the exit method to close the GPU API.z(pynvml failed to shutdown correctly ({})N)
r   r   ZnvmlShutdownr   r   r   r8   r   r   exit)r   er   r   r   rH   (  s   zPlugin.exit)NN)__name__
__module____qualname____doc__r   r   r   r   Z_check_decoratorZ_log_result_decoratorr"   r&   r?   r!   rH   __classcell__r   r   r   r   r   .   s    3
or   c                   C   s   dd t t D S )zQGet a list of NVML device handles, one per device.

    Can throw NVMLError.
    c                 S   s   g | ]}t |qS r   )r   ZnvmlDeviceGetHandleByIndex)r)   r'   r   r   r   
<listcomp>9  s    z&get_device_handles.<locals>.<listcomp>)ranger   ZnvmlDeviceGetCountr   r   r   r   r   4  s   r   c                 C   s(   zt t| W S  tjy   Y dS w )zGet GPU device name.ZNVIDIA)r   r   ZnvmlDeviceGetNameZ	NVMlErrorrG   r   r   r   rB   <  s
   rB   c                 C   s4   zt | }|jd |j W S  t jy   Y dS w )z-Get GPU device memory consumption in percent.g      Y@N)r   ZnvmlDeviceGetMemoryInfousedtotal	NVMLError)rG   Zmemory_infor   r   r   rC   D  s   
rC   c                 C   s&   zt | jW S  t jy   Y dS w z*Get GPU device CPU consumption in percent.N)r   ZnvmlDeviceGetUtilizationRatesZgpurT   rQ   r   r   r   rD   M  s
   rD   c                 C   s(   zt | t jW S  t jy   Y dS w rU   )r   ZnvmlDeviceGetTemperatureZNVML_TEMPERATURE_GPUrT   rQ   r   r   r   rE   U  s   rE   )rM   Zglances.compatr   r   Zglances.loggerr   Zglances.plugins.glances_pluginr   Zpy3nvml.py3nvmlZpy3nvmlr   r   rI   r   Zwarningr8   Zitems_history_listr   r   rB   rC   rD   rE   r   r   r   r   <module>   s:     	