[SERVER-70941] make SystemTickSource monotonic everywhere Created: 28/Oct/22  Updated: 29/Oct/23  Resolved: 31/Oct/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 6.2.0-rc0

Type: Bug Priority: Major - P3
Reporter: Billy Donahue Assignee: Billy Donahue
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Service Arch 2022-11-14
Participants:
Linked BF Score: 160

 Description   

ONLY on macOS it is not monotonic, and this has caused BFs and unpleasant surprises.

https://jira.mongodb.org/browse/BF-26765

https://jira.mongodb.org/browse/SERVER-63651

This isn't hard to fix, and it's very hard to work around it.
We can use std::chrono::steady_clock if implementations meet our needs,
or the macOS mach_absolute_time library https://developer.apple.com/library/archive/qa/qa1398/_index.html

We need the consistent ability to measure small durations to implement fine-grained observability of latencies in the server (PM-2398, INIT-6).



 Comments   
Comment by Githook User [ 31/Oct/22 ]

Author:

{'name': 'Billy Donahue', 'email': 'billy.donahue@mongodb.com', 'username': 'BillyDonahue'}

Message: SERVER-70941 system TickSource via std::chrono::steady_clock
Branch: master
https://github.com/mongodb/mongo/commit/3fffc18136febd871ef8be886c6a4d961a98ec43

Comment by Billy Donahue [ 29/Oct/22 ]

stdlib implementations:
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/src/c%2B%2B11/chrono.cc
https://github.com/llvm/llvm-project/blob/main/libcxx/src/chrono.cpp

std::chrono::steady_clock from libc++ on macOS:

https://github.com/llvm/llvm-project/blob/eb536613a28b3070081900957553beb783448053/libcxx/src/chrono.cpp#L223-L233
That includes an interesting discussion of why mach_absolute_time actually WOULDN'T be right.
The right answer is CLOCK_MONOTONIC_RAW, introduced relatively late, in macOS v10.12.
Fortunately we don't support macOS versions before that.
https://www.mongodb.com/docs/manual/installation/#supported-platforms

After reviewing these, we can be pretty confident that the std::chrono::steady_clock is going to do basically the same thing we would have done by hand, on Linux, macOS, and Win32. We should just use it. It's going to do a better job than we would, and be maintained for free.

macOS: libc++
clock_gettime w/CLOCK_MONOTONIC_RAW

Win32: libc++
QueryPerformanceCounter and QueryPerformanceCounter same as what we were doing.

Linux:
clock_gettime w/CLOCK_MONOTONIC
fallback to clock_gettime w/CLOCK_REALTIME
fallback futher to gettimeofday
fallback further to 0.

These all throw std::system_error if the clock call fails. This is better than the fasserts etc we do.

Generated at Thu Feb 08 06:17:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.