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