Profiling

This is an attempt to map the software profiling ecosystem, with a focus on the OCaml programming language. A review / survey of profilers, their data formats, analysis UIs and converters between data formats.

Architecture of profiling tools, visualisations and data formats

Profilers

There are a number of different profilers available for OCaml, each with their strengths and usecases.

ProfilerPurposeProfile Data FormatLink
ollyobservability tools around the runtime events tracing system introduced in OCaml 5.0Chrome Tracing Format (JSON), Fuchsia Trace Formatruntime-events-tool
magic-tracecollects and displays high-resolution traces of what a process is doingFuchsia Trace Formatmagic-trace
memtraceStatistical Memory Profiling for OCaml aka statmemprofCommon Trace Format (CTF)memtrace
eio-traceeio-trace can be used to record and display traces of programs using the eio libraryFuchsia Trace Formateio-trace
perfGeneral-purpose profiler that uses hardware performance counters on LinuxCommon Trace Format (CTF)
InstrumentsGeneral-purpose profiler that comes with Xcode on macOS
dtraceGeneral Performance analysis tool for FreeBSD, Solaris and MacOSdtrace
eBPFGeneral Performance analysis tool for Linux (similar to DTrace)

The intent is to have a minimal set of Profile Data Formats produced by OCaml tooling that can be used with existing Analysis UI tools.

Visualisation and Analysis UI

NameDescriptionProfile Data FormatLink
PerfettoSystem profiling, app tracing and trace analysisFuchsia Trace Format (others)perfetto.dev
SpeedscopeAn interactive flamegraph visualizerVariousgithub.com/jlfwong/speedscope
memtrace_viewerStatistical Memory ProfilerCTFgithub.com/janestreet/memtrace_viewer
flamegraphStack trace visualiserFolded Stackswww.brendangregg.com/flamegraphs.html
flamescopeVisualization tool for exploring different time ranges as Flame GraphsLinux perf scriptgithub.com/Netflix/flamescope
pprofVisualise profiling data as directed graph or flamegraphsProtocol Buffersgithub.com/google/pprof

Profile Data Format

Chrome Tracing Format is a textual JSON also know as Trace Event Format or Chrome Performance Profile. spec No OCaml libraries for reading / writing

Fuchsia Trace Format is a binary format used to collect, store, and transmit trace records. Focuses on performance. spec Two libraries for reading / writing trace-fuchsia and tracing.

Common Trace Format is a binary trace format designed to be very fast to write. Uses a declaritive language called the Trace Stream Description Language TSDL to describe trace stream layout. spec No OCaml libraries for reading / writing.

Pprof is a common stacktrace profile format using protocol buffers. Used by pprof supported as an input format by Speedscope and commercial monitoring tools (Google Cloud Profiler, Polar Signals, Datadog).

Converters

Nothing to see here!

Walk throughs

  • OCaml 5 Performance part 1/part 2 comprehensive investigation of OCaml 5 performance for an application.
  • Generating CPU Flamegraphs for OCaml on Linux here
  • Generating CPU Flamegraphs for OCaml on MacOS here
  • Memory profiling with statmemprof in 4.14 LTS and 5.3

Resources

This format was inspired by https://profilerpedia.markhansen.co.nz

Prior art:

  • OCaml-bench profiling notes https://github.com/ocaml-bench/notes/blob/master/profiling_notes.md

  • Add Profile-Guided Optimization (PGO) support to the compiler - https://github.com/ocaml/ocaml/issues/12200 [runtime-events-tools]: https://github.com/tarides/runtime_events_tools [magic-trace]: https://github.com/janestreet/magic-trace [memtrace]: https://github.com/janestreet/memtrace [dtrace]: https://dtrace.org/ [eio]: https://github.com/ocaml-multicore/eio [eio-trace]: https://github.com/ocaml-multicore/eio-trace [perfetto.dev]: https://ui.perfetto.dev/ [github.com/jlfwong/speedscope]: https://github.com/jlfwong/speedscope [github.com/janestreet/memtrace_viewer]: https://github.com/janestreet/memtrace_viewer [www.brendangregg.com/flamegraphs.html]: https://www.brendangregg.com/flamegraphs.html [github.com/Netflix/flamescope]: https://github.com/Netflix/flamescope [github.com/google/pprof]: https://github.com/google/pprof/blob/main/doc/README.md