Skip to content

Instantly share code, notes, and snippets.

@santileortiz
Last active October 16, 2018 01:26
Show Gist options
  • Save santileortiz/ae255cf0158217b2e81e1dcd859ac165 to your computer and use it in GitHub Desktop.
Save santileortiz/ae255cf0158217b2e81e1dcd859ac165 to your computer and use it in GitHub Desktop.
Profiling tools
gprof
Prints a report containing time spent in each function. Also prints a report on the call graph by
telling for each function how many times was called and by whom, and also how many times it called
each other function.
1. Compile with flag -pg and optimizations.
2. Run executable, file gmon.out will be created.
3. Run $ gprof <path to binary>
NOTE: This requires some packages related to the kernel, if prompted to install something
in step 3, then install it and repeat everything.
---------------------------------------------------------------------------------------------
strace
Prints every system call made by a process, and can filter them. Common use case:
$ strace -c -S time <binary> // Count all system calls and sort them by time
$ strace -e open <binary> // Print all calls made to open()
Change <binary> to -p <PID> to attach it to a running process.
NOTE: strace outputs to stderr, to grep the output of stderr use:
$ strace <options> 2>&1 | grep <options>
---------------------------------------------------------------------------------------------
perf
It's a very complex tool, at the moment I have used it to get cache misses, brach misses,
page faults etc., the following command adds cache misses to the default event list.
$ perf stat -e task-clock,context-switches,cpu-migrations,page-faults,cycles,instructions,\
cache-references,cache-misses,branches,branch-misses <binary>
---------------------------------------------------------------------------------------------
BCC
On newer kernels (4.1 and above) this tool is available https://github.com/iovisor/bcc seems
to be a powerful way to profile a process. I need to take a closer look at it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment