
    &`iX                     n    d dl Z d dlmZ  e j        e          ZdZdZdZdZ	da
dadad Zd Zd Zd	 ZdS )
    N)get_num_cpusz$/sys/fs/cgroup/cpuacct/cpuacct.usagez/sys/fs/cgroup/cpu.statz
/proc/statc                  b   	 t                      } t                      }t          d}nJ| t          z
  }|t          z
  t	                      z  }||z  }t          |dz  t                      z  d          }|a| at          |d          S # t          $ r t          
                    d           Y dS w xY w)a  Estimate CPU usage percent for Ray pod managed by Kubernetes
    Operator.

    Computed by the following steps
       (1) Replicate the logic used by 'docker stats' cli command.
           See https://github.com/docker/cli/blob/c0a6b1c7b30203fbc28cd619acb901a95a80e30e/cli/command/container/stats_helpers.go#L166.
       (2) Divide by the number of CPUs available to the container, so that
           e.g. full capacity use of 2 CPUs will read as 100%,
           rather than 200%.

    Step (1) above works by
        dividing delta in cpu usage by
        delta in total host cpu usage, averaged over host's cpus.

    Since deltas are not initially available, return 0.0 on first call.
    Ng        d      g      Y@z0Error computing CPU usage of Ray Kubernetes pod.)
_cpu_usage_system_usagelast_system_usagelast_cpu_usage_host_num_cpusroundr   min	Exceptionlogger	exception)	cpu_usagesystem_usagecpu_percent	cpu_deltasystem_deltaquotients         k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/dashboard/k8s_utils.pyr   r      s    &LL	$$KK!N2I(+<<@P@PPL </H3 ?CCK(";&&&   KLLLsss   BB $B.-B.c                  d   	 t          t          t                                                              S # t          $ rp t          t
                                                    } |                     d          d         }t          |                                d                   }|dz  cY S w xY w)z|Compute total cpu usage of the container in nanoseconds
    by reading from cpuacct in cgroups v1 or cpu.stat in cgroups v2.
r   r   i  )intopenCPU_USAGE_PATHreadFileNotFoundErrorCPU_USAGE_PATH_V2split)cpu_stat_textcpu_stat_first_linecpu_usecs      r   r   r   ;   s    4'',,../// 	 	 	.//4466+11$77: *00221566$	s   25 A7B/.B/c                     t          t                                                                        d          d         } |                                 }|d         dk    sJ |dd         }t	          d |D                       }|dz  }|S )a+  
    Computes total CPU usage of the host in nanoseconds.

    Logic taken from here:
    https://github.com/moby/moby/blob/b42ac8d370a8ef8ec720dff0ca9dfb3530ac0a6a/daemon/stats/collector_unix.go#L31

    See also the /proc/stat entry here:
    https://man7.org/linux/man-pages/man5/proc.5.html
    r   r   cpur      c              3   4   K   | ]}t          |          V  d S )N)r   ).0entrys     r   	<genexpr>z _system_usage.<locals>.<genexpr>[   s(      ??5CJJ??????    i )r   PROC_STAT_PATHr   r    sum)cpu_summary_strparts
usage_datatotal_clock_ticksusage_nss        r   r   r   M   s     >**//1177==a@O!!##E8uqsJ??J????? 5(HOr+   c                      t           ct          t                                                                        d          } d | D             }d |D             }t          |          dz
  a t           S )z8Number of physical CPUs, obtained by parsing /proc/stat.Nr   c                 6    g | ]}|                                 S  )r    )r(   lines     r   
<listcomp>z"_host_num_cpus.<locals>.<listcomp>f   s      J J J$ J J Jr+   c                 L    g | ]!}t          |          d k    d|d          v |"S )r   r%   )len)r(   
split_lines     r   r7   z"_host_num_cpus.<locals>.<listcomp>g   sA     
 
 
:""u
1'='= '='='=r+   r   )host_num_cpusr   r,   r   r    r9   )proc_stat_linessplit_proc_stat_lines	cpu_liness      r   r   r   a   s{     ~..3355;;DAA J J/ J J J
 
3
 
 
	 I*r+   )loggingray._private.utilsr   	getLogger__name__r   r   r   r,   container_num_cpusr;   r
   r	   r   r   r   r   r5   r+   r   <module>rD      s     + + + + + +		8	$	$7-   & & &R  $  (    r+   