Launching TestFlows Machine: self-service deterministic simulation testing environment Read the post

Clock faults

Software reads two kinds of time. Elapsed time, CLOCK_MONOTONIC and the TSC, is the machine’s own, and Machine time controls it. The date, CLOCK_REALTIME, is what date, time() and gettimeofday return, and on a real fleet it jumps, drifts and disagrees between hosts. machine clock changes the date the machine reads, for every process in it or for one container, and leaves elapsed time alone.

Command What the date does
jump +5s steps forward or back, as an NTP step or date -s does
set 2027-03-28T00:59:50Z goes to that date
drift +200ppm --container db gains 200 parts per million of machine time, 0.72 seconds an hour
jump +4s --slew 30s --container db moves 4 seconds over 30 seconds of machine time, with no step
jitter -200ms:200ms --every 10m is set to a drawn offset every 10 minutes of machine time
leap insert repeats a second, 23:59:60, at the next UTC midnight
reset returns to machine time, for the whole machine and every container

The machine’s kernel makes each change, so a change takes effect when the run next executes. The run records every change: a replay makes it at the same instruction, a checkpoint keeps the date, and a branch starts with its parent’s date.

The machine’s date

jump, set and reset change the date every process in the machine reads, in every container. The step is a real step of the kernel’s clock: a timer armed for an absolute date sees it, and a timerfd with TFD_TIMER_CANCEL_ON_SET is woken, which is how systemd and cron notice a step.

1
2
machine clock app jump +59m50s
machine run app --within 30s --until 'console~token refresh'

The answer shows the date as it stands and the change waiting for the run:

1
2
3
date:    2026-03-01T00:01:29.614903749Z
offset: +0s from machine time
waiting: 1 change, taken when the run executes

Steps add up: jump +5s and then jump -2s leave the date 3 seconds ahead. set puts the date at a value, whatever steps came before it. Machine refuses a date before 1970, or one on or after 2232-04-18T23:47:16Z, because the kernel cannot set either.

A container’s date

With --container, a change moves one container’s date alone, as an offset from the machine’s date. Every process of the container reads it, including those docker exec and machine exec --container start. The other containers keep the machine’s date.

1
2
machine clock app jump -6m --container client
machine exec app run --container client -- curl -sS https://api:8443/health

The client now reads a date 6 minutes behind the server, so a certificate the server issued a minute ago is not valid yet for the client.

The offset reaches every way a program reads the date. clock_gettime stays as fast as it is outside the machine, because the container’s processes still read the date in the vDSO, without a system call. System calls, POSIX timers, timerfd and clock_nanosleep agree with the container’s date, and so do the futex waits behind pthread_cond_timedwait and sem_timedwait and message queue timeouts. A restarted container keeps its offset, while a container created again starts on the machine’s date.

--container takes a container’s id, its name or its Compose service. Machine finds the container without running the machine, so a clock change made at a breakpoint leaves the run at that breakpoint. Prefer the id: Machine knows it as soon as Docker creates the container, and learns the name shortly after the container starts. machine containers app lists both.

A stopped container that has an offset keeps it, and takes it again when it starts. machine clock app reset --container db returns it to the machine’s date, stopped or running. A container Docker removes, with docker rm or a --fresh restart, loses its offset, and the container that replaces it starts on the machine’s date. At most 8 containers have dates of their own at once; the next is refused until one is reset or removed.

Drift and slew

A clock that runs fast or slow is a fault a jump cannot reproduce: the nodes’ dates move apart gradually rather than in one step, until an assumption about skew breaks. drift makes one container’s date gain or lose time at a rate of machine time:

1
2
3
machine clock app drift +5000ppm --container node2      # 0.5% fast: 3s in 10 minutes
machine run app --within 10m --until 'console~two leaders'
machine clock app drift 0 --container node2 # stopped; the offset reached stays

No read sees a jump, and a timer armed in the container agrees with its drifting date: it fires when the container’s own date reaches the deadline, never before. At +100000ppm, a timerfd armed for 2 seconds ahead by the container’s date fired after 1.821 seconds of machine time, which is 2 seconds of a clock running 10% fast.

--slew moves a container’s date to a new value over a time instead of in one step, as NTP slews a small correction:

1
2
machine clock app jump +4s --for 2m --container db               # off and back, two steps
machine clock app jump +4s --for 2m --slew 30s --container db # drifts off and back, 30s each

A slew runs until the date reaches its new value. A later change to the same container, a drift included, stops the slew at the offset it has reached, so set a drift before you start a slew. A slew moves the date at most half a second for each second of machine time, so jump +4s --slew 8s is the fastest slew of 4 seconds. Machine refuses a shorter one:

1
2
✗ A slew of +4s takes 8s at the least.
Slew it over 8s machine clock app jump +4s --slew 8s --container db

drift applies to a container alone, never to the machine’s date. To make all of the machine’s time run fast or slow, use the clock rate.

Jitter

An NTP-disciplined fleet is never exactly in sync: each host’s offset varies within a bound. jitter sets the date to a value drawn around it each period:

1
2
3
machine clock app jitter -200ms:200ms --every 10m --container api
machine clock app jitter -50ms:50ms --every 1m --container worker
machine run app --within 2h

Every 10 minutes of machine time, api’s offset is set to a value from -200ms to 200ms around the date without the jitter. The first step is taken when the run next executes. A weight in brackets draws a choice more often: -1s[1],0[8],1s[1] is in sync most of the time and a second off now and then. A jitter takes 4 choices at most, each a whole number of microseconds.

The draws come from --seed, or from a seed drawn at random and shown. Give that seed with --seed to step the date the same way again. A replay steps by the same amounts at the same points, and a branch draws on from where its parent was. A jump or a set moves the date under the jitter, which goes on around the new date. machine clock app jitter off stops the jitter and returns the date to where it would be without it.

Leap seconds

In 2012 a leap second livelocked Linux futexes, and services across the industry went down with it. leap insert makes the kernel repeat a second, 23:59:60, at the next UTC midnight of the machine’s date, and leap delete makes it skip 23:59:59:

1
2
3
machine clock app set 2026-12-31T23:59:50Z
machine clock app leap insert
machine run app --within 20s

After the 20 seconds the date is a second behind where it would have been without the leap. With leap delete it is a second ahead. A leap second moves the machine’s date and every container with it, so leap takes no --container. leap off cancels a leap still pending, and so does reset.

Undoing a change after a time

--for 2m takes a change back after 2 minutes of machine time, at the same instruction in every replay. jump +4s --for 2m is a step forward and, 2 minutes later, a step back: the NTP correction that follows a host running with a bad clock. jump, set, drift and jitter take --for.

The undo takes back the command’s own change. What else moved the date meanwhile stays: a drift’s accrual, a leap second, or another program’s step. A later change cancels an undo still pending.

Reading the date

machine clock app reads the date without running the machine. It shows the machine’s date and its offset from machine time, then each container whose date differs from it, with its offset, drift, slew and jitter. To see the date a program reads, ask from inside its container, which runs the machine:

1
machine exec app run --container db -- date -u

The reading looks like this:

1
2
3
4
5
6
7
8
9
date:    2026-12-31T23:59:54.977514123Z
offset: +305d23h58m18.362410928s from machine time
jitter: -200ms:200ms every 10m, seed 42, at -0.022376s after 1 draw
leap: 23:59:60 inserted before 2027-01-01T00:00:00Z
container: compose-db-1
date: 2026-12-31T23:59:57.645176188Z
offset: +2.667662065s from the guest's date
drift: +200ppm
slew: to +6.00600183s at 122.130465170s of machine time

--container db shows one container, and machine -o json clock app gives every field.

A change is taken when the run next executes, so the command that asks for it cannot say whether the machine’s kernel accepts it. Read the clock after the run that takes it. The reading names each change of the last batch the kernel refused, as you typed it, and why:

1
2
3
4
machine clock app set 1970-01-01T00:00:01Z
machine clock app jitter -5ms:5ms --every 1s --seed 1
machine run app --within 1s
machine clock app
1
2
3
4
date:    2026-03-01T00:00:40.556767815Z
offset: +0.000356s from machine time
jitter: -5ms:5ms every 1s, seed 1, at +0.000356s after 1 draw
refused: clock set 1970-01-01T00:00:01Z: the date, the guest's or a container's, would leave the range the kernel takes: from 1970 plus the time since the guest booted, to 2232-04-18T23:47:16Z

The jitter applied and the set did not. A refused change changes nothing.

Clock fault use cases

Each block below is a question a clock fault answers, and the commands that ask it. Run the machine afterwards, with machine run app --within 2m or with an --until the program prints, and read what it did.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Does the program refresh a token before it expires?
machine clock app jump +59m50s
machine run app --within 30s --until 'console~token refresh'

# Does a client accept a certificate issued in its future?
machine clock app jump -6m --container client

# Do two nodes both lead when one clock runs fast?
machine clock app drift +5000ppm --container node2

# Does last-write-wins lose the write from the slow data center?
machine clock app jump +3s --container dc2

# Does the program survive the clock going backwards?
machine clock app jump -2s

# Does the system cope with the NTP correction that follows a bad clock?
machine clock app jump +4s --for 2m --slew 30s --container db

# Does a node shut itself down on skew, and come back?
# At +1000ppm the offset passes a 500ms tolerance after 500s of machine time.
machine clock app drift +1000ppm --container roach3
machine run app --within 10m
machine clock app reset --container roach3

# Does the program survive 23:59:60?
machine clock app set 2026-12-31T23:59:50Z
machine clock app leap insert

# Does a scheduled job run twice or not at all across a time zone change?
# Set TZ in the container first.
machine clock app set 2027-03-28T00:59:50Z

# Does the program handle 2038 and February 29?
machine clock app set 2038-01-19T03:13:30Z
machine clock app set 2028-02-28T23:59:55Z

# Do services that compare timestamps agree when clocks wobble?
machine clock app jitter -200ms:200ms --every 10m --container api

# Does a cache or session expire when its day is over?
machine clock app jump +1d --container redis

How much skew the system tolerates

Fork one branch for each skew at the same checkpoint, and run each branch to the same point. Every branch starts from the identical machine, so the skew is the only difference:

1
2
3
4
5
6
machine checkpoint app steady
for skew in 50ms 100ms 200ms 400ms 800ms; do
machine fork app --at ^steady --name skew-$skew
machine clock app/skew-$skew jump +$skew --container node2
machine run app/skew-$skew --within 2m --until 'console~two leaders'
done

A jump inside one request

Most time bugs are check-then-use: the process checks a token’s expiry, the clock steps, and it uses what it checked. Stop the run where the check is done, jump, and go on:

1
2
3
4
machine containers app                       # auth is 3f2a91c0d4e7
machine run app --until 'tid=412:at=check_expiry'
machine clock app jump +1h --container 3f2a91c0d4e7
machine run app --within 30s

The jump finds the container by its id without running the machine, so the run is still stopped at check_expiry when it takes the jump.

A step back reorders events

A database that stamps rows with the time they were written sorts them by that stamp. Step its container’s date back between two writes, and the second row sorts before the first:

1
2
3
4
5
machine exec app run --container db -- psql -U postgres -tAc "create table ev(id serial, at timestamptz default clock_timestamp())"
machine exec app run --container db -- psql -U postgres -tAc "insert into ev default values"
machine clock app jump -10m --container db
machine exec app run --container db -- psql -U postgres -tAc "insert into ev default values"
machine exec app run --container db -- psql -U postgres -tAc "select id, at from ev order by at"
1
2
2|2026-03-01 00:50:42.230825+00
1|2026-03-01 01:00:41.531503+00

Any code that takes the newest stamp as the latest event, such as last-write-wins or a cursor over at, now picks row 1.

Because every clock change is part of the run’s recording, a failure found under skew comes back when you replay the recording; there is no timing to reconstruct. Investigate it as any other failure, with rewind, diff and the debugger.

What a clock change misses

  • File timestamps. The kernel stamps files with its own clock, so make in a container whose date is behind the machine’s reports clock skew.
  • Socket timestamps. SO_TIMESTAMP and SIOCGSTAMP carry the machine’s date, so ping in a skewed container reports the offset as round trip time.
  • Timers already armed. A container’s change does not move a deadline armed before it; a timerfd with TFD_TIMER_CANCEL_ON_SET is woken so the program can re-arm.
  • A container that sets the date. A container given CAP_SYS_TIME that calls settimeofday sets the machine’s date, as it would set a host’s; its own offset stays.