A Quick Note on Profiling Tools and Commands

Memory usage profiling

The best tool for me is heaptrack. Compared with Valgrind, I found the usage is simpler and the visualization is clearer. It can be used to:

  • Check memory leak
  • Find the function which has the most memory allocation
  • Function calls
  • Flame graph visualization on memory usage

CPU usage profiling

The best explaination on CPU profiling is here and is highly recommended to go throught the link in detail.

I list some usful commands below:

1
2
3
4
perf record -F <frequency> -p <thread id> -a -g -- sleep 30
perf script | ./stackcollapse-perf.pl > out.perf-folded
./flamegraph.pl out.perf-folded > perf.svg
firefox perf.svg

The perf record command samples at 99 Hertz (-F 99) across all CPUs (-a), capturing stack traces so that a call graph (-g) of function ancestry can be generated later. The samples are saved in a perf.data file, which are read by perf script. This does involve CPU, file system, and disk overheads to save the samples to the file system for later processing. On newer kernels (Linux 4.8+) check out the next section on eBPF profile(8) for a lower-overhead approach.

Off-CPU profiling steps can be found out here which includes I/O consumption, wakeup and etc,.

Hotspot is also a useful integrated tool for CPU profiling.

Some Commands to examine CPU Usage

1
2
3
4
5
top # and grep to find the process you are interested
top -H -p <process id> # to list all the associated threads with the process
/proc/cpuinfo # to see the core info
taskset -c op <thread id> # to find the binded cores of the thread
ps -o pid,psr,comm -p <pid> # to list the used core id