Skip to content

Conversation

@jozkee
Copy link
Member

@jozkee jozkee commented Jun 11, 2025

Found on a release/9.0 build https://dev.azure.com/dnceng-public/public/_build/results?buildId=1064508

System.Diagnostics.Tests.ProcessTests.TestTotalProcessorTimeMacOs [FAIL]
  Time 'user' returned by managed API (00:00:01.4886940) should be greated or equal to the time returned by native API (00:00:01.4886980).
  Stack Trace:
    /_/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs(881,0): at System.Diagnostics.Tests.ProcessTests.<TestTotalProcessorTimeMacOs>g__AssertTime|229_0(TimeSpan managed, TimeSpan native, String label)
    /_/src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs(870,0): at System.Diagnostics.Tests.ProcessTests.TestTotalProcessorTimeMacOs()
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
    /_/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.cs(57,0): at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

there was a 4 microsecond difference between the time calculated using macOS' proc_pid_rusage and mach_timebase_info and Process.UserProcessorTime.

I want to believe this was caused by rounding and/or precision differences between the macOS specific methods the test uses vs getrusage, used by the product.

Running the test locally on M1, I see the difference in the assertion is 655us.

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

The PR adds a microsecond-level tolerance to the macOS processor time test to account for small discrepancies between the managed and native APIs.

  • Introduces a ToleranceInMicroseconds constant and computes the time difference in microseconds
  • Updates the assertion to allow a ±20 μs deviation
  • Adjusts the error message to mention the tolerance
Comments suppressed due to low confidence (1)

src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs:882

  • TimeSpan does not have a TotalMicroseconds property. Consider computing microseconds via (managed - native).Ticks / 10 or (managed - native).TotalMilliseconds * 1000.
double differenceUs = (managed - native).TotalMicroseconds;

@tarekgh
Copy link
Member

tarekgh commented Oct 20, 2025

   [Fact]
   [PlatformSpecific(TestPlatforms.OSX)]
   public unsafe void TestTotalProcessorTimeMacOs()
   {

Just to tell, we have

Environment.CpuUsage too. just FYI if you want to double check against it too. not required though.

https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Environment.UnixOrBrowser.cs,82


Refers to: src/libraries/System.Diagnostics.Process/tests/ProcessTests.Unix.cs:857 in 171b2cd. [](commit_id = 171b2cd, deletion_comment = False)

@jozkee jozkee enabled auto-merge (squash) October 20, 2025 21:08
@jozkee
Copy link
Member Author

jozkee commented Oct 21, 2025

/ba-g System.Security.Cryptography.Tests DeadLetter'd

@jozkee jozkee merged commit d852944 into dotnet:main Oct 21, 2025
83 of 85 checks passed
@jozkee jozkee deleted the ProcessorTimeMacOs branch October 21, 2025 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Diagnostics.Process test-bug Problem in test source code (most likely)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants