Skip to content

Mac OS X: System.Diagnostics.Process mis-reporting CPU usage (dramatically) on Apple Silicon #98121

Closed

Description

@huntharo

Description

On Mac OS X, with Apple Silicon, the System.Diagnostics.Process class returns incorrect (substantially underreported - ms when it should be seconds) CPU usage times for the process (and probably for the thread-level stats too).

I discovered this while writing a function to tune the max # of worker threads in the ThreadPool as it reduces CPU usage from 700% to 120% while increasing throughput for my project (pwrdrvr/lambda-dispatch#109). I was always computing that 0 threads would be needed because CPU usage was only going up by 20 ms per 5 seconds despite using 700% CPU.

Reproduction Steps

  1. Obtain Apple Silicon Mac
  2. Run example

Buildable example repo:
https://github.com/huntharo/dotnet-macos-apple-silicon-cpu-time

using System.Diagnostics;

// Initial CPU usage:
var initialCpuUsageMs = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
Console.WriteLine($"Initial CPU usage: {Process.GetCurrentProcess().TotalProcessorTime}");

// Burn 5 seconds of CPU time
var sw = Stopwatch.StartNew();
while (sw.ElapsedMilliseconds < 5000) { }

// Final CPU usage:
Console.WriteLine($"Final CPU usage: {Process.GetCurrentProcess().TotalProcessorTime}");

// Delta:
Console.WriteLine($"Delta CPU usage (ms): {Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - initialCpuUsageMs}");

// Wall time:
Console.WriteLine($"Wall time (ms): {sw.ElapsedMilliseconds}");

Expected behavior

CPU time for burning 5 seconds of CPU in a tight loop should be ~5 seconds as reported in top, activity monitor, etc.

Actual behavior

CPU time is reported as 120 ms

Regression?

This does not appear to be a regression but rather a missing multiplier that "just worked" until Apple Silicon was released and the multiplier changed from 1.

Known Workarounds

None, other than obtaining the multiplier and applying it to the TimeSpan which makes no sense because the TimeSpan should be a duration not a value to be multiplied to then become a duration.

Configuration

  • ARM64
  • Mac OS X Sonoma 14.0
  • .NET 8.0.101
  • This is specific to Apple Silicon as far as I can tell

Other information

Raw Mach Time API In general prefer to use the <time.h> API clock_gettime_nsec_np(3), which deals in the same clocks (and more) in ns units. Conversion of ns to (resp. from) tick units as returned by the mach time APIs is performed by division (resp. multiplication) with the fraction returned by mach_timebase_info().

https://developer.apple.com/documentation/driverkit/mach_timebase_info_data_t

This stackoverflow post contains a very small sliver of info about the API that needs to be called:
https://stackoverflow.com/a/72915413/878903

Activity

  1. ghost added
    untriaged
    on Feb 7, 2024
  2. ghost added
    needs-area-label
    on Feb 7, 2024
  3. added and removed
    needs-area-label
    on Feb 7, 2024
  4. vcsjones commented on Feb 7, 2024

    @vcsjones
    Member

    This looks like a duplicate of #91958.

  5. huntharo commented on Feb 7, 2024

    @huntharo
    Author

    Thanks for the quick note @vcsjones !

    Yes, it does look like that fixes it. However, although that was 5 months ago that was not released or backported to DotNet 8.0, it's only on main. The problem still exists in all released versions (as far as I can tell - it's not on the 8.0 release or 8.0 staging branches which are actively getting changes).

    Is it possible to get this fix released?

  6. added this to the 8.0.x milestone on Mar 22, 2024
  7. removed
    untriaged
    on Jun 25, 2024
  8. locked and limited conversation to collaborators on Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions