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