Fix CPU utilization calculation

Currently, CPU utilization is calculated by (activeTimeDiff /
(activeTimeDiff + idleTimeDiff)), where idleTimeDiff is defined as "idle
time + IO wait time". The idleTimeDiff term is incorrect -- it
should be "everything else". As of current, one can have "kernel
utilization", "userspace utilization" and "overall utilization"
reach "100%" simultaneously which is does not make sense.

This change calculates CPU usage as follows:
* Kernel-space usage is "kernel time delta" / "total time delta".
* Userspace usage is "userspace delta" / "total time delta".
* Overall usage delta is "(kernel time delta + userspace delta)" /
  "total time delta".

Tested:
Compile the "Explicit sampling version of the SmallPT path tracer"
(https://www.kevinbeason.com/smallpt/explicit.cpp) as a workload,
and run two copies of it on the BMC to fully stress the CPU cores.
(Alternatively, any benchmark program can fulfill this purpose, but for
this one, I understand what it does and know it's compute-bound.)

One can see from `htop`, both CPU cores are almost 100% occupied. Most
(around 90%) CPU time is spent in user-space. Remainder of the CPU
usage is attributable to other tasks and background processing.

When one checks the
`/xyz/openbmc_project/sensors/utilization/CPU_Kernel` and
`/xyz/openbmc_project/sensors/utilization/CPU_User` objects, one can see
CPU_User reading ramp up and reach around 90%. CPU_Kernel stabilizes
at 10%. When `smallpt_explicit` is terminated, kernel and userspace CPU
usage re-converge to their normal values.

Change-Id: I7c0e10e08bd2b6c8b3bd1c1a618fffb2739feecc
Signed-off-By: Sui Chen <suichen@google.com>
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
1 file changed