python3 module psutil system performance information usage

  • 2020-10-23 20:10:53
  • OfStack

psutil is a cross-platform library that allows easy access to the processes and utilization of the system, including CPU, memory, disk, network, and other information.

It is mainly used for information monitoring, analysis and limitation of system resources and process management. It implements functions provided by equivalent command line tools, such as: ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, uptime, pidof, tty, taskset, pmap, etc. It currently supports 32-bit and 64-bit linux, windows, OS X, FreeBSD and Sun Solaris operating systems.

1. Installation of psutil module

(1) Source installation psutil


git clone https://github.com/giampaolo/psutil.git
cd psutil
python3 setup.py install

(2) Installation of pip


pip3 install psutil

(3) Installation on windows


C:\python35\python.exe -m pip install psutil

 Or source code compilation and installation: 
make.bat build
make.bat install

2. Obtain CPU information


In [10]: psutil.cpu_times(percpu=False) # To view CPU All the information 
Out[10]: scputimes(user=306.98, nice=2.01, system=337.34, idle=410414.39, iowait=78.37, irq=0.0, softirq=17.42, steal=0.0, guest=0.0, guest_nice=0.0)

#user: Time spent by the user process 
#nice : User mode execution Niced The time taken by the priority process 
#system : Time spent by the kernel-mode process 
#idle : Idle time 
#iowait: Waiting for the I/O Time of completion 
#irq : Time to process hardware interrupts 
#softirq : Time to process software interruptions 
#steal : Time spent running other operating systems in a virtualized environment 
#guest In: linux Run virtual for the client operating system under the control of the kernel CPU The amount of time spent 
#guest_nice : Virtual machine running niced The amount of time spent 

# displays all logical information for CPU


In [7]: psutil.cpu_times(percpu=True) # Show all CPU Logical information 
Out[7]: 
[scputimes(user=45.48, nice=0.31, system=69.41, idle=101285.67, iowait=19.67, irq=0.0, softirq=3.06, steal=0.0, guest=0.0, guest_nice=0.0),
 scputimes(user=110.04, nice=0.46, system=70.63, idle=101210.2, iowait=22.99, irq=0.0, softirq=5.0, steal=0.0, guest=0.0, guest_nice=0.0),
 scputimes(user=58.5, nice=0.5, system=126.64, idle=100934.59, iowait=14.47, irq=0.0, softirq=4.36, steal=0.0, guest=0.0, guest_nice=0.0),
 scputimes(user=92.1, nice=0.72, system=68.3, idle=101146.96, iowait=21.12, irq=0.0, softirq=4.79, steal=0.0, guest=0.0, guest_nice=0.0)]

# Shows the user's time ratio for CPU


In [11]: psutil.cpu_times().user # Display user occupancy CPU Time than 
Out[11]: 307.11

# shows the logical and physical number of CPU


In [8]: psutil.cpu_count(logical=True) # According to CPU The number of logical 
Out[8]: 4

In [9]: psutil.cpu_count(logical=False) # According to CPU The number of physical 
Out[9]: 4

# Returns various CPU statistics as named tuples


In [15]: psutil.cpu_stats() #CPU statistics 
Out[15]: scpustats(ctx_switches=9838934, interrupts=10572621, soft_interrupts=5582125, syscalls=0)

#ctx_switches : The number of up and down switch after startup 
#interrupts : Number of interrupts since startup 
#soft_interrupts : Number of software interrupts after startup 
#syscalls : The number of system calls since startup linux Always be on 0

3. Memory information

psutil.virtual_memory () returns memory usage statistics in bytes


In [20]: mem = psutil.virtual_memory() # Gets full memory information 
In [21]: mem
Out[21]: svmem(total=2078892032, available=1508818944, percent=27.4, used=367063040, free=135192576, active=874614784, inactive=694231040, buffers=122880, cached=1576513536, shared=10444800, slab=255148032)

