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