Is there any C API to calculate per CPU load? /proc/loadavg gives the CPU average load for all available CPUs and /proc/stat doesn't mention anything about active processes per CPU.

How can I obtain the load per CPU?

  • Did you try man proc 5 to see if there is anything else? I couldn't find it with a quick look, but you may have better luck. I would think it is somewhere hidden there, as the system monitor nicely shows what you want (I think). – Bernhard Aug 11 '14 at 6:38
  • yes I looked at man proc 5 it gives me information about the each process but I am looking at the aggregated CPU load. is there any file where per CPU load is updated? that will also suffice my problem – diago Aug 11 '14 at 8:34
  • The load is not usually kept per CPU, since the run queue is a single entity (or at least was when I browsed through the relevant code). – Didi Kohen Aug 11 '14 at 10:46

There is no such thing as per-CPU load.

Load average is defined as the number of processes waiting on system resources. This isn't necessarily CPU. Also since a process might be waiting on CPU time, and isn't actively running on a CPU, you can't attribute it to any particular CPU since the process could end up running on any CPU (unless pinned).

However you can get utilization % per-CPU. Use /proc/stat, and use the 4th field of each CPU to determine how idle that CPU is (See man 5 proc and the /proc/stat section).
Basically read the value, sleep 1 second, read the value again, divide the difference by USER_HZ.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.