coverage.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #!/usr/bin/env python3
  2. #
  3. # Parse and report coverage info from .info files generated by lcov
  4. #
  5. import os
  6. import glob
  7. import csv
  8. import re
  9. import collections as co
  10. import bisect as b
  11. INFO_PATHS = ['tests/*.toml.info']
  12. def collect(paths, **args):
  13. file = None
  14. funcs = []
  15. lines = co.defaultdict(lambda: 0)
  16. pattern = re.compile(
  17. '^(?P<file>SF:/?(?P<file_name>.*))$'
  18. '|^(?P<func>FN:(?P<func_lineno>[0-9]*),(?P<func_name>.*))$'
  19. '|^(?P<line>DA:(?P<line_lineno>[0-9]*),(?P<line_hits>[0-9]*))$')
  20. for path in paths:
  21. with open(path) as f:
  22. for line in f:
  23. m = pattern.match(line)
  24. if m and m.group('file'):
  25. file = m.group('file_name')
  26. elif m and file and m.group('func'):
  27. funcs.append((file, int(m.group('func_lineno')),
  28. m.group('func_name')))
  29. elif m and file and m.group('line'):
  30. lines[(file, int(m.group('line_lineno')))] += (
  31. int(m.group('line_hits')))
  32. # map line numbers to functions
  33. funcs.sort()
  34. def func_from_lineno(file, lineno):
  35. i = b.bisect(funcs, (file, lineno))
  36. if i and funcs[i-1][0] == file:
  37. return funcs[i-1][2]
  38. else:
  39. return None
  40. # reduce to function info
  41. reduced_funcs = co.defaultdict(lambda: (0, 0))
  42. for (file, line_lineno), line_hits in lines.items():
  43. func = func_from_lineno(file, line_lineno)
  44. if not func:
  45. continue
  46. hits, count = reduced_funcs[(file, func)]
  47. reduced_funcs[(file, func)] = (hits + (line_hits > 0), count + 1)
  48. results = []
  49. for (file, func), (hits, count) in reduced_funcs.items():
  50. # discard internal/testing functions (test_* injected with
  51. # internal testing)
  52. if not args.get('everything'):
  53. if func.startswith('__') or func.startswith('test_'):
  54. continue
  55. # discard .8449 suffixes created by optimizer
  56. func = re.sub('\.[0-9]+', '', func)
  57. results.append((file, func, hits, count))
  58. return results
  59. def main(**args):
  60. # find coverage
  61. if not args.get('use'):
  62. # find *.info files
  63. paths = []
  64. for path in args['info_paths']:
  65. if os.path.isdir(path):
  66. path = path + '/*.gcov'
  67. for path in glob.glob(path):
  68. paths.append(path)
  69. if not paths:
  70. print('no .info files found in %r?' % args['info_paths'])
  71. sys.exit(-1)
  72. results = collect(paths, **args)
  73. else:
  74. with open(args['use']) as f:
  75. r = csv.DictReader(f)
  76. results = [
  77. ( result['file'],
  78. result['function'],
  79. int(result['coverage_hits']),
  80. int(result['coverage_count']))
  81. for result in r]
  82. total_hits, total_count = 0, 0
  83. for _, _, hits, count in results:
  84. total_hits += hits
  85. total_count += count
  86. # find previous results?
  87. if args.get('diff'):
  88. with open(args['diff']) as f:
  89. r = csv.DictReader(f)
  90. prev_results = [
  91. ( result['file'],
  92. result['function'],
  93. int(result['coverage_hits']),
  94. int(result['coverage_count']))
  95. for result in r]
  96. prev_total_hits, prev_total_count = 0, 0
  97. for _, _, hits, count in prev_results:
  98. prev_total_hits += hits
  99. prev_total_count += count
  100. # write results to CSV
  101. if args.get('output'):
  102. with open(args['output'], 'w') as f:
  103. w = csv.writer(f)
  104. w.writerow(['file', 'function', 'coverage_hits', 'coverage_count'])
  105. for file, func, hits, count in sorted(results):
  106. w.writerow((file, func, hits, count))
  107. # print results
  108. def dedup_entries(results, by='function'):
  109. entries = co.defaultdict(lambda: (0, 0))
  110. for file, func, hits, count in results:
  111. entry = (file if by == 'file' else func)
  112. entry_hits, entry_count = entries[entry]
  113. entries[entry] = (entry_hits + hits, entry_count + count)
  114. return entries
  115. def diff_entries(olds, news):
  116. diff = co.defaultdict(lambda: (0, 0, 0, 0, 0, 0, 0))
  117. for name, (new_hits, new_count) in news.items():
  118. diff[name] = (
  119. 0, 0,
  120. new_hits, new_count,
  121. new_hits, new_count,
  122. (new_hits/new_count if new_count else 1.0) - 1.0)
  123. for name, (old_hits, old_count) in olds.items():
  124. _, _, new_hits, new_count, _, _, _ = diff[name]
  125. diff[name] = (
  126. old_hits, old_count,
  127. new_hits, new_count,
  128. new_hits-old_hits, new_count-old_count,
  129. ((new_hits/new_count if new_count else 1.0)
  130. - (old_hits/old_count if old_count else 1.0)))
  131. return diff
  132. def sorted_entries(entries):
  133. if args.get('coverage_sort'):
  134. return sorted(entries, key=lambda x: (-(x[1][0]/x[1][1] if x[1][1] else -1), x))
  135. elif args.get('reverse_coverage_sort'):
  136. return sorted(entries, key=lambda x: (+(x[1][0]/x[1][1] if x[1][1] else -1), x))
  137. else:
  138. return sorted(entries)
  139. def sorted_diff_entries(entries):
  140. if args.get('coverage_sort'):
  141. return sorted(entries, key=lambda x: (-(x[1][2]/x[1][3] if x[1][3] else -1), x))
  142. elif args.get('reverse_coverage_sort'):
  143. return sorted(entries, key=lambda x: (+(x[1][2]/x[1][3] if x[1][3] else -1), x))
  144. else:
  145. return sorted(entries, key=lambda x: (-x[1][6], x))
  146. def print_header(by=''):
  147. if not args.get('diff'):
  148. print('%-36s %19s' % (by, 'hits/line'))
  149. else:
  150. print('%-36s %19s %19s %11s' % (by, 'old', 'new', 'diff'))
  151. def print_entries(by='function'):
  152. entries = dedup_entries(results, by=by)
  153. if not args.get('diff'):
  154. print_header(by=by)
  155. for name, (hits, count) in sorted_entries(entries.items()):
  156. print("%-36s %11s %7s" % (name,
  157. '%d/%d' % (hits, count)
  158. if count else '-',
  159. '%.1f%%' % (100*hits/count)
  160. if count else '-'))
  161. else:
  162. prev_entries = dedup_entries(prev_results, by=by)
  163. diff = diff_entries(prev_entries, entries)
  164. print_header(by='%s (%d added, %d removed)' % (by,
  165. sum(1 for _, old, _, _, _, _, _ in diff.values() if not old),
  166. sum(1 for _, _, _, new, _, _, _ in diff.values() if not new)))
  167. for name, (
  168. old_hits, old_count,
  169. new_hits, new_count,
  170. diff_hits, diff_count, ratio) in sorted_diff_entries(
  171. diff.items()):
  172. if ratio or args.get('all'):
  173. print("%-36s %11s %7s %11s %7s %11s%s" % (name,
  174. '%d/%d' % (old_hits, old_count)
  175. if old_count else '-',
  176. '%.1f%%' % (100*old_hits/old_count)
  177. if old_count else '-',
  178. '%d/%d' % (new_hits, new_count)
  179. if new_count else '-',
  180. '%.1f%%' % (100*new_hits/new_count)
  181. if new_count else '-',
  182. '%+d/%+d' % (diff_hits, diff_count),
  183. ' (%+.1f%%)' % (100*ratio) if ratio else ''))
  184. def print_totals():
  185. if not args.get('diff'):
  186. print("%-36s %11s %7s" % ('TOTAL',
  187. '%d/%d' % (total_hits, total_count)
  188. if total_count else '-',
  189. '%.1f%%' % (100*total_hits/total_count)
  190. if total_count else '-'))
  191. else:
  192. ratio = ((total_hits/total_count
  193. if total_count else 1.0)
  194. - (prev_total_hits/prev_total_count
  195. if prev_total_count else 1.0))
  196. print("%-36s %11s %7s %11s %7s %11s%s" % ('TOTAL',
  197. '%d/%d' % (prev_total_hits, prev_total_count)
  198. if prev_total_count else '-',
  199. '%.1f%%' % (100*prev_total_hits/prev_total_count)
  200. if prev_total_count else '-',
  201. '%d/%d' % (total_hits, total_count)
  202. if total_count else '-',
  203. '%.1f%%' % (100*total_hits/total_count)
  204. if total_count else '-',
  205. '%+d/%+d' % (total_hits-prev_total_hits,
  206. total_count-prev_total_count),
  207. ' (%+.1f%%)' % (100*ratio) if ratio else ''))
  208. if args.get('quiet'):
  209. pass
  210. elif args.get('summary'):
  211. print_header()
  212. print_totals()
  213. elif args.get('files'):
  214. print_entries(by='file')
  215. print_totals()
  216. else:
  217. print_entries(by='function')
  218. print_totals()
  219. if __name__ == "__main__":
  220. import argparse
  221. import sys
  222. parser = argparse.ArgumentParser(
  223. description="Parse and report coverage info from .info files \
  224. generated by lcov")
  225. parser.add_argument('info_paths', nargs='*', default=INFO_PATHS,
  226. help="Description of where to find *.info files. May be a directory \
  227. or list of paths. *.info files will be merged to show the total \
  228. coverage. Defaults to %r." % INFO_PATHS)
  229. parser.add_argument('-v', '--verbose', action='store_true',
  230. help="Output commands that run behind the scenes.")
  231. parser.add_argument('-o', '--output',
  232. help="Specify CSV file to store results.")
  233. parser.add_argument('-u', '--use',
  234. help="Don't do any work, instead use this CSV file.")
  235. parser.add_argument('-d', '--diff',
  236. help="Specify CSV file to diff code size against.")
  237. parser.add_argument('-a', '--all', action='store_true',
  238. help="Show all functions, not just the ones that changed.")
  239. parser.add_argument('-A', '--everything', action='store_true',
  240. help="Include builtin and libc specific symbols.")
  241. parser.add_argument('-s', '--coverage-sort', action='store_true',
  242. help="Sort by coverage.")
  243. parser.add_argument('-S', '--reverse-coverage-sort', action='store_true',
  244. help="Sort by coverage, but backwards.")
  245. parser.add_argument('--files', action='store_true',
  246. help="Show file-level coverage.")
  247. parser.add_argument('--summary', action='store_true',
  248. help="Only show the total coverage.")
  249. parser.add_argument('-q', '--quiet', action='store_true',
  250. help="Don't show anything, useful with -o.")
  251. parser.add_argument('--build-dir',
  252. help="Specify the relative build directory. Used to map object files \
  253. to the correct source files.")
  254. sys.exit(main(**vars(parser.parse_args())))