Search code examples
pythonpsutil

psutils, how to correctly calculate a process CPU utilization


I have the following process:

PID = 1245
p   = psutil.Process(PID)

When I calculate the cpu utilization of this process:

print(p.cpu_percent())

it gives something like 25%. While the whole CPU utilization is about 3%:

print(psutil.cpu_pecent())

How come? How can I get a representative percentage of this particular process ?


Solution

  • Most likely due to the 3% being total usage over all cores while 25% is usage of a single core capacity. You can always look at the documentation of psutil.cpu_percent() and psutil.Process.cpu_percent() to get some more in-depth explanation of the behaviour.

    To get the usage of total system capacity you need to divide it by the number of CPUs you got access to:

    PID = 1245
    p   = psutil.Process(PID)
    tot_load_from_process = p.cpu_percent()/psutil.cpu_count()
    print(tot_load_from_process)
    print(psutil.cpu_pecent())