Skip to content

TickCount on macOS/Linux does not include suspend time #119437

@marzent

Description

@marzent

Description

The Environment.TickCount and Environment.TickCount64 APIs on macOS and Linux do not include time spent in system suspend (e.g., sleep or standby), unlike their behavior on Windows.

This creates a cross-platform inconsistency, as Windows uses GetTickCount/GetTickCount64, which count wall-clock time, including suspend periods, and is also specified in the dotnet documentation as such:

Gets the number of milliseconds elapsed since the system started.

On macOS, Environment.TickCount64 uses clock_gettime_nsec_np(CLOCK_UPTIME_RAW) for both low/hi-res timers:

CLOCK_UPTIME_RAW

clock that increments monotonically, in the same man-
ner as CLOCK_MONOTONIC_RAW, but that does not incre-
ment while the system is asleep.  The returned value
is identical to the result of mach_absolute_time()
after the appropriate mach_timebase conversion is
applied.

On Linux, it uses clock_gettime(CLOCK_MONOTONIC, &ts) and the CLOCK_MONOTONIC_COARSE variant for low/hi-res timers respectively:

CLOCK_MONOTONIC

A nonsettable system-wide clock that represents monotonic time
since—as described by POSIX—"some unspecified point in the
past".  On Linux, that point corresponds to the number of sec‐
onds that the system has been running since it was booted.

The CLOCK_MONOTONIC clock is not affected by discontinuous
jumps in the system time (e.g., if the system administrator
manually changes the clock), but is affected by the incremen‐
tal adjustments performed by [adjtime(3)](https://man7.org/linux/man-pages/man3/adjtime.3.html) and NTP.  This clock
does not count time that the system is suspended.  All
CLOCK_MONOTONIC variants guarantee that the time returned by
consecutive calls will not go backwards, but successive calls
may—depending on the architecture—return identical (not-
increased) time values.

CLOCK_MONOTONIC_COARSE (since Linux 2.6.32; Linux-specific)

A faster but less precise version of CLOCK_MONOTONIC.  Use
when you need very fast, but not fine-grained timestamps.
Requires per-architecture support, and probably also architec‐
ture support for this flag in the [vdso(7)](https://man7.org/linux/man-pages/man7/vdso.7.html).

Note how on both platforms the opposite clock is chosen that should be used to include suspend time (i.e CLOCK_MONOTONIC_RAW on macOS and CLOCK_BOOTTIME on Linux.

Reproduction Steps

Observe Environment.TickCount and Environment.TickCount64 not advancing during suspend on Unix-like systems.

Expected behavior

Environment.TickCount and Environment.TickCount64 counting time in suspend as well on Unix-like systems.

Actual behavior

Environment.TickCount and Environment.TickCount64 not advancing during suspend on Unix-like systems.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions