Skip to content
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

Issue 90788 improvments to System.Diagnostics.Stopwatch #90789

Closed

Conversation

andreas-synnerdahl
Copy link

Implementation of changes suggested in Issue 90788 to System.Diagnostics.Stopwatch to improve when measuring small time periods.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Aug 18, 2023
@ghost
Copy link

ghost commented Aug 18, 2023

Tagging subscribers to this area: @tommcdon
See info in area-owners.md if you want to be subscribed.

Issue Details

Implementation of changes suggested in Issue 90788 to System.Diagnostics.Stopwatch to improve when measuring small time periods.

Author: andreas-synnerdahl
Assignees: -
Labels:

area-System.Diagnostics, community-contribution

Milestone: -

@andreas-synnerdahl
Copy link
Author

@dotnet-policy-service agree

@tommcdon
Copy link
Member

@stephentoub

Comment on lines +128 to +137
if (elapsedUntilNow > 0)
{
// When measuring small time periods the Stopwatch.Elapsed*
// properties can return negative values. This is due to
// bugs in the basic input/output system (BIOS) or the hardware
// abstraction layer (HAL) on machines with variable-speed CPUs
// (e.g. Intel SpeedStep).

timeElapsed += elapsedUntilNow;
}
Copy link
Member

Choose a reason for hiding this comment

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

We utilize the monotonic timers available from the underlying OS (QueryPerformanceCounter on Windows, CLOCK_UPTIME_RAW or CLOCK_MONOTONIC on Unix) which have strict contracts around not going backwards,

We can have up to a 1ns resolution, in which case overflow would take ~292 years in the worst possible scenario.

Given the support lifecycle of .NET and pieces of hardware, I'd say you have many other problems if you've not restarted your machine in 292 years ;)

Copy link
Member

@tannergooding tannergooding Aug 18, 2023

Choose a reason for hiding this comment

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

The comment here (copied from the other place above) relates to much older fallback behavior that existed for very old systems before QPC existed (back when we supported cases like Win98 or early Win2k machines).

Nowadays we require a high precision timer to exist and I don't expect that to change at any point in the future, so a simple assert is probably plenty fine.

@stephentoub
Copy link
Member

stephentoub commented Aug 18, 2023

Thanks for trying to improve things here.

I think it'd be better to address #66734 by just deleting the relevant code.

@tommcdon
Copy link
Member

@andreas-synnerdahl thanks for contributing this PR to .NET! Please let us know if you plan to make the suggested code changes to the PR. Otherwise would you mind changing it to 'draft' mode?

@tommcdon tommcdon marked this pull request as draft September 18, 2023 14:20
@tommcdon
Copy link
Member

Hi @andreas-synnerdahl I've moved this PR to draft as there hasn't been much activity lately. Feel free to move it out of draft mode if you plan on addressing the feedback.

@ghost ghost closed this Oct 18, 2023
@ghost
Copy link

ghost commented Oct 18, 2023

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 17, 2023
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Diagnostics community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants