1 # Copyright 2014 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.
5 import telemetry
.timeline
.event_container
as event_container
8 # Doesn't inherit from TimelineEvent because its only a temporary wrapper of a
9 # counter sample into an event. During stable operation, the samples are stored
10 # a dense array of values rather than in the long-form done by an Event.
11 class CounterSample(object):
12 def __init__(self
, counter
, sample_index
):
13 self
._counter
= counter
14 self
._sample
_index
= sample_index
18 return self
._counter
.category
22 return self
._counter
.full_name
26 return self
._counter
.samples
[self
._sample
_index
]
30 return self
._counter
.timestamps
[self
._sample
_index
]
33 def start(self
, start
):
34 self
._counter
.timestamps
[self
._sample
_index
] = start
45 def thread_start(self
):
49 def thread_duration(self
):
57 class Counter(event_container
.TimelineEventContainer
):
58 """ Stores all the samples for a given counter.
60 def __init__(self
, parent
, category
, name
):
61 super(Counter
, self
).__init
__(name
, parent
)
62 self
.category
= category
63 self
.full_name
= category
+ '.' + name
66 self
.series_names
= []
70 def IterChildContainers(self
):
72 yield # pylint: disable=unreachable
74 def IterEventsInThisContainer(self
, event_type_predicate
, event_predicate
):
75 if not event_type_predicate(CounterSample
) or not self
.timestamps
:
78 # Pass event_predicate a reused CounterSample instance to avoid
79 # creating a ton of garbage for rejected samples.
80 test_sample
= CounterSample(self
, 0)
81 for i
in xrange(len(self
.timestamps
)):
82 test_sample
._sample
_index
= i
# pylint: disable=protected-access
83 if event_predicate(test_sample
):
84 yield CounterSample(self
, i
)
88 return len(self
.series_names
)
91 def num_samples(self
):
92 return len(self
.timestamps
)
94 def FinalizeImport(self
):
95 if self
.num_series
* self
.num_samples
!= len(self
.samples
):
97 'Length of samples must be a multiple of length of timestamps.')
101 if not len(self
.samples
):
105 for i
in xrange(self
.num_samples
):
107 for j
in xrange(self
.num_series
):
108 total
+= self
.samples
[i
* self
.num_series
+ j
]
109 self
.totals
.append(total
)
110 if max_total
is None or total
> max_total
:
112 self
.max_total
= max_total