perfetto

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package perfetto implements a streaming wire-format encoder for Perfetto trace files (.pftrace) compatible with the Chrome / Perfetto UI trace viewer.

It generates standard Perfetto protobuf trace packets (TrackDescriptor, TrackEvent) with zero external dependencies. Callers supply the events and timestamps; the package does not observe GPU kernels or memory copies itself.

Visualization

Trace files emitted by this package can be opened directly at https://ui.perfetto.dev.

This package is outside the compatibility promise of github.com/tmc/lib/nvidia. Its API may change or be removed in patch releases.

Example

Example writes a one-slice trace to a buffer. The bytes are a .pftrace file, openable at https://ui.perfetto.dev.

package main

import (
	"bytes"
	"fmt"

	"github.com/tmc/lib/nvidia/x/host/perfetto"
)

func main() {
	var buf bytes.Buffer
	w := perfetto.NewWriter(&buf)
	const track = 1
	if err := w.WriteTrackDescriptor(track, "gpu", 0); err != nil {
		fmt.Println(err)
		return
	}
	if err := w.WriteSliceBegin(track, 1_000_000, "matmul", "kernel"); err != nil {
		fmt.Println(err)
		return
	}
	if err := w.WriteSliceEnd(track, 3_500_000); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(buf.Len() > 0)
}
Output:
true

Index

Examples

Constants

View Source
const (
	FieldTracePacket            = 1
	FieldPacketClockSnapshot    = 6
	FieldPacketTimestamp        = 8
	FieldPacketSequenceID       = 10
	FieldPacketTrackEvent       = 11
	FieldPacketSequenceFlags    = 13
	FieldPacketTimestampClockId = 58
	FieldPacketTrackDesc        = 60

	FieldClockSnapshotClocks = 1
	FieldClockID             = 1
	FieldClockTimestamp      = 2

	ClockRealtime                      = 1
	ClockMonotonic                     = 3
	ClockBoottime                      = 6
	ClockGPU                           = 64
	FieldTrackDescUUID                 = 1
	FieldTrackDescName                 = 2
	FieldTrackDescParentUUID           = 5
	FieldTrackDescSiblingMergeBehavior = 15

	FieldTrackDescProcessUUID = 3
	FieldTrackDescThreadUUID  = 4
	FieldTrackDescCounter     = 8

	FieldCounterDescUnit           = 3
	FieldCounterDescUnitName       = 6
	FieldCounterDescUnitMultiplier = 4

	FieldProcessDescPID  = 1
	FieldProcessDescName = 6

	// ThreadDescriptor field numbers, per
	// protos/perfetto/trace/track_event/thread_descriptor.proto:
	// "optional int32 pid = 1", "optional int64 tid = 2",
	// "optional string thread_name = 5".
	FieldThreadDescPID  = 1
	FieldThreadDescName = 5
	FieldThreadDescTID  = 2

	FieldTrackEventCategories       = 22
	FieldTrackEventName             = 23
	FieldTrackEventType             = 9
	FieldTrackEventTrackUUID        = 11
	FieldTrackEventCounterValue     = 30
	FieldTrackEventDoubleCounterVal = 44
	FieldTrackEventDebugAnnotations = 4
	FieldTrackEventSourceLocation   = 33

	DefaultSequenceID                   = 1
	SequenceFlagIncrementalStateCleared = 1

	FieldDebugAnnotationName        = 10
	FieldDebugAnnotationBoolValue   = 2
	FieldDebugAnnotationUintValue   = 3
	FieldDebugAnnotationIntValue    = 4
	FieldDebugAnnotationDoubleValue = 5
	FieldDebugAnnotationStringValue = 6

	FieldSourceLocationFileName     = 2
	FieldSourceLocationFunctionName = 3
	FieldSourceLocationLineNumber   = 4

	TrackEventTypeSliceBegin = 1
	TrackEventTypeSliceEnd   = 2
	TrackEventTypeInstant    = 3
	TrackEventTypeCounter    = 4
)

Standard Perfetto wire-format constants

Variables

This section is empty.

Functions

This section is empty.

Types

type AnnotationType

type AnnotationType int

AnnotationType denotes the value type stored in a DebugAnnotation.

const (
	AnnotationTypeString AnnotationType = iota
	AnnotationTypeInt
	AnnotationTypeUint
	AnnotationTypeDouble
	AnnotationTypeBool
)

