Skip to content

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 to clock_gettime(CLOCK_MONOTONIC, &ts), then tv_sec times a billion plus tv_nsec.
  • Why does strace show no system call for a million calls to now() — what is the vDSO, and when does glibc's clock_gettime fall 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 / shift calibration — and why is CLOCK_MONOTONIC's multiplier steered by NTP while CLOCK_MONOTONIC_RAW's is fixed at boot?
  • Which OS clock does each library read? libstdc++ on Linux reads CLOCK_MONOTONIC, libc++ on Apple platforms CLOCK_MONOTONIC_RAW, and MSVC's library QueryPerformanceCounterThree 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 — strace on 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 ↗).

See also