Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 2 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 3 | # Copyright OpenEmbedded Contributors |
| 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
| 6 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 7 | |
| 8 | import argparse |
| 9 | import os |
| 10 | import re |
| 11 | import sys |
| 12 | |
| 13 | arg_parser = argparse.ArgumentParser( |
| 14 | description=""" |
| 15 | Reports time consumed for one or more task in a format similar to the standard |
| 16 | Bash 'time' builtin. Optionally sorts tasks by real (wall-clock), user (user |
| 17 | space CPU), or sys (kernel CPU) time. |
| 18 | """) |
| 19 | |
| 20 | arg_parser.add_argument( |
| 21 | "paths", |
| 22 | metavar="path", |
| 23 | nargs="+", |
| 24 | help=""" |
| 25 | A path containing task buildstats. If the path is a directory, e.g. |
| 26 | build/tmp/buildstats, then all task found (recursively) in it will be |
| 27 | processed. If the path is a single task buildstat, e.g. |
| 28 | build/tmp/buildstats/20161018083535/foo-1.0-r0/do_compile, then just that |
| 29 | buildstat will be processed. Multiple paths can be specified to process all of |
| 30 | them. Files whose names do not start with "do_" are ignored. |
| 31 | """) |
| 32 | |
| 33 | arg_parser.add_argument( |
| 34 | "--sort", |
| 35 | choices=("none", "real", "user", "sys"), |
| 36 | default="none", |
| 37 | help=""" |
| 38 | The measurement to sort the output by. Defaults to 'none', which means to sort |
| 39 | by the order paths were given on the command line. For other options, tasks are |
| 40 | sorted in descending order from the highest value. |
| 41 | """) |
| 42 | |
| 43 | args = arg_parser.parse_args() |
| 44 | |
| 45 | # Field names and regexes for parsing out their values from buildstat files |
| 46 | field_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 |
| 54 | task_infos = [] |
| 55 | |
| 56 | def 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 | |
| 82 | def 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 | |
| 93 | for 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 | |
| 99 | def elapsed_time(task_info): |
| 100 | return task_info[1]["elapsed"] |
| 101 | |
| 102 | def tot_user_time(task_info): |
| 103 | return task_info[1]["user"] + task_info[1]["child user"] |
| 104 | |
| 105 | def tot_sys_time(task_info): |
| 106 | return task_info[1]["sys"] + task_info[1]["child sys"] |
| 107 | |
| 108 | if 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 | |
| 112 | first_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 |
| 116 | try: |
| 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 | |
| 136 | except BrokenPipeError: |
| 137 | pass |