#total: Total physical memory 
#available: Available memory 
#used: Memory used 
#free : Completely unused memory 
#active : The memory currently in use 
#inactive : Marked as unused memory 
#buffers : Cache the memory used for file system metadata 
#cached: Cache memory for various files 
#shared: Memory that can be accessed by multiple processes simultaneously 
#slab: Kernel data structure cache memory 


In [22]: mem.total # Get total memory 
Out[22]: 2078892032

In [23]: mem.used # Gets used memory 
Out[23]: 367063040

In [24]: mem.free # Get free memory 
Out[24]: 135192576

In [25]: psutil.swap_memory() # To obtain swap Memory information 
Out[25]: sswap(total=2148528128, used=270336, free=2148257792, percent=0.0, sin=0, sout=12288)

#total : Total swap memory in bytes 
#used : USES swap memory in bytes 
#free : Available swap memory in bytes 
#percent : Percentage used 
#sin : Number of bytes exchanged by the system from disk 
#sout : Number of bytes swapped out by the system from disk 

4. Disk information

psutil.disk_partitions (all=False) : Returns a list of all installed disk partitions as name tuples, including devices, installation points, and file system types, similar to 'df' on Unix.


In [25]: import psutil
In [26]: psutil.disk_partitions(all=False) # Get complete disk information 
Out[26]: 
[sdiskpart(device='/dev/sda3', mountpoint='/', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'),
 sdiskpart(device='/dev/sda5', mountpoint='/home', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'),
 sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota')]

psutil.disk_usage (path) : Returns the disk usage statistics for the partition containing the given path as the specified tuples, including the total, used and free space and percentage usage in bytes, and raises OSError if the path exists.


pip3 install psutil
0

psutil. disk_io_counters(perdisk=False,nowrap=True) # returns system-wide disk I/0 statistics as named tuples, including the following fields:

read_count: read times write_count: number of writes read_bytes: Number of bytes read write_bytes: Number of bytes written read_time: Time read from disk (in milliseconds) write_time: Time (in milliseconds) to write to disk busy_time: time spent on actual I/O read_merged_count: Number of merge reads write_merged_count: Number of merge writes

perdisk returns the same information as the physical disk when True is True; nowrap for True it detects and adjusts the new value in the function call.


pip3 install psutil
1

5. Network information

psutil.net_io_counters (pernic=False,nowrap=True) : Returns the system-wide network I/O statistics as a named tuple, including the following attributes:

bytes_sent: Number of bytes sent bytes_recv: Number of bytes received packets_sent: Number of packets sent packets_recv: Number of packets received errin: Total number of errors received errout: Total number of errors sent dropin: Total number of incoming packets dropped dripout: Total number of outgoing packets dropped (always 0 on OSX and BSD)

If pernic returns the same information for each network interface installed on the True network interface, nowrap as True will detect and adjust these Numbers in the function call, adding the old value to the new value to ensure that the returned number will increase or remain the same, but not decrease, and net_ES168en_clear () can be used to invalidate the nowrap cache.


pip3 install psutil
2

psutil.net_connections (kind='inet') : Returns system-wide socket link, command tuple list returns, each named tuple provides seven properties:

fd: Socket file descriptor. family: Address series, AF_INET, AF_INET6 or AF_UNIX. type: Address type, SOCK_STREAM or SOCK_DGRAM. laddr: Local address as a named tuple or AF_UNIX socket. For the UNIX socket, see the comments below. (ip, port) path raddr: The remote address is the specified tuple, or the absolute address of the UNIX socket. When the remote endpoint is not connected, you get an empty tuple (AF_INET *) or (AF_UNIX). For the UNIX socket, see the comments below. (ip, port) path "" status: Represents the state of the TCP connection. pid: Open socket for the process PID, if it is retrievable, otherwise None . On some platforms, such as Linux, the availability of this field varies according to process permissions (root is required).

The values of the kind parameter include:

inet: ipv4 and ipv6

inet4: ipv4

inet6: ipv6

tcp: TCP

tcp4: TCP over ipv4

tcp6: TCP over ipv6

udp: UDP

dup4: udp based on ipv4

cpu6: udp based on ipv6

Unix: UNIX socket (udp and TCP protocols)

all: The sum of all possible families and agreements


In [86]: psutil.net_connections(kind='tcp')
Out[86]: 
[sconn(fd=3, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='127.0.0.1', port=9090), raddr=(), status='LISTEN', pid=103599),
 sconn(fd=4, family=<AddressFamily.AF_INET6: 10>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='::', port=22), raddr=(), status='LISTEN', pid=1179),
 sconn(fd=13, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='127.0.0.1', port=25), raddr=(), status='LISTEN', pid=1279),
 sconn(fd=10, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='0.0.0.0', port=3306), raddr=(), status='LISTEN', pid=70099),
 sconn(fd=3, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='0.0.0.0', port=22), raddr=(), status='LISTEN', pid=1179),
 sconn(fd=3, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='192.168.146.139', port=22), raddr=addr(ip='192.168.146.1', port=4238), status='ESTABLISHED', pid=122738),
 sconn(fd=12, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='127.0.0.1', port=9001), raddr=(), status='LISTEN', pid=103596),
 sconn(fd=14, family=<AddressFamily.AF_INET6: 10>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='::1', port=25), raddr=(), status='LISTEN', pid=1279)]

