flighttrace
A zero-cgo, zero-TCC anomaly-triggered post-mortem bundle example built on Go 1.27 runtime/trace.FlightRecorder and github.com/tmc/apple/x/unifiedtrace.
Instead of continuously writing large trace files, the caller owns an always-on trace.FlightRecorder (MinAge: 2s, MaxBytes: 4 MiB) and a bounded unifiedtrace.Ring. When an injected latency spike (lock stall + heavy heap allocation + extra Metal GPU work) crosses the step latency threshold, the detector applies cooldown/debounce, captures a synchronized snapshot via ring.Snapshot(fr), and writes a self-contained post-mortem bundle via unifiedtrace.WriteBundle:
trace.out — raw Go 1.27 execution trace (open with go tool trace trace.out).
timeline.json.gz — compact gzipped Chrome Trace Event JSON (open in Perfetto or chrome://tracing) correlating:
- Go runtime/trace intervals (user tasks/regions, STW GC pauses, GC cycles, mark assists, mutex lock waits, and heap alloc/goal counters)
x/metalcounters per-batch GPU dispatch spans (metal.dispatch, ph:"X") resolved to CPU monotonic nanoseconds via ResolveRangeNanos
x/signpost coarse step/snapshot intervals (ph:"X")
- 5 Hz
x/mempressure (PhysFootprint, ResidentSize) and x/thermal (CurrentState) counters (ph:"C") and instant markers (ph:"i")
heap.pb.gz and goroutine.pb.gz — pprof profiles captured at trigger time (open with go tool pprof).
Usage
Run with CGO_ENABLED=0:
CGO_ENABLED=0 go run ./examples/metal/flighttrace -out /tmp/flighttrace-bundle
Run vet and tests:
CGO_ENABLED=0 go vet ./examples/metal/flighttrace
CGO_ENABLED=0 go test -v -count=1 -timeout 60s ./examples/metal/flighttrace
Flags
| Flag |
Default |
Description |
-out |
temp dir |
Directory to write the post-mortem bundle |
-steps |
16 |
Number of synthetic Metal decode steps |
-spike-step |
10 |
Step index on which to inject a latency spike |
-threshold |
25ms |
Step latency threshold that triggers a snapshot |
-cooldown |
500ms |
Minimum interval between snapshot triggers |
-elements |
524288 |
float32 elements per Metal compute dispatch |
-iters |
64 |
Inner FMA iterations per GPU thread |
Measured Output (Apple M4 Max, Go 1.27.0, CGO_ENABLED=0)
FlightRecorder Anomaly Post-Mortem Summary (zero-cgo, zero-TCC)
Device: Apple M4 Max | Counter backend: MTLCounterSampleBuffer (dispatch_boundary=false) | Checksum: 0.7930
Trigger: step=10 latency=34.553ms threshold=25ms cooldown=500ms (triggers=1 debounced=0)
Snapshot window: 298.41 ms | Clock cal: 5 samples slope=1.000000000 drift=+0.00 ppm max_res=0.0 ns sleep_discontinuity=false
Go runtime: stw=11 (480.9 µs) gc_cycles=5 mark_assists=3 lock_waits=5 regions=56 heap_counters=251
GPU telemetry: command_buffers=11 exec=5.29 ms metal.dispatch_spans=24 signposts=11
System counters: samples=4 markers=2 phys_footprint=183.8 MiB resident=37.3 MiB thermal=fair
Chrome Trace verified (timeline.json.gz): total=425 ph:C=255 ph:i=7 ph:X=148 ph:M=15
BUNDLE FILE SIZE (BYTES)
----------- ------------
trace.out 47289
timeline.json.gz 5959
heap.pb.gz 5617
goroutine.pb.gz 1048
Files
main.go — CLI entrypoint and flags (-out, -steps, -spike-step, -threshold, -cooldown, -elements, -iters).
workload.go — synthetic Metal compute decode loop with pprof.Do labels, x/metalcounters GPU dispatch spans, 5 Hz x/mempressure + x/thermal sampler, latency anomaly gate with cooldown/debounce, bundle writing, and timeline.json.gz verification.
main_test.go — cooldown unit test and live end-to-end bundle verification test.