psutil工具使用介绍

psutil使用指南


系统相关方法介绍


CPU
psutil.cpu_times(percpu=False)

说明:
  返回一个元组,包含cpu运行时间的各个参数。每一个参数的值代表cpu在给定模式下运行的秒数

  • user —运行在用户态的普通进程所消耗的cpu时间,在linux中这个时间包括guest时间
  • system —运行在内核态的进程所消耗的cpu时间
  • idle —cpu空闲时间

平台指定参数:

nice (UNIX): time spent by niced (prioritized) processes executing in user mode; on Linux this also includes guest_nice time
iowait (Linux): time spent waiting for I/O to complete
irq (Linux, BSD): time spent for servicing hardware interrupts
softirq (Linux): time spent for servicing software interrupts
steal (Linux 2.6.11+): time spent by other operating systems running in a virtualized environment
guest (Linux 2.6.24+): time spent running a virtual CPU for guest operating systems under the control of the Linux kernel
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)
interrupt (Windows): time spent for servicing hardware interrupts ( similar to “irq” on UNIX)
dpc (Windows): time spent servicing deferred procedure calls (DPCs); DPCs are interrupts that run at a lower priority than standard interrupts.

当参数percpu=True时,返回一个列表,包含系统里所有逻辑CPU的运行参数。第一个参数代表第一个CPU,第二个参数代表第二个CPU,以此类推。

The order of the list is consistent across calls

In [1]: psutil.cpu_times()
Out[1]: scputimes(user=292.39, nice=0.08, system=99.33, idle=300619.63, iowait=64.31, irq=0.0, softirq=4.03, steal=0.0, guest=0.0, guest_nice=0.0)

In [2]: psutil.cpu_times(percpu=True)
Out[2]:
[scputimes(user=118.54, nice=0.07, system=46.51, idle=150864.2, iowait=27.19, irq=0.0, softirq=0.5, steal=0.0, guest=0.0, guest_nice=0.0),
 scputimes(user=174.09, nice=0.0, system=52.96, idle=150757.58, iowait=37.15, irq=0.0, softirq=3.53, steal=0.0, guest=0.0, guest_nice=0.0)]

psutil.cpu_percent(interval=None, percpu=False)

说明:
  返回一个浮点数,表示在整个系统中cpu的利用率百分比。参数interval为0.0或者None时,将立即返回自上一次调用后的时间内的CPU运行时间结果。参数大于0.0时,则返回时间间隔内的CPU运行时间结果。因此,当interval是None或者0.0时,第一次调用的结果将返回0.0,你需要忽略这个返回值。如果需要精确的结果,推荐将interval设置为至少为0.1s。当percpu为True时,将返回一个列表,包含每个CPU的利用率。

In [31]: psutil.cpu_percent(interval=1, percpu=False)
Out[31]: 0.0

In [32]: psutil.cpu_percent(interval=1, percpu=True)
Out[32]: [0.0, 0.0]

psutil.cpu_times_percent(interval=None, percpu=False)

说明:
  和cpu_times的返回值类似,只不过是换为了百分比形式。并且interval为None或者0.0时的返回情况和上一个方法的结果一样。不推荐使用第一次返回值。


psutil.cpu_count(logical=True)

说明:
  返回逻辑CPU个数,如果不确定时,将返回None。如果logical=False,返回CPU物理核数。但是在OpenBSD和NetBSD系统中,psutil.cpu_count(logical=False)返回None。

len(psutil.Process().cpu_affinity())

返回当前可被进程使用的cpu数目

>>> psutil.cpu_count()
4
>>> psutil.cpu_count(logical=False)
2
>>> len(psutil.Process().cpu_affinity())
1

psutil.cpu_stats()

该方法在4.1.0中新增


psutil.cpu_freq(percpu=False)

该方法在5.1.0中新增,不过我装的是5.4.0,还是会报下面的错误
虚拟机下报错:

In [41]: psutil.cpu_freq(percpu=False)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-41-1a21b94419c9> in <module>()
----> 1 psutil.cpu_freq(percpu=False)

AttributeError: 'module' object has no attribute 'cpu_freq'

memory
psutil.virtual_memory()
psutil.swap_memory()

Disks
psutil.disk_partitions(all=False)

all=False时,尝试只返回物理设备(磁盘,cd-rom驱动,USB keys),不过并不是全平台可靠的。(e.g. on BSD this parameter is ignored)


psutil.disk_usage(path)

返回包含给定路径的磁盘的统计信息。


psutil.disk_io_counters(perdisk=False, nowrap=True)

返回系统级别的I/O统计信息

read_count: number of reads
write_count: number of writes
read_bytes: number of bytes read
write_bytes: number of bytes written

平台差异性返回值:
read_time: (all except NetBSD and OpenBSD) time spent reading from disk (in milliseconds)
write_time: (all except NetBSD and OpenBSD) time spent writing to disk (in milliseconds)
busy_time: (Linux, FreeBSD) time spent doing actual I/Os (in milliseconds)
read_merged_count (Linux): number of merged reads (see iostat doc)
write_merged_count (Linux): number of merged writes (see iostats doc)

If perdisk is True return the same information for every physical disk installed on the system as a dictionary with partition names as the keys and the named tuple described above as the values. See iotop.py for an example application. On some systems such as Linux, on a very busy or long-lived system, the numbers returned by the kernel may overflow and wrap (restart from zero). If nowrap is True psutil will detect and adjust those numbers across function calls and add “old value” to “new value” so that the returned numbers will always be increasing or remain the same, but never decrease. disk_io_counters.cache_clear() can be used to invalidate the nowrap cache.


Network
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值