===Code profiling===

  • Compile and link with the gcc flag <code>-pg</code>, along with the optimisation flag of interest (e.g. <code>-o3</code>). You probably want to enable debugging as well (<code>-g</code>).
  • Note that profiling is only performed on objects that are statically linked --- objects within a dynamically linked library will not be profiled. You probably need to compile/link with the <code>-static</code> flag as well.
  • Note that if your program calls another program compiled for profiling, gmon.out will get stepped on. For example, when running psphot, you need to turn off the plotting option (SAVE.PLOTS FALSE) to get a complete profile.
  • For the IPP programs build with the autotools, you can achieve the above by specifying <code>--enable-profile --disable-shared</code> when you use <code>configure</code> or <code>autogen.sh</code>.
  • Run your code a lot. If it takes a while to run, then that should be OK. If it is a short program, you want to run it multiple times, moving the <code>gmon.out</code> each time. For example:

<pre> #!/bin/sh for ((i=0;i<10;i++)) do

./myProgram mv -f gmon.out gmon.$i;

done </pre>

  • Run gprof on the "gmon.out" file(s): <code>gprof -p -q -l myProgram gmon.* > profile.txt</code>
    • <code>-p</code> gives the flat profile.
    • <code>-q</code> gives the call graph analysis.
    • <code>-l</code> does the analysis line-by-line (instead of functions).
    • You can also get annotated source code using the <code>-A -y -x</code> switches (annotate, separate files, annotate lines).

===Code path coverage===

  • Compile and link with the gcc flags <code>-fprofile-arcs -ftest-coverage</code>.
  • Run the code through whatever exercise you want to test the path coverage on. This will generate a few extra files for each source file (<code>.bb,.bbg</code>).
  • Run <code>gcov</code> on the source code: <code>gcov myProgram.c</code>
    • This generates a file ending in <code>.gcov</code>, e.g., <code>myProgram.c.gcov</code>, which contains the source code, annotated at the start of the line with the number of times the line was executed. Lines that did not execute at all are marked by hash marks, <code>######</code>.
  • NOTE: For some reason, <code>gcov</code> doesn't like being passed a source file in a directory other than the current directory. For example, <code>gcov myDirectory/myProgram.c</code> won't work, because <code>gcov</code> claims it can't find the source file (though it will output the percentage of path coverage).