Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Clocks

Which clock to read for timestamps and durations, and time sync on the node.

Which clock for what

needreadwhy
a timestamp written anywhere: the wire, a log, storageCLOCK_REALTIME: std::time::SystemTime::now()UTC. Every thread and process on the host reads the same kernel clock, through the vDSO when the clock source is the counter (T12)
a duration or timer in one processquanta: Clock::now(), or Clock::raw() then Clock::delta_as_nanos()reads the CPU counter, scaled by one calibration per process against CLOCK_MONOTONIC; without a usable counter it reads CLOCK_MONOTONIC (quanta-0.13.0 src/lib.rs:333-344). Do not compare quanta values between processes: each calibrates on its own
a duration off the hot pathstd::time::InstantCLOCK_MONOTONIC: never goes back
flowchart TD
    Q["What are you measuring?"]
    Q -->|"a stamp on the wire, a log or a row"| R["CLOCK_REALTIME"]
    Q -->|"a duration or a timer in this process"| C["quanta, the CPU counter"]
    Q -->|"a duration off the hot path"| I["Instant, CLOCK_MONOTONIC"]

Not minstant for portable code: it reads the counter only on Linux x86 and x86_64. On every other target, Graviton and macOS included, it reads SystemTime, a wall clock that can go back (minstant-0.1.7 src/lib.rs:71-82).

Rules

  • Do not subtract two CLOCK_REALTIME stamps for a duration: chrony or an admin can step the clock back.
  • Build the quanta Clock before the loop. Its first calibration busy-waits (quanta’s limit: 200 ms, src/lib.rs:198), and quanta::Instant::now() calibrates a global clock on first use.
  • Do not stamp with a *_COARSE clock: it moves once a kernel tick (1–10 ms, by CONFIG_HZ).
  • A one-way time between two hosts is only as good as both hosts’ time sync. Use the same time source on every host that compares stamps.

Aeron’s clocks

Aeron keeps its own clocks (aeron_clock.c:91-136 in the Aeron tree shipped with rusteron-client 0.3.6). The driver’s agents read a cached copy (aeron_clock_cache_t).

Aeron APILinux sourcereturns
aeron_nano_clock()CLOCK_MONOTONICns since boot
aeron_epoch_clock()CLOCK_REALTIME_COARSEms since the Unix epoch, one kernel tick resolution
aeron_clock_cache_tboth, cachedas of the last refresh

Aeron::nano_clock() and Aeron::epoch_clock() read Aeron’s time; rusteron has no setter for them.

Set up time sync

Set it on the node: a pod shares the node’s CLOCK_REALTIME.

  • AWS: chrony on the Amazon Time Sync Service. Not systemd-timesyncd: it keeps no error bound.

    server 169.254.169.123 prefer iburst minpoll 4 maxpoll 4
    makestep 0.0001 3
    

    For PTP-grade time: an instance type with a PTP hardware clock (EC2 PhcSupport), the ENA PTP clock enabled (in-tree ENA from Linux 6.17: devlink enable_phc; Amazon’s driver: phc_enable=1), then:

    refclock PHC /dev/ptp_ena poll 0 delay 0.000010 prefer
    server 169.254.169.123 iburst minpoll 4 maxpoll 4
    makestep 0.0001 3
    

    The service smears leap seconds: remove any leapseclist or leapsectz line.

  • A host with another PHC (a NIC or hypervisor clock): chrony on it (refclock PHC /dev/<device>; for a NIC’s clock also linuxptp’s ptp4l and phc2sys).

  • x86 and Graviton are set up the same way.

Check it with ergon-tune: T11 (how CLOCK_REALTIME is kept: PTP-grade, NTP or unsynchronised, and its error bound) and T12 (the kernel’s clock source) before the loop, then ClockWatch::check from housekeeping. On the node: chronyc tracking.