Skip to content

Instantly share code, notes, and snippets.

@singe
Last active January 16, 2022 16:05
Show Gist options
  • Save singe/70010e2f48a7ad8fdcbab177eeb9b18a to your computer and use it in GitHub Desktop.
Save singe/70010e2f48a7ad8fdcbab177eeb9b18a to your computer and use it in GitHub Desktop.
macOS Perf Approaches

Remember to compile with debug.

Use DTrace - onCPU

  1. Clone https://github.com/brendangregg/FlameGraph

  2. Trace command sudo dtrace -c '<command>' -o out.stacks -n 'profile-997 /execname == "<command name>"/ { @[ustack(100)] = count(); }' > /dev/null

  3. Create Graph FlameGraph/stackcollapse.pl out.stacks | FlameGraph/flamegraph.pl > graph1.svg

Cons, won't tell you what took the longest, just what used the most CPU time.

Instruments.app - elapsed time

https://stackoverflow.com/questions/11445619/profiling-c-on-mac-os-x/11445777#11445777

Doesn't care whether it's IO, CPU etc, will show you where your time slow downs are. Resulting output is can be selectivley collapsed/expanded to zoom in to affected parts.

In-code for Rust

Use https://docs.rs/pprof/latest/pprof/

It's programmable for more specific investigations. Only onCPU for now.

Simple example:

let guard = pprof::ProfilerGuard::new(100).unwrap();

<do some stuff>

if let Ok(report) = guard.report().build() {
  let file = File::create("flamegraph.svg").unwrap();
  report.flamegraph(file).unwrap();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment