What happens in now()¶
Level: 301 · deep dive
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.
One line: On Linux, steady_clock::now() is an ordinary function call into clock_gettime, and from there into the vDSO — kernel code mapped into your process — which reads the CPU's time-stamp counter and scales it, without a system call.
The questions this page has to answer¶
- What does
steady_clock::now()compile to? The talk shows GCC's version: a call toclock_gettime(CLOCK_MONOTONIC, &ts), thentv_sectimes a billion plustv_nsec. - Why does
straceshow no system call for a million calls tonow()— what is the vDSO, and when does glibc'sclock_gettimefall back to a real system call? - What does the vDSO code do: a sequence lock around a time-stamp-counter read and the kernel's
mult/shiftcalibration — and why isCLOCK_MONOTONIC's multiplier steered by NTP whileCLOCK_MONOTONIC_RAW's is fixed at boot? - Which OS clock does each library read? libstdc++ on Linux reads
CLOCK_MONOTONIC, libc++ on Apple platformsCLOCK_MONOTONIC_RAW, and MSVC's libraryQueryPerformanceCounter— Three clocks has the first measurements. - What changes inside a virtual machine whose clock source cannot use the time-stamp counter?
- How do you check all of this on your own machine —
straceon Linux, and what on macOS?
The central evidence — a system-call trace with nothing in it — exists only on Linux, so it will be a dated fence rather than an answer key.
In the talk¶
What is now()? — from the slide What happens in now()? to The whole process (slide source ↗).