1# Copyright 2013 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import telemetry.core.timeline.event_container as event_container 6 7# Doesn't inherit from TimelineEvent because its only a temporary wrapper of a 8# counter sample into an event. During stable operation, the samples are stored 9# a dense array of values rather than in the long-form done by an Event. 10class CounterSample(object): 11 def __init__(self, counter, sample_index): 12 self._counter = counter 13 self._sample_index = sample_index 14 15 @property 16 def name(self): 17 return None 18 19 @property 20 def start(self): 21 return self._counter.timestamps[self._sample_index] 22 23 @start.setter 24 def start(self, start): 25 self._counter.timestamps[self._sample_index] = start 26 27 @property 28 def duration(self): 29 return 0 30 31 @property 32 def end(self): 33 return self.start 34 35 36class Counter(event_container.TimelineEventContainer): 37 """ Stores all the samples for a given counter. 38 """ 39 def __init__(self, parent, category, name): 40 super(Counter, self).__init__(name, parent) 41 self.category = category 42 self.full_name = category + '.' + name 43 self.samples = [] 44 self.timestamps = [] 45 self.series_names = [] 46 self.totals = [] 47 self.max_total = 0 48 49 def IterChildContainers(self): 50 return iter([]) 51 52 def IterEventsInThisContainer(self): 53 for i in range(len(self.timestamps)): 54 yield CounterSample(self, i) 55 56 @property 57 def num_series(self): 58 return len(self.series_names) 59 60 @property 61 def num_samples(self): 62 return len(self.timestamps) 63 64 def FinalizeImport(self): 65 if self.num_series * self.num_samples != len(self.samples): 66 raise ValueError( 67 'Length of samples must be a multiple of length of timestamps.') 68 69 self.totals = [] 70 self.max_total = 0 71 if not len(self.samples): 72 return 73 74 max_total = None 75 for i in xrange(self.num_samples): 76 total = 0 77 for j in xrange(self.num_series): 78 total += self.samples[i * self.num_series + j] 79 self.totals.append(total) 80 if max_total is None or total > max_total: 81 max_total = total 82 self.max_total = max_total 83