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#
pub enum CounterUnitWhat a counter track measures, which a trace viewer uses to format its values.
Count#
CountA plain number of things.
Bytes#
BytesA size in bytes.
Nanoseconds#
NanosecondsA duration in nanoseconds.
Percent#
PercentA percentage, where 100 is one whole.
Perfetto#
pub struct PerfettoOne 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#
impl PerfettoAssociated function make#
pub fn make(pid: i32) -> SelfStarts the packet sequence of the process pid. Nothing is written until the first batch or
sample.
Method encodeInto#
pub effect fn encodeInto(self: &mut Self, identity: &Resource, batch: &Batch<'_>, output: &mut Bytes) -> ()
! OutOfMemoryError ? &mut AllocatorReplaces 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#
pub effect fn sample(self: &mut Self, identity: &Resource, name: string<'static>, unit: CounterUnit, time: u64,
value: f64, output: &mut Bytes) -> () ! OutOfMemoryError ? &mut AllocatorAppends 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#
impl Sink<WriterError | OutOfMemoryError ? &mut Writer | &mut Allocator> for PerfettoOperation send#
effect fn send(self: &mut Self, identity: &Resource, batch: &Batch<'_>) -> u64
! WriterError | OutOfMemoryError ? &mut Writer | &mut AllocatorFieldValue#
pub union FieldValueA value a decoded event carries in one of its fields.
Bool#
BoolA truth value.
Field value#
pub value: boolInteger#
IntegerA signed integer.
Field value#
pub value: i64Unsigned#
UnsignedAn unsigned integer.
Field value#
pub value: u64Double#
DoubleA floating-point number.
Field value#
pub value: f64Text#
TextText.
Field value#
pub value: StringField#
pub struct FieldOne field of a decoded event: the name and value of one of its debug annotations, such as an attribute of a span.
Field key#
pub key: StringThe annotation's name, such as code.file.path.
Field value#
pub value: FieldValueThe annotation's value.
Ids#
pub struct IdsThe 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#
pub traceHigh: u64The high 64 bits of the trace ID.
Field traceLow#
pub traceLow: u64The low 64 bits of the trace ID.
Field span#
pub span: u64The span's own ID, or for a log the ID of the span current when it was written.
Field parent#
pub parent: u64The parent span's ID; zero for a root.
Implementation Copy for Ids#
impl Copy for IdsEvent#
pub union EventOne item of a Perfetto trace that PerfettoReader decodes, in the order the trace holds it.
Process#
ProcessA process's track.
Field track#
pub track: u64Field pid#
pub pid: i64Field name#
pub name: StringThread#
ThreadA thread's track, in the process pid.
Field track#
pub track: u64Field pid#
pub pid: i64Field tid#
pub tid: i64Counter#
CounterA track of counter samples below the track parent, zero for none, measured in unit.
Field track#
pub track: u64Field parent#
pub parent: u64Field name#
pub name: StringField unit#
pub unit: CounterUnitNamed#
NamedA track with only a name, such as a process's log or spans track.
Field track#
pub track: u64Field parent#
pub parent: u64Field name#
pub name: StringSlice#
SliceA 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#
pub track: u64Field start#
pub start: u64Field end#
pub end: u64Field name#
pub name: StringField ids#
pub ids: IdsField fields#
pub fields: List<Field>Begin#
BeginThe start of a slice whose end comes later in the trace, as another writer may write one.
Field track#
pub track: u64Field time#
pub time: u64Field name#
pub name: StringField ids#
pub ids: IdsField fields#
pub fields: List<Field>End#
EndThe end of the latest slice begun on track that has not ended.
Field track#
pub track: u64Field time#
pub time: u64Instant#
InstantSomething that happened at one time, such as a log record.
Field track#
pub track: u64Field time#
pub time: u64Field name#
pub name: StringField ids#
pub ids: IdsField fields#
pub fields: List<Field>Sample#
SampleA counter's value at time.
Field track#
pub track: u64Field time#
pub time: u64Field value#
pub value: f64PerfettoError#
pub struct PerfettoErrorBytes 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#
pub offset: usizeWhere, in the bytes passed to PerfettoReader.read, the packet at fault starts.
PerfettoReader#
pub struct PerfettoReaderDecodes 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#
impl PerfettoReaderAssociated function make#
pub fn make() -> SelfA reader at the start of a trace.
Method read#
pub effect fn read(self: &mut Self, bytes: &[u8], events: &mut List<Event>) -> usize
! PerfettoError | OutOfMemoryError ? &mut AllocatorDecodes 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#
PerfettoErrorfor bytes that are not a Perfetto trace, at the packet at fault; the events of the packets before it are appended.OutOfMemoryErrorif theAllocatorcannot hold the events or the interned names.
Method finish#
pub effect fn finish(self: &mut Self, events: &mut List<Event>) -> () ! OutOfMemoryError ? &mut AllocatorHands 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.