blob: 4b145d91597d7ac67990f86ec9c1418fb4c1cdc4 [file] [log] [blame]
Quentin Perret7a827e22017-05-17 18:17:29 +01001# Copyright 2017 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
15from __future__ import division
16import csv
17import re
18
19from devlib.platform.gem5 import Gem5SimulationPlatform
20from devlib.instrument import Instrument, CONTINUOUS, MeasurementsCsv
21from devlib.exception import TargetError, HostError
22
23
24class Gem5PowerInstrument(Instrument):
25 '''
26 Instrument enabling power monitoring in gem5
27 '''
28
29 mode = CONTINUOUS
30 roi_label = 'power_instrument'
Marc Bonnici411719d2017-08-18 17:06:28 +010031 site_mapping = {'timestamp': 'sim_seconds'}
Marc Bonnici7dd934a2017-08-03 16:41:10 +010032
Quentin Perret7a827e22017-05-17 18:17:29 +010033 def __init__(self, target, power_sites):
34 '''
Marc Bonnici7dd934a2017-08-03 16:41:10 +010035 Parameter power_sites is a list of gem5 identifiers for power values.
Quentin Perret7a827e22017-05-17 18:17:29 +010036 One example of such a field:
37 system.cluster0.cores0.power_model.static_power
38 '''
39 if not isinstance(target.platform, Gem5SimulationPlatform):
40 raise TargetError('Gem5PowerInstrument requires a gem5 platform')
41 if not target.has('gem5stats'):
42 raise TargetError('Gem5StatsModule is not loaded')
43 super(Gem5PowerInstrument, self).__init__(target)
44
45 # power_sites is assumed to be a list later
46 if isinstance(power_sites, list):
47 self.power_sites = power_sites
48 else:
49 self.power_sites = [power_sites]
Marc Bonnici411719d2017-08-18 17:06:28 +010050 self.add_channel('timestamp', 'time')
Quentin Perret7a827e22017-05-17 18:17:29 +010051 for field in self.power_sites:
52 self.add_channel(field, 'power')
53 self.target.gem5stats.book_roi(self.roi_label)
54 self.sample_period_ns = 10000000
Marc Bonnici7dd934a2017-08-03 16:41:10 +010055 # Sample rate must remain unset as gem5 does not provide samples
56 # at regular intervals therefore the reported timestamp should be used.
57 self.sample_rate_hz = None
Quentin Perret7a827e22017-05-17 18:17:29 +010058 self.target.gem5stats.start_periodic_dump(0, self.sample_period_ns)
Quentin Perret38258eb2017-07-21 16:13:31 +010059 self._base_stats_dump = 0
Quentin Perret7a827e22017-05-17 18:17:29 +010060
61 def start(self):
62 self.target.gem5stats.roi_start(self.roi_label)
63
64 def stop(self):
65 self.target.gem5stats.roi_end(self.roi_label)
Marc Bonnici7dd934a2017-08-03 16:41:10 +010066
Quentin Perret7a827e22017-05-17 18:17:29 +010067 def get_data(self, outfile):
68 active_sites = [c.site for c in self.active_channels]
69 with open(outfile, 'wb') as wfh:
70 writer = csv.writer(wfh)
71 writer.writerow([c.label for c in self.active_channels]) # headers
Marc Bonnici411719d2017-08-18 17:06:28 +010072 sites_to_match = [self.site_mapping.get(s, s) for s in active_sites]
73 for rec, rois in self.target.gem5stats.match_iter(sites_to_match,
Quentin Perret38258eb2017-07-21 16:13:31 +010074 [self.roi_label], self._base_stats_dump):
Quentin Perret34d73e62017-08-17 10:40:10 +010075 writer.writerow([rec[s] for s in active_sites])
Marc Bonnici049b2752017-08-03 16:43:32 +010076 return MeasurementsCsv(outfile, self.active_channels, self.sample_rate_hz)
Marc Bonnici7dd934a2017-08-03 16:41:10 +010077
Quentin Perret7a827e22017-05-17 18:17:29 +010078 def reset(self, sites=None, kinds=None, channels=None):
79 super(Gem5PowerInstrument, self).reset(sites, kinds, channels)
Quentin Perret38258eb2017-07-21 16:13:31 +010080 self._base_stats_dump = self.target.gem5stats.next_dump_no()
Quentin Perret7a827e22017-05-17 18:17:29 +010081