{"id":2761,"date":"2010-08-21T09:26:26","date_gmt":"2010-08-21T16:26:26","guid":{"rendered":"http:\/\/multimedia.cx\/eggs\/?p=2761"},"modified":"2020-07-25T22:55:20","modified_gmt":"2020-07-26T05:55:20","slug":"ffmpeg-and-code-coverage-tools","status":"publish","type":"post","link":"https:\/\/multimedia.cx\/eggs\/ffmpeg-and-code-coverage-tools\/","title":{"rendered":"FFmpeg and Code Coverage Tools"},"content":{"rendered":"<p>Code coverage tools likely occupy the same niche as profiling tools: Tools that you&#8217;re <em>supposed<\/em> to use somewhere during the software engineering process but probably never quite get around to it, usually because you&#8217;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 <a href=\"http:\/\/ffmpeg.org\/\">FFmpeg<\/a> project, would be curious to know how much code is being exercised, especially since many of the FATE test specs explicitly claim to be &#8220;exercising XYZ subsystem&#8221;.<\/p>\n<p>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&#8217;m sure I was just overlooking some more obvious solutions about which you all will be happy to enlighten me.<\/p>\n<p><strong>Results<\/strong><br \/>\nI&#8217;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. <a href=\"http:\/\/spreadsheets.google.com\/pub?key=0AjHexWy1UYqidDhwXzctSDA2NjZCNFVaTXFfTldUV2c&#038;hl=en&#038;output=html\">This Google spreadsheet<\/a> contains 3 sheets: The <a href=\"http:\/\/spreadsheets.google.com\/pub?key=0AjHexWy1UYqidDhwXzctSDA2NjZCNFVaTXFfTldUV2c&#038;hl=en&#038;gid=0\">first contains code coverage stats<\/a> 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 <a href=\"http:\/\/spreadsheets.google.com\/pub?key=0AjHexWy1UYqidDhwXzctSDA2NjZCNFVaTXFfTldUV2c&#038;hl=en&#038;gid=1\">second sheet has files<\/a> for which no stats were generated. The <a href=\"http:\/\/spreadsheets.google.com\/pub?key=0AjHexWy1UYqidDhwXzctSDA2NjZCNFVaTXFfTldUV2c&#038;hl=en&#038;gid=2\">third sheet has &#8220;problems&#8221;<\/a>. These files were rejected by my ad-hoc script.<\/p>\n<p><a href=\"http:\/\/spreadsheets.google.com\/pub?key=0AjHexWy1UYqidDhwXzctSDA2NjZCNFVaTXFfTldUV2c&#038;hl=en&#038;gid=0&#038;output=csv\">Here&#8217;s a link to the data in CSV<\/a> if you want to play with it yourself.<\/p>\n<p><strong>Using gcov with FFmpeg<\/strong> <!--more--><br \/>\nTo 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:<\/p>\n<pre>\r\n  .\/configure \\\r\n    --extra-cflags=\"-fprofile-arcs -ftest-coverage\" \\\r\n    --extra-ldflags=\"-fprofile-arcs -ftest-coverage\"\r\n<\/pre>\n<p>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 <code>'gcov sourcefile.c'<\/code>. 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.<\/p>\n<p>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 <code>'-o, --object-directory'<\/code> option.<\/p>\n<p><strong>Resetting Statistics<\/strong><br \/>\nStatistics in the .gcda files are cumulative. Should you wish to reset the statistics, doing this in the build directory should suffice:<\/p>\n<pre>\r\n  find . -name \"*.gcda\" | xargs rm -f\r\n<\/pre>\n<p><strong>Getting Project-Wide Data<\/strong><br \/>\nAs 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,<\/p>\n<pre>\r\nfor file in `find . -name \"*.c\"` \\\r\ndo \\\r\n  echo \"*****\" $file \\\r\n  gcov -o `dirname $file` `basename $file` \\\r\ndone > ffmpeg-code-coverage.txt 2>&1\r\n<\/pre>\n<p>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.<\/p>\n<p><strong>Further Work<\/strong><br \/>\nI&#8217;m sure there are better ways to do this, and I&#8217;m sure you all will let me know what they are. But I have to get the ball rolling somehow.<\/p>\n<p>There&#8217;s also <a href=\"http:\/\/testcocoon.org\/\">TestCocoon<\/a>. I&#8217;d like to try that program and see if it addresses some of gcov&#8217;s shortcomings (assuming they are indeed shortcomings rather than oversights).<\/p>\n<p><strong>Source for script: process-gcov-slop.py<\/strong><\/p>\n<p><script src=\"https:\/\/gist.github.com\/multimediamike\/ab76f189535dbbe025357ee201f0bacc.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learning how to apply the GNU gcov tool against FFmpeg<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101,55],"tags":[],"class_list":["post-2761","post","type-post","status-publish","format-standard","hentry","category-fate-server","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/2761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/comments?post=2761"}],"version-history":[{"count":11,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/2761\/revisions"}],"predecessor-version":[{"id":4606,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/2761\/revisions\/4606"}],"wp:attachment":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/media?parent=2761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/categories?post=2761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/tags?post=2761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}