coverage.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. def openio(path, mode='r'):
  61. if path == '-':
  62. if 'r' in mode:
  63. return os.fdopen(os.dup(sys.stdin.fileno()), 'r')
  64. else:
  65. return os.fdopen(os.dup(sys.stdout.fileno()), 'w')
  66. else:
  67. return open(path, mode)
  68. # find coverage
  69. if not args.get('use'):
  70. # find *.info files
  71. paths = []
  72. for path in args['info_paths']:
  73. if os.path.isdir(path):
  74. path = path + '/*.gcov'
  75. for path in glob.glob(path):
  76. paths.append(path)
  77. if not paths:
  78. print('no .info files found in %r?' % args['info_paths'])
  79. sys.exit(-1)
  80. results = collect(paths, **args)
  81. else:
  82. with openio(args['use']) as f:
  83. r = csv.DictReader(f)
  84. results = [
  85. ( result['file'],
  86. result['name'],
  87. int(result['coverage_hits']),
  88. int(result['coverage_count']))
  89. for result in r
  90. if result.get('coverage_hits') not in {None, ''}
  91. if result.get('coverage_count') not in {None, ''}]
  92. total_hits, total_count = 0, 0
  93. for _, _, hits, count in results:
  94. total_hits += hits
  95. total_count += count
  96. # find previous results?
  97. if args.get('diff'):
  98. try:
  99. with openio(args['diff']) as f:
  100. r = csv.DictReader(f)
  101. prev_results = [
  102. ( result['file'],
  103. result['name'],
  104. int(result['coverage_hits']),
  105. int(result['coverage_count']))
  106. for result in r
  107. if result.get('coverage_hits') not in {None, ''}
  108. if result.get('coverage_count') not in {None, ''}]
  109. except FileNotFoundError:
  110. prev_results = []
  111. prev_total_hits, prev_total_count = 0, 0
  112. for _, _, hits, count in prev_results:
  113. prev_total_hits += hits
  114. prev_total_count += count
  115. # write results to CSV
  116. if args.get('output'):
  117. merged_results = co.defaultdict(lambda: {})
  118. other_fields = []
  119. # merge?
  120. if args.get('merge'):
  121. try:
  122. with openio(args['merge']) as f:
  123. r = csv.DictReader(f)
  124. for result in r:
  125. file = result.pop('file', '')
  126. func = result.pop('name', '')
  127. result.pop('coverage_hits', None)
  128. result.pop('coverage_count', None)
  129. merged_results[(file, func)] = result
  130. other_fields = result.keys()
  131. except FileNotFoundError:
  132. pass
  133. for file, func, hits, count in results:
  134. merged_results[(file, func)]['coverage_hits'] = hits
  135. merged_results[(file, func)]['coverage_count'] = count
  136. with openio(args['output'], 'w') as f:
  137. w = csv.DictWriter(f, ['file', 'name', *other_fields, 'coverage_hits', 'coverage_count'])
  138. w.writeheader()
  139. for (file, func), result in sorted(merged_results.items()):
  140. w.writerow({'file': file, 'name': func, **result})
  141. # print results
  142. def dedup_entries(results, by='name'):
  143. entries = co.defaultdict(lambda: (0, 0))
  144. for file, func, hits, count in results:
  145. entry = (file if by == 'file' else func)
  146. entry_hits, entry_count = entries[entry]
  147. entries[entry] = (entry_hits + hits, entry_count + count)
  148. return entries
  149. def diff_entries(olds, news):
  150. diff = co.defaultdict(lambda: (0, 0, 0, 0, 0, 0, 0))
  151. for name, (new_hits, new_count) in news.items():
  152. diff[name] = (
  153. 0, 0,
  154. new_hits, new_count,
  155. new_hits, new_count,
  156. (new_hits/new_count if new_count else 1.0) - 1.0)
  157. for name, (old_hits, old_count) in olds.items():
  158. _, _, new_hits, new_count, _, _, _ = diff[name]
  159. diff[name] = (
  160. old_hits, old_count,
  161. new_hits, new_count,
  162. new_hits-old_hits, new_count-old_count,
  163. ((new_hits/new_count if new_count else 1.0)
  164. - (old_hits/old_count if old_count else 1.0)))
  165. return diff
  166. def sorted_entries(entries):
  167. if args.get('coverage_sort'):
  168. return sorted(entries, key=lambda x: (-(x[1][0]/x[1][1] if x[1][1] else -1), x))
  169. elif args.get('reverse_coverage_sort'):
  170. return sorted(entries, key=lambda x: (+(x[1][0]/x[1][1] if x[1][1] else -1), x))
  171. else:
  172. return sorted(entries)
  173. def sorted_diff_entries(entries):
  174. if args.get('coverage_sort'):
  175. return sorted(entries, key=lambda x: (-(x[1][2]/x[1][3] if x[1][3] else -1), x))
  176. elif args.get('reverse_coverage_sort'):
  177. return sorted(entries, key=lambda x: (+(x[1][2]/x[1][3] if x[1][3] else -1), x))
  178. else:
  179. return sorted(entries, key=lambda x: (-x[1][6], x))
  180. def print_header(by=''):
  181. if not args.get('diff'):
  182. print('%-36s %19s' % (by, 'hits/line'))
  183. else:
  184. print('%-36s %19s %19s %11s' % (by, 'old', 'new', 'diff'))
  185. def print_entry(name, hits, count):
  186. print("%-36s %11s %7s" % (name,
  187. '%d/%d' % (hits, count)
  188. if count else '-',
  189. '%.1f%%' % (100*hits/count)
  190. if count else '-'))
  191. def print_diff_entry(name,
  192. old_hits, old_count,
  193. new_hits, new_count,
  194. diff_hits, diff_count,
  195. ratio):
  196. print("%-36s %11s %7s %11s %7s %11s%s" % (name,
  197. '%d/%d' % (old_hits, old_count)
  198. if old_count else '-',
  199. '%.1f%%' % (100*old_hits/old_count)
  200. if old_count else '-',
  201. '%d/%d' % (new_hits, new_count)
  202. if new_count else '-',
  203. '%.1f%%' % (100*new_hits/new_count)
  204. if new_count else '-',
  205. '%+d/%+d' % (diff_hits, diff_count),
  206. ' (%+.1f%%)' % (100*ratio) if ratio else ''))
  207. def print_entries(by='name'):
  208. entries = dedup_entries(results, by=by)
  209. if not args.get('diff'):
  210. print_header(by=by)
  211. for name, (hits, count) in sorted_entries(entries.items()):
  212. print_entry(name, hits, count)
  213. else:
  214. prev_entries = dedup_entries(prev_results, by=by)
  215. diff = diff_entries(prev_entries, entries)
  216. print_header(by='%s (%d added, %d removed)' % (by,
  217. sum(1 for _, old, _, _, _, _, _ in diff.values() if not old),
  218. sum(1 for _, _, _, new, _, _, _ in diff.values() if not new)))
  219. for name, (
  220. old_hits, old_count,
  221. new_hits, new_count,
  222. diff_hits, diff_count, ratio) in sorted_diff_entries(
  223. diff.items()):
  224. if ratio or args.get('all'):
  225. print_diff_entry(name,
  226. old_hits, old_count,
  227. new_hits, new_count,
  228. diff_hits, diff_count,
  229. ratio)
  230. def print_totals():
  231. if not args.get('diff'):
  232. print_entry('TOTAL', total_hits, total_count)
  233. else:
  234. ratio = ((total_hits/total_count
  235. if total_count else 1.0)
  236. - (prev_total_hits/prev_total_count
  237. if prev_total_count else 1.0))
  238. print_diff_entry('TOTAL',
  239. prev_total_hits, prev_total_count,
  240. total_hits, total_count,
  241. total_hits-prev_total_hits, total_count-prev_total_count,
  242. ratio)
  243. if args.get('quiet'):
  244. pass
  245. elif args.get('summary'):
  246. print_header()
  247. print_totals()
  248. elif args.get('files'):
  249. print_entries(by='file')
  250. print_totals()
  251. else:
  252. print_entries(by='name')
  253. print_totals()
  254. if __name__ == "__main__":
  255. import argparse
  256. import sys
  257. parser = argparse.ArgumentParser(
  258. description="Parse and report coverage info from .info files \
  259. generated by lcov")
  260. parser.add_argument('info_paths', nargs='*', default=INFO_PATHS,
  261. help="Description of where to find *.info files. May be a directory \
  262. or list of paths. *.info files will be merged to show the total \
  263. coverage. Defaults to %r." % INFO_PATHS)
  264. parser.add_argument('-v', '--verbose', action='store_true',
  265. help="Output commands that run behind the scenes.")
  266. parser.add_argument('-o', '--output',
  267. help="Specify CSV file to store results.")
  268. parser.add_argument('-u', '--use',
  269. help="Don't do any work, instead use this CSV file.")
  270. parser.add_argument('-d', '--diff',
  271. help="Specify CSV file to diff code size against.")
  272. parser.add_argument('-m', '--merge',
  273. help="Merge with an existing CSV file when writing to output.")
  274. parser.add_argument('-a', '--all', action='store_true',
  275. help="Show all functions, not just the ones that changed.")
  276. parser.add_argument('-A', '--everything', action='store_true',
  277. help="Include builtin and libc specific symbols.")
  278. parser.add_argument('-s', '--coverage-sort', action='store_true',
  279. help="Sort by coverage.")
  280. parser.add_argument('-S', '--reverse-coverage-sort', action='store_true',
  281. help="Sort by coverage, but backwards.")
  282. parser.add_argument('-F', '--files', action='store_true',
  283. help="Show file-level coverage.")
  284. parser.add_argument('-Y', '--summary', action='store_true',
  285. help="Only show the total coverage.")
  286. parser.add_argument('-q', '--quiet', action='store_true',
  287. help="Don't show anything, useful with -o.")
  288. parser.add_argument('--build-dir',
  289. help="Specify the relative build directory. Used to map object files \
  290. to the correct source files.")
  291. sys.exit(main(**vars(parser.parse_args())))