Skip to content

magic-trace - Intel PT High-Resolution Tracing Cheatsheet

magic-trace - Intel PT High-Resolution Tracing Cheatsheet

magic-trace (by Jane Street) captures and displays high-resolution execution traces using Intel Processor Trace (Intel PT). Where a sampling profiler tells you where time goes on average, magic-trace records the actual control flow — every function entry and exit — for the last few milliseconds, at nanosecond resolution. That makes it uniquely suited to rare, latency-sensitive events: the one request in ten thousand that took 40ms, where an averaged profile shows you nothing.

Requirements

  • Intel CPU with Processor Trace support (most modern Intel)
  • Linux with perf and PT enabled
  • Root or appropriate perf_event_paranoid setting
  • Binaries with frame pointers / symbols for readable output

Installation

MethodCommand
Binarydownload magic-trace from GitHub Releases
opam (OCaml)opam install magic_trace
Permissionssysctl kernel.perf_event_paranoid=-1 (or 1)
Verifymagic-trace --help

Two Capture Modes

ModeCommandUse
Attach to runningmagic-trace attach -pid <pid>Live service
Run a commandmagic-trace run ./my-programReproducible run
# Attach, capture on Ctrl-C, write a trace
sudo magic-trace attach -pid $(pgrep -n myservice)

Snapshot Triggers

The core idea: keep a rolling buffer and snapshot when something interesting happens.

TriggerHow
ManualPress Ctrl-C while attached
Magic breakpointApp calls a designated symbol to trigger a snapshot
TimerCapture after a fixed duration
Symbol trigger-trigger <symbol> to snapshot on a function call
# Snapshot whenever a specific function is hit
sudo magic-trace attach -pid <pid> -trigger 'handle_slow_path'

Because the buffer holds the preceding few milliseconds, you see what led up to the event, not just the event itself.

Viewing Traces

magic-trace emits a trace viewable in the Perfetto UI.

StepAction
1Capture produces trace.fxt (or similar)
2Open ui.perfetto.dev
3Load the trace file
4Zoom into the microsecond range of interest
5Read the exact call sequence and durations
Perfetto controlDoes
W/SZoom in/out
A/DPan
Click a sliceDuration and function detail
Select a rangeSummary of what ran

What You Can See

Questionmagic-trace answer
Which function caused this 40ms spike?Exact call, exact duration
Did we take an unexpected branch?Full control flow, visible
Where did the time go inside one request?Nanosecond-level breakdown
Was it a syscall, a lock, or compute?The call sequence shows it

Options Worth Knowing

FlagPurpose
-multi-threadTrace all threads
-durationLength of capture window
-trigger SYMBOLSnapshot on a symbol
-full-executionTrace an entire short run
-output FILETrace output path

Limitations

LimitationNote
Intel onlyRequires Intel PT (no AMD/ARM)
Short windowMilliseconds, not minutes
Symbol qualityStripped binaries yield poor traces
OverheadLow but non-zero while tracing
Aspectmagic-traceperfPerfetto
DataFull control flow (Intel PT)Sampled stacksSystem-wide timeline
ResolutionNanosecondsSample intervalEvent-level
WindowLast few msWhole runConfigurable
Best forRare latency spikesAverage hot pathsSystem-wide correlation

Use perf for average hot paths, Perfetto for system-wide timelines, and magic-trace when you need the exact story of one rare slow event.

Resources