type DebugAnnotation

type DebugAnnotation struct {
	Name        string
	StringValue string
	IntValue    int64
	UintValue   uint64
	DoubleValue float64
	BoolValue   bool
	Type        AnnotationType
}

DebugAnnotation represents a structured metadata key-value annotation attached to a Perfetto slice.

func NewBoolAnnotation

func NewBoolAnnotation(name string, val bool) DebugAnnotation

NewBoolAnnotation creates a boolean debug annotation.

func NewDoubleAnnotation

func NewDoubleAnnotation(name string, val float64) DebugAnnotation

NewDoubleAnnotation creates a floating-point debug annotation.

func NewIntAnnotation

func NewIntAnnotation(name string, val int64) DebugAnnotation

NewIntAnnotation creates an integer debug annotation.

func NewStringAnnotation

func NewStringAnnotation(name, val string) DebugAnnotation

NewStringAnnotation creates a string debug annotation.

func NewUintAnnotation

func NewUintAnnotation(name string, val uint64) DebugAnnotation

NewUintAnnotation creates an unsigned integer debug annotation.

type SourceLocation

type SourceLocation struct {
	FileName     string
	FunctionName string
	LineNumber   uint32
}

SourceLocation specifies source file and line coordinates for timeline slice inspection.

type StackFrame

type StackFrame struct {
	Function string
	File     string
	Line     uint32
}

StackFrame is a symbolized frame. Call stacks use leaf-first order.

type Writer

type Writer struct {
	// TimestampClockID, when nonzero, sets the clock of timestamped packets.
	// Set it before writing. Zero preserves the protocol default.
	TimestampClockID uint64
	// SequenceID identifies incremental state. Set it before writing.
	SequenceID uint64
	// contains filtered or unexported fields
}

Writer encodes and writes Perfetto binary trace files.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a new Perfetto trace writer writing into w.

func (*Writer) InternCallstack

func (pw *Writer) InternCallstack(frames []StackFrame) (uint64, error)

InternCallstack writes interned symbols and returns a sequence-local stack ID. Equal stacks share an ID. Perfetto stores frame IDs root first.

Example
package main

import (
	"bytes"
	"fmt"
	"github.com/tmc/lib/nvidia/x/host/perfetto"
)

func main() {
	var b bytes.Buffer
	w := perfetto.NewWriter(&b)
	id, _ := w.InternCallstack([]perfetto.StackFrame{{Function: "main.work"}})
	fmt.Println(id)
}
Output:
1

func (*Writer) WriteClockSnapshot

func (pw *Writer) WriteClockSnapshot(hostMonotonicNs, gpuClockNs uint64) error

WriteClockSnapshot writes correlated host monotonic clock and GPU device clock timestamps to establish absolute timeline calibration across disparate hardware domains.

func (*Writer) WriteCounterDescriptor

func (pw *Writer) WriteCounterDescriptor(uuid uint64, name string, unitName string, parentUUID uint64) error

WriteCounterDescriptor registers a continuous numerical counter track.

func (*Writer) WriteCounterValue

func (pw *Writer) WriteCounterValue(trackUUID uint64, tsNs uint64, val int64) error

WriteCounterValue records an integer counter value on a counter track at tsNs.

Example

A counter track carries a scalar sampled over time, such as bytes resident on the device.

package main

import (
	"bytes"
	"fmt"

	"github.com/tmc/lib/nvidia/x/host/perfetto"
)

func main() {
	var buf bytes.Buffer
	w := perfetto.NewWriter(&buf)
	const counter = 2
	if err := w.WriteCounterDescriptor(counter, "device bytes", "bytes", 0); err != nil {
		fmt.Println(err)
		return
	}
	for i, v := range []int64{0, 4096, 8192} {
		if err := w.WriteCounterValue(counter, uint64(i)*1_000_000, v); err != nil {
			fmt.Println(err)
			return
		}
	}
	fmt.Println(buf.Len() > 0)
}
Output:
true

func (*Writer) WriteDoubleCounterValue

func (pw *Writer) WriteDoubleCounterValue(trackUUID uint64, tsNs uint64, val float64) error

WriteDoubleCounterValue records a floating point counter value on a counter track at tsNs.

func (*Writer) WriteFlowInstant

func (pw *Writer) WriteFlowInstant(trackUUID, tsNs uint64, name, category string, annotations []DebugAnnotation, flowIDs, terminatingFlowIDs []uint64) error

WriteFlowInstant records an instant with explicit outgoing and terminating flow IDs. IDs are trace-global; each must be unique to the caller's flow. The caller supplies ordering evidence. A flow does not infer causality or establish a dependency from timestamp overlap or submission correlation. An ID cannot both originate and terminate on the same event.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/tmc/lib/nvidia/x/host/perfetto"
)

func main() {
	var output bytes.Buffer
	w := perfetto.NewWriter(&output)
	w.WriteTrackDescriptor(1, "producer", 0)
	w.WriteTrackDescriptor(2, "consumer", 0)
	w.WriteFlowInstant(1, 100, "event recorded", "event_wait", nil, []uint64{7}, nil)
	w.WriteFlowInstant(2, 200, "event waited", "event_wait", nil, nil, []uint64{7})
	fmt.Println(output.Len() > 0)
}
Output:
true

func (*Writer) WriteInstant

func (pw *Writer) WriteInstant(trackUUID uint64, tsNs uint64, name string, category string) error

WriteInstant records an instant marker event on trackUUID at timestamp ns.

func (*Writer) WriteInstantWithCallstack

func (pw *Writer) WriteInstantWithCallstack(trackUUID, tsNs uint64, name, category string, annotations []DebugAnnotation, callstackID uint64) error

WriteInstantWithCallstack records a timestamped sample with an interned stack.

Example
package main

import (
	"bytes"
	"fmt"
	"github.com/tmc/lib/nvidia/x/host/perfetto"
)

func main() {
	var b bytes.Buffer
	w := perfetto.NewWriter(&b)
	id, _ := w.InternCallstack([]perfetto.StackFrame{{Function: "main.work"}})
	err := w.WriteInstantWithCallstack(1, 100, "CPU sample", "go", nil, id)
	fmt.Println(err)
}
Output:
<nil>

func (*Writer) WriteProcessDescriptor

func (pw *Writer) WriteProcessDescriptor(uuid uint64, pid int32, processName string) error

WriteProcessDescriptor registers a process track.

func (*Writer) WriteSliceBegin

func (pw *Writer) WriteSliceBegin(trackUUID uint64, tsNs uint64, name string, category string) error

WriteSliceBegin records the start of a timed slice on trackUUID at timestamp ns.

func (*Writer) WriteSliceBeginWithAnnotations

func (pw *Writer) WriteSliceBeginWithAnnotations(trackUUID uint64, tsNs uint64, name string, category string, annotations []DebugAnnotation, loc *SourceLocation) error

WriteSliceBeginWithAnnotations records the start of a timed slice with debug annotations and optional source location.

func (*Writer) WriteSliceBeginWithCallstack

func (pw *Writer) WriteSliceBeginWithCallstack(trackUUID, tsNs uint64, name, category string, annotations []DebugAnnotation, callstackID uint64) error

WriteSliceBeginWithCallstack records a slice with an interned language stack. callstackID must have been returned by this writer's InternCallstack.

Example
package main

import (
	"bytes"
	"fmt"
	"github.com/tmc/lib/nvidia/x/host/perfetto"
)

func main() {
	var b bytes.Buffer
	w := perfetto.NewWriter(&b)
	id, _ := w.InternCallstack([]perfetto.StackFrame{{Function: "main.work"}})
	err := w.WriteSliceBeginWithCallstack(1, 100, "launch", "go", nil, id)
	fmt.Println(err)
}
Output:
<nil>

func (*Writer) WriteSliceEnd

func (pw *Writer) WriteSliceEnd(trackUUID uint64, tsNs uint64) error

WriteSliceEnd records the end of the active slice on trackUUID at timestamp ns.

func (*Writer) WriteThreadDescriptor

func (pw *Writer) WriteThreadDescriptor(uuid uint64, pid int32, tid int32, threadName string) error

WriteThreadDescriptor registers a thread track grouped under a process track.

func (*Writer) WriteTrackDescriptor

func (pw *Writer) WriteTrackDescriptor(uuid uint64, name string, parentUUID uint64) error

WriteTrackDescriptor registers a track in the trace timeline. Distinct UUIDs remain separate even when sibling names are identical.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL