]> git.ipfire.org Git - thirdparty/systemd.git/blob - tools/analyze-dump-sort.py
Merge pull request #32324 from mrc0mmand/more-website-fixes
[thirdparty/systemd.git] / tools / analyze-dump-sort.py
1 #!/usr/bin/python
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # pylint: disable=consider-using-with
4
5 """
6 A helper to compare 'systemd-analyze dump' outputs.
7
8 systemd-analyze dump >/var/tmp/dump1
9 (reboot)
10 tools/analyze-dump-sort.py /var/tmp/dump1 → this does a diff from dump1 to current
11
12 systemd-analyze dump >/var/tmp/dump2
13 tools/analyze-dump-sort.py /var/tmp/{dump1,dump2} → this does a diff from dump1 to dump2
14 """
15
16 import argparse
17 import subprocess
18 import tempfile
19
20
21 def sort_dump(sourcefile, destfile=None):
22 if destfile is None:
23 destfile = tempfile.NamedTemporaryFile('wt')
24
25 units = {}
26 unit = []
27
28 same = []
29
30 for line in sourcefile:
31 line = line.rstrip()
32
33 header = line.split(':')[0]
34 if 'Timestamp' in header or 'Invocation ID' in header or 'PID' in header:
35 line = header + ': …'
36
37 if line.startswith('->'):
38 if unit:
39 units[unit[0]] = unit
40 unit = [line]
41 elif line.startswith('\t'):
42 assert unit
43
44 if same and same[0].startswith(header):
45 same.append(line)
46 else:
47 unit.extend(sorted(same, key=str.lower))
48 same = [line]
49 else:
50 print(line, file=destfile)
51
52 if unit:
53 units[unit[0]] = unit
54
55 for unit in sorted(units.values()):
56 print('\n'.join(unit), file=destfile)
57
58 destfile.flush()
59 return destfile
60
61 def parse_args():
62 p = argparse.ArgumentParser(description=__doc__)
63 p.add_argument('one')
64 p.add_argument('two', nargs='?')
65 p.add_argument('--user', action='store_true')
66 return p.parse_args()
67
68 if __name__ == '__main__':
69 opts = parse_args()
70
71 one = sort_dump(open(opts.one))
72 if opts.two:
73 two = sort_dump(open(opts.two))
74 else:
75 user = ['--user'] if opts.user else []
76 two = subprocess.run(['systemd-analyze', 'dump', *user],
77 capture_output=True, text=True, check=True)
78 two = sort_dump(two.stdout.splitlines())
79 with subprocess.Popen(['diff', '-U10', one.name, two.name], stdout=subprocess.PIPE) as diff:
80 subprocess.Popen(['less'], stdin=diff.stdout)