3 {Meʍ@sddlZddlZddlZddlZddlmZddlmZddlm Z dZ ddZ dd Z Gd d d Z d d ZddZGdddZGdddZGdddZGdddZGdddZGdddZGdddZGdddZGd d!d!ZGd"d#d#Zed$krddlZeZx0eejjD]Ze ed%ejeq*WeZ!x0ee!j!jD]Z"e e"d%e!j!e"qbWeZ#e d&e#j$d'x,ee#jD]Z%e e%d(e#e%qWe d)d*d+ee&ej'd,Z(x8ee(j)D]*Ze e(j*ej+d-d.e(j*ej,qWe d*d+x&e(j-ej'd/D]Z.e e.d0qDWeZ/e e/d,eZ0xFej1d,e0j2x$e0D]Z#e e#d%e0e#qWe d2qtWdS)3N)reduce)range) bitmasklistz0.7.3cCstj}ttjd|S)z) Return True if running on s390 or s390x Zs390)platformmachineboolresearch)rr /usr/lib/python3.6/procfs.pyis_s390sr c Cs:|drtdd|djSy |ddSdSdS)z Returns the process command line, if available in the given `process' class, if not available, falls back to using the comm (short process name) in its pidstat key. cmdlinecSs |d|S)Nz %sr )abr r r !sz!process_cmdline..statcommN)rstrip)Zpid_infor r r process_cmdlines  rc+@sNeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$d Z%d!Z&d!Z'd"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;dd?d@dAdBdCdDdEdFdGdHdIdJdKdLg+Z(dcdNdOZ)dPdQZ*dRdSZ+dTdUZ,dVdWZ-dXdYZ.dZd[Z/ddd\d]Z0d^d_Z1d`daZ2dbS)epidstatam Provides a dictionary to access the fields in the per process /proc/PID/stat files. One can obtain the available fields by asking for the keys of the dictionary, e.g.: >>> p = procfs.pidstat(1) >>> print p.keys() ['majflt', 'rss', 'cnswap', 'cstime', 'pid', 'session', 'startstack', 'startcode', 'cmajflt', 'blocked', 'exit_signal', 'minflt', 'nswap', 'environ', 'priority', 'state', 'delayacct_blkio_ticks', 'policy', 'rt_priority', 'ppid', 'nice', 'cutime', 'endcode', 'wchan', 'num_threads', 'sigcatch', 'comm', 'stime', 'sigignore', 'tty_nr', 'kstkeip', 'utime', 'tpgid', 'itrealvalue', 'kstkesp', 'rlim', 'signal', 'pgrp', 'flags', 'starttime', 'cminflt', 'vsize', 'processor'] And then access the various process properties using it as a dictionary: >>> print p['comm'] systemd >>> print p['priority'] 20 >>> print p['state'] S Please refer to the 'procfs(5)' man page, by using: $ man 5 procfs To see information for each of the above fields, it is part of the 'man-pages' RPM package.  @iiiii i@iiiiiii i@iiiiiii i@lpidrstateZppidZpgrpZsessionZtty_nrZtpgidflagsZminfltZcminfltZmajfltZcmajfltutimeZstimeZcutimeZcstimeZpriorityniceZ num_threadsZ itrealvalueZ starttimeZvsizeZrssZrlimZ startcodeZendcodeZ startstackZkstkespZkstkeipsignalZblockedZ sigignoreZsigcatchZwchanZnswapZcnswapZ exit_signal processor rt_priorityZpolicyZdelayacct_blkio_ticksenviron/procc Cs0||_y|j|Wntk r*YnXdS)N)rloadFileNotFoundError)selfrbasedirr r r __init__~s zpidstat.__init__cCs |j|S)N)fields)r+ fieldnamer r r __getitem__szpidstat.__getitem__cCst|jjS)N)listr.keys)r+r r r r2sz pidstat.keyscCst|jjS)N)r1r.values)r+r r r r3szpidstat.valuescCs ||jkS)N)r.)r+r/r r r has_keyszpidstat.has_keycCs|jS)N)r.)r+r r r itemssz pidstat.itemscCs ||jkS)N)r.)r+r/r r r __contains__szpidstat.__contains__cCsyt|d|jd}Wntk r0YnX|jjjd}|j|djd|dj}i|_tt |t |j }xft |D]Z}|j |}||}|dkr|jd|jd<qyt ||j|<Wq||j|<YqXqWdS) N/z/statz) rz (rrz()) openrr*readlinersplitcloser.minlenproc_stat_fieldsrint)r+r,fr. nr_fieldsiZattrnamevaluer r r r)s$ z pidstat.loadcCst|jd|j@S)z Returns true if this process has a fixed smp affinity mask, not allowing it to be moved to a different set of CPUs. r!)rr.PF_THREAD_BOUND)r+r r r is_bound_to_cpuszpidstat.is_bound_to_cpucCsNg}xDt|D]8}|dddkr$qt||}||jd@r|j|qW|S)a Returns a list with all the process flags known, details depend on kernel version, declared in the file include/linux/sched.h in the kernel sources. As of v4.2-rc7 these include (from include/linux/sched.h comments): PF_EXITING Getting shut down PF_EXITPIDONE Pi exit done on shut down PF_VCPU I'm a virtual CPU PF_WQ_WORKER I'm a workqueue worker PF_FORKNOEXEC Forked but didn't exec PF_MCE_PROCESS Process policy on mce errors PF_SUPERPRIV Used super-user privileges PF_DUMPCORE Dumped core PF_SIGNALED Killed by a signal PF_MEMALLOC Allocating memory PF_NPROC_EXCEEDED Set_user noticed that RLIMIT_NPROC was exceeded PF_USED_MATH If unset the fpu must be initialized before use PF_USED_ASYNC Used async_schedule*(), used by module init PF_NOFREEZE This thread should not be frozen PF_FROZEN Frozen for system suspend PF_FSTRANS Inside a filesystem transaction PF_KSWAPD I am kswapd PF_MEMALLOC_NOIO Allocating memory without IO involved PF_LESS_THROTTLE Throttle me less: I clean memory PF_KTHREAD I am a kernel thread PF_RANDOMIZE Randomize virtual address space PF_SWAPWRITE Allowed to write to swap PF_NO_SETAFFINITY Userland is not allowed to meddle with cpus_allowed PF_MCE_EARLY Early kill for mce process policy PF_MUTEX_TESTER Thread belongs to the rt mutex tester PF_FREEZER_SKIP Freezer should not count it as freezable PF_SUSPEND_TASK This thread called freeze_processes and should not be frozen NZPF_r!)dirgetattrr.append)r+ZsflagsattrrCr r r process_flagss& zpidstat.process_flagsN)r()r()3__name__ __module__ __qualname____doc__Z PF_ALIGNWARNZ PF_STARTINGZ PF_EXITINGZ PF_EXITPIDONEZPF_VCPUZ PF_WQ_WORKERZ PF_FORKNOEXECZPF_MCE_PROCESSZ PF_SUPERPRIVZ PF_DUMPCOREZ PF_SIGNALEDZ PF_MEMALLOCZPF_NPROC_EXCEEDEDZ PF_FLUSHERZ PF_USED_MATHZ PF_USED_ASYNCZ PF_NOFREEZEZ PF_FROZENZ PF_FSTRANSZ PF_KSWAPDZPF_MEMALLOC_NOIOZ PF_SWAPOFFZPF_LESS_THROTTLEZ PF_KTHREADZ PF_RANDOMIZEZ PF_SWAPWRITEZPF_SPREAD_PAGEZPF_SPREAD_SLABrDPF_NO_SETAFFINITYZ PF_MCE_EARLYZ PF_MEMPOLICYZPF_MUTEX_TESTERZPF_FREEZER_SKIPZPF_FREEZER_NOSIGZPF_SUSPEND_TASKr>r-r0r2r3r4r5r6r)rErKr r r r r*st     rc Cs2d}ytt|j|dd|@SdSdS)Nirr!T)rr? processes)r+rrPr r r cannot_set_affinitys rRc Cs8d}y$tt|j|j|dd|@SdSdS)Nirr!T)rr?rQthreads)r+rtidrPr r r cannot_set_thread_affinitys rUc@sTeZdZdZdddZddZddZd d Zd d Zd dZ ddZ dddZ dS) pidstatusa Provides a dictionary to access the fields in the per process /proc/PID/status files. This provides additional information about processes and threads to what can be obtained with the procfs.pidstat() class. One can obtain the available fields by asking for the keys of the dictionary, e.g.: >>> import procfs >>> p = procfs.pidstatus(1) >>> print p.keys() ['VmExe', 'CapBnd', 'NSpgid', 'Tgid', 'NSpid', 'VmSize', 'VmPMD', 'ShdPnd', 'State', 'Gid', 'nonvoluntary_ctxt_switches', 'SigIgn', 'VmStk', 'VmData', 'SigCgt', 'CapEff', 'VmPTE', 'Groups', 'NStgid', 'Threads', 'PPid', 'VmHWM', 'NSsid', 'VmSwap', 'Name', 'SigBlk', 'Mems_allowed_list', 'VmPeak', 'Ngid', 'VmLck', 'SigQ', 'VmPin', 'Mems_allowed', 'CapPrm', 'Seccomp', 'VmLib', 'Cpus_allowed', 'Uid', 'SigPnd', 'Pid', 'Cpus_allowed_list', 'TracerPid', 'CapInh', 'voluntary_ctxt_switches', 'VmRSS', 'FDSize'] >>> print p["Pid"] 1 >>> print p["Threads"] 1 >>> print p["VmExe"] 1248 kB >>> print p["Cpus_allowed"] f >>> print p["SigQ"] 0/30698 >>> print p["VmPeak"] 320300 kB >>> Please refer to the 'procfs(5)' man page, by using: $ man 5 procfs To see information for each of the above fields, it is part of the 'man-pages' RPM package. In the man page there will be references to further documentation, like referring to the "getrlimit(2)" man page when explaining the "SigQ" line/field. /proccCs||_|j|dS)N)rr))r+rr,r r r r-!szpidstatus.__init__cCs |j|S)N)r.)r+r/r r r r0%szpidstatus.__getitem__cCst|jjS)N)r1r.r2)r+r r r r2(szpidstatus.keyscCst|jjS)N)r1r.r3)r+r r r r3+szpidstatus.valuescCs ||jkS)N)r.)r+r/r r r r4.szpidstatus.has_keycCs|jS)N)r.)r+r r r r51szpidstatus.itemscCs ||jkS)N)r.)r+r/r r r r64szpidstatus.__contains__cCsi|_t|d|jdn}xf|jD]Z}|jd}t|dkrDq(|d}|dj}yt||j|<Wq(||j|<Yq(Xq(WWdQRXdS)Nr7z/status:rrr)r.r8r readlinesr:r=rr?)r+r,r@liner.namerCr r r r)7s   zpidstatus.loadN)rW)rW) rLrMrNrOr-r0r2r3r4r5r6r)r r r r rVs& rVc@sReZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ dS)processa+ Information about a process with a given pid, provides a dictionary with two entries, instances of different wrappers for /proc/ process related meta files: "stat" and "status", see the documentation for procfs.pidstat and procfs.pidstatus for further info about those classes. /proccCs||_||_dS)N)rr,)r+rr,r r r r-Nszprocess.__init__c Cst||s|dkrX|dkr t}nt}yt||||j|jWqtk rTYqXnF|dkrj|jn4|dkr||jn"|dkr|j n|dkr|j t ||S)Nrstatusr rScgroupsr')rr^) hasattrrrVsetattrrr,r* load_cmdline load_threads load_cgroups load_environrH)r+rJZsclassr r r r0Rs$     zprocess.__getitem__cCs t||S)N)r`)r+rJr r r r4jszprocess.has_keycCs t||S)N)r`)r+rJr r r r6mszprocess.__contains__cCsvy>td|jd"}|jjjddd|_WdQRXWn2tk rXd|_Yntk rpd|_YnXdS)Nz/proc/z/cmdliner)r8rr9rr:r r*UnicodeDecodeError)r+r@r r r rbps* zprocess.load_cmdlinecCs"td|jd|_|j|j=dS)Nz/proc/z/task/)pidstatsrrS)r+r r r rc{szprocess.load_threadsc Cstd|_td|jdR}xJt|jD]:}t|jdkrT|jd|dd|_q(|dd|_q(WWdQRXdS) Nz/proc/z/cgroupr,rrgrg)r_r8rreversedrYr=)r+r@rZr r r rds zprocess.load_cgroupsc Cshi|_td|jdF}x>|jjdD],}t|dkr*|jd}|d|j|d<q*WWdQRXdS)aD Loads the environment variables for this process. The entries then become available via the 'environ' member, or via the 'environ' dict key when accessing as p["environ"]. E.g.: >>> all_processes = procfs.pidstats() >>> firefox_pid = all_processes.find_by_name("firefox") >>> firefox_process = all_processes[firefox_pid[0]] >>> print firefox_process["environ"]["PWD"] /home/acme >>> print len(firefox_process.environ.keys()) 66 >>> print firefox_process["environ"]["SHELL"] /bin/bash >>> print firefox_process["environ"]["USERNAME"] acme >>> print firefox_process["environ"]["HOME"] /home/acme >>> print firefox_process["environ"]["MAIL"] /var/spool/mail/acme >>> z/proc/z/environrfr=rN)r'r8rr9r:r=)r+r@xyr r r res   zprocess.load_environN)r]) rLrMrNrOr-r0r4r6rbrcrdrer r r r r\Fs   r\c@seZdZdZd$ddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#S)%ria# Provides access to all the processes in the system, to get a picture of how many processes there are at any given moment. The entries can be accessed as a dictionary, keyed by pid. Also there are methods to find processes that match a given COMM or regular expression. /proccCs||_i|_|jdS)N)r,rQreload)r+r,r r r r-szpidstats.__init__cCs |j|S)N)rQ)r+keyr r r r0szpidstats.__getitem__c Csy |j|=Wn YnXdS)N)rQ)r+rrr r r __delitem__s zpidstats.__delitem__cCst|jjS)N)r1rQr2)r+r r r r2sz pidstats.keyscCst|jjS)N)r1rQr3)r+r r r r3szpidstats.valuescCs ||jkS)N)rQ)r+rrr r r r4szpidstats.has_keycCs|jS)N)rQ)r+r r r r5szpidstats.itemscCs ||jkS)N)rQ)r+rrr r r r6szpidstats.__contains__c CsV|`i|_tj|j}x:|D]2}y t|}WnwYnXt||j|j|<qWdS)a' This operation will throw away the current dictionary contents, if any, and read all the pid files from /proc/, instantiating a 'process' instance for each of them. This is a high overhead operation, and should be avoided if the perf python binding can be used to detect when new threads appear and existing ones terminate. In RHEL it is found in the python-perf rpm package. More information about the perf facilities can be found in the 'perf_event_open' man page. N)rQoslistdirr,r?r\)r+pidsZspidrr r r rqs   zpidstats.reloadc Cshg}xHt|jjD]6}y|j|jWqtk rH|j|YqXqWx|D] }|j|=qTWdS)N)r1rQr2rcOSErrorrI)r+Z to_removerr r r reload_threadss zpidstats.reload_threadsc Csn|dd}g}xXt|jjD]F}y$||j|ddkrF|j|Wq tk rd|j|=Yq Xq W|S)Nrr)r1rQr2rIIOError)r+r[rvrr r r find_by_names zpidstats.find_by_namec Csdg}xZt|jjD]H}y&|j|j|ddr<|j|Wqtk rZ|j|=YqXqW|S)Nrr)r1rQr2matchrIrz)r+regexrvrr r r find_by_regexszpidstats.find_by_regexc Cs`g}xVt|jjD]D}y"|jt|j|r8|j|Wqtk rV|j|=YqXqW|S)N)r1rQr2r|rrIrz)r+r}rvrr r r find_by_cmdline_regexszpidstats.find_by_cmdline_regexc sd}d}gx|d|}|j|}| sFtfdd|DdkrHPxF|D]>}y||j|dd7}WqNtk r|j|=YqNXqNW|7|d7}qW|jd }|S) Nrrjr7csg|]}|kr|qSr r ).0n)processed_pidsr r 'sz0pidstats.get_per_cpu_rtprios..rr&rrk)r{r=rQrzr)r+basenamecpu prioritiesr[rvrr )rr get_per_cpu_rtprios s"     zpidstats.get_per_cpu_rtpriosc sd}d}gx|j|}| s8tfdd|Ddkr:PxF|D]>}y||j|dd7}Wq@tk r||j|=Yq@Xq@W|7|d7}qW|jd}|S) Nrrjcsg|]}|kr|qSr r )rr)rr r r=sz(pidstats.get_rtprios..rr&rrk)r{r=rQrzr)r+r[rrrvrr )rr get_rtprios7s      zpidstats.get_rtprioscCs|j|djS)zQ Checks if a given pid can't have its SMP affinity mask changed. r)rQrE)r+rr r r rEMszpidstats.is_bound_to_cpuN)rp)rLrMrNrOr-r0rsr2r3r4r5r6rqrxr{r~rrrrEr r r r ris"    ric@speZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZdS) interruptsam Information about IRQs in the system. A dictionary keyed by IRQ number will have as its value another dictionary with "cpu", "type" and "users" keys, with the SMP affinity mask, type of IRQ and the drivers associated with each interrupt. The information comes from the /proc/interrupts file, documented in 'man procfs(5)', for instance, the 'cpu' dict is an array with one entry per CPU present in the sistem, each value being the number of interrupts that took place per CPU. E.g.: >>> import procfs >>> interrupts = procfs.interrupts() >>> thunderbolt_irq = interrupts.find_by_user("thunderbolt") >>> print thunderbolt_irq 34 >>> thunderbolt = interrupts[thunderbolt_irq] >>> print thunderbolt {'affinity': [0, 1, 2, 3], 'type': 'PCI-MSI', 'cpu': [3495, 0, 81, 0], 'users': ['thunderbolt']} >>> cCsi|_|jdS)N)rrq)r+r r r r-mszinterrupts.__init__cCs|jt|S)N)rstr)r+rrr r r r0qszinterrupts.__getitem__cCst|jjS)N)r1rr2)r+r r r r2tszinterrupts.keyscCst|jjS)N)r1rr3)r+r r r r3wszinterrupts.valuescCst||jkS)N)rr)r+rrr r r r4zszinterrupts.has_keycCs|jS)N)r)r+r r r r5}szinterrupts.itemscCst||jkS)N)rr)r+rrr r r r6szinterrupts.__contains__cCs|`i|_td}x|jD]}|j}|j}|ddddkrRt||_q|djd}i|j|<|j|dd||j|<y t|}WnwYnX|j ||j|d<qWWdQRXdS)Nz/proc/interruptsrrFZCPUrXrZaffinity) rr8rYrr:r=nr_cpus parse_entryr?parse_affinity)r+r@rZr.irqZnirqr r r rqs"    zinterrupts.reloadcCsi}g|d<|djt|dt|}||jkr|ddd|d|jD7<||jkr||j|d<||jdkrdd||djdD|d <ng|d <|S) NrrcSsg|] }t|qSr )r?)rrBr r r rsz*interrupts.parse_entry..rtypecSsg|] }|jqSr )r)rrr r r rsrkusers)rIr?r=rr:)r+r.rZdictrAr r r rs $ zinterrupts.parse_entrycCsLy0td|d}|j}WdQRXt||jStk rFdgSXdS)Nz /proc/irq/z /smp_affinityr)r8r9rrrz)r+rr@rZr r r rs  zinterrupts.parse_affinitycCs@x:t|jjD](}d|j|kr||j|dkr|SqWdS)a Looks up a interrupt number by the name of one of its users" E.g.: >>> import procfs >>> interrupts = procfs.interrupts() >>> thunderbolt_irq = interrupts.find_by_user("thunderbolt") >>> print thunderbolt_irq 34 >>> thunderbolt = interrupts[thunderbolt_irq] >>> print thunderbolt {'affinity': [0, 1, 2, 3], 'type': 'PCI-MSI', 'cpu': [3495, 0, 81, 0], 'users': ['thunderbolt']} >>> rN)r1rr2)r+userrBr r r find_by_users zinterrupts.find_by_usercCs^g}xTt|jjD]B}d|j|kr(qx,|j|dD]}|j|r8|j|Pq8WqW|S)a Looks up a interrupt number by a regex that matches names of its users" E.g.: >>> import procfs >>> import re >>> interrupts = procfs.interrupts() >>> usb_controllers = interrupts.find_by_user_regex(re.compile(".*hcd")) >>> print usb_controllers ['22', '23', '31'] >>> print [ interrupts[irq]["users"] for irq in usb_controllers ] [['ehci_hcd:usb4'], ['ehci_hcd:usb3'], ['xhci_hcd']] >>> r)r1rr2r|rI)r+r}ZirqsrBrr r r find_by_user_regexs   zinterrupts.find_by_user_regexN)rLrMrNrOr-r0r2r3r4r5r6rqrrrrr r r r rTsrc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)r a9 Parses the kernel command line (/proc/cmdline), turning it into a dictionary." Useful to figure out if some kernel boolean knob has been turned on, as well as to find the value associated to other kernel knobs. It can also be used to find out about parameters passed to the init process, such as 'BOOT_IMAGE', etc. E.g.: >>> import procfs >>> kcmd = procfs.cmdline() >>> print kcmd.keys() ['LANG', 'BOOT_IMAGE', 'quiet', 'rhgb', 'rd.lvm.lv', 'ro', 'root'] >>> print kcmd["BOOT_IMAGE"] /vmlinuz-4.3.0-rc1+ >>> cCsi|_|jdS)N)optionsparse)r+r r r r-szcmdline.__init__c CsjtdX}xP|jjjD]<}|jd}t|dkrFd|j|d<q|d|j|d<qWWdQRXdS)Nz /proc/cmdlinermrTr)r8r9rr:r=r)r+r@Zoptionr.r r r rs    z cmdline.parsecCs |j|S)N)r)r+rrr r r r0szcmdline.__getitem__cCst|jjS)N)r1rr2)r+r r r r2sz cmdline.keyscCst|jjS)N)r1rr3)r+r r r r3szcmdline.valuescCs|jS)N)r)r+r r r r5 sz cmdline.itemsN) rLrMrNrOr-rr0r2r3r5r r r r r s r c@sBeZdZdZdddZddZddZd d Zd d Zd dZ dS)cpuinfoa Dictionary with information about CPUs in the system. Please refer to 'man procfs(5)' for further information about the '/proc/cpuinfo' file, that is the source of the information provided by this class. The 'man lscpu(1)' also has information about a program that uses the '/proc/cpuinfo' file. Using this class one can obtain the number of CPUs in a system: >>> cpus = procfs.cpuinfo() >>> print cpus.nr_cpus 4 It is also possible to figure out aspects of the CPU topology, such as how many CPU physical sockets exists, i.e. groups of CPUs sharing components such as CPU memory caches: >>> print len(cpus.sockets) 1 Additionally dictionary with information common to all CPUs in the system is available: >>> print cpus["model name"] Intel(R) Core(TM) i7-3667U CPU @ 2.00GHz >>> print cpus["cache size"] 4096 KB >>> /proc/cpuinfocCs i|_d|_g|_|j|dS)Nr)tagsrsocketsr)r+filenamer r r r-.szcpuinfo.__init__cCs|j|jS)N)rlower)r+rrr r r r04szcpuinfo.__getitem__cCst|jjS)N)r1rr2)r+r r r r27sz cpuinfo.keyscCst|jjS)N)r1rr3)r+r r r r3:szcpuinfo.valuescCs|jS)N)r)r+r r r r5=sz cpuinfo.itemsc Cs(t|}x|jD]}|j}|s&q|jd}|djj}|dkrX|jd7_qtrv|dkrv|jd7_q|dkrq|dj|j|<|dkr|j|}||jkr|jj |qWWdQRX|jrt |jp|jd|jkrt |jdpd|_ d |jkrt |jd pd|j |_ dS) NrXrr%rz cpu numberzcore idz physical idZsiblingsz cpu cores)r8rYrr:rrr rrrIr=r?Z nr_socketsZnr_cores)r+rr@rZr.ZtagnameZ socket_idr r r r@s2     z cpuinfo.parseN)r) rLrMrNrOr-r0r2r3r5rr r r r rs rc@s8eZdZdZddZddZddZdd Zd d Zd S) smaps_libat Representation of an mmap in place for a process. Can be used to figure out which processes have an library mapped, etc. The 'perm' member can be used to figure out executable mmaps, i.e. libraries. The 'vm_start' and 'vm_end' in turn can be used when trying to resolve processor instruction pointer addresses to a symbol name in a library. c Cs|dj}dd|djdD\|_|_|d|_t|dd|_|djd \|_|_t|d |_t |d kr|d |_ nd|_ i|_ x^|ddD]N}|j}|ddd j }yt|d|j |<Wq||j |<YqXqWdS) NrcSsg|]}t|dqS)r)r?)rrr r r rjsz&smaps_lib.__init__..-rrrrFrXrrg) r:vm_startZvm_endZpermsr?offsetmajorminorinoder=r[rr)r+linesr.rZtagr r r r-hs"     zsmaps_lib.__init__cCs|j|jS)N)rr)r+rrr r r r0}szsmaps_lib.__getitem__cCst|jjS)N)r1rr2)r+r r r r2szsmaps_lib.keyscCst|jjS)N)r1rr3)r+r r r r3szsmaps_lib.valuescCs|jS)N)r)r+r r r r5szsmaps_lib.itemsN) rLrMrNrOr-r0r2r3r5r r r r r\s  rc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)smapsa~ List of libraries mapped by a process. Parses the lines in the /proc/PID/smaps file, that is further documented in the procfs(5) man page. Example: Listing the executable maps for the 'sshd' process: >>> import procfs >>> processes = procfs.pidstats() >>> sshd = processes.find_by_name("sshd") >>> sshd_maps = procfs.smaps(sshd[0]) >>> for i in range(len(sshd_maps)): ... if 'x' in sshd_maps[i].perms: ... print "%s: %s" % (sshd_maps[i].name, sshd_maps[i].perms) ... /usr/sbin/sshd: r-xp /usr/lib64/libnss_files-2.20.so: r-xp /usr/lib64/librt-2.20.so: r-xp /usr/lib64/libkeyutils.so.1.5: r-xp /usr/lib64/libkrb5support.so.0.1: r-xp /usr/lib64/libfreebl3.so: r-xp /usr/lib64/libpthread-2.20.so: r-xp ... cCs||_g|_|jdS)N)rentriesrq)r+rr r r r-szsmaps.__init__cCsxg}|s|jj}|sdS|j|x<|j}|s6P|j}|jdddkr^|j|q(Pq(W|jjt||S)NrrrXrg)r9rrIr:rr)r+r@rZrr r r rs    zsmaps.parse_entrycCs t|jS)N)r=r)r+r r r __len__sz smaps.__len__cCs |j|S)N)r)r+indexr r r r0szsmaps.__getitem__c CsJd}td|jd}x|j||}|sPqWWdQRXt|j|_dS)Nz/proc/z/smaps)r8rrr=r nr_entries)r+rZr@r r r rqs z smaps.reloadcCsNg}xDt|jD]6}|j|jr|j|jj|dkr|j|j|qW|S)Nr)rrrr[findrI)r+ZfragmentresultrBr r r find_by_name_fragments  zsmaps.find_by_name_fragmentN) rLrMrNrOr-rrr0rqrr r r r rs rc@s eZdZdZddZddZdS)cpustatz CPU statistics, obtained from a line in the '/proc/stat' file, Please refer to 'man procfs(5)' for further information about the '/proc/stat' file, that is the source of the information provided by this class. cCst|d|_dd|ddD\|_|_|_|_|_|_|_t|dkrpt |d|_ t|dkrpt |d|_ dS)NrcSsg|] }t|qSr )r?)rrBr r r rsz$cpustat.__init__..rr) r[rr#systemidleiowaitrsoftirqr=r?stealguest)r+r.r r r r-s  2  zcpustat.__init__cCsxd|jd|jd|jd|jd|jd|jd|j}t|drV|d |j7}t|d rp|d |j 7}|d S) Nz< user: z, nice: z , system: z, idle: z , iowait: z, irq: z , softirq: rz , steal: rz , guest: >) rr#rrrrrr`rr)r+sr r r __repr__s <  zcpustat.__repr__N)rLrMrNrOr-rr r r r rsrc@sReZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ dS) cpusstatsaN Dictionary with information about CPUs in the system. First entry in the dictionary gives an aggregate view of all CPUs, each other entry is about separate CPUs. Please refer to 'man procfs(5)' for further information about the '/proc/stat' file, that is the source of the information provided by this class. /proc/statcCs*i|_d|_tjd|_||_|jdS)Nr)rtimertsysconfhertzrrq)r+rr r r r-s  zcpusstats.__init__cCs t|jS)N)iterr)r+r r r __iter__szcpusstats.__iter__cCs |j|S)N)r)r+rrr r r r0szcpusstats.__getitem__cCstt|jjS)N)r=r1rr2)r+r r r r szcpusstats.__len__cCst|jjS)N)r1rr2)r+r r r r2 szcpusstats.keyscCst|jjS)N)r1rr3)r+r r r r3szcpusstats.valuescCs|jS)N)r)r+r r r r5szcpusstats.itemsc Cs:|j}i|_t|jv}xn|jD]b}|jj}|dddjdkrLq"t|}|jdkrdd}nt |jddd}||j|<q"WWdQRX|j }t j |_ |r6|j |}||j } xxt |jj D]f} | |krd| _q|j| } || } | j| j| j| j| j| j} | | d| _t| jd| _qWdS)NrrFrrd)rr8rrYrr:rrr[r?rrr1r2Zusagerr#rr<)r+Z last_entriesr@rZr.cidxZ last_timeZ delta_secZ interval_hzrZcurrprevZdeltar r r rqs6       zcpusstats.reloadN)r) rLrMrNrOr-rr0rr2r3r5rqr r r r rs r__main__z: z cpuinfo data: z processorsrmzsmaps: r(rz#x rZSize z ----------)3rtrrr functoolsrZ six.movesrZprocfs.utilistrVERSIONr rrrRrUrVr\rirr rrrrrrLsysZintsr1r2rBprintrorrrr?argvrrrrr[rrZpsZcsZsleeprqr r r r  sj   >  Me* .N.KB *