Skip to content

Stopwatch.GetTimestamp() doesn't take into account sleep time on Unix #77945

@iSazonov

Description

@iSazonov

Description

Stopwatch.GetTimestamp() doesn't take into account suspend/sleep time on Unix but does on Windows.

Discovered in PowerShell repo PowerShell/PowerShell#18469

Reproduction Steps

  1. Get Stopwatch.GetTimestamp() value
  2. Sleep/hibernate your notebook
  3. Wait a while and wake up the notebook
  4. Get Stopwatch.GetTimestamp() value

Expected behavior

Stopwatch.GetTimestamp() value takes into account the sleep time.

Actual behavior

Stopwatch.GetTimestamp() value doesn't take into account the sleep time on Unix.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

Related code

uint64_t SystemNative_GetTimestamp(void)
{
#if HAVE_CLOCK_GETTIME_NSEC_NP
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
#else
struct timespec ts;
int result = clock_gettime(CLOCK_MONOTONIC, &ts);
assert(result == 0); // only possible errors are if MONOTONIC isn't supported or &ts is an invalid address
(void)result; // suppress unused parameter warning in release builds
return ((uint64_t)(ts.tv_sec) * SecondsToNanoSeconds) + (uint64_t)(ts.tv_nsec);
#endif
}

uses CLOCK_UPTIME_RAW - the bug seems to be right here. Should it be CLOCK_BOOTTIME?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Threadingin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions