├── profile_perf.sh ├── profile_gprof.sh ├── profile_valgrind.sh ├── profile_gperftools.sh └── cpuload.cpp /profile_perf.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # build the program 4 | g++ -std=c++11 -g cpuload.cpp -o cpuload 5 | 6 | # run the program; generates the profiling data file (perf.data) 7 | perf record -g ./cpuload 8 | 9 | # analyze the profile 10 | perf report 11 | -------------------------------------------------------------------------------- /profile_gprof.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # build the program with profiling support (-gp) 4 | g++ -std=c++11 -pg cpuload.cpp -o cpuload 5 | 6 | # run the program; generates the profiling data file (gmon.out) 7 | ./cpuload 8 | 9 | # print the callgraph to stdout 10 | gprof --graph --brief cpuload -------------------------------------------------------------------------------- /profile_valgrind.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # build the program (no special flags are needed) 4 | g++ -std=c++11 -g cpuload.cpp -o cpuload 5 | 6 | # run the program with callgrind; generates a file profile.callgrind that can be viewed with kcachegrind 7 | valgrind --tool=callgrind --demangle=yes --callgrind-out-file=profile.callgrind ./cpuload 8 | 9 | # open profile.callgrind with kcachegrind 10 | kcachegrind profile.callgrind -------------------------------------------------------------------------------- /profile_gperftools.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # build the program; For our demo program, we specify -DWITHGPERFTOOLS to enable the gperftools specific #ifdefs 4 | g++ -std=c++11 -DWITHGPERFTOOLS -lprofiler -g cpuload.cpp -o cpuload 5 | 6 | # run the program; generates the profiling data file (profile.log in our example) 7 | ./cpuload 8 | 9 | # convert profile.log to callgrind compatible format 10 | pprof --callgrind ./cpuload profile.log > profile.callgrind 11 | 12 | # open profile.callgrind with kcachegrind 13 | kcachegrind profile.callgrind 14 | -------------------------------------------------------------------------------- /cpuload.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #ifdef WITHGPERFTOOLS 5 | #include 6 | #endif 7 | 8 | using namespace std; 9 | 10 | int foo(vector v) { 11 | int result = 0; 12 | for(auto x: v) { 13 | result += x; 14 | } 15 | return result % 1000; 16 | } 17 | 18 | int main() { 19 | #ifdef WITHGPERFTOOLS 20 | ProfilerStart("profile.log"); 21 | #endif 22 | vector v; 23 | v.push_back(1); 24 | 25 | int result = 0; 26 | for (int i=0; i<10000; i++) { 27 | result = foo(v); 28 | v.push_back(result); 29 | } 30 | #ifdef WITHGPERFTOOLS 31 | ProfilerStop(); 32 | #endif 33 | cout << result << "\n"; 34 | return 1; 35 | } --------------------------------------------------------------------------------