Skip to content

Commit

Permalink
Added coverage-sort to scripts/coverage.py
Browse files Browse the repository at this point in the history
scripts/coverage.py was missed originally because it's not ran as often
as the others. Since it requires run-time info, it's usually only used
in CI.
  • Loading branch information
geky committed Mar 11, 2022
1 parent f5286ab commit 20c58dc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ override CFLAGS += -Wextra -Wshadow -Wjump-misses-init -Wundef

ifdef VERBOSE
override TESTFLAGS += -v
override CALLSFLAGS += -v
override CODEFLAGS += -v
override DATAFLAGS += -v
override COVERAGEFLAGS += -v
Expand All @@ -53,14 +54,18 @@ override TESTFLAGS += --exec="$(EXEC)"
endif
ifdef BUILDDIR
override TESTFLAGS += --build-dir="$(BUILDDIR:/=)"
override CALLSFLAGS += --build-dir="$(BUILDDIR:/=)"
override CODEFLAGS += --build-dir="$(BUILDDIR:/=)"
override DATAFLAGS += --build-dir="$(BUILDDIR:/=)"
override CALLSFLAGS += --build-dir="$(BUILDDIR:/=)"
override COVERAGEFLAGS += --build-dir="$(BUILDDIR:/=)"
endif
ifneq ($(NM),nm)
override CODEFLAGS += --nm-tool="$(NM)"
override DATAFLAGS += --nm-tool="$(NM)"
endif
override CODEFLAGS += -S
override DATAFLAGS += -S
override COVERAGEFLAGS += -s


# commands
Expand All @@ -80,11 +85,11 @@ tags:

.PHONY: code
code: $(OBJ)
./scripts/code.py -S $^ $(CODEFLAGS)
./scripts/code.py $^ $(CODEFLAGS)

.PHONY: data
data: $(OBJ)
./scripts/data.py -S $^ $(DATAFLAGS)
./scripts/data.py $^ $(DATAFLAGS)

.PHONY: calls
calls: $(CGI)
Expand Down
28 changes: 25 additions & 3 deletions scripts/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ def diff_entries(olds, news):
- (old_hits/old_count if old_count else 1.0)))
return diff

def sorted_entries(entries):
if args.get('coverage_sort'):
return sorted(entries, key=lambda x: (-(x[1][0]/x[1][1] if x[1][1] else -1), x))
elif args.get('reverse_coverage_sort'):
return sorted(entries, key=lambda x: (+(x[1][0]/x[1][1] if x[1][1] else -1), x))
else:
return sorted(entries)

def sorted_diff_entries(entries):
if args.get('coverage_sort'):
return sorted(entries, key=lambda x: (-(x[1][2]/x[1][3] if x[1][3] else -1), x))
elif args.get('reverse_coverage_sort'):
return sorted(entries, key=lambda x: (+(x[1][2]/x[1][3] if x[1][3] else -1), x))
else:
return sorted(entries, key=lambda x: (-x[1][6], x))

def print_header(by=''):
if not args.get('diff'):
print('%-36s %19s' % (by, 'hits/line'))
Expand All @@ -159,7 +175,7 @@ def print_entries(by='function'):

if not args.get('diff'):
print_header(by=by)
for name, (hits, count) in sorted(entries.items()):
for name, (hits, count) in sorted_entries(entries.items()):
print("%-36s %11s %7s" % (name,
'%d/%d' % (hits, count)
if count else '-',
Expand All @@ -174,8 +190,7 @@ def print_entries(by='function'):
for name, (
old_hits, old_count,
new_hits, new_count,
diff_hits, diff_count, ratio) in sorted(diff.items(),
key=lambda x: (-x[1][6], x)):
diff_hits, diff_count, ratio) in sorted_diff_entries(diff.items()):
if ratio or args.get('all'):
print("%-36s %11s %7s %11s %7s %11s%s" % (name,
'%d/%d' % (old_hits, old_count)
Expand Down Expand Up @@ -248,10 +263,17 @@ def print_totals():
help="Show all functions, not just the ones that changed.")
parser.add_argument('-A', '--everything', action='store_true',
help="Include builtin and libc specific symbols.")
parser.add_argument('-s', '--coverage-sort', action='store_true',
help="Sort by coverage.")
parser.add_argument('-S', '--reverse-coverage-sort', action='store_true',
help="Sort by coverage, but backwards.")
parser.add_argument('--files', action='store_true',
help="Show file-level coverage.")
parser.add_argument('--summary', action='store_true',
help="Only show the total coverage.")
parser.add_argument('-q', '--quiet', action='store_true',
help="Don't show anything, useful with -o.")
parser.add_argument('--build-dir',
help="Specify the relative build directory. Used to map object files \
to the correct source files.")
sys.exit(main(**vars(parser.parse_args())))

0 comments on commit 20c58dc

Please sign in to comment.