Skip to content

Add experimental macOS tracing backend for Apple Silicon - #384

Open
leo-ai-for-trading wants to merge 1 commit into
janestreet:masterfrom
leo-ai-for-trading:codex/macos-sampling-prototype
Open

Add experimental macOS tracing backend for Apple Silicon#384
leo-ai-for-trading wants to merge 1 commit into
janestreet:masterfrom
leo-ai-for-trading:codex/macos-sampling-prototype

Conversation

@leo-ai-for-trading

Copy link
Copy Markdown

Summary

This adds an experimental, self-contained macOS backend for Apple Silicon:

  • records a command or attaches to a PID with Xcode Instruments Time Profiler;
  • exports and parses the symbolicated time-profile XML schema in native OCaml;
  • resolves xctrace id/ref and sentinel compression;
  • reconstructs gap-aware sampled call-stack intervals;
  • writes Perfetto/magic-trace.org-compatible JSON or JSON.gz;
  • adds an OCaml synthetic Processor Trace model and a 40 ns decoder-throughput gate;
  • includes correctness tests, a deterministic workload, reproducible overhead benchmarks, and detailed documentation.

The macOS implementation uses only the OCaml standard library and unix. The C source is a benchmark/demo workload, not part of the tracing backend.

Motivation

The existing backend is Linux/Intel-specific because it relies on Intel Processor Trace. This contribution provides an immediately testable Apple Silicon fallback without modifying the existing Linux backend, while establishing an OCaml software boundary and performance harness for a future hardware-backed Apple Processor Trace implementation.

Fidelity and limitations

The working M1 Pro path uses Time Profiler sampling, not hardware control-flow tracing:

  • the validated median sampling period is 1,000,000 ns;
  • calls that begin and end between samples are not visible;
  • emitted interval boundaries are explicitly marked as estimated;
  • function-trigger snapshots and a pre-trigger rolling buffer are not implemented.

The synthetic model uses an internal packet format with exact 40 ns virtual event spacing. It validates reconstruction correctness and decoder throughput only. It is not Apple's packet format and is never presented as a real target capture.

A real Apple Processor Trace capture provider, ARM64 packet decoder, image/load-address correlation, scheduling reconstruction, and hardware validation remain future work.

Validation

Focused macOS build and tests:

dune build macos/magic_trace_macos.exe \
  macos/benchmark_overhead.exe \
  macos/mock_processor_trace_cli.exe \
  macos/benchmark_mock_processor_trace.exe

dune runtest macos --no-buffer

All macOS correctness tests pass, including XML references, sentinel reuse, sampled interval reconstruction, trace JSON, exact synthetic 40 ns call boundaries, timing anchors, deterministic checksums, and overwrite protection.

Real M1 Pro recording smoke test:

samples=1984
estimated_intervals=2634
sampling_period_ns=1000000
gzip_json_validation=PASS

Balanced Time Profiler overhead benchmark:

runs=10
mean_overhead=+0.779%
standard_deviation=1.264%
approximate_95%_confidence_interval=[-0.004%, +1.563%]
overhead_gate=PASS

Synthetic 10,000,000-event decoder benchmark:

required_rate=25.000M events/s
median_decode_rate=67.361M events/s
realtime_factor=2.69x
synthetic_packet_bandwidth=12.555MB/s
decoder_realtime_gate=PASS

The complete upstream @all alias was not available in the local standalone environment because existing Linux targets require packages and tools such as core, ppx_jane, vec, ocaml-crunch, and LLVM utilities. The new macOS targets build and test independently of those dependencies.

Scope

The implementation is isolated under macos/, with only a short root README link and trace-output ignore entries outside that directory. It does not alter the existing Linux/Intel Processor Trace behavior.

@leo-ai-for-trading
leo-ai-for-trading marked this pull request as ready for review July 25, 2026 21:51
Signed-off-by: Attilio Pittelli <attilio.pittelli@gmail.com>
@leo-ai-for-trading
leo-ai-for-trading force-pushed the codex/macos-sampling-prototype branch from d4f24a1 to 2244afc Compare July 25, 2026 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant