TimeStamp::Now() and TimeStamp::NowLoRes() behave differently over computer sleep over platforms
Categories
(Core :: mozglue, defect, P4)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox43 | --- | affected |
People
(Reporter: gfritzsche, Assigned: jstutte)
References
(Depends on 1 open bug, Blocks 3 open bugs)
Details
(Whiteboard: [measurement:client])
Attachments
(1 file, 10 external files, 8 obsolete files)
|
10.73 KB,
text/html
|
Details |
| Reporter | ||
Updated•11 years ago
|
| Reporter | ||
Updated•11 years ago
|
| Reporter | ||
Comment 1•11 years ago
|
||
Comment 2•11 years ago
|
||
| Reporter | ||
Comment 3•11 years ago
|
||
Comment 4•11 years ago
|
||
| Reporter | ||
Comment 5•11 years ago
|
||
| Reporter | ||
Comment 6•11 years ago
|
||
Updated•11 years ago
|
Updated•11 years ago
|
Comment 7•11 years ago
|
||
| Reporter | ||
Comment 8•11 years ago
|
||
Comment 9•11 years ago
|
||
| Reporter | ||
Comment 10•11 years ago
|
||
| Reporter | ||
Updated•11 years ago
|
Comment 11•11 years ago
|
||
Comment 12•11 years ago
|
||
| Reporter | ||
Comment 13•11 years ago
|
||
Comment 14•11 years ago
|
||
Comment 15•11 years ago
|
||
Comment 16•10 years ago
|
||
Comment 17•10 years ago
|
||
| Reporter | ||
Updated•10 years ago
|
| Reporter | ||
Updated•9 years ago
|
| Reporter | ||
Updated•9 years ago
|
Comment 18•8 years ago
|
||
Comment 19•5 years ago
|
||
TimeStamp::NowLoRes now always returns timestamps that increment during the
device is suspended.
Depends on D98317
Updated•5 years ago
|
Updated•4 years ago
|
Comment 20•2 years ago
|
||
This is an issue with TimeStamp::Now() and TimeStamp::NowLoRes() (note: Lo not Low).
The link in the initial report is broken (but the description is still accurate). Here are the current links to the implementation:
- Windows: https://searchfox.org/mozilla-central/rev/1f46481d6c16f27c989e72b898fd1fddce9f445f/mozglue/misc/TimeStamp_windows.cpp#536-548
- macOS: https://searchfox.org/mozilla-central/rev/1f46481d6c16f27c989e72b898fd1fddce9f445f/mozglue/misc/TimeStamp_darwin.cpp#41-50,143-145
- Linux: https://searchfox.org/mozilla-central/rev/1f46481d6c16f27c989e72b898fd1fddce9f445f/mozglue/misc/TimeStamp_posix.cpp#208-215
- declarations of Now / NowLoRes: https://searchfox.org/mozilla-central/rev/1f46481d6c16f27c989e72b898fd1fddce9f445f/mozglue/misc/TimeStamp.h#428-442
| Assignee | ||
Updated•9 days ago
|
| Assignee | ||
Comment 22•9 days ago
|
||
I'd like to revive this, as it keeps showing up in different disguises (bug 2089250, bug 2074047, bug 1834683, bug 1709767). Current state:
- Windows: since bug 1995254,
TimeStamp::Now()is QPC only, which counts sleep (bug 2074047 is a consequence of that). - macOS:
mach_absolute_time, does not count sleep. - Linux/Android:
CLOCK_MONOTONIC, does not count sleep.
AwakeTimeStamp gives us a consistent "awake only" clock on all platforms, but TimeStamp remains "whatever the platform does". The most visible consequence is that nsITimer deadlines are based on TimeStamp::Now() and TimerThread::DoAfterSleep only re-scans the pending timers: on Windows, timers that became due during sleep fire on wake, on macOS and Linux every pending timer is pushed out by the sleep duration. ScheduledTask and now the update badge in bug 2089250 work around this with Date.now(), and every other long-running timer has the same problem. The same applies to TimeStamp-based expirations like DNS TTLs, the CORS preflight cache or HTTP cache validity, which live longer by the sleep duration on macOS and Linux.
History: bug 1178890 briefly shifted all pending timers by the time slept on wake, which bug 1197152 removed again because timers created between resume and the wake notification got shifted too. The behaviour chosen there (bug 1197152 comment 47) is that delays are absolute and overdue timers fire on resume, but that only holds where TimeStamp counts sleep, i.e. on Windows. The timer storm on wake that motivated the original adjustment in bug 197863 was most likely the old TYPE_REPEATING_PRECISE catching up on missed ticks; that type is now an alias of TYPE_REPEATING_PRECISE_CAN_SKIP (bug 1190735).
Proposal: make TimeStamp count sleep on all platforms, as it already does on Windows, and use AwakeTimeStamp where awake time is what we want.
- Make
NowLoRes()an alias ofNow()everywhere. It already is on Windows and macOS; only Linux/Android still useCLOCK_MONOTONIC_COARSE. None of the ~115 callers needs the coarse resolution as such: they are TTLs, timeouts, rate limits and millisecond-level telemetry, and the hottest one (the socket transport poll loop) is dominated by thepoll()syscall. RemovingNowLoRes()can then be a mechanical follow-up. - Move the callers that want awake time to
AwakeTimeStamp. Known candidates are the event page idle timeout (bug 1834683) and the AsyncShutdown countdown (already done in bug 2074047). Given that Windows has been counting sleep for years, I expect this list to stay short. - Switch macOS to
mach_continuous_timeand Linux/Android toCLOCK_BOOTTIME, Nightly first. The minimum OS versions that were a concern in 2015 are long below what we support. - Remove the
Date.now()workarounds inScheduledTaskand the update listener.
Open questions:
- Cost of
CLOCK_BOOTTIMEon Linux and Android: there is no coarse variant, and we need to check that it is served by the vDSO on all kernels and architectures we support. This needs a microbenchmark before anything else. pthread_cond_timedwaitwithCLOCK_MONOTONICdoes not expire during suspend, so timely timer firing after wake still relies onwake_notificationarriving, which we need to verify on Linux and Android.- Rust's
std::time::Instantuses the same platform clocks asTimeStamptoday and would diverge after a sleep on macOS and Linux. We need to check whether anything compares the two. performance.now()and rAF timestamps share a timeline per spec, while CSS animations should not tick during suspend (bug 1709767 comment 16). Bug 1709767 needs its own decision on that, independent of the timer fix here.
| Assignee | ||
Comment 23•8 days ago
|
||
The page checks whether Firefox's clocks and timers count time spent in system sleep (suspend).
- Open the page as a local file in the Firefox build you want to test, ideally with a fresh profile:
firefox -no-remote -profile "$(mktemp -d)" file:///path/to/bug1204823-sleep-test.html - Keep the tab in the foreground and click "Arm". This starts a timer that is due 3 minutes later, a 1 s interval and a long animation.
- Within 3 minutes, suspend the machine (systemctl suspend, closing the lid, or on Linux
sudo rtcwake -m mem -s 600, which wakes it by itself after 10 minutes). Don't start the sleep with less than 15 s left on the timer. - Stay asleep for at least 6 minutes; 10 is better.
- Wake and unlock the machine, bring the tab to the front, click "Check", then press any key (for the keydown check).
- Click "Copy results" and save the JSON.
Rows in the results:
- Timer due during sleep: should fire within 5 s of wake. Old behaviour: it fires after the awake time that was left.
- performance.now() vs Date.now(): should agree to within 2 s. Old behaviour: performance.now() is behind by the sleep time.
- Interval across sleep: the performance.now() gap should match the wall-clock gap. Old behaviour: about 1 s.
- Animation across sleep: should be finished. Old behaviour: still running.
- Click / keydown event timeStamp and requestAnimationFrame: should pass on both old and new builds. A failure with an offset about the size of the sleep means old and new clocks are being mixed.
INCONCLUSIVE means the run didn't set up the case (timer fired before the sleep, sleep too short, or too little time left on the timer). Repeat the run.
If an unpatched build passes the first four rows, the machine probably didn't really suspend. On Linux, check with: journalctl -b | grep -i suspend
| Assignee | ||
Comment 24•8 days ago
|
||
AwakeTimeStamp gains FromMicroseconds, FromTruncatedMilliseconds and
ToTimeStamp, for timestamps that the system reports on its awake clock.
ToTimeStamp keeps the difference between the two clocks until a measurement
proves that it changed, so that converted timestamps keep their order.
| Assignee | ||
Comment 25•8 days ago
|
||
NSEvent timestamps and CVDisplayLink host times are on the awake clock, which
TimeStamp also uses today. Convert them through AwakeTimeStamp instead of
TimeStamp::FromSystemTime, so that they stay correct once TimeStamp counts
system sleep. No behaviour change.
| Assignee | ||
Comment 26•8 days ago
|
||
Android event times and Choreographer frame times are on CLOCK_MONOTONIC,
which TimeStamp also uses today. Convert them through AwakeTimeStamp instead of
TimeStamp::FromSystemTime, so that they stay correct once TimeStamp counts
system sleep. No behaviour change.
| Assignee | ||
Comment 27•8 days ago
|
||
Wayland compositors report event and frame callback times in milliseconds of
CLOCK_MONOTONIC, truncated to 32 bits, which TimeStamp also uses today. Convert
them through AwakeTimeStamp instead of TimeStamp::FromSystemTime, so that they
stay correct once TimeStamp counts system sleep.
No behaviour change, except that truncated times are now unwrapped correctly
after 49.7 days of uptime or when slightly ahead of now.
| Assignee | ||
Comment 28•8 days ago
|
||
It assumes that system timestamps are on the same clock as TimeStamp, which
stops being true once TimeStamp counts system sleep. It has no users left.
| Assignee | ||
Comment 29•8 days ago
|
||
Once TimeStamp counts sleep, the timer thread, which waits on a clock that
does not, needs the wake notification to recompute its wait. Only sleep
notifications stay ignored on macOS, as they may come without a matching
wake notification. The pref is renamed to match its narrower meaning.
| Assignee | ||
Comment 30•8 days ago
|
||
The X server time does not advance during sleep. Once TimeStamp counts
sleep, the converter would place events after a wake in the past by the
sleep duration until its skew correction catches up. Converting on a
timeline that follows the awake clock avoids that, and the result is moved
onto the TimeStamp timeline.
| Assignee | ||
Comment 31•8 days ago
|
||
The hidden window check runs on a GLib timer, which does not advance during
sleep. Once TimeStamp counts sleep, the window would be reported as occluded
once after a wake and the vsync rate estimate would be disturbed. Frame
callback times are on the awake clock too.
| Assignee | ||
Comment 32•8 days ago
|
||
The following patches forward wake_notification to child processes. The
network change and captive portal recheck it triggers in nsIOService are
parent-only work, and the parent already forwards the link change to content
processes.
| Assignee | ||
Comment 33•8 days ago
|
||
Once TimeStamp counts sleep, timers that became due during sleep only fire
promptly after wake when the timer thread gets wake_notification, which so
far only the parent process receives.
Content processes now also skip input event telemetry for events from before
the wake, as the parent already does.
| Assignee | ||
Comment 34•8 days ago
|
||
Its timer thread waits on a clock that does not advance during sleep, so like
content processes it needs the wake notification to fire timers that became
due while the system was asleep.
| Assignee | ||
Comment 35•8 days ago
|
||
Its timer thread waits on a clock that does not advance during sleep, so like
content processes it needs the wake notification to fire timers that became
due while the system was asleep.
| Assignee | ||
Comment 36•8 days ago
|
||
Their timer threads wait on a clock that does not advance during sleep, so
like content processes they need the wake notification to fire timers that
became due while the system was asleep.
| Assignee | ||
Comment 37•8 days ago
|
||
Android has no system wake notification, and the device usually sleeps while
Firefox is in the background. So the timer thread handles
application-foreground like wake_notification, and the parent forwards it to
the GPU, RDD and utility processes as a wake. Content processes already get
application-foreground.
| Assignee | ||
Comment 38•8 days ago
|
||
TimeStamp already counts sleep on Windows (QueryPerformanceCounter). Switch
macOS to mach_continuous_time and Linux/Android to CLOCK_BOOTTIME so that
deadlines and expirations behave the same everywhere. NowLoRes() becomes the
same as Now().
The raw clock getters used to align with external tools keep returning values
on the awake timelines.
Telemetry session lengths now include sleep on macOS and Linux, as they
already do on Windows.
| Assignee | ||
Comment 39•8 days ago
|
||
MOZ_TIMESTAMP_SIMULATED_SLEEP_S=<seconds> makes TimeStamp behave as if the
system had slept that long since boot, so code that still mixes it with the
awake clock is off by that amount.
On Android the environment is set up by Java after mozglue has started, so the
variable is read again once that is done and the startup timestamps taken
before are shifted. The gtest and junit harnesses forward it to the device.
| Assignee | ||
Comment 40•8 days ago
|
||
Timers that became due during sleep now fire on wake on all platforms, so the
scheduler tick no longer needs the wake notification to run.
Updated•8 days ago
|
Comment 41•6 days ago
|
||
Comment on attachment 9676247 [details]
WIP: Bug 1204823 Part 6 - Handle wake notifications in TimerThread on macOS. r?#xpcom-reviewers
Revision D340442 was moved to bug 2090731. Setting attachment 9676247 [details] to obsolete.
Comment 42•6 days ago
|
||
Comment on attachment 9676250 [details]
WIP: Bug 1204823 Part 9 - Only handle wake_notification in the parent's nsIOService. r?#necko-reviewers
Revision D340445 was moved to bug 2090731. Setting attachment 9676250 [details] to obsolete.
Comment 43•6 days ago
|
||
Comment on attachment 9676251 [details]
WIP: Bug 1204823 Part 10 - Forward wake_notification to content processes. r?#dom-reviewers
Revision D340446 was moved to bug 2090731. Setting attachment 9676251 [details] to obsolete.
Comment 44•6 days ago
|
||
Comment on attachment 9676252 [details]
WIP: Bug 1204823 Part 11 - Forward wake_notification to the GPU process. r?#gfx-reviewers
Revision D340447 was moved to bug 2090731. Setting attachment 9676252 [details] to obsolete.
Comment 45•6 days ago
|
||
Comment on attachment 9676253 [details]
WIP: Bug 1204823 Part 12 - Forward wake_notification to the RDD process. r?#media-playback-reviewers
Revision D340448 was moved to bug 2090731. Setting attachment 9676253 [details] to obsolete.
Comment 46•6 days ago
|
||
Comment on attachment 9676254 [details]
WIP: Bug 1204823 Part 13 - Forward wake_notification to utility processes. r?#ipc-reviewers
Revision D340449 was moved to bug 2090731. Setting attachment 9676254 [details] to obsolete.
Comment 47•6 days ago
|
||
Comment on attachment 9676255 [details]
WIP: Bug 1204823 Part 14 - Treat returning to the foreground as a wake on Android. r?#xpcom-reviewers,#geckoview-reviewers
Revision D340450 was moved to bug 2090731. Setting attachment 9676255 [details] to obsolete.
Updated•6 days ago
|
Updated•6 days ago
|
Updated•6 days ago
|
Updated•6 days ago
|
Updated•6 days ago
|
Updated•6 days ago
|
Updated•6 days ago
|
Updated•6 days ago
|
Updated•6 days ago
|
Updated•5 days ago
|
Updated•5 days ago
|
Updated•5 days ago
|
Updated•5 days ago
|
Updated•5 days ago
|
Updated•5 days ago
|
Updated•5 days ago
|
Updated•5 days ago
|
Updated•5 days ago
|
Updated•5 days ago
|
| Assignee | ||
Comment 48•2 days ago
|
||
The behavior of TimeStamp currently directly influences also the web observable behavior of DOM timeouts (and performance.now()). The current inconsistency across systems is the same for Firefox and Chrome, currently, see https://github.com/whatwg/html/issues/6759.
As Windows is the outlier, we may want to advocate to do the same there as on other OS, that is ignore sleeping time and just resume as if no time had passed. This means that some consumers should just not use nsITimer when they really want wall clock alarms (and we may need a mechanism for those, too).
Description
•