Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 1 | # Copyright 2015 ARM Limited |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
Sergei Trofimov | df81742 | 2017-06-06 14:15:40 +0100 | [diff] [blame] | 15 | from __future__ import division |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 16 | import csv |
| 17 | import logging |
Leo Yan | 3229bb1 | 2016-01-05 15:01:18 +0800 | [diff] [blame] | 18 | import collections |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 19 | |
| 20 | from devlib.utils.types import numeric |
| 21 | |
| 22 | |
| 23 | # Channel modes describe what sort of measurement the instrument supports. |
| 24 | # Values must be powers of 2 |
| 25 | INSTANTANEOUS = 1 |
| 26 | CONTINUOUS = 2 |
| 27 | |
Sergei Trofimov | df81742 | 2017-06-06 14:15:40 +0100 | [diff] [blame] | 28 | MEASUREMENT_TYPES = {} # populated further down |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 29 | |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 30 | |
Sergei Trofimov | df81742 | 2017-06-06 14:15:40 +0100 | [diff] [blame] | 31 | class MeasurementType(object): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 32 | |
Sergei Trofimov | df81742 | 2017-06-06 14:15:40 +0100 | [diff] [blame] | 33 | def __init__(self, name, units, category=None, conversions=None): |
| 34 | self.name = name |
| 35 | self.units = units |
| 36 | self.category = category |
| 37 | self.conversions = {} |
| 38 | if conversions is not None: |
| 39 | for key, value in conversions.iteritems(): |
| 40 | if not callable(value): |
| 41 | msg = 'Converter must be callable; got {} "{}"' |
| 42 | raise ValueError(msg.format(type(value), value)) |
| 43 | self.conversions[key] = value |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 44 | |
Sergei Trofimov | df81742 | 2017-06-06 14:15:40 +0100 | [diff] [blame] | 45 | def convert(self, value, to): |
| 46 | if isinstance(to, basestring) and to in MEASUREMENT_TYPES: |
| 47 | to = MEASUREMENT_TYPES[to] |
| 48 | if not isinstance(to, MeasurementType): |
| 49 | msg = 'Unexpected conversion target: "{}"' |
| 50 | raise ValueError(msg.format(to)) |
Marc Bonnici | 5ef99f2 | 2017-08-02 16:59:10 +0100 | [diff] [blame] | 51 | if to.name == self.name: |
| 52 | return value |
Sergei Trofimov | df81742 | 2017-06-06 14:15:40 +0100 | [diff] [blame] | 53 | if not to.name in self.conversions: |
| 54 | msg = 'No conversion from {} to {} available' |
| 55 | raise ValueError(msg.format(self.name, to.name)) |
| 56 | return self.conversions[to.name](value) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 57 | |
| 58 | def __cmp__(self, other): |
| 59 | if isinstance(other, MeasurementType): |
| 60 | other = other.name |
| 61 | return cmp(self.name, other) |
| 62 | |
| 63 | def __str__(self): |
| 64 | return self.name |
| 65 | |
Sergei Trofimov | df81742 | 2017-06-06 14:15:40 +0100 | [diff] [blame] | 66 | def __repr__(self): |
| 67 | if self.category: |
| 68 | text = 'MeasurementType({}, {}, {})' |
| 69 | return text.format(self.name, self.units, self.category) |
| 70 | else: |
| 71 | text = 'MeasurementType({}, {})' |
| 72 | return text.format(self.name, self.units) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 73 | |
| 74 | |
Sergei Trofimov | 01b5cff | 2017-09-07 14:26:04 +0100 | [diff] [blame] | 75 | # Standard measures. In order to make sure that downstream data processing is not tied |
| 76 | # to particular insturments (e.g. a particular method of mearuing power), instruments |
| 77 | # must, where possible, resport their measurments formatted as on of the standard types |
| 78 | # defined here. |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 79 | _measurement_types = [ |
Sergei Trofimov | 01b5cff | 2017-09-07 14:26:04 +0100 | [diff] [blame] | 80 | # For whatever reason, the type of measurement could not be established. |
Sergei Trofimov | 59f36fc | 2017-06-07 10:07:22 +0100 | [diff] [blame] | 81 | MeasurementType('unknown', None), |
Sergei Trofimov | 01b5cff | 2017-09-07 14:26:04 +0100 | [diff] [blame] | 82 | |
| 83 | # Generic measurements |
| 84 | MeasurementType('count', 'count'), |
| 85 | MeasurementType('percent', 'percent'), |
| 86 | |
| 87 | # Time measurement. While there is typically a single "canonical" unit |
| 88 | # used for each type of measurmenent, time may be measured to a wide variety |
| 89 | # of events occuring at a wide range of scales. Forcing everying into a |
| 90 | # single scale will lead to inefficient and awkward to work with result tables. |
| 91 | # Coversion functions between the formats are specified, so that downstream |
| 92 | # processors that expect all times time be at a particular scale can automatically |
| 93 | # covert without being familar with individual instruments. |
Sergei Trofimov | 2afa8f8 | 2017-08-30 14:27:03 +0100 | [diff] [blame] | 94 | MeasurementType('time', 'seconds', 'time', |
Sergei Trofimov | 871c59a | 2017-06-06 14:17:03 +0100 | [diff] [blame] | 95 | conversions={ |
Marc Bonnici | 2de2b36 | 2017-07-25 16:19:08 +0100 | [diff] [blame] | 96 | 'time_us': lambda x: x * 1000000, |
Marc Bonnici | 9b465c2 | 2017-08-18 09:59:23 +0100 | [diff] [blame] | 97 | 'time_ms': lambda x: x * 1000, |
Sergei Trofimov | 871c59a | 2017-06-06 14:17:03 +0100 | [diff] [blame] | 98 | } |
| 99 | ), |
Sergei Trofimov | 2afa8f8 | 2017-08-30 14:27:03 +0100 | [diff] [blame] | 100 | MeasurementType('time_us', 'microseconds', 'time', |
Sergei Trofimov | 871c59a | 2017-06-06 14:17:03 +0100 | [diff] [blame] | 101 | conversions={ |
Marc Bonnici | 2de2b36 | 2017-07-25 16:19:08 +0100 | [diff] [blame] | 102 | 'time': lambda x: x / 1000000, |
Marc Bonnici | 9b465c2 | 2017-08-18 09:59:23 +0100 | [diff] [blame] | 103 | 'time_ms': lambda x: x / 1000, |
| 104 | } |
| 105 | ), |
Sergei Trofimov | 2afa8f8 | 2017-08-30 14:27:03 +0100 | [diff] [blame] | 106 | MeasurementType('time_ms', 'milliseconds', 'time', |
Marc Bonnici | 9b465c2 | 2017-08-18 09:59:23 +0100 | [diff] [blame] | 107 | conversions={ |
| 108 | 'time': lambda x: x / 1000, |
| 109 | 'time_us': lambda x: x * 1000, |
Sergei Trofimov | 871c59a | 2017-06-06 14:17:03 +0100 | [diff] [blame] | 110 | } |
| 111 | ), |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 112 | |
Sergei Trofimov | 01b5cff | 2017-09-07 14:26:04 +0100 | [diff] [blame] | 113 | # Measurements related to thermals. |
| 114 | MeasurementType('temperature', 'degrees', 'thermal'), |
| 115 | |
| 116 | # Measurements related to power end energy consumption. |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 117 | MeasurementType('power', 'watts', 'power/energy'), |
| 118 | MeasurementType('voltage', 'volts', 'power/energy'), |
| 119 | MeasurementType('current', 'amps', 'power/energy'), |
| 120 | MeasurementType('energy', 'joules', 'power/energy'), |
| 121 | |
Sergei Trofimov | 01b5cff | 2017-09-07 14:26:04 +0100 | [diff] [blame] | 122 | # Measurments realted to data transfer, e.g. neworking, |
| 123 | # memory, or backing storage. |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 124 | MeasurementType('tx', 'bytes', 'data transfer'), |
| 125 | MeasurementType('rx', 'bytes', 'data transfer'), |
| 126 | MeasurementType('tx/rx', 'bytes', 'data transfer'), |
Sergei Trofimov | 871c59a | 2017-06-06 14:17:03 +0100 | [diff] [blame] | 127 | |
Sergei Trofimov | 01b5cff | 2017-09-07 14:26:04 +0100 | [diff] [blame] | 128 | MeasurementType('fps', 'fps', 'ui render'), |
Sergei Trofimov | 871c59a | 2017-06-06 14:17:03 +0100 | [diff] [blame] | 129 | MeasurementType('frames', 'frames', 'ui render'), |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 130 | ] |
Sergei Trofimov | df81742 | 2017-06-06 14:15:40 +0100 | [diff] [blame] | 131 | for m in _measurement_types: |
| 132 | MEASUREMENT_TYPES[m.name] = m |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 133 | |
| 134 | |
| 135 | class Measurement(object): |
| 136 | |
| 137 | __slots__ = ['value', 'channel'] |
| 138 | |
| 139 | @property |
| 140 | def name(self): |
| 141 | return '{}_{}'.format(self.channel.site, self.channel.kind) |
| 142 | |
| 143 | @property |
| 144 | def units(self): |
| 145 | return self.channel.units |
| 146 | |
| 147 | def __init__(self, value, channel): |
| 148 | self.value = value |
| 149 | self.channel = channel |
| 150 | |
| 151 | def __cmp__(self, other): |
Sergei Trofimov | 8479af4 | 2017-09-07 10:13:59 +0100 | [diff] [blame] | 152 | if hasattr(other, 'value'): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 153 | return cmp(self.value, other.value) |
| 154 | else: |
| 155 | return cmp(self.value, other) |
| 156 | |
| 157 | def __str__(self): |
| 158 | if self.units: |
| 159 | return '{}: {} {}'.format(self.name, self.value, self.units) |
| 160 | else: |
| 161 | return '{}: {}'.format(self.name, self.value) |
| 162 | |
| 163 | __repr__ = __str__ |
| 164 | |
| 165 | |
| 166 | class MeasurementsCsv(object): |
| 167 | |
Marc Bonnici | d3c3015 | 2017-08-03 12:13:48 +0100 | [diff] [blame] | 168 | def __init__(self, path, channels=None, sample_rate_hz=None): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 169 | self.path = path |
| 170 | self.channels = channels |
Marc Bonnici | d3c3015 | 2017-08-03 12:13:48 +0100 | [diff] [blame] | 171 | self.sample_rate_hz = sample_rate_hz |
Sergei Trofimov | 59f36fc | 2017-06-07 10:07:22 +0100 | [diff] [blame] | 172 | if self.channels is None: |
| 173 | self._load_channels() |
Sergei Trofimov | 8479af4 | 2017-09-07 10:13:59 +0100 | [diff] [blame] | 174 | headings = [chan.label for chan in self.channels] |
| 175 | self.data_tuple = collections.namedtuple('csv_entry', headings) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 176 | |
| 177 | def measurements(self): |
Sergei Trofimov | dfd0b8e | 2017-08-21 11:01:42 +0100 | [diff] [blame] | 178 | return list(self.iter_measurements()) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 179 | |
Sergei Trofimov | dfd0b8e | 2017-08-21 11:01:42 +0100 | [diff] [blame] | 180 | def iter_measurements(self): |
Sergei Trofimov | 8479af4 | 2017-09-07 10:13:59 +0100 | [diff] [blame] | 181 | for row in self._iter_rows(): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 182 | values = map(numeric, row) |
| 183 | yield [Measurement(v, c) for (v, c) in zip(values, self.channels)] |
| 184 | |
Sergei Trofimov | 8479af4 | 2017-09-07 10:13:59 +0100 | [diff] [blame] | 185 | def values(self): |
| 186 | return list(self.iter_values()) |
| 187 | |
| 188 | def iter_values(self): |
| 189 | for row in self._iter_rows(): |
| 190 | values = map(numeric, row) |
| 191 | yield self.data_tuple(*values) |
| 192 | |
Sergei Trofimov | 59f36fc | 2017-06-07 10:07:22 +0100 | [diff] [blame] | 193 | def _load_channels(self): |
Sergei Trofimov | 8479af4 | 2017-09-07 10:13:59 +0100 | [diff] [blame] | 194 | header = [] |
| 195 | with open(self.path, 'rb') as fh: |
| 196 | reader = csv.reader(fh) |
| 197 | header = reader.next() |
Sergei Trofimov | 59f36fc | 2017-06-07 10:07:22 +0100 | [diff] [blame] | 198 | |
| 199 | self.channels = [] |
| 200 | for entry in header: |
| 201 | for mt in MEASUREMENT_TYPES: |
| 202 | suffix = '_{}'.format(mt) |
| 203 | if entry.endswith(suffix): |
| 204 | site = entry[:-len(suffix)] |
| 205 | measure = mt |
Sergei Trofimov | 59f36fc | 2017-06-07 10:07:22 +0100 | [diff] [blame] | 206 | break |
| 207 | else: |
Sergei Trofimov | 8479af4 | 2017-09-07 10:13:59 +0100 | [diff] [blame] | 208 | if entry in MEASUREMENT_TYPES: |
| 209 | site = None |
| 210 | measure = entry |
| 211 | else: |
| 212 | site = entry |
| 213 | measure = 'unknown' |
Sergei Trofimov | 59f36fc | 2017-06-07 10:07:22 +0100 | [diff] [blame] | 214 | |
Sergei Trofimov | 9192deb | 2017-08-31 17:38:33 +0100 | [diff] [blame] | 215 | chan = InstrumentChannel(site, measure) |
Sergei Trofimov | 59f36fc | 2017-06-07 10:07:22 +0100 | [diff] [blame] | 216 | self.channels.append(chan) |
| 217 | |
Sergei Trofimov | 8479af4 | 2017-09-07 10:13:59 +0100 | [diff] [blame] | 218 | def _iter_rows(self): |
| 219 | with open(self.path, 'rb') as fh: |
| 220 | reader = csv.reader(fh) |
| 221 | reader.next() # headings |
| 222 | for row in reader: |
| 223 | yield row |
| 224 | |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 225 | |
| 226 | class InstrumentChannel(object): |
| 227 | |
| 228 | @property |
| 229 | def label(self): |
Sergei Trofimov | 07ba177 | 2017-09-07 09:52:20 +0100 | [diff] [blame] | 230 | if self.site is not None: |
| 231 | return '{}_{}'.format(self.site, self.kind) |
| 232 | return self.kind |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 233 | |
Sergei Trofimov | 9192deb | 2017-08-31 17:38:33 +0100 | [diff] [blame] | 234 | name = label |
| 235 | |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 236 | @property |
| 237 | def kind(self): |
| 238 | return self.measurement_type.name |
| 239 | |
| 240 | @property |
| 241 | def units(self): |
| 242 | return self.measurement_type.units |
| 243 | |
Sergei Trofimov | 9192deb | 2017-08-31 17:38:33 +0100 | [diff] [blame] | 244 | def __init__(self, site, measurement_type, **attrs): |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 245 | self.site = site |
| 246 | if isinstance(measurement_type, MeasurementType): |
| 247 | self.measurement_type = measurement_type |
| 248 | else: |
| 249 | try: |
| 250 | self.measurement_type = MEASUREMENT_TYPES[measurement_type] |
| 251 | except KeyError: |
| 252 | raise ValueError('Unknown measurement type: {}'.format(measurement_type)) |
| 253 | for atname, atvalue in attrs.iteritems(): |
| 254 | setattr(self, atname, atvalue) |
| 255 | |
| 256 | def __str__(self): |
| 257 | if self.name == self.label: |
| 258 | return 'CHAN({})'.format(self.label) |
| 259 | else: |
| 260 | return 'CHAN({}, {})'.format(self.name, self.label) |
| 261 | |
| 262 | __repr__ = __str__ |
| 263 | |
| 264 | |
| 265 | class Instrument(object): |
| 266 | |
| 267 | mode = 0 |
| 268 | |
| 269 | def __init__(self, target): |
| 270 | self.target = target |
| 271 | self.logger = logging.getLogger(self.__class__.__name__) |
Leo Yan | 3229bb1 | 2016-01-05 15:01:18 +0800 | [diff] [blame] | 272 | self.channels = collections.OrderedDict() |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 273 | self.active_channels = [] |
Brendan Jackman | 49b547a | 2017-04-26 15:07:05 +0100 | [diff] [blame] | 274 | self.sample_rate_hz = None |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 275 | |
| 276 | # channel management |
| 277 | |
| 278 | def list_channels(self): |
| 279 | return self.channels.values() |
| 280 | |
| 281 | def get_channels(self, measure): |
| 282 | if hasattr(measure, 'name'): |
| 283 | measure = measure.name |
Brendan Jackman | 1bc29d7 | 2017-04-25 15:56:14 +0100 | [diff] [blame] | 284 | return [c for c in self.list_channels() if c.kind == measure] |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 285 | |
Sergei Trofimov | 9192deb | 2017-08-31 17:38:33 +0100 | [diff] [blame] | 286 | def add_channel(self, site, measure, **attrs): |
| 287 | chan = InstrumentChannel(site, measure, **attrs) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 288 | self.channels[chan.label] = chan |
| 289 | |
| 290 | # initialization and teardown |
| 291 | |
| 292 | def setup(self, *args, **kwargs): |
| 293 | pass |
| 294 | |
| 295 | def teardown(self): |
| 296 | pass |
| 297 | |
Sergei Trofimov | 390a544 | 2016-09-02 14:03:33 +0100 | [diff] [blame] | 298 | def reset(self, sites=None, kinds=None, channels=None): |
Brendan Jackman | 1513db0 | 2017-05-10 11:18:21 +0100 | [diff] [blame] | 299 | self.active_channels = [] |
Sergei Trofimov | 390a544 | 2016-09-02 14:03:33 +0100 | [diff] [blame] | 300 | if kinds is None and sites is None and channels is None: |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 301 | self.active_channels = sorted(self.channels.values(), key=lambda x: x.label) |
Brendan Jackman | 1513db0 | 2017-05-10 11:18:21 +0100 | [diff] [blame] | 302 | elif channels is not None: |
| 303 | if sites is not None or kinds is not None: |
| 304 | raise ValueError( |
| 305 | 'sites and kinds should not be set if channels is set') |
| 306 | for chan_name in channels: |
Sergei Trofimov | 390a544 | 2016-09-02 14:03:33 +0100 | [diff] [blame] | 307 | try: |
| 308 | self.active_channels.append(self.channels[chan_name]) |
| 309 | except KeyError: |
| 310 | msg = 'Unexpected channel "{}"; must be in {}' |
| 311 | raise ValueError(msg.format(chan_name, self.channels.keys())) |
Brendan Jackman | 1513db0 | 2017-05-10 11:18:21 +0100 | [diff] [blame] | 312 | else: |
| 313 | if isinstance(sites, basestring): |
| 314 | sites = [sites] |
| 315 | if isinstance(kinds, basestring): |
| 316 | kinds = [kinds] |
| 317 | else: |
| 318 | for chan in self.channels.values(): |
| 319 | if (kinds is None or chan.kind in kinds) and \ |
| 320 | (sites is None or chan.site in sites): |
| 321 | self.active_channels.append(chan) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 322 | |
| 323 | # instantaneous |
| 324 | |
| 325 | def take_measurement(self): |
| 326 | pass |
| 327 | |
| 328 | # continuous |
| 329 | |
| 330 | def start(self): |
| 331 | pass |
| 332 | |
| 333 | def stop(self): |
| 334 | pass |
| 335 | |
| 336 | def get_data(self, outfile): |
| 337 | pass |
Sergei Trofimov | 823ce71 | 2017-08-30 14:55:42 +0100 | [diff] [blame] | 338 | |
| 339 | def get_raw(self): |
| 340 | return [] |