gpt4 book ai didi

linux - psutil cpu_percent v/s psutil cpu_times_percent

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:19:00 29 4
gpt4 key购买 nike

我正在尝试为我的 Linux 服务器编写一个小的异常警报应用程序,我发现 psutil 是一个非常方便的库。

不过,我不是很清楚psutil.cpu_percent(interval=1)psutil.cpu_times_percent(interval=1)的区别

当我运行前者时,我第一次得到以下信息:

  percentage avg CPU utilization:
0.2

cpu_times_percent(interval=1) 给我:

 CPU percentage time:
scputimes(user=0.0, nice=0.0, system=0.0, idle=100.0, iowait=0.0, irq=0.0,
softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)

我第二次运行相同的程序时,我得到:

 percentage avg CPU utilization:
0.0

但是,cpu_times_percent() 给我以下输出:

 CPU percentage time:
scputimes(user=0.2, nice=0.0, system=0.0, idle=99.8, iowait=0.0, irq=0.0,
softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)

此外,从 cpu_times() 获得的 CPU IO 等待时间为我提供了以下输出:

  CPU IO wait time:
165.98

然而,正如您从上面的观察中看到的,% 中的 iowait 为零。

基于以上观察,我有以下问题:-

  • cpu_percent() 究竟返回了什么?
    • 是用户空间+系统+Nice的总百分比吗?
  • 如何像上面观察到的那样为零?
  • cpu_percent()cpu_times_percent( interval=1) 有什么区别?
    • 为什么它们返回不同的值?

IO 等待时间是否源自 CPU 空闲时间(因为如果进程在等待 IO 时被阻塞,CPU 不会做任何事情)?从 cpu_times 中提取时为什么不为零但在 cpu_times_percent 中显示为零?

我正在努力弄清上述方法并决定使用哪一种。我正在寻找的是总 CPU 利用率百分比。类似地,iowait 作为百分比。

最佳答案

如果您为 psutil.cpu_percent 提供帮助,它会清楚地说明您第一次运行时可能会给出 0.0。所以使用 psutil.cpu_times_percent 使用 interval 参数即 psutil.cpu_times_percent(interval=0.1)

>>> help(psutil.cpu_times_percent)
Help on function cpu_times_percent in module psutil:

cpu_times_percent(interval=None, percpu=False)
Same as cpu_percent() but provides utilization percentages
for each specific CPU time as is returned by cpu_times().
For instance, on Linux we'll get:

>>> cpu_times_percent()
cpupercent(user=4.8, nice=0.0, system=4.8, idle=90.5, iowait=0.0,
irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
>>>

*interval* and *percpu* arguments have the same meaning as in
cpu_percent().
>>> help(psutil.cpu_percent)
Help on function cpu_percent in module psutil:

cpu_percent(interval=None, percpu=False)
Return a float representing the current system-wide CPU
utilization as a percentage.

When *interval* is > 0.0 compares system CPU times elapsed before
and after the interval (blocking).

When *interval* is 0.0 or None compares system CPU times elapsed
since last call or module import, returning immediately (non
blocking). That means the first time this is called it will
return a meaningless 0.0 value which you should ignore.
In this case is recommended for accuracy that this function be
called with at least 0.1 seconds between calls.

When *percpu* is True returns a list of floats representing the
utilization as a percentage for each CPU.
First element of the list refers to first CPU, second element
to second CPU and so on.
The order of the list is consistent across calls.

Examples:

>>> # blocking, system-wide
>>> psutil.cpu_percent(interval=1)
2.0
>>>
>>> # blocking, per-cpu
>>> psutil.cpu_percent(interval=1, percpu=True)
[2.0, 1.0]
>>>
>>> # non-blocking (percentage since last call)
>>> psutil.cpu_percent(interval=None)
2.9
>>>

关于linux - psutil cpu_percent v/s psutil cpu_times_percent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46508421/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com