blob: 8f71b29b7749d6c1269454242e0f7fcb70e35f9d [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001#!/usr/bin/env python3
Brad Bishopc342db32019-05-15 21:57:59 -04002#
Patrick Williams92b42cb2022-09-03 06:53:57 -05003# Copyright OpenEmbedded Contributors
4#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
6#
Brad Bishop6e60e8b2018-02-01 10:27:11 -05007
8import argparse
9import os
10import re
11import sys
12
13arg_parser = argparse.ArgumentParser(
14 description="""
15Reports time consumed for one or more task in a format similar to the standard
16Bash 'time' builtin. Optionally sorts tasks by real (wall-clock), user (user
17space CPU), or sys (kernel CPU) time.
18""")
19
20arg_parser.add_argument(
21 "paths",
22 metavar="path",
23 nargs="+",
24 help="""
25A path containing task buildstats. If the path is a directory, e.g.
26build/tmp/buildstats, then all task found (recursively) in it will be
27processed. If the path is a single task buildstat, e.g.
28build/tmp/buildstats/20161018083535/foo-1.0-r0/do_compile, then just that
29buildstat will be processed. Multiple paths can be specified to process all of
30them. Files whose names do not start with "do_" are ignored.
31""")
32
33arg_parser.add_argument(
34 "--sort",
35 choices=("none", "real", "user", "sys"),
36 default="none",
37 help="""
38The measurement to sort the output by. Defaults to 'none', which means to sort
39by the order paths were given on the command line. For other options, tasks are
40sorted in descending order from the highest value.
41""")
42
43args = arg_parser.parse_args()
44
45# Field names and regexes for parsing out their values from buildstat files
46field_regexes = (("elapsed", ".*Elapsed time: ([0-9.]+)"),
47 ("user", "rusage ru_utime: ([0-9.]+)"),
48 ("sys", "rusage ru_stime: ([0-9.]+)"),
49 ("child user", "Child rusage ru_utime: ([0-9.]+)"),
50 ("child sys", "Child rusage ru_stime: ([0-9.]+)"))
51
52# A list of (<path>, <dict>) tuples, where <path> is the path of a do_* task
53# buildstat file and <dict> maps fields from the file to their values
54task_infos = []
55
56def save_times_for_task(path):
57 """Saves information for the buildstat file 'path' in 'task_infos'."""
58
59 if not os.path.basename(path).startswith("do_"):
60 return
61
62 with open(path) as f:
63 fields = {}
64
65 for line in f:
66 for name, regex in field_regexes:
67 match = re.match(regex, line)
68 if match:
69 fields[name] = float(match.group(1))
70 break
71
72 # Check that all expected fields were present
73 for name, regex in field_regexes:
74 if name not in fields:
75 print("Warning: Skipping '{}' because no field matching '{}' could be found"
76 .format(path, regex),
77 file=sys.stderr)
78 return
79
80 task_infos.append((path, fields))
81
82def save_times_for_dir(path):
83 """Runs save_times_for_task() for each file in path and its subdirs, recursively."""
84
85 # Raise an exception for os.walk() errors instead of ignoring them
86 def walk_onerror(e):
87 raise e
88
89 for root, _, files in os.walk(path, onerror=walk_onerror):
90 for fname in files:
91 save_times_for_task(os.path.join(root, fname))
92
93for path in args.paths:
94 if os.path.isfile(path):
95 save_times_for_task(path)
96 else:
97 save_times_for_dir(path)
98
99def elapsed_time(task_info):
100 return task_info[1]["elapsed"]
101
102def tot_user_time(task_info):
103 return task_info[1]["user"] + task_info[1]["child user"]
104
105def tot_sys_time(task_info):
106 return task_info[1]["sys"] + task_info[1]["child sys"]
107
108if args.sort != "none":
109 sort_fn = {"real": elapsed_time, "user": tot_user_time, "sys": tot_sys_time}
110 task_infos.sort(key=sort_fn[args.sort], reverse=True)
111
112first_entry = True
113
114# Catching BrokenPipeError avoids annoying errors when the output is piped into
115# e.g. 'less' or 'head' and not completely read
116try:
117 for task_info in task_infos:
118 real = elapsed_time(task_info)
119 user = tot_user_time(task_info)
120 sys = tot_sys_time(task_info)
121
122 if not first_entry:
123 print()
124 first_entry = False
125
126 # Mimic Bash's 'time' builtin
127 print("{}:\n"
128 "real\t{}m{:.3f}s\n"
129 "user\t{}m{:.3f}s\n"
130 "sys\t{}m{:.3f}s"
131 .format(task_info[0],
132 int(real//60), real%60,
133 int(user//60), user%60,
134 int(sys//60), sys%60))
135
136except BrokenPipeError:
137 pass