1

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 ?

zezo
  • 445
  • 4
  • 16

1 Answers1

1

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())
Regretful
  • 336
  • 2
  • 10
  • How can I calculate this percentage out of all cores. is `usage=.03/.25` is the solution ? or how can I get a representative percentage of this particular process ? – zezo Jan 14 '22 at 10:47
  • I updated the answer with some code, hopefully it solves your problem. – Regretful Jan 14 '22 at 12:56
  • Thank you. I still see some differences when compared with the cpu usage from `task manager` – zezo Jan 15 '22 at 00:53
  • Is the CPU usage constant? Otherwise measuring at different times will yield different results (at the risk of being obvious). – Regretful Jan 16 '22 at 08:01