1 #! /usr/bin/env python3
4 # --------------------------------------------------------------------
5 # --- Cachegrind's merger. cg_merge.in ---
6 # --------------------------------------------------------------------
8 # This file is part of Cachegrind, a Valgrind tool for cache
11 # Copyright (C) 2002-2023 Nicholas Nethercote
14 # This program is free software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License as
16 # published by the Free Software Foundation; either version 2 of the
17 # License, or (at your option) any later version.
19 # This program is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, see <http://www.gnu.org/licenses/>.
27 # The GNU General Public License is contained in the file COPYING.
29 # This script merges Cachegrind output files.
31 # Use `make pymerge` to "build" this script every time it is changed. This runs
32 # the formatters, type-checkers, and linters on `cg_merge.in` and then
33 # generates `cg_merge`.
35 # This is a cut-down version of `cg_annotate.in`.
37 from __future__ import annotations
41 from argparse import ArgumentParser, Namespace
42 from collections import defaultdict
43 from typing import DefaultDict, NoReturn, TextIO
46 # A typed wrapper for parsed args.
47 class Args(Namespace):
48 # None of these fields are modified after arg parsing finishes.
50 cgout_filename: list[str]
55 "Merge multiple Cachegrind output files. Deprecated; use "
56 "`cg_annotate` with multiple Cachegrind output files instead."
58 p = ArgumentParser(description=desc)
60 p.add_argument("--version", action="version", version="%(prog)s-@VERSION@")
67 help="output file (default: stdout)",
73 metavar="cachegrind-out-file",
74 help="file produced by Cachegrind",
77 return p.parse_args(namespace=Args())
80 # Args are stored in a global for easy access.
83 # A single instance of this class is constructed, from `args` and the `events:`
84 # line in the cgout file.
89 def __init__(self, text: str) -> None:
90 self.events = text.split()
91 self.num_events = len(self.events)
93 # Raises a `ValueError` exception on syntax error.
94 def mk_cc(self, str_counts: list[str]) -> Cc:
95 # This is slightly faster than a list comprehension.
96 counts = list(map(int, str_counts))
98 if len(counts) == self.num_events:
100 elif len(counts) < self.num_events:
101 # Add zeroes at the end for any missing numbers.
102 counts.extend([0] * (self.num_events - len(counts)))
108 def mk_empty_cc(self) -> Cc:
109 # This is much faster than a list comprehension.
110 return [0] * self.num_events
113 # A "cost centre", which is a dumb container for counts. Always the same length
114 # as `Events.events`, but it doesn't even know event names. `Events.mk_cc` and
115 # `Events.mk_empty_cc` are used for construction.
117 # This used to be a class with a single field `counts: list[int]`, but this
118 # type is very hot and just using a type alias is much faster.
122 # Add the counts in `a_cc` to `b_cc`.
123 def add_cc_to_cc(a_cc: Cc, b_cc: Cc) -> None:
124 for i, a_count in enumerate(a_cc):
128 # Per-line CCs, organised by filename, function name, and line number.
129 DictLineCc = DefaultDict[int, Cc]
130 DictFnDictLineCc = DefaultDict[str, DictLineCc]
131 DictFlDictFnDictLineCc = DefaultDict[str, DictFnDictLineCc]
134 def die(msg: str) -> NoReturn:
135 print("cg_merge: error:", msg, file=sys.stderr)
142 cumul_dict_fl_dict_fn_dict_line_cc: DictFlDictFnDictLineCc,
143 cumul_summary_cc: Cc,
144 ) -> tuple[list[str], str, Events]:
145 # The file format is described in Cachegrind's manual.
147 cgout_file = open(cgout_filename, "r", encoding="utf-8")
148 except OSError as err:
154 def parse_die(msg: str) -> NoReturn:
155 die(f"{cgout_file.name}:{cgout_line_num}: {msg}")
157 def readline() -> str:
158 nonlocal cgout_line_num
160 return cgout_file.readline()
162 # Read "desc:" lines.
164 while line := readline():
165 if m := re.match(r"desc:\s+(.*)", line):
166 desc.append(m.group(1))
170 # Read "cmd:" line. (`line` is already set from the "desc:" loop.)
171 if m := re.match(r"cmd:\s+(.*)", line):
174 parse_die("missing a `command:` line")
176 # Read "events:" line.
178 if m := re.match(r"events:\s+(.*)", line):
179 events = Events(m.group(1))
181 parse_die("missing an `events:` line")
183 def mk_empty_dict_line_cc() -> DictLineCc:
184 return defaultdict(events.mk_empty_cc)
186 def mk_empty_dict_fn_dict_line_cc() -> DictFnDictLineCc:
187 return defaultdict(mk_empty_dict_line_cc)
189 summary_cc_present = False
194 # The `cumul_*` values are passed in by reference and are modified by
195 # this function. But they can't be properly initialized until the
196 # `events:` line of the first file is read and the number of events is
197 # known. So we initialize them in an invalid state, and then
198 # reinitialize them properly here, before their first use.
200 cumul_dict_fl_dict_fn_dict_line_cc.default_factory = (
201 mk_empty_dict_fn_dict_line_cc
203 cumul_summary_cc.extend(events.mk_empty_cc())
205 # Line matching is done in order of pattern frequency, for speed.
206 while line := readline():
207 if line[0].isdigit():
208 split_line = line.split()
210 line_num = int(split_line[0])
211 cc = events.mk_cc(split_line[1:])
213 parse_die("malformed or too many event counts")
215 # Record this CC at the file/func/line level.
216 add_cc_to_cc(cc, cumul_dict_fl_dict_fn_dict_line_cc[fl][fn][line_num])
218 elif line.startswith("fn="):
221 elif line.startswith("fl="):
223 # A `fn=` line should follow, overwriting the "???".
226 elif m := re.match(r"summary:\s+(.*)", line):
227 summary_cc_present = True
229 add_cc_to_cc(events.mk_cc(m.group(1).split()), cumul_summary_cc)
231 parse_die("malformed or too many event counts")
233 elif line == "\n" or line.startswith("#"):
234 # Skip empty lines and comment lines.
238 parse_die(f"malformed line: {line[:-1]}")
240 # Check if summary line was present.
241 if not summary_cc_present:
242 parse_die("missing `summary:` line, aborting")
244 # In `cg_annotate.in` and `cg_diff.in` we check that the file's summary CC
245 # matches the totals of the file's individual CCs, but not here. That's
246 # because in this script we don't collect the file's CCs in isolation,
247 # instead we just add them to the accumulated CCs, for speed. This makes it
248 # difficult to do the per-file checking.
250 return (desc, cmd, events)
254 desc1: list[str] | None = None
258 # Different places where we accumulate CC data. Initialized to invalid
259 # states prior to the number of events being known.
260 cumul_dict_fl_dict_fn_dict_line_cc: DictFlDictFnDictLineCc = defaultdict(None)
261 cumul_summary_cc: Cc = []
263 for n, filename in enumerate(args.cgout_filename):
264 is_first_file = n == 0
265 (desc_n, cmd_n, events_n) = read_cgout_file(
268 cumul_dict_fl_dict_fn_dict_line_cc,
271 # We reuse the description and command from the first file, like the
272 # the old C version of `cg_merge`.
279 if events1.events != events_n.events:
280 die("events in data files don't match")
282 def write_output(f: TextIO) -> None:
283 # These assertions hold because the loop above executes at least twice.
286 assert cumul_dict_fl_dict_fn_dict_line_cc is not None
287 assert cumul_summary_cc
289 for desc_line in desc1:
290 print("desc:", desc_line, file=f)
291 print("cmd:", cmd1, file=f)
292 print("events:", *events1.events, sep=" ", file=f)
294 for fl, dict_fn_dict_line_cc in cumul_dict_fl_dict_fn_dict_line_cc.items():
295 print(f"fl={fl}", file=f)
296 for fn, dict_line_cc in dict_fn_dict_line_cc.items():
297 print(f"fn={fn}", file=f)
298 for line, cc in dict_line_cc.items():
299 print(line, *cc, file=f)
301 print("summary:", *cumul_summary_cc, sep=" ", file=f)
305 with open(args.output, "w", encoding="utf-8") as f:
307 except OSError as err:
310 write_output(sys.stdout)
313 if __name__ == "__main__":