sunny
sunny

Reputation: 653

psutil cpu_percent v/s psutil cpu_times_percent

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:-

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.

Upvotes: 1

Views: 3267

Answers (1)

Sandeep Lade
Sandeep Lade

Reputation: 1943

If you help for psutil.cpu_percent it clearly says when you run first time it may give 0.0. So use psutil.cpu_times_percent using interval argument i.e 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
      >>>

Upvotes: 1

Related Questions