Solaris: fix a few compiler warnings
[valgrind.git] / cachegrind / cg_merge.in
blob1201114ded0fcf26d4bc7762f8d025ab7657e483
1 #! /usr/bin/env python3
2 # pyright: strict
4 # --------------------------------------------------------------------
5 # --- Cachegrind's merger.                             cg_merge.in ---
6 # --------------------------------------------------------------------
8 # This file is part of Cachegrind, a high-precision tracing profiler
9 # built with Valgrind.
11 # Copyright (C) 2002-2023 Nicholas Nethercote
12 #    njn@valgrind.org
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
39 import re
40 import sys
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.
49     output: str
50     cgout_filename: list[str]
52     @staticmethod
53     def parse() -> Args:
54         desc = (
55             "Merge multiple Cachegrind output files. Deprecated; use "
56             "`cg_annotate` with multiple Cachegrind output files instead."
57         )
58         p = ArgumentParser(description=desc)
60         p.add_argument("--version", action="version", version="%(prog)s-@VERSION@")
62         p.add_argument(
63             "-o",
64             dest="output",
65             type=str,
66             metavar="FILE",
67             help="output file (default: stdout)",
68         )
70         p.add_argument(
71             "cgout_filename",
72             nargs="+",
73             metavar="cachegrind-out-file",
74             help="file produced by Cachegrind",
75         )
77         return p.parse_args(namespace=Args())
80 # Args are stored in a global for easy access.
81 args = Args.parse()
83 # A single instance of this class is constructed, from `args` and the `events:`
84 # line in the cgout file.
85 class Events:
86     # The event names.
87     events: list[str]
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:
99             pass
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)))
103         else:
104             raise ValueError
106         return 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.
119 Cc = list[int]
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):
125         b_cc[i] += a_count
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)
136     sys.exit(1)
139 def read_cgout_file(
140     cgout_filename: str,
141     is_first_file: bool,
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.
146     try:
147         cgout_file = open(cgout_filename, "r", encoding="utf-8")
148     except OSError as err:
149         die(f"{err}")
151     with cgout_file:
152         cgout_line_num = 0
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
159             cgout_line_num += 1
160             return cgout_file.readline()
162         # Read "desc:" lines.
163         desc: list[str] = []
164         while line := readline():
165             if m := re.match(r"desc:\s+(.*)", line):
166                 desc.append(m.group(1))
167             else:
168                 break
170         # Read "cmd:" line. (`line` is already set from the "desc:" loop.)
171         if m := re.match(r"cmd:\s+(.*)", line):
172             cmd = m.group(1)
173         else:
174             parse_die("missing a `command:` line")
176         # Read "events:" line.
177         line = readline()
178         if m := re.match(r"events:\s+(.*)", line):
179             events = Events(m.group(1))
180         else:
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
191         fl = ""
192         fn = ""
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.
199         if is_first_file:
200             cumul_dict_fl_dict_fn_dict_line_cc.default_factory = (
201                 mk_empty_dict_fn_dict_line_cc
202             )
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()
209                 try:
210                     line_num = int(split_line[0])
211                     cc = events.mk_cc(split_line[1:])
212                 except ValueError:
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="):
219                 fn = line[3:-1]
221             elif line.startswith("fl="):
222                 fl = line[3:-1]
223                 # A `fn=` line should follow, overwriting the "???".
224                 fn = "???"
226             elif m := re.match(r"summary:\s+(.*)", line):
227                 summary_cc_present = True
228                 try:
229                     add_cc_to_cc(events.mk_cc(m.group(1).split()), cumul_summary_cc)
230                 except ValueError:
231                     parse_die("malformed or too many event counts")
233             elif line == "\n" or line.startswith("#"):
234                 # Skip empty lines and comment lines.
235                 pass
237             else:
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)
253 def main() -> None:
254     desc1: list[str] | None = None
255     cmd1 = None
256     events1 = 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(
266             filename,
267             is_first_file,
268             cumul_dict_fl_dict_fn_dict_line_cc,
269             cumul_summary_cc,
270         )
271         # We reuse the description and command from the first file, like the
272         # the old C version of `cg_merge`.
273         if is_first_file:
274             desc1 = desc_n
275             cmd1 = cmd_n
276             events1 = events_n
277         else:
278             assert events1
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.
284         assert desc1
285         assert events1
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)
303     if args.output:
304         try:
305             with open(args.output, "w", encoding="utf-8") as f:
306                 write_output(f)
307         except OSError as err:
308             die(f"{err}")
309     else:
310         write_output(sys.stdout)
313 if __name__ == "__main__":
314     main()