blob: c8f179e76b6b77b59b5e28f8ac191e6a6a799b62 [file] [log] [blame]
Sergei Trofimov4e6afe92015-10-09 09:30:04 +01001# 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#
15from __future__ import division
16import os
17import csv
18import signal
19import tempfile
20import struct
21import subprocess
22
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010023from devlib.instrument import Instrument, CONTINUOUS, MeasurementsCsv
24from devlib.exception import HostError
25from devlib.utils.misc import which
26
27
28class EnergyProbeInstrument(Instrument):
29
30 mode = CONTINUOUS
31
32 def __init__(self, target, resistor_values,
33 labels=None,
34 device_entry='/dev/ttyACM0',
35 ):
36 super(EnergyProbeInstrument, self).__init__(target)
37 self.resistor_values = resistor_values
38 if labels is not None:
39 self.labels = labels
40 else:
41 self.labels = ['PORT_{}'.format(i)
42 for i in xrange(len(resistor_values))]
43 self.device_entry = device_entry
44 self.caiman = which('caiman')
45 if self.caiman is None:
46 raise HostError('caiman must be installed on the host '
47 '(see https://github.com/ARM-software/caiman)')
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010048 self.attributes_per_sample = 3
49 self.bytes_per_sample = self.attributes_per_sample * 4
50 self.attributes = ['power', 'voltage', 'current']
51 self.command = None
52 self.raw_output_directory = None
53 self.process = None
Brendan Jackman49b547a2017-04-26 15:07:05 +010054 self.sample_rate_hz = 10000 # Determined empirically
Sergei Trofimov823ce712017-08-30 14:55:42 +010055 self.raw_data_file = None
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010056
57 for label in self.labels:
58 for kind in self.attributes:
59 self.add_channel(label, kind)
60
Sergei Trofimov390a5442016-09-02 14:03:33 +010061 def reset(self, sites=None, kinds=None, channels=None):
62 super(EnergyProbeInstrument, self).reset(sites, kinds, channels)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010063 self.raw_output_directory = tempfile.mkdtemp(prefix='eprobe-caiman-')
64 parts = ['-r {}:{} '.format(i, int(1000 * rval))
65 for i, rval in enumerate(self.resistor_values)]
66 rstring = ''.join(parts)
67 self.command = '{} -d {} -l {} {}'.format(self.caiman, self.device_entry, rstring, self.raw_output_directory)
Sergei Trofimov823ce712017-08-30 14:55:42 +010068 self.raw_data_file = None
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010069
70 def start(self):
71 self.logger.debug(self.command)
72 self.process = subprocess.Popen(self.command,
73 stdout=subprocess.PIPE,
74 stderr=subprocess.PIPE,
75 stdin=subprocess.PIPE,
76 preexec_fn=os.setpgrp,
77 shell=True)
78
79 def stop(self):
Brendan Jackmanfdc0c042017-03-24 13:35:00 +000080 self.process.poll()
81 if self.process.returncode is not None:
82 stdout, stderr = self.process.communicate()
83 raise HostError(
84 'Energy Probe: Caiman exited unexpectedly with exit code {}.\n'
85 'stdout:\n{}\nstderr:\n{}'.format(self.process.returncode,
86 stdout, stderr))
Leo Yana48775e2017-06-05 19:14:19 +080087 os.killpg(self.process.pid, signal.SIGINT)
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010088
89 def get_data(self, outfile): # pylint: disable=R0914
90 all_channels = [c.label for c in self.list_channels()]
91 active_channels = [c.label for c in self.active_channels]
92 active_indexes = [all_channels.index(ac) for ac in active_channels]
93
94 num_of_ports = len(self.resistor_values)
95 struct_format = '{}I'.format(num_of_ports * self.attributes_per_sample)
96 not_a_full_row_seen = False
Sergei Trofimov823ce712017-08-30 14:55:42 +010097 self.raw_data_file = os.path.join(self.raw_output_directory, '0000000000')
Sergei Trofimov4e6afe92015-10-09 09:30:04 +010098
Sergei Trofimov823ce712017-08-30 14:55:42 +010099 self.logger.debug('Parsing raw data file: {}'.format(self.raw_data_file))
100 with open(self.raw_data_file, 'rb') as bfile:
Sergei Trofimov4e6afe92015-10-09 09:30:04 +0100101 with open(outfile, 'wb') as wfh:
102 writer = csv.writer(wfh)
103 writer.writerow(active_channels)
104 while True:
105 data = bfile.read(num_of_ports * self.bytes_per_sample)
106 if data == '':
107 break
108 try:
109 unpacked_data = struct.unpack(struct_format, data)
110 row = [unpacked_data[i] / 1000 for i in active_indexes]
111 writer.writerow(row)
112 except struct.error:
113 if not_a_full_row_seen:
114 self.logger.warn('possibly missaligned caiman raw data, row contained {} bytes'.format(len(data)))
115 continue
116 else:
117 not_a_full_row_seen = True
Marc Bonnici049b2752017-08-03 16:43:32 +0100118 return MeasurementsCsv(outfile, self.active_channels, self.sample_rate_hz)
Sergei Trofimov823ce712017-08-30 14:55:42 +0100119
120 def get_raw(self):
121 return [self.raw_data_file]