Python – psutil (CPU , MEM)

Loading

測試環境為 CentOS8 (虛擬機)

Python 的 psutil (python system and process utilities) 套件是一個可以跨平台讀取系統 CPU 與 memory (這邊說明), 其他如 disks 與 network 請參考 – https://benjr.tw/105296 , sensors 資訊與使用率工具.

安裝 psutil 套件.

[root@localhost ~]# pip install psutil
[root@localhost ~]# python3
Python 3.6.8 (default, Sep 21 2021, 20:17:36)
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.

匯入 psutil 套件.

>>> import psutil

CPU

先來看一下與 CPU 相關資訊,下面指令可以看 CPU 的核心數,核心有分實體與邏輯顆數,透過 logical = True (邏輯 CPU 數量) 或是 False (實體 CPU 數量)來指定.

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

因為我的是虛擬機所以看到的實體與邏輯 CPU 數量都一樣.

檢視 CPU 的工作頻率,有分 current(目前) , min (最低) 與 max(最高) ,單位為 Mhz.

>>> psutil.cpu_freq()

好像我是虛擬機的緣故,所以看不到資訊.

檢視 CPU 在指定時間 interval 的使用率, 透過 percpu = True 來檢視單顆 CPU 或是 False 檢視全部 CPU.

>>> psutil.cpu_percent(interval=5, percpu=True)
[0.0, 0.0, 0.0, 0.0]
>>> psutil.cpu_percent(interval=5, percpu=False)
0.0

透過下面可以檢視 CPU 資源在不同使用狀態下 (如 user , system 與 idle) 的個別花費秒數,回傳值會依據不同平台略有不同.

>>> psutil.cpu_times()
scputimes(user=253.15, nice=14.22, system=246.0, idle=469523.2, iowait=5.95, irq=247.52, softirq=157.34, steal=0.0, guest=0.0, guest_nice=0.0)
  1. user: time spent by normal processes executing in user mode; on Linux this also includes guest time
  2. system: time spent by processes executing in kernel mode
  3. idle: time spent doing nothing

以下屬性會因為使用平台而異.

  1. nice (UNIX): time spent by niced (prioritized) processes executing in user mode; on Linux this also includes guest_nice time
  2. iowait (Linux): time spent waiting for I/O to complete. This is not accounted in idle time counter.
  3. irq (Linux, BSD): time spent for servicing hardware interrupts
  4. softirq (Linux): time spent for servicing software interrupts
  5. steal (Linux 2.6.11+): time spent by other operating systems running in a virtualized environment
  6. guest (Linux 2.6.24+): time spent running a virtual CPU for guest operating systems under the control of the Linux kernel
  7. guest_nice (Linux 3.2.0+): time spent running a niced guest (virtual CPU for guest operating systems under the control of the Linux kernel)
  8. interrupt (Windows): time spent for servicing hardware interrupts ( similar to “irq” on UNIX)
  9. dpc (Windows): time spent servicing deferred procedure calls (DPCs); DPCs are interrupts that run at a lower priority than standard interrupts.

預設 percpu=False 可以指定為 True 來檢視單顆 CPU 狀態.

>>> psutil.cpu_times(percpu=True)
[scputimes(user=37.87, nice=3.28, system=44.2, idle=117496.38, iowait=0.69, irq=56.23, softirq=6.25, steal=0.0, guest=0.0, guest_nice=0.0), scputimes(user=35.9, nice=3.58, system=111.74, idle=117338.95, iowait=4.45, irq=58.53, softirq=11.22, steal=0.0, guest=0.0, guest_nice=0.0), scputimes(user=30.9, nice=3.14, system=42.73, idle=117479.69, iowait=0.32, irq=63.51, softirq=11.42, steal=0.0, guest=0.0, guest_nice=0.0), scputimes(user=148.48, nice=4.22, system=47.35, idle=117246.12, iowait=0.48, irq=69.25, softirq=128.46, steal=0.0, guest=0.0, guest_nice=0.0)]

更多關於 CPU 資訊請參考說明文件 https://psutil.readthedocs.io/en/latest/#cpu

Memory

檢視記憶體的使用狀況.

>>> print(psutil.virtual_memory())
svmem(total=8118042624, available=6396657664, percent=21.2, used=1388883968, free=4685815808, active=215633920, inactive=2646134784, buffers=3276800, cached=2040066048, shared=20049920)
  1. total: total physical memory (exclusive swap).
  2. available: the memory that can be given instantly to processes without the system going into swap. This is calculated by summing different memory values depending on the platform and it is supposed to be used to monitor actual memory usage in a cross platform fashion.

以下屬性會因為使用平台而異.

  1. used: memory used, calculated differently depending on the platform and designed for informational purposes only. total – free does not necessarily match used.
  2. free: memory not being used at all (zeroed) that is readily available; note that this doesn’t reflect the actual memory available (use available instead). total – used does not necessarily match free.
  3. active (UNIX): memory currently in use or very recently used, and so it is in RAM.
  4. inactive (UNIX): memory that is marked as not used.
  5. buffers (Linux, BSD): cache for things like file system metadata.
  6. cached (Linux, BSD): cache for various things.
  7. shared (Linux, BSD): memory that may be simultaneously accessed by multiple processes.
  8. slab (Linux): in-kernel data structures cache.
  9. wired (BSD, macOS): memory that is marked to always stay in RAM. It is never moved to disk.

檢視 swap memory 虛擬記憶體的使用狀況.

>>> psutil.swap_memory()
sswap(total=4226805760, used=0, free=4226805760, percent=0.0, sin=0, sout=0)
  1. total: total swap memory in bytes
  2. used: used swap memory in bytes
  3. free: free swap memory in bytes
  4. percent: the percentage usage calculated as (total – available) / total * 100
  5. sin: the number of bytes the system has swapped in from disk (cumulative)
  6. sout: the number of bytes the system has swapped out from disk (cumulative)

更多關於 Memory 資訊請參考說明文件 https://psutil.readthedocs.io/en/latest/#memory

沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料