psutil module of python notes

Posted by Dillenger on Tue, 21 May 2019 00:28:09 +0200

I. psutil module

psutil is a cross-platform library( http://code.google.com/p/psutil/ It can easily acquire the process of system operation and system utilization (including CPU, memory, disk, network, etc.) information. It is mainly used in system monitoring, analysis and restriction of system resources and process management. It implements the functions provided by the same command line tools, such as ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, iotop, uptime, pidof, tty, taskset, pmap, etc. At present, it supports 32-bit and 64-bit operating systems such as Linux, Windows, OS X, FreeBSD and Sun Solaris.

2. installation

 wget https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.tar.gz
 tar -xzvf psutil-2.0.0.tar.gz
 cd psutil-2.0.0
 python setup.py install

3. use

Get system performance information (CPU, memory, disk, network)

3.1CPU correlation

View cpu Information

import Psutil
//View all cpu Information
>>> psutil.cpu_times()
scputimes(user=11677.09, nice=57.93, system=148675.58, idle=2167147.79, iowait=260828.48, irq=7876.28, softirq=0.0, steal=3694.59, guest=0.0, guest_nice=0.0)

User Time, the percentage of time spent executing user processes;
System Time, the percentage of time spent executing kernel processes and interrupts;
Wait IO, the percentage of time that the CPU is idle due to IO waiting;
Idle, percentage of time CPU is idle
Nice, it can be understood that the CPU scheduling priority of user-space processes ranges from [-20,19]
irq, hard interrupt
softirq, soft interrupt
Generally speaking, idel + user + nice is about 100%

Display all logic information of cpu

>>> psutil.cpu_times(percpu=True)
[scputimes(user=11684.17, nice=57.93, system=148683.01, idle=2168982.08, iowait=260833.18, irq=7882.35, softirq=0.0, steal=3697.3, guest=0.0, guest_nice=0.0)]

View user's cpu time ratio

>>> psutil.cpu_times().user
11684.4

View the number of cpu logic

>>> psutil.cpu_count()
1

View the physical number of CPUs

>>> psutil.cpu_count(logical=False)
1

3.2 View System Memory

>>> import psutil
>>> mem = psutil.virtual_memory()
>>> mem
#All information in system memory
svmem(total=1040662528, available=175054848, percent=83.2, used=965718016, free=74944512, active=566755328, inactive=59457536, buffers=9342976, cached=90767360)

The Difference between buffer and cache

Both are data in RAM. In short, buffers are about to be written to disk, and cache is read from disk. The purpose of both is to improve IO performance and be managed by OS, not to apply the memory allocated by itself, but to make extra use of the free memory by OS itself as needed. Because this part only caches, reduces IO and improves performance, as long as the application needs, OS can write buffers directly to disk, delete cache to get free memory for application use.

buffer is the area used to store data between devices with different speeds or priorities. buffers are designed to read and write disks, which centralize decentralized write operations to reduce disk fragmentation and hard disk tracing, thereby improving system performance.
cache is often used on disk I/O requests. If multiple processes want to access a file, the file is then cached to facilitate next access, which provides system performance. Cached is to save the read data, and when re-read, if hit (find the data needed), do not read the hard disk, if not hit read the hard disk. The data will be organized according to the reading frequency, the most frequently read content will be placed in the easiest location to find, and the content that no longer reads will be kept in the back row until it is deleted.

So:
-/+ The meaning of buffers/cache is that the use of memory is the sum of the actual current use of memory minus buffers/cache; the free memory is the sum of the actual free memory plus buffers/cache. So it is - / +
When checking free memory to determine whether there is a memory leak in an application, it can only be based on Free's third behavior. The second one is not very useful, but it can see the current buffer and cache size of OS.

Total System Memory

>>> mem.total
1040662528

The system already uses memory

>>> mem.used
965718016

System free memory

>>> mem.free
112779264

Get swap memory information

>>> psutil.swap_memory()
sswap(total=0, used=0, free=0, percent=0, sin=0, sout=0)

3.3 Read Disk Parameters

Disk utilization is obtained using the psutil.disk_usage method.
Disk IO information includes read_count (read IO), write_count (write IO)
read_bytes(IO writing section), read_time (disk reading time), write_time (disk writing time), which are used for IO information.

psutil.disk_io_counters()

Get complete information about the disk

psutil.disk_partitions()

Get the parameters of the partition table

psutil.disk_usage('/')   #Get / partition status

Get the total number of hard disk IO s

psutil.disk_io_counters()

Get the number of IO s for a single partition

psutil.disk_io_counters(perdisk=True)    
#perdisk=TrueParameter acquisition for a single partition IO Number

3.4 Read Network Information

Network information is similar to disk IO information and involves several key points, including byes_send (number of bytes sent), byte_recv = XXX (number of bytes received), pack-ets_send = XXXX (number of bytes sent),
Pack-ets_recv = XXXX (number of packets received), which is used for network information

Obtaining the Total IO Information of the Network

psutil.net_io_counters()  

Output each interface information of the network

psutil.net_io_counters(pernic=True)     #pernic=True

Get the current system user login information

psutil.users()

Get boot time

psutil.boot_time() #Returns in linux time format
datetime.datetime.fromtimestamp(psutil.boot_time ()).strftime("%Y-%m-%d %H: %M: %S") #Converting to Natural Time Format

3.5 System Process Management

Get the process information of the current system, get the running status of the current program, including the start time of the process, view the CPU affinity setting, memory utilization, IO information
socket connection, number of threads, etc.
Getting process information
View the whole process of the system

psutil.pids()

View a single process

p = psutil.Process(2423) 
p.name()   #Process name
p.exe()    #The bin path of the process
p.cwd()    #Absolute path to the working directory of a process
p.status()   #Process state
p.create_time()  #Process creation time
p.uids()    #Process uid information
p.gids()    #gid information for processes
p.cpu_times()   #cpu time information of a process, including user and system cpu Information
p.cpu_affinity()  #get process cpu affinity, if you want to set cpu affinity, cpu number as a reference
p.memory_percent()  #Process memory utilization
p.memory_info()    #Process memory rss,vms information
p.io_counters()    #IO information for processes, including reading and writing IO numbers and parameters
p.connectios()   #Return process list
p.num_threads()  #Number of threads opened by a process
//After listening to the open method of psutil to start the application, you can track the relevant information of the program.
from subprocess import PIPE
p = psutil.Popen(["/usr/bin/python", "-c", "print('hello')"],stdout=PIPE)
p.name()
p.username()

Topics: network Python Linux Google