~ejddlZddlZddlmZddlmZddlmZddlmZddlm Z GddZ d Z y) N)datetime) timedelta)sysstats)detect_jiffies) to_timestampc,eZdZdZ ddZdZdZy)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). Nc|"tjj}|tj|}||_||_|xs t|_||_ y)N) r BootTimesget_last_boot_timerutcfromtimestamp _boot_time _proc_dirr_jiffies_per_sec_uptime)selfproc_dirjiffies boot_timeuptimes 7/usr/lib/python3/dist-packages/landscape/lib/process.py__init__zProcessInformation.__init__s\   **,??AI   11)+; c#Ktj|jD]&} t|}|j |}|s#|(y#t$rY5wxYww)z8Get process information for all processes on the system.N)oslistdirrint ValueErrorget_process_info)rfilename process_id process_infos rget_all_process_infoz'ProcessInformation.get_all_process_info$s^ 4>>2 #H  ]  00/cmdline and /proc//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. pidcmdlinerrstatus:NamenameStatezT (tracing stop)asciistateUiduidGidgidVmSizezvm-sizestat z percent-cpuNz,Skipping process (PID %s) without boot time.z start-time)rpathjoinrstropenreadlinebasenamesplitclosestriplowerencoderreadrr get_uptimecalculate_pcpurrrloggingwarningrOSError)rr! cmd_line_name process_dirr"filecmd_linelinepartsr1 value_parts start_timeutimestimerpcpudeltas rrz#ProcessInformation.get_process_info/sl ggll4>>3z?C z* P  [))<)<)>%)) /3 ]+!!Z43H3H%HI??*OOF"  .:OOe+. \*  \ !<',&%% ,   -c : J   sn*N&AM':N"C3M*'M*=:N8CM?<N M?(NM''N*M<<N?NN N N )z/procNNN)__name__ __module__ __qualname____doc__rr#rrrr r s%  #frr czd}||z}|||z z }|r |dz|z |z }ttt|dddS)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); rdgX@r,)roundmaxmin)rTrUrrShertzrV total_timesecondss rrHrHsR DJ U*+GC%''1 St_a(! ,,r) rIrrr landscape.librlandscape.lib.jiffiesrlandscape.lib.timestamprr rHr\rrrhs, "00JJZ-r