Rosetta 2 error: incorrect value for values reported by libproc

Number:rdar://FB9546856 Date Originated:2021-08-21
Status:Open Resolved:
Product:macOS Product Version:11.5.2 (20G95)
Classification:Incorrrect/Unexpected Behavior Reproducible:yes
Apologies for the chosen category. I tried searching for a category specific to XNU, Mach, Darwin, Rosetta, etc., but couldn’t find any.

`libproc.h` has a struct called `proc_taskinfo`, whose `pti_total_user` and `pti_total_system` fields report the amount of user and system CPU time a given process has consumed over its lifetime. Under Rosetta 2 on an M1 Mac Mini, these values are wrong, off by a factor of ~41.666x.

When running natively on x86_64 (tested on an Intel i5-5257U), `mach_timebase_info` returns `1/1`, thus the mach timebase units are equivalent to nanoseconds. The values of `pti_total_user` are correct (They match the CPU time reported by Activity

When running natively on arm64e (tested on an M1 Mac mini), `mach_timebase_info` returns `125/3`. Multiplying the values of `pti_total_user` by this constant (~41.666) yields a value that’s correct in nanoseconds (They also match the CPU time reported by Activity

However, under Rosetta 2 on the same M1 Mac Mini, there’s a mismatch:
  - `mach_timebase_info` returns a factor of `1/1`, just like on Intel
  - The values of `pti_total_user` are the same as native M1 execution, which are native mach tick units (each of which is 3/125th’s of a nanosecond)

Thus, if you take the value of `pti_total_user`, multiply it by the factor returned by `mach_timebase_info` (as the documentation prescribes, you get a time in nanoseconds which is ~41.666x *too small*.

Working around this requires that you:
1. add a special case to detect when running an M1 under Rosetta 2
2. ignore the result of `mach_timebase_info ` completely
3. multiply by the same 125/3 factor as if `mach_timebase_info` was run natively


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!