Inconsistent CPUTime reporting by getProcessCPUTime
Summary
Refer to the function getProcessCPUTime
in
https://gitlab.haskell.org/ghc/ghc/-/blob/master/rts/posix/GetTime.c .
When using clock_gettime it reports the total CPU time:
return getClockTime(CLOCK_PROCESS_CPUTIME_ID);
But when it defaults to getrusage it reports just the user
time which is different from what we report in the case described above which is cpu time = user time + system time.
getrusage(RUSAGE_SELF, &t);
return SecondsToTime(t.ru_utime.tv_sec) + USToTime(t.ru_utime.tv_usec);
This leads to inconsistent interpretation of this stat depending on which implementation is used at runtime.
Steps to reproduce
I noticed this discrepancy when trying to match the ghc reported stats with the stats that I measured by some other means. Looking at the code I found that there are two different implementation and my system is picking up the getrusage implementation for some reason. We can reproduce it by changing the GHC code to force it to use one or the other implementation, not sure if there is a way to force it at runtime though.
Expected behavior
Irrespective of using clock_gettime or getrusage it should report the total cpu time.
Environment
- GHC version used: 9.2 (the issue is present in all versions)
Optional:
- Operating System: Linux
- System Architecture: x86_64