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 | # |
| 15 | from __future__ import division |
| 16 | import os |
| 17 | import csv |
| 18 | import signal |
| 19 | import tempfile |
| 20 | import struct |
| 21 | import subprocess |
| 22 | |
| 23 | try: |
| 24 | import pandas |
| 25 | except ImportError: |
| 26 | pandas = None |
| 27 | |
| 28 | from devlib.instrument import Instrument, CONTINUOUS, MeasurementsCsv |
| 29 | from devlib.exception import HostError |
| 30 | from devlib.utils.misc import which |
| 31 | |
| 32 | |
| 33 | class EnergyProbeInstrument(Instrument): |
| 34 | |
| 35 | mode = CONTINUOUS |
| 36 | |
| 37 | def __init__(self, target, resistor_values, |
| 38 | labels=None, |
| 39 | device_entry='/dev/ttyACM0', |
| 40 | ): |
| 41 | super(EnergyProbeInstrument, self).__init__(target) |
| 42 | self.resistor_values = resistor_values |
| 43 | if labels is not None: |
| 44 | self.labels = labels |
| 45 | else: |
| 46 | self.labels = ['PORT_{}'.format(i) |
| 47 | for i in xrange(len(resistor_values))] |
| 48 | self.device_entry = device_entry |
| 49 | self.caiman = which('caiman') |
| 50 | if self.caiman is None: |
| 51 | raise HostError('caiman must be installed on the host ' |
| 52 | '(see https://github.com/ARM-software/caiman)') |
| 53 | if pandas is None: |
| 54 | self.logger.info("pandas package will significantly speed up this instrument") |
| 55 | self.logger.info("to install it try: pip install pandas") |
| 56 | self.attributes_per_sample = 3 |
| 57 | self.bytes_per_sample = self.attributes_per_sample * 4 |
| 58 | self.attributes = ['power', 'voltage', 'current'] |
| 59 | self.command = None |
| 60 | self.raw_output_directory = None |
| 61 | self.process = None |
| 62 | |
| 63 | for label in self.labels: |
| 64 | for kind in self.attributes: |
| 65 | self.add_channel(label, kind) |
| 66 | |
Sergei Trofimov | 390a544 | 2016-09-02 14:03:33 +0100 | [diff] [blame] | 67 | def reset(self, sites=None, kinds=None, channels=None): |
| 68 | super(EnergyProbeInstrument, self).reset(sites, kinds, channels) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 69 | self.raw_output_directory = tempfile.mkdtemp(prefix='eprobe-caiman-') |
| 70 | parts = ['-r {}:{} '.format(i, int(1000 * rval)) |
| 71 | for i, rval in enumerate(self.resistor_values)] |
| 72 | rstring = ''.join(parts) |
| 73 | self.command = '{} -d {} -l {} {}'.format(self.caiman, self.device_entry, rstring, self.raw_output_directory) |
| 74 | |
| 75 | def start(self): |
| 76 | self.logger.debug(self.command) |
| 77 | self.process = subprocess.Popen(self.command, |
| 78 | stdout=subprocess.PIPE, |
| 79 | stderr=subprocess.PIPE, |
| 80 | stdin=subprocess.PIPE, |
| 81 | preexec_fn=os.setpgrp, |
| 82 | shell=True) |
| 83 | |
| 84 | def stop(self): |
Brendan Jackman | fdc0c04 | 2017-03-24 13:35:00 +0000 | [diff] [blame^] | 85 | self.process.poll() |
| 86 | if self.process.returncode is not None: |
| 87 | stdout, stderr = self.process.communicate() |
| 88 | raise HostError( |
| 89 | 'Energy Probe: Caiman exited unexpectedly with exit code {}.\n' |
| 90 | 'stdout:\n{}\nstderr:\n{}'.format(self.process.returncode, |
| 91 | stdout, stderr)) |
Sergei Trofimov | 4e6afe9 | 2015-10-09 09:30:04 +0100 | [diff] [blame] | 92 | os.killpg(self.process.pid, signal.SIGTERM) |
| 93 | |
| 94 | def get_data(self, outfile): # pylint: disable=R0914 |
| 95 | all_channels = [c.label for c in self.list_channels()] |
| 96 | active_channels = [c.label for c in self.active_channels] |
| 97 | active_indexes = [all_channels.index(ac) for ac in active_channels] |
| 98 | |
| 99 | num_of_ports = len(self.resistor_values) |
| 100 | struct_format = '{}I'.format(num_of_ports * self.attributes_per_sample) |
| 101 | not_a_full_row_seen = False |
| 102 | raw_data_file = os.path.join(self.raw_output_directory, '0000000000') |
| 103 | |
| 104 | self.logger.debug('Parsing raw data file: {}'.format(raw_data_file)) |
| 105 | with open(raw_data_file, 'rb') as bfile: |
| 106 | with open(outfile, 'wb') as wfh: |
| 107 | writer = csv.writer(wfh) |
| 108 | writer.writerow(active_channels) |
| 109 | while True: |
| 110 | data = bfile.read(num_of_ports * self.bytes_per_sample) |
| 111 | if data == '': |
| 112 | break |
| 113 | try: |
| 114 | unpacked_data = struct.unpack(struct_format, data) |
| 115 | row = [unpacked_data[i] / 1000 for i in active_indexes] |
| 116 | writer.writerow(row) |
| 117 | except struct.error: |
| 118 | if not_a_full_row_seen: |
| 119 | self.logger.warn('possibly missaligned caiman raw data, row contained {} bytes'.format(len(data))) |
| 120 | continue |
| 121 | else: |
| 122 | not_a_full_row_seen = True |
| 123 | return MeasurementsCsv(outfile, self.active_channels) |