Perf profiling analysis program performance
Perf profiling analysis program performance Program Performance Analysis
Perf collects call stacks of a program at a certain frequency and performs statistical analysis on the call stacks. If a code path appears more ordinary in the collection result, it means that the program consumes more time on the code path. In this way, we can quickly find the code path with the most frequent program calls.
Perf command
Perf record-F 99-p $ (pidof test1)-g -- sleep 300 this command is used to collect call stacks related to the test1 program. The sampling frequency is 99Hz, -g indicates that the call stack is printed, and -- sleep 30 indicates that the sampling time is 300 seconds.
Perf report-g -- stdio command is used to view the result
Example Analysis
To understand the role of this tool, I specially wrote a program for analysis.
#includeint functiona(void){ int c = 0; int count = 0; for(c = 1; c < 20000; c++) { count++; } return;}int functionb(void){ int c = 0; int count = 0; for(c = 1; c < 40000; c++) { count++; } return;}int main(int argc, char * argv[]){ for(;;) { functiona(); printf("call function a\n"); functionb(); printf("call function b\n"); } return;}
Compile the command gcc-g-o test1 test1.c
Test process
- Test1>/de v/null &
- Perf record-F 99-p $ (pidof test1)-g -- sleep 300
- Perf script> out. perf
- ./Stackcollapse-perf.pl out. perf> out. folded
- ./Flamegraph. pl out. folded> out. svg
Test Result Analysis