| 1 |
=============== |
| 2 |
C coverage tool |
| 3 |
=============== |
| 4 |
|
| 5 |
This directory contains a tool to generate C code-coverage reports |
| 6 |
using valgrind's callgrind tool. |
| 7 |
|
| 8 |
Prerequisites |
| 9 |
------------- |
| 10 |
|
| 11 |
* `Valgrind <https://valgrind.org/>`_ (3.5.0 tested, earlier |
| 12 |
versions may work) |
| 13 |
|
| 14 |
* `Pygments <https://pygments.org/>`_ (0.11 or later required) |
| 15 |
|
| 16 |
C code-coverage |
| 17 |
--------------- |
| 18 |
|
| 19 |
Generating C code coverage reports requires two steps: |
| 20 |
|
| 21 |
* Collecting coverage results (from valgrind) |
| 22 |
|
| 23 |
* Generating a report from one or more sets of results |
| 24 |
|
| 25 |
For most cases, it is good enough to do:: |
| 26 |
|
| 27 |
> c_coverage_collect.sh python -c "import numpy; numpy.test()" |
| 28 |
> c_coverage_report.py callgrind.out.pid |
| 29 |
|
| 30 |
which will run all of the NumPy unit tests, create a directory called |
| 31 |
`coverage` and place the coverage results there. |
| 32 |
|
| 33 |
In a more advanced scenario, you may wish to run individual unit tests |
| 34 |
(since running under valgrind slows things down) and combine multiple |
| 35 |
results files together in a single report. |
| 36 |
|
| 37 |
Collecting results |
| 38 |
`````````````````` |
| 39 |
|
| 40 |
To collect coverage results, you merely run the python interpreter |
| 41 |
under valgrind's callgrind tool. The `c_coverage_collect.sh` helper |
| 42 |
script will pass all of the required arguments to valgrind. |
| 43 |
|
| 44 |
For example, in typical usage, you may want to run all of the NumPy |
| 45 |
unit tests:: |
| 46 |
|
| 47 |
> c_coverage_collect.sh python -c "import numpy; numpy.test()" |
| 48 |
|
| 49 |
This will output a file ``callgrind.out.pid`` containing the results of |
| 50 |
the run, where ``pid`` is the process id of the run. |
| 51 |
|
| 52 |
Generating a report |
| 53 |
``````````````````` |
| 54 |
|
| 55 |
To generate a report, you pass the ``callgrind.out.pid`` output file to |
| 56 |
the `c_coverage_report.py` script:: |
| 57 |
|
| 58 |
> c_coverage_report.py callgrind.out.pid |
| 59 |
|
| 60 |
To combine multiple results files together, simply list them on the |
| 61 |
commandline or use wildcards:: |
| 62 |
|
| 63 |
> c_coverage_report.py callgrind.out.* |
| 64 |
|
| 65 |
Options |
| 66 |
''''''' |
| 67 |
|
| 68 |
* ``--directory``: Specify a different output directory |
| 69 |
|
| 70 |
* ``--pattern``: Specify a regex pattern to match for source files. |
| 71 |
The default is `numpy`, so it will only include source files whose |
| 72 |
path contains the string `numpy`. If, for instance, you wanted to |
| 73 |
include all source files covered (that are available on your |
| 74 |
system), pass ``--pattern=.``. |
| 75 |
|
| 76 |
* ``--format``: Specify the output format(s) to generate. May be |
| 77 |
either ``text`` or ``html``. If ``--format`` is not provided, |
| 78 |
both formats will be output. |
| 79 |
|
| 80 |
Reading a report |
| 81 |
---------------- |
| 82 |
|
| 83 |
The C code coverage report is a flat directory of files, containing |
| 84 |
text and/or html files. The files are named based on their path in |
| 85 |
the original source tree with slashes converted to underscores. |
| 86 |
|
| 87 |
Text reports |
| 88 |
```````````` |
| 89 |
|
| 90 |
The text reports add a prefix to each line of source code: |
| 91 |
|
| 92 |
- '>' indicates the line of code was run |
| 93 |
|
| 94 |
- '!' indicates the line of code was not run |
| 95 |
|
| 96 |
HTML reports |
| 97 |
```````````` |
| 98 |
|
| 99 |
The HTML report highlights the code that was run in green. |
| 100 |
|
| 101 |
The HTML report has special support for the "generated" functions in |
| 102 |
NumPy. Each run line of code also contains a number in square |
| 103 |
brackets indicating the number of different generated functions the |
| 104 |
line was run in. Hovering the mouse over the line will display a list |
| 105 |
of the versions of the function in which the line was run. These |
| 106 |
numbers can be used to see if a particular line was run in all |
| 107 |
versions of the function. |
| 108 |
|
| 109 |
Caveats |
| 110 |
------- |
| 111 |
|
| 112 |
The coverage results occasionally misses lines that clearly must have |
| 113 |
been run. This usually can be traced back to the compiler optimizer |
| 114 |
removing lines because they are tautologically impossible or to |
| 115 |
combine lines together. Compiling NumPy without optimizations helps, |
| 116 |
but not completely. Even despite this flaw, this tool is still |
| 117 |
helpful in identifying large missed blocks or functions. |