-
-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug in impl Sub<CpuUsage> for CpuUsage #199
Comments
I got the same behaviour on a Linux machine (Intel(R) Xeon(R) CPU E5-2650, 48 cores, including HT). |
I tried inside a Docker container on my mac ( Note that I assigned a single core to the Docker VM on my mac:
If I look at
See What is weird about the above is that the VM has a single CPU (a single core even). Both |
I've opened an issue on psutil which seems to suffer from this too: giampaolo/psutil#1700 |
As reported on Gitter, I think I've found a bug in the
impl Sub<CpuUsage> for CpuUsage
.I was trying to get the cpu usage and verify against
htop
'sCPU%
column. It turned out there was a factor of 2 in whatheim
was reporting vs what htop was reporting.The verification is simple. I create two threads which busy-loop long enough to be measurable (say 5 seconds). Because two threads are fully busy, I expected to see 200% as the CPU usage. This is what
htop
reports.heim
on the contrary reports 100%.Note that this is hardware dependent. I've ran this on my macbook pro which contains a i7-7820HQ. This (single) CPU is a quad-core + hyperthreading.
htop
sees the 8 cores and reports a % for each.Here's the really simple code I've used:
On my machine, the busy-loop takes just less than seconds.
When run, the code abode reports:
Note that I am not using
--release
as I don't want the loop to be optimized out.I looked at the code
impl Sub<CpuUsage> for CpuUsage
code and I think I've found the bug.After having calculated the difference in time between the two
CpuUsage
:https://github.com/heim-rs/heim/blob/v0.0.9/heim-process/src/process/cpu_usage.rs#L25
this difference is multiplied by the
cpu_count
:https://github.com/heim-rs/heim/blob/v0.0.9/heim-process/src/process/cpu_usage.rs#L33-L34
A ratio is obtained by dividing the time difference of user+system by the time difference between the two
CpuCount
:https://github.com/heim-rs/heim/blob/v0.0.9/heim-process/src/process/cpu_usage.rs#L37
This results in the ration containing a
1 / cpu_count
.Then later on the ratio is multipled by the
cpu_count
:https://github.com/heim-rs/heim/blob/v0.0.9/heim-process/src/process/cpu_usage.rs#L38
This cancels out the previous
1 / cpu_count
.At this point, the variable
single_cpu_ratio
is independent of thecpu_count
, which I believe is not the expected behaviour.I was told the code was inspired/ported from Python's
psutils
package:https://github.com/giampaolo/psutil/blob/e65cc95de72828caed74c7916530dd74fca351e3/psutil/__init__.py#L958-L1048
Looking at this code, the comments does mention the core scaling. See:
https://github.com/giampaolo/psutil/blob/e65cc95de72828caed74c7916530dd74fca351e3/psutil/__init__.py#L1024-L1026
and:
https://github.com/giampaolo/psutil/blob/e65cc95de72828caed74c7916530dd74fca351e3/psutil/__init__.py#L1033-L1035
Surprisingly, the Python code seems to also suffer from the "ratio is independent of cpu count" issue! The
timer()
function is scaled by the number of cpu:https://github.com/giampaolo/psutil/blob/e65cc95de72828caed74c7916530dd74fca351e3/psutil/__init__.py#L999
This timer() output is used to measure the time difference
delta_time
:https://github.com/giampaolo/psutil/blob/e65cc95de72828caed74c7916530dd74fca351e3/psutil/__init__.py#L1002
https://github.com/giampaolo/psutil/blob/e65cc95de72828caed74c7916530dd74fca351e3/psutil/__init__.py#L1018
So at this point
delta_time
is scaled by the number of cpu.The ratio is then taken:
https://github.com/giampaolo/psutil/blob/e65cc95de72828caed74c7916530dd74fca351e3/psutil/__init__.py#L1027
resulting in a factor of
1 / cpu_count
for theoverall_cpus_percent
. The finally theoverall_cpus_percent
variable is multiplied back by the number of cpu, cancelling it out!https://github.com/giampaolo/psutil/blob/e65cc95de72828caed74c7916530dd74fca351e3/psutil/__init__.py#L1047
Looking at
psutils
's issue tracker I find the following:The text was updated successfully, but these errors were encountered: