silk.trace.perfetto.

Perfetto trace encoding and decoding for recorder batches and counter samples.

A Perfetto trace is a sequence of TracePacket messages, each one written as field 1 of the Trace message. Bytes made of whole packets are a trace on their own, and so is a file that several writers append whole packets to. The Perfetto UI and trace_processor open it, with each span as a slice on the track of the thread that opened it, each log as an instant on the process's log track, and each metric series and counter sample as a counter track. Perfetto writes such a trace a batch at a time, and PerfettoReader reads one back a chunk at a time, so a reader can follow a file that a running program still appends to.

Import as Perfetto with import silk.trace.perfetto { Perfetto }.

Public declarations: 8.

CounterUnit#

silk
pub enum CounterUnit

What a counter track measures, which a trace viewer uses to format its values.

Count#

silk
Count

A plain number of things.

Bytes#

silk
Bytes

A size in bytes.

Nanoseconds#

silk
Nanoseconds

A duration in nanoseconds.

Percent#

silk
Percent

A percentage, where 100 is one whole.

Perfetto#

silk
pub struct Perfetto

One process's Perfetto packet sequence: the names it has interned and the tracks it has described.

When to use#

Keep one Perfetto for each process that writes a trace, and pass it every batch and sample the process writes, so that each name and track is written once. As a Sink it writes each batch to the active Writer.

Details#

The process ID numbers the sequence, so the packets of processes that append to one file at the same time keep their interned names apart. A span becomes a slice on the track of the thread that opened it, or on the process's spans track when the target gives no thread ID, with its attributes, source location and error as arguments, and its ID and its parent's as span.id and parent.id. The trace a span belongs to is written as trace.id.high and trace.id.low on the first span of each trace in a row, and holds for the spans after it in the sequence, so a reader rebuilds every parent link and trace whatever thread or process a parent ran on (PerfettoReader). A log becomes an instant named by its template on the process's log track, with its message, severity and the IDs of the span current when it was written. Each metric series becomes a counter track named by its instrument and attributes; a histogram has no single value over time and is left out. Times are the recorder's Unix nanoseconds.

Gotchas#

A trace viewer nests slices by their times. Two spans of one thread that start at the same nanosecond nest in the order they were written, which is the order they ended, so a span and its first child must start at different times. The recorder's tick counters ensure it on Apple and Linux targets, where opening a span takes longer than one tick.

Implementation Perfetto#

silk
impl Perfetto

Associated function make#

silk
pub fn make(pid: i32) -> Self

Starts the packet sequence of the process pid. Nothing is written until the first batch or sample.

Method encodeInto#

silk
pub effect fn encodeInto(self: &mut Self, identity: &Resource, batch: &Batch<'_>, output: &mut Bytes) -> ()
    ! OutOfMemoryError ? &mut Allocator

Replaces the contents of output with the packets of batch, preceded by the descriptions of the tracks and names they use first. identity names the process the first time.

Method sample#

silk
pub effect fn sample(self: &mut Self, identity: &Resource, name: string<'static>, unit: CounterUnit, time: u64,
    value: f64, output: &mut Bytes) -> () ! OutOfMemoryError ? &mut Allocator

Appends to output the value value of the counter name at time, in Unix nanoseconds, and the counter's track the first time, measured in unit. identity names the process the first time.

Implementation Sink<WriterError | OutOfMemoryError ? &mut Writer | &mut Allocator> for Perfetto#

silk
impl Sink<WriterError | OutOfMemoryError ? &mut Writer | &mut Allocator> for Perfetto

Operation send#

silk
effect fn send(self: &mut Self, identity: &Resource, batch: &Batch<'_>) -> u64
    ! WriterError | OutOfMemoryError ? &mut Writer | &mut Allocator

FieldValue#

silk
pub union FieldValue

A value a decoded event carries in one of its fields.

Bool#

silk
Bool

A truth value.

Field value#

silk
pub value: bool

Integer#

silk
Integer

A signed integer.

Field value#

silk
pub value: i64

Unsigned#

silk
Unsigned

An unsigned integer.

Field value#

silk
pub value: u64

Double#

silk
Double

A floating-point number.

Field value#

silk
pub value: f64

Text#

silk
Text

Text.

Field value#

silk
pub value: String

Field#

silk
pub struct Field

One field of a decoded event: the name and value of one of its debug annotations, such as an attribute of a span.

Field key#

silk
pub key: String

The annotation's name, such as code.file.path.

Field value#

silk
pub value: FieldValue

The annotation's value.

Ids#

silk
pub struct Ids

The IDs that place a decoded slice or instant in its OpenTelemetry trace. Each is zero where the trace does not say, as for another writer's event or a log written outside every span.

Field traceHigh#

silk
pub traceHigh: u64

The high 64 bits of the trace ID.

Field traceLow#

silk
pub traceLow: u64

The low 64 bits of the trace ID.

Field span#

silk
pub span: u64

The span's own ID, or for a log the ID of the span current when it was written.

Field parent#

silk
pub parent: u64

The parent span's ID; zero for a root.

Implementation Copy for Ids#

silk
impl Copy for Ids

Event#

silk
pub union Event

One item of a Perfetto trace that PerfettoReader decodes, in the order the trace holds it.

Process#

silk
Process

A process's track.

Field track#

silk
pub track: u64

Field pid#

silk
pub pid: i64

Field name#

silk
pub name: String

Thread#

silk
Thread

A thread's track, in the process pid.

Field track#

silk
pub track: u64

Field pid#

silk
pub pid: i64

Field tid#

silk
pub tid: i64

Counter#

silk
Counter

A track of counter samples below the track parent, zero for none, measured in unit.

Field track#

silk
pub track: u64

Field parent#

silk
pub parent: u64

Field name#

silk
pub name: String

Field unit#

silk
pub unit: CounterUnit

Named#

silk
Named

A track with only a name, such as a process's log or spans track.

Field track#

silk
pub track: u64

Field parent#

silk
pub parent: u64

Field name#

silk
pub name: String

Slice#

silk
Slice

A slice whose start and end the trace holds one after the other, as Perfetto writes every span: its times in Unix nanoseconds, its IDs and its fields.

Field track#

silk
pub track: u64

Field start#

silk
pub start: u64

Field end#

silk
pub end: u64

Field name#

silk
pub name: String

Field ids#

silk
pub ids: Ids

Field fields#

silk
pub fields: List<Field>

Begin#

silk
Begin

The start of a slice whose end comes later in the trace, as another writer may write one.

Field track#

silk
pub track: u64

Field time#

silk
pub time: u64

Field name#

silk
pub name: String

Field ids#

silk
pub ids: Ids

Field fields#

silk
pub fields: List<Field>

End#

silk
End

The end of the latest slice begun on track that has not ended.

Field track#

silk
pub track: u64

Field time#

silk
pub time: u64

Instant#

silk
Instant

Something that happened at one time, such as a log record.

Field track#

silk
pub track: u64

Field time#

silk
pub time: u64

Field name#

silk
pub name: String

Field ids#

silk
pub ids: Ids

Field fields#

silk
pub fields: List<Field>

Sample#

silk
Sample

A counter's value at time.

Field track#

silk
pub track: u64

Field time#

silk
pub time: u64

Field value#

silk
pub value: f64

PerfettoError#

silk
pub struct PerfettoError

Bytes that are not a Perfetto trace: a malformed field, text that is not UTF-8, or an interned name that no earlier packet of its sequence defined.

Field offset#

silk
pub offset: usize

Where, in the bytes passed to PerfettoReader.read, the packet at fault starts.

PerfettoReader#

silk
pub struct PerfettoReader

Decodes a Perfetto trace into Events, a chunk of bytes at a time.

When to use#

Read a trace that Perfetto or another TrackEvent writer recorded: a whole file at once, or a file a running program still appends to, by passing each newly appended chunk to read with whatever the last call left unread before it.

Details#

A packet's interned names stay with its sequence until the sequence says its state was cleared, so a reader must see a sequence from its first packet. Perfetto writes each span's start and end side by side, and the reader joins them into one Event.Slice; it holds a start until the next packet shows whether its end follows, and finish hands over the last one. A span's trace comes from the latest trace.id fields of its sequence. Packet timestamps are read as they are: a trace that times packets by clock snapshots or timestamp deltas reads with the raw values.

Implementation PerfettoReader#

silk
impl PerfettoReader

Associated function make#

silk
pub fn make() -> Self

A reader at the start of a trace.

Method read#

silk
pub effect fn read(self: &mut Self, bytes: &[u8], events: &mut List<Event>) -> usize
    ! PerfettoError | OutOfMemoryError ? &mut Allocator

Decodes the whole packets at the start of bytes, appends their events to events, and returns how many bytes it decoded. A packet that bytes ends inside is left unread: pass its bytes again, with what follows them, to the next call.

Errors#

  • PerfettoError for bytes that are not a Perfetto trace, at the packet at fault; the events of the packets before it are appended.

  • OutOfMemoryError if the Allocator cannot hold the events or the interned names.

Method finish#

silk
pub effect fn finish(self: &mut Self, events: &mut List<Event>) -> () ! OutOfMemoryError ? &mut Allocator

Hands over, as an Event.Begin, the slice start the last packet left waiting for its end. Call it once no more bytes of the trace will come.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page