blob: fa94c65539945490a171f7f1a81319a339fde86f [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001#
2# Copyright (c) 2017, Intel Corporation.
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005#
6"""Functionality for analyzing buildstats"""
7import json
8import logging
9import os
10import re
Andrew Geissler517393d2023-01-13 08:55:19 -060011from collections import namedtuple
Brad Bishopd7bf8c12018-02-25 22:55:05 -050012from statistics import mean
13
14
15log = logging.getLogger()
16
17
18taskdiff_fields = ('pkg', 'pkg_op', 'task', 'task_op', 'value1', 'value2',
19 'absdiff', 'reldiff')
20TaskDiff = namedtuple('TaskDiff', ' '.join(taskdiff_fields))
21
22
23class BSError(Exception):
24 """Error handling of buildstats"""
25 pass
26
27
28class BSTask(dict):
29 def __init__(self, *args, **kwargs):
30 self['start_time'] = None
31 self['elapsed_time'] = None
32 self['status'] = None
33 self['iostat'] = {}
34 self['rusage'] = {}
35 self['child_rusage'] = {}
36 super(BSTask, self).__init__(*args, **kwargs)
37
38 @property
39 def cputime(self):
40 """Sum of user and system time taken by the task"""
41 rusage = self['rusage']['ru_stime'] + self['rusage']['ru_utime']
42 if self['child_rusage']:
43 # Child rusage may have been optimized out
44 return rusage + self['child_rusage']['ru_stime'] + self['child_rusage']['ru_utime']
45 else:
46 return rusage
47
48 @property
49 def walltime(self):
50 """Elapsed wall clock time"""
51 return self['elapsed_time']
52
53 @property
54 def read_bytes(self):
55 """Bytes read from the block layer"""
56 return self['iostat']['read_bytes']
57
58 @property
59 def write_bytes(self):
60 """Bytes written to the block layer"""
61 return self['iostat']['write_bytes']
62
63 @property
64 def read_ops(self):
65 """Number of read operations on the block layer"""
66 if self['child_rusage']:
67 # Child rusage may have been optimized out
68 return self['rusage']['ru_inblock'] + self['child_rusage']['ru_inblock']
69 else:
70 return self['rusage']['ru_inblock']
71
72 @property
73 def write_ops(self):
74 """Number of write operations on the block layer"""
75 if self['child_rusage']:
76 # Child rusage may have been optimized out
77 return self['rusage']['ru_oublock'] + self['child_rusage']['ru_oublock']
78 else:
79 return self['rusage']['ru_oublock']
80
81 @classmethod
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060082 def from_file(cls, buildstat_file, fallback_end=0):
83 """Read buildstat text file. fallback_end is an optional end time for tasks that are not recorded as finishing."""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050084 bs_task = cls()
85 log.debug("Reading task buildstats from %s", buildstat_file)
86 end_time = None
87 with open(buildstat_file) as fobj:
88 for line in fobj.readlines():
89 key, val = line.split(':', 1)
90 val = val.strip()
91 if key == 'Started':
92 start_time = float(val)
93 bs_task['start_time'] = start_time
94 elif key == 'Ended':
95 end_time = float(val)
96 elif key.startswith('IO '):
97 split = key.split()
98 bs_task['iostat'][split[1]] = int(val)
99 elif key.find('rusage') >= 0:
100 split = key.split()
101 ru_key = split[-1]
102 if ru_key in ('ru_stime', 'ru_utime'):
103 val = float(val)
104 else:
105 val = int(val)
106 ru_type = 'rusage' if split[0] == 'rusage' else \
107 'child_rusage'
108 bs_task[ru_type][ru_key] = val
109 elif key == 'Status':
110 bs_task['status'] = val
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600111 # If the task didn't finish, fill in the fallback end time if specified
112 if start_time and not end_time and fallback_end:
113 end_time = fallback_end
114 if start_time and end_time:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115 bs_task['elapsed_time'] = end_time - start_time
116 else:
117 raise BSError("{} looks like a invalid buildstats file".format(buildstat_file))
118 return bs_task
119
120
121class BSTaskAggregate(object):
122 """Class representing multiple runs of the same task"""
123 properties = ('cputime', 'walltime', 'read_bytes', 'write_bytes',
124 'read_ops', 'write_ops')
125
126 def __init__(self, tasks=None):
127 self._tasks = tasks or []
128 self._properties = {}
129
130 def __getattr__(self, name):
131 if name in self.properties:
132 if name not in self._properties:
133 # Calculate properties on demand only. We only provide mean
134 # value, so far
135 self._properties[name] = mean([getattr(t, name) for t in self._tasks])
136 return self._properties[name]
137 else:
138 raise AttributeError("'BSTaskAggregate' has no attribute '{}'".format(name))
139
140 def append(self, task):
141 """Append new task"""
142 # Reset pre-calculated properties
143 assert isinstance(task, BSTask), "Type is '{}' instead of 'BSTask'".format(type(task))
144 self._properties = {}
145 self._tasks.append(task)
146
147
148class BSRecipe(object):
149 """Class representing buildstats of one recipe"""
150 def __init__(self, name, epoch, version, revision):
151 self.name = name
152 self.epoch = epoch
153 self.version = version
154 self.revision = revision
155 if epoch is None:
156 self.evr = "{}-{}".format(version, revision)
157 else:
158 self.evr = "{}_{}-{}".format(epoch, version, revision)
159 self.tasks = {}
160
161 def aggregate(self, bsrecipe):
162 """Aggregate data of another recipe buildstats"""
163 if self.nevr != bsrecipe.nevr:
164 raise ValueError("Refusing to aggregate buildstats, recipe version "
165 "differs: {} vs. {}".format(self.nevr, bsrecipe.nevr))
166 if set(self.tasks.keys()) != set(bsrecipe.tasks.keys()):
167 raise ValueError("Refusing to aggregate buildstats, set of tasks "
168 "in {} differ".format(self.name))
169
170 for taskname, taskdata in bsrecipe.tasks.items():
171 if not isinstance(self.tasks[taskname], BSTaskAggregate):
172 self.tasks[taskname] = BSTaskAggregate([self.tasks[taskname]])
173 self.tasks[taskname].append(taskdata)
174
175 @property
176 def nevr(self):
177 return self.name + '-' + self.evr
178
179
180class BuildStats(dict):
181 """Class representing buildstats of one build"""
182
183 @property
184 def num_tasks(self):
185 """Get number of tasks"""
186 num = 0
187 for recipe in self.values():
188 num += len(recipe.tasks)
189 return num
190
191 @classmethod
192 def from_json(cls, bs_json):
193 """Create new BuildStats object from JSON object"""
194 buildstats = cls()
195 for recipe in bs_json:
196 if recipe['name'] in buildstats:
197 raise BSError("Cannot handle multiple versions of the same "
198 "package ({})".format(recipe['name']))
199 bsrecipe = BSRecipe(recipe['name'], recipe['epoch'],
200 recipe['version'], recipe['revision'])
201 for task, data in recipe['tasks'].items():
202 bsrecipe.tasks[task] = BSTask(data)
203
204 buildstats[recipe['name']] = bsrecipe
205
206 return buildstats
207
208 @staticmethod
209 def from_file_json(path):
210 """Load buildstats from a JSON file"""
211 with open(path) as fobj:
212 bs_json = json.load(fobj)
213 return BuildStats.from_json(bs_json)
214
215
216 @staticmethod
217 def split_nevr(nevr):
218 """Split name and version information from recipe "nevr" string"""
219 n_e_v, revision = nevr.rsplit('-', 1)
220 match = re.match(r'^(?P<name>\S+)-((?P<epoch>[0-9]{1,5})_)?(?P<version>[0-9]\S*)$',
221 n_e_v)
222 if not match:
223 # If we're not able to parse a version starting with a number, just
224 # take the part after last dash
225 match = re.match(r'^(?P<name>\S+)-((?P<epoch>[0-9]{1,5})_)?(?P<version>[^-]+)$',
226 n_e_v)
227 name = match.group('name')
228 version = match.group('version')
229 epoch = match.group('epoch')
230 return name, epoch, version, revision
231
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600232 @staticmethod
233 def parse_top_build_stats(path):
234 """
235 Parse the top-level build_stats file for build-wide start and duration.
236 """
237 with open(path) as fobj:
238 for line in fobj.readlines():
239 key, val = line.split(':', 1)
240 val = val.strip()
241 if key == 'Build Started':
242 start = float(val)
243 elif key == "Elapsed time":
244 elapsed = float(val.split()[0])
245 return start, elapsed
246
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500247 @classmethod
248 def from_dir(cls, path):
249 """Load buildstats from a buildstats directory"""
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600250 top_stats = os.path.join(path, 'build_stats')
251 if not os.path.isfile(top_stats):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500252 raise BSError("{} does not look like a buildstats directory".format(path))
253
254 log.debug("Reading buildstats directory %s", path)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500255 buildstats = cls()
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600256 build_started, build_elapsed = buildstats.parse_top_build_stats(top_stats)
257 build_end = build_started + build_elapsed
258
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500259 subdirs = os.listdir(path)
260 for dirname in subdirs:
261 recipe_dir = os.path.join(path, dirname)
Andrew Geissler517393d2023-01-13 08:55:19 -0600262 if dirname == "reduced_proc_pressure" or not os.path.isdir(recipe_dir):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500263 continue
264 name, epoch, version, revision = cls.split_nevr(dirname)
265 bsrecipe = BSRecipe(name, epoch, version, revision)
266 for task in os.listdir(recipe_dir):
267 bsrecipe.tasks[task] = BSTask.from_file(
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600268 os.path.join(recipe_dir, task), build_end)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500269 if name in buildstats:
270 raise BSError("Cannot handle multiple versions of the same "
271 "package ({})".format(name))
272 buildstats[name] = bsrecipe
273
274 return buildstats
275
276 def aggregate(self, buildstats):
277 """Aggregate other buildstats into this"""
278 if set(self.keys()) != set(buildstats.keys()):
279 raise ValueError("Refusing to aggregate buildstats, set of "
Andrew Geissler99467da2019-02-25 18:54:23 -0600280 "recipes is different: %s" % (set(self.keys()) ^ set(buildstats.keys())))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500281 for pkg, data in buildstats.items():
282 self[pkg].aggregate(data)
283
284
Brad Bishop96ff1982019-08-19 13:50:42 -0400285def diff_buildstats(bs1, bs2, stat_attr, min_val=None, min_absdiff=None, only_tasks=[]):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500286 """Compare the tasks of two buildstats"""
287 tasks_diff = []
288 pkgs = set(bs1.keys()).union(set(bs2.keys()))
289 for pkg in pkgs:
290 tasks1 = bs1[pkg].tasks if pkg in bs1 else {}
291 tasks2 = bs2[pkg].tasks if pkg in bs2 else {}
Brad Bishop96ff1982019-08-19 13:50:42 -0400292 if only_tasks:
293 tasks1 = {k: v for k, v in tasks1.items() if k in only_tasks}
294 tasks2 = {k: v for k, v in tasks2.items() if k in only_tasks}
295
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500296 if not tasks1:
297 pkg_op = '+'
298 elif not tasks2:
299 pkg_op = '-'
300 else:
301 pkg_op = ' '
302
303 for task in set(tasks1.keys()).union(set(tasks2.keys())):
304 task_op = ' '
305 if task in tasks1:
306 val1 = getattr(bs1[pkg].tasks[task], stat_attr)
307 else:
308 task_op = '+'
309 val1 = 0
310 if task in tasks2:
311 val2 = getattr(bs2[pkg].tasks[task], stat_attr)
312 else:
313 val2 = 0
314 task_op = '-'
315
316 if val1 == 0:
317 reldiff = float('inf')
318 else:
319 reldiff = 100 * (val2 - val1) / val1
320
321 if min_val and max(val1, val2) < min_val:
322 log.debug("Filtering out %s:%s (%s)", pkg, task,
323 max(val1, val2))
324 continue
325 if min_absdiff and abs(val2 - val1) < min_absdiff:
326 log.debug("Filtering out %s:%s (difference of %s)", pkg, task,
327 val2-val1)
328 continue
329 tasks_diff.append(TaskDiff(pkg, pkg_op, task, task_op, val1, val2,
330 val2-val1, reldiff))
331 return tasks_diff
332
333
334class BSVerDiff(object):
335 """Class representing recipe version differences between two buildstats"""
336 def __init__(self, bs1, bs2):
337 RecipeVerDiff = namedtuple('RecipeVerDiff', 'left right')
338
339 recipes1 = set(bs1.keys())
340 recipes2 = set(bs2.keys())
341
342 self.new = dict([(r, bs2[r]) for r in sorted(recipes2 - recipes1)])
343 self.dropped = dict([(r, bs1[r]) for r in sorted(recipes1 - recipes2)])
344 self.echanged = {}
345 self.vchanged = {}
346 self.rchanged = {}
347 self.unchanged = {}
348 self.empty_diff = False
349
350 common = recipes2.intersection(recipes1)
351 if common:
352 for recipe in common:
353 rdiff = RecipeVerDiff(bs1[recipe], bs2[recipe])
354 if bs1[recipe].epoch != bs2[recipe].epoch:
355 self.echanged[recipe] = rdiff
356 elif bs1[recipe].version != bs2[recipe].version:
357 self.vchanged[recipe] = rdiff
358 elif bs1[recipe].revision != bs2[recipe].revision:
359 self.rchanged[recipe] = rdiff
360 else:
361 self.unchanged[recipe] = rdiff
362
363 if len(recipes1) == len(recipes2) == len(self.unchanged):
364 self.empty_diff = True
365
366 def __bool__(self):
367 return not self.empty_diff