Skip to content

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.

byte 0byte 1byte 2byte 304812162024283236404448525660646872768084889296100104Magic Numberu64 · 8BSegment Sizeu32 · 4BMin Versionu8 · 1BMax Versionu8 · 1BGenerationu16 · 2BAs-Of TSCu64 · 8BAs-Of Timestampi64, i64 · 16BVoid-After Timestampi64, i64 · 16BPeriodu64 · 8BPeriod Erroru64 · 8BBoundi64 · 8BDisruption Markeru64 · 8BMax Driftu32 · 4BClock Statusi32 · 4BDisruptionSupportu8 · 1BPeriod Shiftu8 · 1BPeriodError Shiftu8 · 1BPadding5B
The 104-byte v3 segment, four bytes per row, read top to bottom. Colors group related fields.
FieldTypeDescription
Magic Numberu64Signature identifying a ClockBound shared memory segment.
Segment Sizeu32Size of the shared memory segment, in bytes.
Min Versionu8Oldest layout version backward-compatible with this one.
Max Versionu8Most recent layout version supported.
Generationu16Incremented around updates: odd before an update, even after. On rollover it wraps to 2, not 0.
As-Of TSCu64The TSC counter value used to create the as-of timestamp.
As-Of Timestampi64, i64The calculated timestamp (tv_sec, tv_nsec) at the TSC value.
Void-After Timestampi64, i64Time after which the bound should not be trusted (tv_sec, tv_nsec).
Periodu64Fractional part of the estimated TSC period.
Period Erroru64Fractional part of the relative error of the period estimate.
Boundi64Absolute upper bound on accuracy vs. reference time at the as-of timestamp (ns).
Disruption Markeru64Last disruption marker value read from the VMClock.
Max Driftu32Maximum clock drift rate between updates, in parts per billion (ppb).
Clock Statusi320 Unknown, 1 Synchronized, 2 FreeRunning, 3 Disrupted.
Clock Disruption Supportu81 if clock-disruption support is enabled, else 0.
Period Shiftu8Scale applied to Period.
Period Error Shiftu8Scale 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        nanoseconds

the 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 + bound

Both 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.

ReleaseProtocolDefault shm path
1.xshm (v1)/var/run/clockbound/shm
2.xshm (v2)/var/run/clockbound/shm0
3.xshm (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.