I am trying to write a small anomaly alarming application for my linux servers, and I found psutil
to be a very handy library.
However, I am not very clear about the difference between psutil.cpu_percent(interval=1)
and psutil.cpu_times_percent(interval=1)
When I run the former, the first time I get the following:
percentage avg CPU utilization:
0.2
The cpu_times_percent(interval=1)
gives me:
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)
The second time I run the same, I get:
percentage avg CPU utilization:
0.0
However, cpu_times_percent()
gives me the following output:
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)
Moreover, the CPU IO wait time obtained from cpu_times()
, gives me the following output:
CPU IO wait time:
165.98
However as you can see from the observation above, iowait
in % is zero.
Based on the above observation, I have the following questions:-
- What exactly is returned by
cpu_percent()
?- Is it the % total in the user space+ system+Nice?.
- How is it zero as observed above?
- What is the difference between
cpu_percent()
andcpu_times_percent( interval=1)?
.- Why do they return different values ?
Is the IO wait time derived from the CPU idle time (since CPU is not doing anything if the processes are blocked while waiting for IO)?
How is it not zero when extracted from cpu_times
but shown as zero in the cpu_times_percent
?
I am trying to make sense for myself out the above methods and decide on which one to use.
What I am looking for is the total CPU utilization as a percentage.
Similarly iowait
as a percentage.