Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # |
| 3 | # Script for comparing buildstats from two different builds |
| 4 | # |
| 5 | # Copyright (c) 2016, Intel Corporation. |
| 6 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 7 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 8 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 9 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 10 | import argparse |
| 11 | import glob |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 12 | import logging |
| 13 | import math |
| 14 | import os |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 15 | import sys |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 16 | from operator import attrgetter |
| 17 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 18 | # Import oe libs |
| 19 | scripts_path = os.path.dirname(os.path.realpath(__file__)) |
| 20 | sys.path.append(os.path.join(scripts_path, 'lib')) |
| 21 | from buildstats import BuildStats, diff_buildstats, taskdiff_fields, BSVerDiff |
| 22 | |
| 23 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 24 | # Setup logging |
| 25 | logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") |
| 26 | log = logging.getLogger() |
| 27 | |
| 28 | |
| 29 | class ScriptError(Exception): |
| 30 | """Exception for internal error handling of this script""" |
| 31 | pass |
| 32 | |
| 33 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 34 | def read_buildstats(path, multi): |
| 35 | """Read buildstats""" |
| 36 | if not os.path.exists(path): |
| 37 | raise ScriptError("No such file or directory: {}".format(path)) |
| 38 | |
| 39 | if os.path.isfile(path): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 40 | return BuildStats.from_file_json(path) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 41 | |
| 42 | if os.path.isfile(os.path.join(path, 'build_stats')): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 43 | return BuildStats.from_dir(path) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 44 | |
| 45 | # Handle a non-buildstat directory |
| 46 | subpaths = sorted(glob.glob(path + '/*')) |
| 47 | if len(subpaths) > 1: |
| 48 | if multi: |
| 49 | log.info("Averaging over {} buildstats from {}".format( |
| 50 | len(subpaths), path)) |
| 51 | else: |
| 52 | raise ScriptError("Multiple buildstats found in '{}'. Please give " |
| 53 | "a single buildstat directory of use the --multi " |
| 54 | "option".format(path)) |
| 55 | bs = None |
| 56 | for subpath in subpaths: |
| 57 | if os.path.isfile(subpath): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 58 | _bs = BuildStats.from_file_json(subpath) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 59 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 60 | _bs = BuildStats.from_dir(subpath) |
| 61 | if bs is None: |
| 62 | bs = _bs |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 63 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 64 | bs.aggregate(_bs) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 65 | if not bs: |
| 66 | raise ScriptError("No buildstats found under {}".format(path)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 67 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 68 | return bs |
| 69 | |
| 70 | |
| 71 | def print_ver_diff(bs1, bs2): |
| 72 | """Print package version differences""" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 73 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 74 | diff = BSVerDiff(bs1, bs2) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 75 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 76 | maxlen = max([len(r) for r in set(bs1.keys()).union(set(bs2.keys()))]) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 77 | fmt_str = " {:{maxlen}} ({})" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 78 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 79 | if diff.new: |
| 80 | print("\nNEW RECIPES:") |
| 81 | print("------------") |
| 82 | for name, val in sorted(diff.new.items()): |
| 83 | print(fmt_str.format(name, val.nevr, maxlen=maxlen)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 84 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 85 | if diff.dropped: |
| 86 | print("\nDROPPED RECIPES:") |
| 87 | print("----------------") |
| 88 | for name, val in sorted(diff.dropped.items()): |
| 89 | print(fmt_str.format(name, val.nevr, maxlen=maxlen)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 90 | |
| 91 | fmt_str = " {0:{maxlen}} {1:<20} ({2})" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 92 | if diff.rchanged: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 93 | print("\nREVISION CHANGED:") |
| 94 | print("-----------------") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 95 | for name, val in sorted(diff.rchanged.items()): |
| 96 | field1 = "{} -> {}".format(val.left.revision, val.right.revision) |
| 97 | field2 = "{} -> {}".format(val.left.nevr, val.right.nevr) |
| 98 | print(fmt_str.format(name, field1, field2, maxlen=maxlen)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 99 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 100 | if diff.vchanged: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 101 | print("\nVERSION CHANGED:") |
| 102 | print("----------------") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 103 | for name, val in sorted(diff.vchanged.items()): |
| 104 | field1 = "{} -> {}".format(val.left.version, val.right.version) |
| 105 | field2 = "{} -> {}".format(val.left.nevr, val.right.nevr) |
| 106 | print(fmt_str.format(name, field1, field2, maxlen=maxlen)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 107 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 108 | if diff.echanged: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 109 | print("\nEPOCH CHANGED:") |
| 110 | print("--------------") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 111 | for name, val in sorted(diff.echanged.items()): |
| 112 | field1 = "{} -> {}".format(val.left.epoch, val.right.epoch) |
| 113 | field2 = "{} -> {}".format(val.left.nevr, val.right.nevr) |
| 114 | print(fmt_str.format(name, field1, field2, maxlen=maxlen)) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 115 | |
| 116 | |
| 117 | def print_task_diff(bs1, bs2, val_type, min_val=0, min_absdiff=0, sort_by=('absdiff',)): |
| 118 | """Diff task execution times""" |
| 119 | def val_to_str(val, human_readable=False): |
| 120 | """Convert raw value to printable string""" |
| 121 | def hms_time(secs): |
| 122 | """Get time in human-readable HH:MM:SS format""" |
| 123 | h = int(secs / 3600) |
| 124 | m = int((secs % 3600) / 60) |
| 125 | s = secs % 60 |
| 126 | if h == 0: |
| 127 | return "{:02d}:{:04.1f}".format(m, s) |
| 128 | else: |
| 129 | return "{:d}:{:02d}:{:04.1f}".format(h, m, s) |
| 130 | |
| 131 | if 'time' in val_type: |
| 132 | if human_readable: |
| 133 | return hms_time(val) |
| 134 | else: |
| 135 | return "{:.1f}s".format(val) |
| 136 | elif 'bytes' in val_type and human_readable: |
| 137 | prefix = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi'] |
| 138 | dec = int(math.log(val, 2) / 10) |
| 139 | prec = 1 if dec > 0 else 0 |
| 140 | return "{:.{prec}f}{}B".format(val / (2 ** (10 * dec)), |
| 141 | prefix[dec], prec=prec) |
| 142 | elif 'ops' in val_type and human_readable: |
| 143 | prefix = ['', 'k', 'M', 'G', 'T', 'P'] |
| 144 | dec = int(math.log(val, 1000)) |
| 145 | prec = 1 if dec > 0 else 0 |
| 146 | return "{:.{prec}f}{}ops".format(val / (1000 ** dec), |
| 147 | prefix[dec], prec=prec) |
| 148 | return str(int(val)) |
| 149 | |
| 150 | def sum_vals(buildstats): |
| 151 | """Get cumulative sum of all tasks""" |
| 152 | total = 0.0 |
| 153 | for recipe_data in buildstats.values(): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 154 | for bs_task in recipe_data.tasks.values(): |
| 155 | total += getattr(bs_task, val_type) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 156 | return total |
| 157 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 158 | if min_val: |
| 159 | print("Ignoring tasks less than {} ({})".format( |
| 160 | val_to_str(min_val, True), val_to_str(min_val))) |
| 161 | if min_absdiff: |
| 162 | print("Ignoring differences less than {} ({})".format( |
| 163 | val_to_str(min_absdiff, True), val_to_str(min_absdiff))) |
| 164 | |
| 165 | # Prepare the data |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 166 | tasks_diff = diff_buildstats(bs1, bs2, val_type, min_val, min_absdiff) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 167 | |
| 168 | # Sort our list |
| 169 | for field in reversed(sort_by): |
| 170 | if field.startswith('-'): |
| 171 | field = field[1:] |
| 172 | reverse = True |
| 173 | else: |
| 174 | reverse = False |
| 175 | tasks_diff = sorted(tasks_diff, key=attrgetter(field), reverse=reverse) |
| 176 | |
| 177 | linedata = [(' ', 'PKG', ' ', 'TASK', 'ABSDIFF', 'RELDIFF', |
| 178 | val_type.upper() + '1', val_type.upper() + '2')] |
| 179 | field_lens = dict([('len_{}'.format(i), len(f)) for i, f in enumerate(linedata[0])]) |
| 180 | |
| 181 | # Prepare fields in string format and measure field lengths |
| 182 | for diff in tasks_diff: |
| 183 | task_prefix = diff.task_op if diff.pkg_op == ' ' else ' ' |
| 184 | linedata.append((diff.pkg_op, diff.pkg, task_prefix, diff.task, |
| 185 | val_to_str(diff.absdiff), |
| 186 | '{:+.1f}%'.format(diff.reldiff), |
| 187 | val_to_str(diff.value1), |
| 188 | val_to_str(diff.value2))) |
| 189 | for i, field in enumerate(linedata[-1]): |
| 190 | key = 'len_{}'.format(i) |
| 191 | if len(field) > field_lens[key]: |
| 192 | field_lens[key] = len(field) |
| 193 | |
| 194 | # Print data |
| 195 | print() |
| 196 | for fields in linedata: |
| 197 | print("{:{len_0}}{:{len_1}} {:{len_2}}{:{len_3}} {:>{len_4}} {:>{len_5}} {:>{len_6}} -> {:{len_7}}".format( |
| 198 | *fields, **field_lens)) |
| 199 | |
| 200 | # Print summary of the diffs |
| 201 | total1 = sum_vals(bs1) |
| 202 | total2 = sum_vals(bs2) |
| 203 | print("\nCumulative {}:".format(val_type)) |
| 204 | print (" {} {:+.1f}% {} ({}) -> {} ({})".format( |
| 205 | val_to_str(total2 - total1), 100 * (total2-total1) / total1, |
| 206 | val_to_str(total1, True), val_to_str(total1), |
| 207 | val_to_str(total2, True), val_to_str(total2))) |
| 208 | |
| 209 | |
| 210 | def parse_args(argv): |
| 211 | """Parse cmdline arguments""" |
| 212 | description=""" |
| 213 | Script for comparing buildstats of two separate builds.""" |
| 214 | parser = argparse.ArgumentParser( |
| 215 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 216 | description=description) |
| 217 | |
| 218 | min_val_defaults = {'cputime': 3.0, |
| 219 | 'read_bytes': 524288, |
| 220 | 'write_bytes': 524288, |
| 221 | 'read_ops': 500, |
| 222 | 'write_ops': 500, |
| 223 | 'walltime': 5} |
| 224 | min_absdiff_defaults = {'cputime': 1.0, |
| 225 | 'read_bytes': 131072, |
| 226 | 'write_bytes': 131072, |
| 227 | 'read_ops': 50, |
| 228 | 'write_ops': 50, |
| 229 | 'walltime': 2} |
| 230 | |
| 231 | parser.add_argument('--debug', '-d', action='store_true', |
| 232 | help="Verbose logging") |
| 233 | parser.add_argument('--ver-diff', action='store_true', |
| 234 | help="Show package version differences and exit") |
| 235 | parser.add_argument('--diff-attr', default='cputime', |
| 236 | choices=min_val_defaults.keys(), |
| 237 | help="Buildstat attribute which to compare") |
| 238 | parser.add_argument('--min-val', default=min_val_defaults, type=float, |
| 239 | help="Filter out tasks less than MIN_VAL. " |
| 240 | "Default depends on --diff-attr.") |
| 241 | parser.add_argument('--min-absdiff', default=min_absdiff_defaults, type=float, |
| 242 | help="Filter out tasks whose difference is less than " |
| 243 | "MIN_ABSDIFF, Default depends on --diff-attr.") |
| 244 | parser.add_argument('--sort-by', default='absdiff', |
| 245 | help="Comma-separated list of field sort order. " |
| 246 | "Prepend the field name with '-' for reversed sort. " |
| 247 | "Available fields are: {}".format(', '.join(taskdiff_fields))) |
| 248 | parser.add_argument('--multi', action='store_true', |
| 249 | help="Read all buildstats from the given paths and " |
| 250 | "average over them") |
| 251 | parser.add_argument('buildstats1', metavar='BUILDSTATS1', help="'Left' buildstat") |
| 252 | parser.add_argument('buildstats2', metavar='BUILDSTATS2', help="'Right' buildstat") |
| 253 | |
| 254 | args = parser.parse_args(argv) |
| 255 | |
| 256 | # We do not nedd/want to read all buildstats if we just want to look at the |
| 257 | # package versions |
| 258 | if args.ver_diff: |
| 259 | args.multi = False |
| 260 | |
| 261 | # Handle defaults for the filter arguments |
| 262 | if args.min_val is min_val_defaults: |
| 263 | args.min_val = min_val_defaults[args.diff_attr] |
| 264 | if args.min_absdiff is min_absdiff_defaults: |
| 265 | args.min_absdiff = min_absdiff_defaults[args.diff_attr] |
| 266 | |
| 267 | return args |
| 268 | |
| 269 | |
| 270 | def main(argv=None): |
| 271 | """Script entry point""" |
| 272 | args = parse_args(argv) |
| 273 | if args.debug: |
| 274 | log.setLevel(logging.DEBUG) |
| 275 | |
| 276 | # Validate sort fields |
| 277 | sort_by = [] |
| 278 | for field in args.sort_by.split(','): |
| 279 | if field.lstrip('-') not in taskdiff_fields: |
| 280 | log.error("Invalid sort field '%s' (must be one of: %s)" % |
| 281 | (field, ', '.join(taskdiff_fields))) |
| 282 | sys.exit(1) |
| 283 | sort_by.append(field) |
| 284 | |
| 285 | try: |
| 286 | bs1 = read_buildstats(args.buildstats1, args.multi) |
| 287 | bs2 = read_buildstats(args.buildstats2, args.multi) |
| 288 | |
| 289 | if args.ver_diff: |
| 290 | print_ver_diff(bs1, bs2) |
| 291 | else: |
| 292 | print_task_diff(bs1, bs2, args.diff_attr, args.min_val, |
| 293 | args.min_absdiff, sort_by) |
| 294 | except ScriptError as err: |
| 295 | log.error(str(err)) |
| 296 | return 1 |
| 297 | return 0 |
| 298 | |
| 299 | if __name__ == "__main__": |
| 300 | sys.exit(main()) |