Definition
Tracing buffer loss is the loss of trace records, the records that a tracer writes for observed events, because the buffer that holds them is full when a new record arrives. A trace buffer is a fixed-size region of memory between producers, the code that writes records as events happen, and a consumer, which drains the records to storage or to another computer. When records arrive faster than they are drained for long enough, the buffer fills, and the tracer discards new records, overwrites old ones or makes producers wait.
The idea
A tracer records 1000 events per second, and the link to the computer that stores them carries only 800 records per second. A buffer of 100 records absorbs the difference for half a second; after that, 200 records per second have nowhere to go. The trace that arrives still looks perfect: every record is well formed and the timestamps increase. Nothing in the records themselves shows that one event in five is missing, so a reader who counts events or checks their order reaches a wrong conclusion without knowing it.
Loss also strikes the part of the run that matters most. Buffers fill during bursts, so the lost records are those from the busiest moments, often the moments under study. A full voicemail box is a fair picture: the owner hears only the messages it kept, and a caller who found it full left nothing. A phone that also counts missed calls tells its owner what is missing, and a tracer must do the same: report what it lost, where and how much. The picture stops at one point: a voicemail box never keeps half a message, but a trace can keep half a record.
How it works
A trace buffer is usually a ring buffer §14.15: a fixed array §14.5 of record slots used in a circle, with a write position that producers advance and a read position that the consumer advances. When the write position would overtake the read position, the buffer is full, and the tracer applies its overflow policy.
Drop newest, also called discard mode: keep the records already stored and discard each new one. The trace keeps the start of a burst and loses its end.
Overwrite oldest, also called overwrite or flight-recorder mode: write each new record over the oldest unread one. The trace keeps the most recent records, which suits a question such as what happened just before a crash, and loses earlier history.
Block the producer: make the producer wait until a slot is free. No record is lost, but the traced program is slowed exactly when it is busiest, so its timing is no longer the timing under study. Code that must not wait, such as an interrupt handler §6.12, the code the processor runs when a device signals it, cannot use this policy.
Three mechanisms let a reader see the loss.
A loss counter. The tracer counts every record it discards or overwrites. A good tracer writes the count into the trace at the place of the gap, as a special record stating that records were lost there, and also reports the total; the Linux profiler
perf, for example, inserts such lost-records entries.Sequence numbers. Every attempted record receives the next number from a counter, whether or not the record is stored. A reader who sees record 30 followed by record 32 knows that exactly one record, number 31, is missing at that point.
Framing. Each record carries its own length, a length prefix §4.30, and a commit mark or a checksum §4.32, a short value computed from the record’s bytes, that the producer writes last. A record cut short, because the producer was interrupted, the buffer wrapped over it or the system stopped in the middle, fails the check and is reported as partial instead of being read as data.
These indicators must themselves be reliable. With several producers, the counter and the sequence numbers need atomic operations §9.8, updates that other processors cannot interleave with. A counter of fixed width wraps around after its largest value. A trace cut off by a crash can end before the final count is written. Fix an acceptance rule before the analysis: for example, a trace supports a conclusion about counts or order only if its loss counters are zero, its sequence numbers have no gaps and every record passes its framing check.
Worked example
Rates. Let the capacity be records, with records arriving at a steady 1000 per second and drained at a steady 800 per second. The backlog grows by records per second, so an empty buffer fills in second. Check: in 0.5 second, records arrive and drain, which leaves . From then on, 200 records per second are lost. Over a 2-second burst, 2000 records arrive, drain and 100 remain in the buffer, so are lost, which is , or 15%. Real arrivals come in bursts, so the buffer can fill sooner than this steady-rate calculation says.
Sequence numbers. A buffer with capacity holds 6 records, numbered 21 to 26. Then, in this order, records 27 and 28 arrive (8 stored, full), the consumer drains 21 and 22 (6 stored), records 29 and 30 arrive (8 stored, full), and record 31 arrives. Under drop newest, record 31 is discarded and the loss counter becomes 1; the buffer holds 23 to 30. When the consumer later reads record 30 and then the next stored record, 32, the jump from 30 to 32 shows one missing record, number 31, at exactly that place. Under overwrite oldest, record 31 replaces record 23 instead, the buffer holds 24 to 31, and the reader sees a jump from 22 to 24.
Order matters. Totals alone do not fix the loss. Start again with 6 records stored, and let the same 5 records arrive and the same 2 be drained, but with all 5 arrivals before both drains. The buffer is full after 2 arrivals, because , and the other 3 arrivals are discarded. The loss is 3, not 1, and the final occupancy is . Only a counter or sequence numbers tell the reader which of the two histories happened.
Stated precisely
Let be the number of records in the buffer at the start of an interval, the number of records that producers attempted to write during it, the number drained, the number discarded or overwritten, and the number in the buffer at the end. Every attempted record, and every record present at the start, is at the end either still in the buffer, drained or lost, so In the first order above, ; in the second, . Under overwrite oldest, the overwritten record counts in and the new record counts in .
Let every attempted record take the next number from a counter of bits, which wraps from to 0. If two records read one after the other carry numbers and , then records were lost between them, where is the remainder of after dividing by . This holds provided fewer than records were attempted from to ; a loss of records or more between two reads is invisible to the check.
A trace is complete for one type of event over an interval when all of the following hold: every occurrence of the event executes a probe; at every producer, shown by counters whose own updates cannot be lost; there are no sequence gaps and no framing errors; and the trace covers the whole interval, with no cut at its end. The identity above accounts for records only. It says nothing about whether a stored record is whole; that needs the framing check.
Limits and common mistakes
A zero loss counter covers only records that reached the tracer. An event on a path without a probe produces neither a record nor a loss count; that is a question of instrumentation coverage §14.35.
A larger buffer delays loss but does not prevent it. When records arrive at an average rate above the drain rate , an empty buffer fills after seconds, which grows only in proportion to the capacity ; any finite buffer fills eventually.
Loss is not random. It concentrates in bursts, so the surviving records under-represent the busiest periods. In statistical terms the lost records are missing not at random §11.29, and averages computed from the surviving records are biased.
Blocking producers avoids loss but changes the timing of the program under study, and can hide the very behavior being traced. This is a form of instrumentation perturbation §18.1.7, the change that observing a program causes in the program’s behavior.
Many tracers keep one buffer per processor core. One buffer can lose records while another loses none, and a trace merged from them by timestamp gives an order that looks complete but is not.
Overwrite mode silently discards history. A trace taken in that mode answers questions about its final window only, and the report must say so.
A trace in which every record parses can still be incomplete. A valid format is not evidence of completeness.
Questions answered
How are overflow, dropped-record counts, and partial records exposed?
They are exposed by information that the tracer adds to the trace for this purpose; the surviving records cannot reveal them by themselves.
Overflow is exposed by stating the overflow policy (drop newest, overwrite oldest, or block the producer) in the trace, and by a marker record written at each place where the buffer was full, so the reader knows when overflow happened.
Dropped-record counts are exposed by a counter of discarded or overwritten records, reported in each marker and as a total, and independently by sequence numbers: every attempted record takes the next number, so a jump from to between two stored records means that records were lost at that point.
Partial records are exposed by framing: each record carries its length and a commit mark or checksum written last, and the reader rejects any record whose length, mark or checksum does not match.
A rule fixed before the analysis then decides which traces are accepted: for example, a trace supports conclusions about counts or order only with zero loss, no sequence gaps and no framing errors. If producers block instead of dropping, the report must say so, because blocking changes the timing of the traced program. The absence of a record is never, by itself, evidence that the event did not happen.
Related entries: Ring buffer (§14.15), Runtime instrumentation (§14.24), Missing data and attrition (§11.29), Instrumentation coverage (§14.35), Backpressure (§9.30).