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 | perf record -F <frequency> -p <thread id> -a -g -- sleep 30 |
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 | top # and grep to find the process you are interested |