Skip to content

Commit 47f005c

Browse files
committed
Add binary trace format documentation
1 parent 4789737 commit 47f005c

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ It's self contained, so one can debug a record of a version of a program from a
99
The record data consists of a stream of objects, each of which describes a certain program event: call, step, return etc.
1010

1111
A trace contains several files:
12-
* trace.json : the record data
13-
* trace_metadata.json : metadata for the recorded program
14-
* trace_paths.json : a list of the recorded files
15-
* `files/`: a folder including all the source/repository files copied into the trace.
12+
* `trace.json` – the event data in JSON format
13+
* `trace.bin` – the same event stream encoded in a binary format (see
14+
[docs/trace_binary_spec.md](docs/trace_binary_spec.md))
15+
* `trace_metadata.json` – metadata for the recorded program
16+
* `trace_paths.json` – a list of the recorded files
17+
* `files/` – a folder including all the source/repository files copied into the trace.
1618

17-
The record data format is currently json which matches the record event rust types from `src/types.rs`.
19+
The event stream can be stored either as JSON (`trace.json`) or in the
20+
binary format (`trace.bin`). Both representations correspond to the Rust
21+
types in `src/types.rs`.
1822

1923
We plan on
2024
* defining a more precise document or a list of examples or a specification.
21-
* producing a more optimized next version of this format, probably based on a binary format.
25+
* producing a more optimized version of the format. A binary variant is now
26+
available as `trace.bin`.
2227

2328
A future goal of this format is to make it possible to stream traces: to be able to replay them while they're still being recorded.
2429
This is one of the reasons for the decision to maintain a single "stream" of events currently.

docs/trace_binary_spec.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# CodeTracer Trace Binary Format
2+
3+
This document describes the binary format stored in `trace.bin` files produced by the `runtime_tracing` crate. The binary format contains the same information as the JSON trace but encoded using [Cap'n Proto](https://capnproto.org/) for efficiency.
4+
5+
## File Layout
6+
7+
A binary trace begins with an 8‑byte header:
8+
9+
```
10+
C0 DE 72 AC E2 00 00 00
11+
```
12+
13+
* The first five bytes (`C0 DE 72 AC E2`) identify the file as a CodeTracer trace.
14+
* The remaining three bytes are reserved for versioning. They are zero for the initial version. Non‑zero values indicate an incompatible future format.
15+
16+
After the header comes a Cap'n Proto message serialized with the packed encoding. The schema for this message is defined in [`runtime_tracing/src/trace.capnp`](../runtime_tracing/src/trace.capnp). The root object is `Trace`, which contains an array of `TraceLowLevelEvent` values.
17+
18+
The mapping between the Rust data structures and the Cap'n Proto schema is implemented in `capnptrace.rs`. Helper functions `write_trace` and `read_trace` write and read the binary format.
19+
20+
## Usage
21+
22+
To write a binary trace:
23+
24+
```rust
25+
use runtime_tracing::{Tracer, TraceEventsFileFormat};
26+
# let mut tracer = Tracer::new("prog", &[]);
27+
# // record events
28+
tracer.store_trace_events(Path::new("trace.bin"), TraceEventsFileFormat::Binary)?;
29+
```
30+
31+
To read it back:
32+
33+
```rust
34+
let mut tracer = Tracer::new("prog", &[]);
35+
tracer.load_trace_events(Path::new("trace.bin"), TraceEventsFileFormat::Binary)?;
36+
```
37+
38+
## Summary
39+
40+
`trace.bin` provides a compact representation of the same event stream described in [Trace JSON Format](trace_json_spec.md). It starts with the 8‑byte magic header followed by a packed Cap'n Proto `Trace` message.

0 commit comments

Comments
 (0)