HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/landscape/lib/__pycache__/process.cpython-38.pyc
U

y)�]��@slddlmZddlZddlZddlmZmZddlmZddlm	Z	ddl
mZGdd�de�Z
d	d
�ZdS)�)�absolute_importN)�	timedelta�datetime)�sysstats)�to_timestamp)�detect_jiffiesc@s*eZdZdZd
dd�Zdd�Zdd	�ZdS)�ProcessInformationaI
    @param proc_dir: The directory to use for process information.
    @param jiffies: The value to use for jiffies per second.
    @param boot_time: An alternate value to use for the last boot time.  If
        None, the system last boot time will be used.
    @param uptime: The uptime value to use (for unit tests only).
    �/procNcCsH|dkrt����}|dk	r&t�|�}||_||_|p:t�|_||_	dS)N)
r�	BootTimes�get_last_boot_timer�utcfromtimestamp�
_boot_time�	_proc_dirr�_jiffies_per_sec�_uptime)�self�proc_dirZjiffiesZ	boot_time�uptime�r�7/usr/lib/python3/dist-packages/landscape/lib/process.py�__init__s
zProcessInformation.__init__c	csPt�|j�D]>}zt|�}Wntk
r4YqYnX|�|�}|r|VqdS)z8Get process information for all processes on the system.N)�os�listdirr�int�
ValueError�get_process_info)r�filename�
process_id�process_inforrr�get_all_process_info s

z'ProcessInformation.get_all_process_infocCs�d}tj�|jt|��}d|i}�z*ttj�|d�d�}z"|��}tj�|�	d�d�}W5|��Xttj�|d�d�}�z|D]�}|�	dd	�}|dd
kr�|�
�p�|d	�
�|d<q�|ddkr�|d	�
�}	|	d
kr�|	��}	|	d�d�|d<q�|ddk�r |d	�	�}
t
|
d�|d<q�|ddk�rL|d	�	�}
t
|
d�|d<q�|ddkr�|d	�	�}
t
|
d�|d<�q|q�W5|��Xttj�|d�d�}z�|���	�}t
|d�}t
|d�}t
|d�}
|j�p�t��}t||
|||j�}||d<td||j�}|jdk�r*t�d�W�WdSt|j|�|d<W5|��XWntk
�rdYdSXd|k�r�d|k�r�d|k�r�d|k�r�d|k�r�d|k�s�t�|S)a
        Parse the /proc/<pid>/cmdline and /proc/<pid>/status files for
        information about the running process with process_id.

        The /proc filesystem doesn't behave like ext2, open files can disappear
        during the read process.
        ��pidZcmdline�r�r�status�:��Name�name�StatezT (tracing stop)�ascii�stateZUid�uidZGid�gidZVmSizezvm-size�stat��
�zpercent-cpuNz,Skipping process (PID %s) without boot time.z
start-time)r�path�joinr�str�open�close�readline�basename�split�strip�lower�encoder�readrr�
get_uptime�calculate_pcpurrr
�logging�warningr�IOError�AssertionError)rrZ
cmd_line_nameZprocess_dirr�fileZcmd_line�line�partsr+Zvalue_parts�
start_time�utime�stimer�pcpu�deltarrrr+s�

�

��
�����z#ProcessInformation.get_process_info)r	NNN)�__name__�
__module__�__qualname__�__doc__rrrrrrrrs�
rcCsBd}||}|||}|r,|d||}ttt|d�d�d�S)ao
    Implement ps' algorithm to calculate the percentage cpu utilisation for a
    process.::

    unsigned long long total_time;   /* jiffies used by this process */
    unsigned pcpu = 0;               /* scaled %cpu, 99 means 99% */
    unsigned long long seconds;      /* seconds of process life */
    total_time = pp->utime + pp->stime;
    if(include_dead_children) total_time += (pp->cutime + pp->cstime);
    seconds = seconds_since_boot - pp->start_time / hertz;
    if(seconds) pcpu = (total_time * 100ULL / hertz) / seconds;
    if (pcpu > 99U) pcpu = 99U;
    return snprintf(outbuf, COLWID, "%2u", pcpu);
    r�dg�X@r&)�round�max�min)rHrIrrGZhertzrJZ
total_time�secondsrrrr?�sr?)�
__future__rr@rrrZ
landscape.libr�landscape.lib.timestamprZlandscape.lib.jiffiesr�objectrr?rrrr�<module>sz