2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # pylint: disable=consider-using-with
6 A helper to compare 'systemd-analyze dump' outputs.
8 systemd-analyze dump >/var/tmp/dump1
10 tools/analyze-dump-sort.py /var/tmp/dump1 → this does a diff from dump1 to current
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
21 def sort_dump(sourcefile
, destfile
=None):
23 destfile
= tempfile
.NamedTemporaryFile('wt')
30 for line
in sourcefile
:
33 header
= line
.split(':')[0]
34 if 'Timestamp' in header
or 'Invocation ID' in header
or 'PID' in header
:
37 if line
.startswith('->'):
41 elif line
.startswith('\t'):
44 if same
and same
[0].startswith(header
):
47 unit
.extend(sorted(same
, key
=str.lower
))
50 print(line
, file=destfile
)
55 for unit
in sorted(units
.values()):
56 print('\n'.join(unit
), file=destfile
)
62 p
= argparse
.ArgumentParser(description
=__doc__
)
64 p
.add_argument('two', nargs
='?')
65 p
.add_argument('--user', action
='store_true')
68 if __name__
== '__main__':
71 one
= sort_dump(open(opts
.one
))
73 two
= sort_dump(open(opts
.two
))
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
)