cg_annotate: Remove support for user-annotated files.

They're of little use, and removing them opens the possibility of adding
`cg_merge`'s profile-merging functionality into `cg_annotate` itself.
This commit is contained in:
Nicholas Nethercote 2023-03-29 07:19:07 +11:00
parent 3d0d7a1924
commit 004ba8c0da
11 changed files with 85 additions and 151 deletions

View File

@ -42,7 +42,7 @@ import re
import sys
from argparse import ArgumentParser, BooleanOptionalAction, Namespace
from collections import defaultdict
from typing import Callable, DefaultDict, NewType, NoReturn, TextIO
from typing import DefaultDict, NewType, NoReturn, TextIO
class Args(Namespace):
@ -56,11 +56,10 @@ class Args(Namespace):
sort: list[str]
threshold: float # a percentage
show_percs: bool
auto: bool
annotate: bool
context: int
include: list[str]
cgout_filename: list[str]
src_filenames: list[str]
@staticmethod
def parse() -> Args:
@ -73,7 +72,9 @@ class Args(Namespace):
return f
raise ValueError
def add_bool_argument(p: ArgumentParser, name: str, help: str) -> None:
def add_bool_argument(
p: ArgumentParser, new_name: str, old_name: str, help: str
) -> None:
"""
Add a bool argument that defaults to true.
@ -81,28 +82,29 @@ class Args(Namespace):
The latter two were the forms supported by the old Perl version of
`cg_annotate`, and are now deprecated.
"""
flag = "--" + name
dest = name.replace("-", "_")
new_flag = "--" + new_name
old_flag = "--" + old_name
dest = new_name.replace("-", "_")
# Note: the default value is always printed with `BooleanOptionalAction`,
# due to an argparse bug: https://github.com/python/cpython/issues/83137.
p.add_argument(
flag,
new_flag,
default=True,
action=BooleanOptionalAction,
help=help,
)
p.add_argument(
f"{flag}=yes",
f"{old_flag}=yes",
dest=dest,
action="store_true",
help=f"(deprecated) same as --{name}",
help=f"(deprecated) same as --{new_name}",
)
p.add_argument(
f"{flag}=no",
f"{old_flag}=no",
dest=dest,
action="store_false",
help=f"(deprecated) same as --no-{name}",
help=f"(deprecated) same as --no-{new_name}",
)
p = ArgumentParser(description="Process a Cachegrind output file.")
@ -134,10 +136,12 @@ class Args(Namespace):
add_bool_argument(
p,
"show-percs",
"show-percs",
"show a percentage for each non-zero count",
)
add_bool_argument(
p,
"annotate",
"auto",
"annotate all source files containing functions that reached the "
"event count threshold",
@ -164,12 +168,6 @@ class Args(Namespace):
metavar="cachegrind-out-file",
help="file produced by Cachegrind",
)
p.add_argument(
"src_filenames",
nargs="*",
metavar="source-files",
help="source files to annotate (usually not needed due to --auto)",
)
return p.parse_args(namespace=Args())
@ -527,14 +525,7 @@ def print_cachegrind_profile(desc: str, cmd: str, events: Events) -> None:
for include_dirname in args.include[1:]:
print(f" {include_dirname}")
if len(args.src_filenames) == 0:
print("User annotated: ")
else:
print(f"User annotated: {args.src_filenames[0]}")
for src_filename in args.src_filenames[1:]:
print(f" {src_filename}")
print("Auto-annotation: ", "on" if args.auto else "off")
print("Annotation: ", "on" if args.annotate else "off")
print()
@ -736,39 +727,31 @@ def print_annotated_src_file(
# This (partially) consumes `dict_fl_dict_line_cc`.
def print_annotated_src_files(
events: Events,
threshold_src_filenames: set[str],
ann_src_filenames: set[str],
dict_fl_dict_line_cc: DictFlDictLineCc,
summary_cc: Cc,
) -> AnnotatedCcs:
annotated_ccs = AnnotatedCcs(events)
def pair_with(label: str) -> Callable[[str], tuple[str, str]]:
return lambda s: (s, label)
def add_dict_line_cc_to_cc(dict_line_cc: DictLineCc | None, accum_cc: Cc) -> None:
if dict_line_cc:
for line_cc in dict_line_cc.values():
accum_cc += line_cc
# If auto-annotating, add interesting files (excluding "???").
all_src_filenames = set(map(pair_with("User"), args.src_filenames))
if args.auto:
threshold_src_filenames.discard("???")
dict_line_cc = dict_fl_dict_line_cc.pop("???", None)
add_dict_line_cc_to_cc(dict_line_cc, annotated_ccs.files_unknown_cc)
all_src_filenames.update(map(pair_with("Auto"), threshold_src_filenames))
# Exclude the unknown ("???") file, which is unannotatable.
ann_src_filenames.discard("???")
dict_line_cc = dict_fl_dict_line_cc.pop("???", None)
add_dict_line_cc_to_cc(dict_line_cc, annotated_ccs.files_unknown_cc)
# Prepend "" to the include dirnames so things work in the case where the
# filename has the full path.
include_dirnames = args.include.copy()
include_dirnames.insert(0, "")
def print_ann_fancy(ann_type: str, src_filename: str) -> None:
print_fancy(f"{ann_type}-annotated source file: {src_filename}")
def print_ann_fancy(src_filename: str) -> None:
print_fancy(f"Annotated source file: {src_filename}")
for src_filename, ann_type in sorted(all_src_filenames):
for src_filename in sorted(ann_src_filenames):
readable = False
for include_dirname in include_dirnames:
if include_dirname == "":
@ -779,21 +762,15 @@ def print_annotated_src_files(
try:
with open(full_src_filename, "r", encoding="utf-8") as src_file:
dict_line_cc = dict_fl_dict_line_cc.pop(src_filename, None)
if dict_line_cc is not None:
print_ann_fancy(ann_type, src_file.name) # includes full path
print_annotated_src_file(
events,
dict_line_cc,
src_file,
annotated_ccs,
summary_cc,
)
else:
# This only happens for user-specified files that are
# readable but not mentioned in the cgout file.
print_ann_fancy(ann_type, src_filename)
print("This file was not mentioned by the data file")
print()
assert dict_line_cc is not None
print_ann_fancy(src_file.name) # includes full path
print_annotated_src_file(
events,
dict_line_cc,
src_file,
annotated_ccs,
summary_cc,
)
readable = True
break
@ -804,7 +781,7 @@ def print_annotated_src_files(
dict_line_cc = dict_fl_dict_line_cc.pop(src_filename, None)
add_dict_line_cc_to_cc(dict_line_cc, annotated_ccs.unreadable_cc)
print_ann_fancy(ann_type, src_filename)
print_ann_fancy(src_filename)
print("This file was unreadable")
print()
@ -821,29 +798,27 @@ def print_annotation_summary(
annotated_ccs: AnnotatedCcs,
summary_cc: Cc,
) -> None:
# If we did any annotating, show how many events were covered by annotated
# lines above.
if args.auto or args.src_filenames:
printer = CcPrinter(events, annotated_ccs.ccs(), summary_cc)
print_fancy("Annotation summary")
printer.print_events("")
print()
# Show how many events were covered by annotated lines above.
printer = CcPrinter(events, annotated_ccs.ccs(), summary_cc)
print_fancy("Annotation summary")
printer.print_events("")
print()
total_cc = events.mk_empty_cc()
for (cc, label) in zip(annotated_ccs.ccs(), AnnotatedCcs.labels):
printer.print_cc(cc, label)
total_cc += cc
total_cc = events.mk_empty_cc()
for (cc, label) in zip(annotated_ccs.ccs(), AnnotatedCcs.labels):
printer.print_cc(cc, label)
total_cc += cc
print()
print()
# Internal sanity check.
if summary_cc != total_cc:
msg = (
"`summary:` line doesn't match computed annotated counts\n"
f"- summary: {summary_cc}\n"
f"- annotated: {total_cc}"
)
die(msg)
# Internal sanity check.
if summary_cc != total_cc:
msg = (
"`summary:` line doesn't match computed annotated counts\n"
f"- summary: {summary_cc}\n"
f"- annotated: {total_cc}"
)
die(msg)
def main() -> None:
@ -862,13 +837,14 @@ def main() -> None:
print_summary(events, summary_cc)
threshold_src_filenames = print_function_summary(events, dict_flfn_cc, summary_cc)
ann_src_filenames = print_function_summary(events, dict_flfn_cc, summary_cc)
annotated_ccs = print_annotated_src_files(
events, threshold_src_filenames, dict_fl_dict_line_cc, summary_cc
)
if args.annotate:
annotated_ccs = print_annotated_src_files(
events, ann_src_filenames, dict_fl_dict_line_cc, summary_cc
)
print_annotation_summary(events, annotated_ccs, summary_cc)
print_annotation_summary(events, annotated_ccs, summary_cc)
if __name__ == "__main__":

View File

@ -24,7 +24,7 @@ EXTRA_DIST = \
ann2.post.exp ann2.stderr.exp ann2.vgtest ann2.cgout \
ann2-basic.rs ann2-more-recent-than-cgout.rs \
ann2-negatives.rs ann2-past-the-end.rs \
ann2-unmentioned.rs ann2-aux/ann2-via-I.rs \
ann2-aux/ann2-via-I.rs \
chdir.vgtest chdir.stderr.exp \
clreq.vgtest clreq.stderr.exp \
dlclose.vgtest dlclose.stderr.exp dlclose.stdout.exp \

View File

@ -9,8 +9,7 @@ Events shown: Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw
Event sort order: Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw
Threshold: 0.1
Include dirs:
User annotated:
Auto-annotation: on
Annotation: on
--------------------------------------------------------------------------------
-- Summary
@ -27,7 +26,7 @@ Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw file:fu
5,000,000 (100.0%) 0 0 -2,000,000 (100.0%) 0 0 0 0 0 a.c:MAIN
--------------------------------------------------------------------------------
-- Auto-annotated source file: a.c
-- Annotated source file: a.c
--------------------------------------------------------------------------------
Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw

View File

@ -9,8 +9,7 @@ Events shown: One Two
Event sort order: One Two
Threshold: 0.1
Include dirs:
User annotated:
Auto-annotation: on
Annotation: on
--------------------------------------------------------------------------------
-- Summary
@ -29,7 +28,7 @@ One Two file:function
100 (4.8%) -100 (-5.3%) aux/ann-diff2-basic.rs:basic1
--------------------------------------------------------------------------------
-- Auto-annotated source file: aux/ann-diff2-basic.rs
-- Annotated source file: aux/ann-diff2-basic.rs
--------------------------------------------------------------------------------
This file was unreadable

View File

@ -10,8 +10,7 @@ Events shown: A B C
Event sort order: A B C
Threshold: 0.1
Include dirs:
User annotated:
Auto-annotation: on
Annotation: on
--------------------------------------------------------------------------------
-- Summary
@ -31,7 +30,7 @@ A B C file:function
10 (11.6%) 5 (4.4%) 0 ann-merge-x.rs:x2
--------------------------------------------------------------------------------
-- Auto-annotated source file: ann-merge-x.rs
-- Annotated source file: ann-merge-x.rs
--------------------------------------------------------------------------------
A B C
@ -42,7 +41,7 @@ A B C
20 (23.3%) 10 (8.8%) 5 (3.4%) five
--------------------------------------------------------------------------------
-- Auto-annotated source file: ann-merge-y.rs
-- Annotated source file: ann-merge-y.rs
--------------------------------------------------------------------------------
A B C

View File

@ -11,8 +11,7 @@ Events shown: Ir I1mr ILmr
Event sort order: Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw
Threshold: 0.1
Include dirs:
User annotated:
Auto-annotation: on
Annotation: on
--------------------------------------------------------------------------------
-- Summary
@ -37,42 +36,42 @@ Ir I1mr ILmr file:function
6,898 2 2 /build/glibc-OTsEL5/glibc-2.27/elf/dl-misc.c:_dl_name_match_p
--------------------------------------------------------------------------------
-- Auto-annotated source file: /build/glibc-OTsEL5/glibc-2.27/elf/../sysdeps/x86_64/dl-machine.h
-- Annotated source file: /build/glibc-OTsEL5/glibc-2.27/elf/../sysdeps/x86_64/dl-machine.h
--------------------------------------------------------------------------------
This file was unreadable
--------------------------------------------------------------------------------
-- Auto-annotated source file: /build/glibc-OTsEL5/glibc-2.27/elf/dl-lookup.c
-- Annotated source file: /build/glibc-OTsEL5/glibc-2.27/elf/dl-lookup.c
--------------------------------------------------------------------------------
This file was unreadable
--------------------------------------------------------------------------------
-- Auto-annotated source file: /build/glibc-OTsEL5/glibc-2.27/elf/dl-misc.c
-- Annotated source file: /build/glibc-OTsEL5/glibc-2.27/elf/dl-misc.c
--------------------------------------------------------------------------------
This file was unreadable
--------------------------------------------------------------------------------
-- Auto-annotated source file: /build/glibc-OTsEL5/glibc-2.27/elf/dl-tunables.c
-- Annotated source file: /build/glibc-OTsEL5/glibc-2.27/elf/dl-tunables.c
--------------------------------------------------------------------------------
This file was unreadable
--------------------------------------------------------------------------------
-- Auto-annotated source file: /build/glibc-OTsEL5/glibc-2.27/elf/dl-tunables.h
-- Annotated source file: /build/glibc-OTsEL5/glibc-2.27/elf/dl-tunables.h
--------------------------------------------------------------------------------
This file was unreadable
--------------------------------------------------------------------------------
-- Auto-annotated source file: /build/glibc-OTsEL5/glibc-2.27/elf/do-rel.h
-- Annotated source file: /build/glibc-OTsEL5/glibc-2.27/elf/do-rel.h
--------------------------------------------------------------------------------
This file was unreadable
--------------------------------------------------------------------------------
-- Auto-annotated source file: /build/glibc-OTsEL5/glibc-2.27/string/../sysdeps/x86_64/strcmp.S
-- Annotated source file: /build/glibc-OTsEL5/glibc-2.27/string/../sysdeps/x86_64/strcmp.S
--------------------------------------------------------------------------------
This file was unreadable
--------------------------------------------------------------------------------
-- Auto-annotated source file: a.c
-- Annotated source file: a.c
--------------------------------------------------------------------------------
Ir I1mr ILmr

View File

@ -11,8 +11,7 @@ Events shown: Dw Dr Ir
Event sort order: Dr
Threshold: 0.1
Include dirs:
User annotated: a.c
Auto-annotation: off
Annotation: off
--------------------------------------------------------------------------------
-- Summary
@ -33,27 +32,3 @@ Dw Dr Ir file:function
2,490 (13.8%) 5,219 (0.1%) 21,821 (0.4%) /build/glibc-OTsEL5/glibc-2.27/elf/../sysdeps/x86_64/dl-machine.h:_dl_relocate_object
0 5,158 (0.1%) 25,408 (0.5%) /build/glibc-OTsEL5/glibc-2.27/string/../sysdeps/x86_64/strcmp.S:strcmp
--------------------------------------------------------------------------------
-- User-annotated source file: a.c
--------------------------------------------------------------------------------
Dw Dr Ir
1 (0.0%) 0 2 (0.0%) int main(void) {
1 (0.0%) 0 1 (0.0%) int z = 0;
1 (0.0%) 2,000,001 (49.3%) 3,000,004 (57.4%) for (int i = 0; i < 1000000; i++) {
0 2,000,000 (49.3%) 2,000,000 (38.2%) z += i;
. . . }
0 1 (0.0%) 6 (0.0%) return z % 256;
0 2 (0.0%) 2 (0.0%) }
--------------------------------------------------------------------------------
-- Annotation summary
--------------------------------------------------------------------------------
Dw Dr Ir
3 (0.0%) 4,000,004 (98.6%) 5,000,015 (95.6%) annotated: files known & above threshold & readable, line numbers known
0 0 0 annotated: files known & above threshold & readable, line numbers unknown
0 0 0 unannotated: files known & above threshold & unreadable
18,002 (100.0%) 57,951 (1.4%) 229,738 (4.4%) unannotated: files known & below threshold
0 0 0 unannotated: files unknown

View File

@ -2,5 +2,5 @@
# the post-processing of the `ann1.cgout` file.
prog: ../../tests/true
vgopts: --cachegrind-out-file=cachegrind.out
post: touch ann1.cgout && python3 ../cg_annotate --sort=Dr --show=Dw,Dr,Ir --auto=no ann1.cgout a.c
post: touch ann1.cgout && python3 ../cg_annotate --sort=Dr --show=Dw,Dr,Ir --auto=no ann1.cgout
cleanup: rm cachegrind.out

View File

@ -1 +0,0 @@
one

View File

@ -10,9 +10,7 @@ Threshold: 0.5
Include dirs: ann2-no-such-dir
ann2-no-such-dir-2
ann2-aux
User annotated: ann2-unmentioned.rs
ann2-no-such-file.rs
Auto-annotation: on
Annotation: on
--------------------------------------------------------------------------------
-- Summary
@ -40,7 +38,7 @@ A SomeCount VeryLongEventName file:function
500 (0.5%) 0 0 ann2-basic.rs:f4
--------------------------------------------------------------------------------
-- Auto-annotated source file: ann2-basic.rs
-- Annotated source file: ann2-basic.rs
--------------------------------------------------------------------------------
A SomeCount VeryLongEventName
@ -68,12 +66,12 @@ A SomeCount VeryLongEventName
300 (0.3%) 0 0 twenty
--------------------------------------------------------------------------------
-- Auto-annotated source file: ann2-could-not-be-found.rs
-- Annotated source file: ann2-could-not-be-found.rs
--------------------------------------------------------------------------------
This file was unreadable
--------------------------------------------------------------------------------
-- Auto-annotated source file: ann2-more-recent-than-cgout.rs
-- Annotated source file: ann2-more-recent-than-cgout.rs
--------------------------------------------------------------------------------
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@ WARNING @@
@ -91,7 +89,7 @@ A SomeCount VeryLongEventName
-- line 4 ----------------------------------------
--------------------------------------------------------------------------------
-- Auto-annotated source file: ann2-negatives.rs
-- Annotated source file: ann2-negatives.rs
--------------------------------------------------------------------------------
A SomeCount VeryLongEventName
@ -113,12 +111,7 @@ A SomeCount VeryLongEventName
-- line 13 ----------------------------------------
--------------------------------------------------------------------------------
-- User-annotated source file: ann2-no-such-file.rs
--------------------------------------------------------------------------------
This file was unreadable
--------------------------------------------------------------------------------
-- Auto-annotated source file: ann2-past-the-end.rs
-- Annotated source file: ann2-past-the-end.rs
--------------------------------------------------------------------------------
A SomeCount VeryLongEventName
@ -138,12 +131,7 @@ A SomeCount VeryLongEventName
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
--------------------------------------------------------------------------------
-- User-annotated source file: ann2-unmentioned.rs
--------------------------------------------------------------------------------
This file was not mentioned by the data file
--------------------------------------------------------------------------------
-- Auto-annotated source file: ann2-aux/ann2-via-I.rs
-- Annotated source file: ann2-aux/ann2-via-I.rs
--------------------------------------------------------------------------------
A SomeCount VeryLongEventName

View File

@ -8,6 +8,6 @@ vgopts: --cachegrind-out-file=cachegrind.out
# The `sleep` is to ensure the mtime of the second touched file is greater than
# the mtime of the first touched file.
post: touch ann2.cgout && sleep 0.1 && touch ann2-more-recent-than-cgout.rs && python3 ../cg_annotate --context 2 --auto --show-percs=yes --threshold=0.5 -Iann2-no-such-dir --include ann2-no-such-dir-2 -I=ann2-aux ann2.cgout ann2-unmentioned.rs ann2-no-such-file.rs
post: touch ann2.cgout && sleep 0.1 && touch ann2-more-recent-than-cgout.rs && python3 ../cg_annotate --context 2 --annotate --show-percs=yes --threshold=0.5 -Iann2-no-such-dir --include ann2-no-such-dir-2 -I=ann2-aux ann2.cgout
cleanup: rm cachegrind.out