Protocol
The ClockBound shared-memory protocol (version 3). Use it to build a custom client in any language that can mmap a file.
Overview
Protocol version 3 corresponds to ClockBound daemon and client releases 3.0.0 and greater. Communication between daemon and client happens entirely through shared memory, mapped by default to a file at /var/run/clockbound/shm1.
Byte ordering is the native endianness of the CPU — for example, x86_64 and ARM-based Graviton CPUs are little-endian.
Segment layout
The version-3 segment is 104 bytes, laid out as shown below.
| Field | Type | Description |
|---|---|---|
| Magic Number | u64 | Signature identifying a ClockBound shared memory segment. |
| Segment Size | u32 | Size of the shared memory segment, in bytes. |
| Min Version | u8 | Oldest layout version backward-compatible with this one. |
| Max Version | u8 | Most recent layout version supported. |
| Generation | u16 | Incremented around updates: odd before an update, even after. On rollover it wraps to 2, not 0. |
| As-Of TSC | u64 | The TSC counter value used to create the as-of timestamp. |
| As-Of Timestamp | i64, i64 | The calculated timestamp (tv_sec, tv_nsec) at the TSC value. |
| Void-After Timestamp | i64, i64 | Time after which the bound should not be trusted (tv_sec, tv_nsec). |
| Period | u64 | Fractional part of the estimated TSC period. |
| Period Error | u64 | Fractional part of the relative error of the period estimate. |
| Bound | i64 | Absolute upper bound on accuracy vs. reference time at the as-of timestamp (ns). |
| Disruption Marker | u64 | Last disruption marker value read from the VMClock. |
| Max Drift | u32 | Maximum clock drift rate between updates, in parts per billion (ppb). |
| Clock Status | i32 | 0 Unknown, 1 Synchronized, 2 FreeRunning, 3 Disrupted. |
| Clock Disruption Support | u8 | 1 if clock-disruption support is enabled, else 0. |
| Period Shift | u8 | Scale applied to Period. |
| Period Error Shift | u8 | Scale applied to Period Error. |
Reading consistently
The Generation field lets a reader detect a torn read. It is set to an odd number before the daemon updates the segment and an even number after. A client should read the generation, read the payload, then read the generation again: if it changed or is odd, retry.
Computing the clock error bound at read time
Reading the TSC (rdtsc on x86, cntvct_el0 on aarch64) gives now_tsc. With the two scaled fractions from the segment:
period = Period / 2^(64 + Period Shift) seconds per tick
period_err = Period Error / 2^(64 + Period Error Shift) dimensionless
elapsed = (now_tsc - As-Of TSC) * period * 1e9 nanosecondsthe current time and its bound, both in nanoseconds, are:
now = As-Of Timestamp + elapsed
bound = Bound + elapsed * (Max Drift * 1e-9 + period_err)
earliest = now - bound
latest = now + boundBoth growth terms are rates applied to the elapsed time: the oscillator's worst-case drift (Max Drift, in ppb) and the relative error of the period estimate. The result is only meaningful before the Void-After Timestamp and while the Clock Status permits.
Older protocol versions
Protocol versions 1 and 2 (and a pre-1.0 Unix datagram socket protocol) are documented in the repository's protocol reference. Version 3 is the current layout and the one new clients should target.
Version compatibility
ClockBound's shared-memory layout has evolved across major versions. The daemon and client must agree on the protocol; new clients should target version 3.
| Release | Protocol | Default shm path |
|---|---|---|
| 1.x | shm (v1) | /var/run/clockbound/shm |
| 2.x | shm (v2) | /var/run/clockbound/shm0 |
| 3.x | shm (v3) | /var/run/clockbound/shm1 |
As of the 3.0 line, support for ClockBound 2.x clients is deprecated and will be removed in a future release. Plan migrations to the 3.x clients.