Breaking Eggs And Making Omelettes

Topics On Multimedia Technology and Reverse Engineering


Archives:

FFmpeg and Code Coverage Tools

August 21st, 2010 by Multimedia Mike

Code coverage tools likely occupy the same niche as profiling tools: Tools that you’re supposed to use somewhere during the software engineering process but probably never quite get around to it, usually because you’re too busy adding features or fixing bugs. But there may come a day when you wish to learn how much of your code is actually being exercised in normal production use. For example, the team charged with continuously testing the FFmpeg project, would be curious to know how much code is being exercised, especially since many of the FATE test specs explicitly claim to be “exercising XYZ subsystem”.

The primary GNU code coverage tool is called gcov and is probably already on your GNU-based development system. I set out to determine how much FFmpeg source code is exercised while running the full FATE suite. I ran into some problems when trying to use gcov on a project-wide scale. I spackled around those holes with some very ad-hoc solutions. I’m sure I was just overlooking some more obvious solutions about which you all will be happy to enlighten me.

Results
I’ve learned to cut to the chase earlier in blog posts (results first, methods second). With that, here are the results I produced from this experiment. This Google spreadsheet contains 3 sheets: The first contains code coverage stats for a bunch of FFmpeg C files sorted first by percent coverage (ascending), then by number of lines (descending), thus highlighting which files have the most uncovered code (ffserver.c currently tops that chart). The second sheet has files for which no stats were generated. The third sheet has “problems”. These files were rejected by my ad-hoc script.

Here’s a link to the data in CSV if you want to play with it yourself.

Using gcov with FFmpeg
To instrument a program for gcov analysis, compile and link the target program with the -fprofile-arcs and -ftest-coverage options. These need to be applied at both the compile and link stages, so in the case of FFmpeg, configure with:

  ./configure \
    --extra-cflags="-fprofile-arcs -ftest-coverage" \
    --extra-ldflags="-fprofile-arcs -ftest-coverage"

The building process results in a bunch of .gcno files which pertain to code coverage. After running the program as normal, a bunch of .gcda files are generated. To get coverage statistics from these files, run 'gcov sourcefile.c'. This will print some basic statistics as well as generate a corresponding .gcov file with more detailed information about exactly which lines have been executed, and how many times.

Be advised that the source file must either live in the same directory from which gcov is invoked, or else the path to the source must be given to gcov via the '-o, --object-directory' option.

Resetting Statistics
Statistics in the .gcda files are cumulative. Should you wish to reset the statistics, doing this in the build directory should suffice:

  find . -name "*.gcda" | xargs rm -f

Getting Project-Wide Data
As mentioned, I had to get a little creative here to get a big picture of FFmpeg code coverage. After building FFmpeg with the code coverage options and running FATE,

for file in `find . -name "*.c"` \
do \
  echo "*****" $file \
  gcov -o `dirname $file` `basename $file` \
done > ffmpeg-code-coverage.txt 2>&1

After that, I ran the ffmpeg-code-coverage.txt file through a custom Python script to print out the 3 CSV files that I later dumped into the Google Spreadsheet.

Further Work
I’m sure there are better ways to do this, and I’m sure you all will let me know what they are. But I have to get the ball rolling somehow.

There’s also TestCocoon. I’d like to try that program and see if it addresses some of gcov’s shortcomings (assuming they are indeed shortcomings rather than oversights).

Source for script: process-gcov-slop.py

  1. #!/usr/bin/python
  2.  
  3. import re
  4.  
  5. lines = open("ffmpeg-code-coverage.txt").read().splitlines()
  6. no_coverage = ""
  7. coverage = "filename, % covered, total lines\n"
  8. problems = ""
  9.  
  10. stats_exp = re.compile('Lines executed:(\d+\.\d+)% of (\d+)')
  11. for i in xrange(len(lines)):
  12.     line = lines[i]
  13.     if line.startswith("***** "):
  14.         filename = line[line.find('./')+2:]
  15.         i += 1
  16.         if lines[i].find(":cannot open graph file") != -1:
  17.             no_coverage += filename + '\n'
  18.         else:
  19.             while lines[i].find(filename) == -1 and not lines[i].startswith("***** "):
  20.                 i += 1
  21.             try:
  22.                 (percent, total_lines) = stats_exp.findall(lines[i+1])[0]
  23.                 coverage += filename + ', ' + percent + ', ' + total_lines + '\n'
  24.             except IndexError:
  25.                 problems += filename + '\n'
  26.  
  27. open("no_coverage.csv", 'w').write(no_coverage)
  28. open("coverage.csv", 'w').write(coverage)
  29. open("problems.csv", 'w').write(problems)

Posted in FATE Server, Python | 10 Comments »

10 Responses

  1. Reimar Says:

    What, no coverage for truemotion1? Shame on you Mike for never adding a test :-P

  2. Multimedia Mike Says:

    What? No TM1 test? That’s odd.

    Wait, I remember the problem– the decoder is not endian-safe. It only runs correctly on little endian machines.

  3. Reimar Says:

    As far as I can tell, this is fixed now.

  4. Vitor Says:

    Very nice work!

  5. Multimedia Mike Says:

    Thanks, Vitor (assuming you were talking to me rather than Reimar). Now go forth and expand FATE’s test coverage. :-)

  6. John K Says:

    Another tool for aggregating gcov reports: https://software.sandia.gov/trac/fast/wiki/gcovr

  7. compn Says:

    but what aggregates the aggregators? :)

  8. Vitor Says:

    After latest changes (hand picked):

    — orig.csv 2010-09-01 12:07:21.307787721 +0200
    +++ cc.csv 2010-09-01 12:12:23.023805430 +0200
    +libavcodec/a64multienc.c,0,156
    -libavcodec/ansi.c,0,215
    +libavcodec/ansi.c,49.3,215
    -libavcodec/binkaudio.c,29.37,126
    +libavcodec/binkaudio.c,93.65,126
    -libavcodec/dxa.c,37.35,166
    +libavcodec/dxa.c,89.76,166
    -libavcodec/faxcompr.c,8.57,140
    +libavcodec/faxcompr.c,47.86,140
    -libavcodec/mjpegbdec.c,0,69
    +libavcodec/mjpegbdec.c,91.3,69
    -libavcodec/mpegaudiodec.c,60.85,1065
    -libavcodec/mpegaudiodec_float.c,0,29
    +libavcodec/mpegaudiodec.c,63.29,1065
    +libavcodec/mpegaudiodec_float.c,100,29
    -libavcodec/opt.c,50.2,247
    +libavcodec/opt.c,59.51,247
    -libavcodec/rv30.c,9.49,158
    -libavcodec/rv30dsp.c,86.36,44
    -libavcodec/rv34.c,89.27,811
    +libavcodec/rv30.c,89.24,158
    +libavcodec/rv30dsp.c,100,44
    +libavcodec/rv34.c,94.94,811
    -libavcodec/s3tc.c,0,48
    +libavcodec/s3tc.c,85.42,48
    -libavcodec/truemotion1.c,0,299
    +libavcodec/truemotion1.c,75.25,299
    -libavcodec/txd.c,0,76
    +libavcodec/txd.c,74.67,75
    -libavcodec/vc1.c,40.11,571
    -libavcodec/vc1dec.c,60.19,1854
    +libavcodec/vc1.c,66.55,571
    +libavcodec/vc1dec.c,69.63,1854
    -libavcodec/vp3.c,12.39,952
    +libavcodec/vp3.c,68.96,944
    -libavcodec/ws-snd1.c,0,67
    +libavcodec/ws-snd1.c,88.06,67
    +libavformat/a64.c,0,53
    -libavformat/asfcrypt.c,0,64
    -libavformat/asfdec.c,69.34,636
    +libavformat/asfcrypt.c,100,64
    +libavformat/asfdec.c,70.6,636
    -libavformat/mp3.c,49.4,168
    +libavformat/mp3.c,54.17,168
    -libavformat/sauce.c,0,48
    +libavformat/sauce.c,52.08,48
    -libavformat/tty.c,0,45
    -libavformat/txd.c,8.82,34
    +libavformat/tty.c,68.89,45
    +libavformat/txd.c,82.35,34
    -libavformat/westwood.c,82.55,149
    +libavformat/westwood.c,87.25,149
    -libavutil/des.c,0,72
    +libavutil/des.c,84.72,72
    -libavutil/rc4.c,0,26
    +libavutil/rc4.c,96.15,26

  9. Anon Says:

    You may also like to look at http://code.google.com/p/trucov/

  10. Multimedia Mike Says:

    @Anon: Thanks, I’ll check that out. I’m also studying up on lcov.