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