In [87]: psutil.net_connections(kind='inet4')
Out[87]: 
[sconn(fd=3, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='127.0.0.1', port=9090), raddr=(), status='LISTEN', pid=103599),
 sconn(fd=13, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='127.0.0.1', port=25), raddr=(), status='LISTEN', pid=1279),
 sconn(fd=10, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='0.0.0.0', port=3306), raddr=(), status='LISTEN', pid=70099),
 sconn(fd=3, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='0.0.0.0', port=22), raddr=(), status='LISTEN', pid=1179),
 sconn(fd=3, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='192.168.146.139', port=22), raddr=addr(ip='192.168.146.1', port=4238), status='ESTABLISHED', pid=122738),
 sconn(fd=6, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_DGRAM: 2>, laddr=addr(ip='0.0.0.0', port=68), raddr=(), status='NONE', pid=119605),
 sconn(fd=12, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='127.0.0.1', port=9001), raddr=(), status='LISTEN', pid=103596),
 sconn(fd=1, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_DGRAM: 2>, laddr=addr(ip='127.0.0.1', port=323), raddr=(), status='NONE', pid=741)]

psutil.net_if_addrs () : Returns the associated address of each network interface on the system in a dictionary manner.


pip3 install psutil
4

psutil.net_if_stats() : Returns the information of the network interface installed on the system as a dictionary, including whether isup started, duplex duplex mode, speed rate, mtu maximum transmission unit, in bytes


pip3 install psutil
5

6. Other system information


pip3 install psutil
6

7. System process management


pip3 install psutil
7

psutil.process_iter(attrs=None,ad_value=None) : Returns 1 iterator process, generating 1 class instance for all running processes on the local machine.

psutil.pid_exists (pid) : Checks to see if the given PID exists in the current process list.

psutil.wait_procs(procs,timeout=None,callback=None) : a handy function that waits for process to terminate the list of instances, returning a tuple indicating which processes have disappeared and which are still alive.

class ES329en.Popen (*args,**kwargs) : It starts a child process and treats it exactly as if it were using ES333en.Popen1. It also provides methods for all psutil.Process classes. The PURPOSE of the Popen class is to obtain information about the application process started by the user in order to track the running state of the application process.


In [18]: import psutil

In [19]: from subprocess import PIPE

In [20]: p = psutil.Popen(["/usr/bin/python","-c","print('hello world')"],stdout=
 ...: PIPE)

In [21]: p.name()
Out[21]: 'python'

In [22]: p.username()
Out[22]: 'root'

In [23]: p.communicate()
Out[23]: (b'hello world\n', None)

Process filtering instance:


pip3 install psutil
9

For more information, see the official documentation: psutil.readhtedocs.io


Related articles: