Expand description
§Many Cores, One Order
A trace of one vCPU is a sequence. A trace of several is several
sequences, and a simulator of a multicore memory system wants to know how
they interleave. This chapter explains the two orderings kvmtrace
offers, what each one guarantees, and how the guarantee was tested.
§Two orders
Every record carries a Sequence:
| Field | Meaning | Always exact? |
|---|---|---|
local | position in this vCPU’s own stream | yes |
global | position in one session-wide order, shared by all vCPUs | depends on the mode |
host_tsc | the host’s TSC when the record was made | a timestamp, not an order |
The per-vCPU order is exact in every mode: one vCPU executes one
instruction per step, in program order. The global order is what
TraceOptions::ordering chooses.
§Ordering::Serialized: an exact total order
In serialized mode the session has a token, and a vCPU must hold it for the whole of a step: from entering the guest, through the one instruction, to the moment its record has a global sequence number. Because only one vCPU executes guest instructions at a time, the global order is the order in which instructions executed, and memory order follows from it.
The token is a fair, first-come-first-served ticket lock
(Session::token), so runnable vCPUs take turns
one instruction at a time. (The first version used a plain Mutex; it
was correct, but one vCPU often re-took it thousands of times in a row,
which gives an order that is legal but nothing like a real machine’s.)
There is one exception. A vCPU that is halted (hlt, mwait, or
waiting for INIT/SIPI) would block inside the kernel while holding the
token, and the vCPU that would wake it could never run. So a halted vCPU
enters the guest without the token; when it wakes, its first record is
flagged UNSERIALIZED, meaning its
place in the global order is approximate. Those are wake-up instructions
in idle loops, rarely what an experiment is about, and they are flagged
so you can tell.
§Ordering::Concurrent: per-vCPU exact, global approximate
In concurrent mode the vCPUs step in parallel, and global comes from an
atomic counter incremented when each record is made. That is close to
real time but not memory order: two vCPUs can execute in one order and
record in the other. Use it when you want speed and only per-core
streams, or as a timestamped approximation.
§How we know the serialized order is right
A claim like “global order equals memory order” needs a test whose answer
the tracer cannot fake. lock xadd provides one. Two threads, pinned to
different vCPUs and released together, each do
lock xadd [counter], rax ; rax = 1 before; old value after
mov [log + k*8], rax ; remember what we got4096 times. Each xadd returns the counter’s previous value, so the
values the threads log are the order in which their xadds reached
memory, as decided by the hardware cache-coherence protocol, not by the
tracer. The test then takes the traced xadds, sorts them by global,
and checks that the k-th one returned exactly k. Which log slot (thread
and iteration) each traced xadd belongs to is read off the store that
follows it, also from the trace.
On a Linux guest under QEMU with two vCPUs:
traced xadds bound to their log slot: 8192 of 8192
interleaving: thread 0 has 437 of the first 4096 xadds in global order
RESULT: global order == memory order for every xaddThe “interleaving” line matters as much as the result: if the threads had
not actually raced, a correct answer would prove little. The same check
in tests/ordering.rs
runs on MiniVm in every cargo test.
§Merging
kvmtrace merge DIR prints every vCPU’s records interleaved by global.
From Rust, open one KvtReader per vCPU and do a
k-way merge on seq().global. Every reader yields records in increasing
global order, so a heap of the readers’ next items is all you need.