• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Copyright (C) 2015, ARM Limited and contributors.
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19
20import logging
21import getopt
22import sys
23
24# Sampler configuration
25config = {
26    'period'  : 0,
27    'samples' : 0,
28}
29
30class OdroidSampler(object):
31
32    sysfs_base = '/sys/devices/12c60000.i2c/i2c-4/i2c-dev/i2c-4/device'
33    sysfs = {
34        'arm' : sysfs_base + '/4-0040',
35        'kfc' : sysfs_base + '/4-0045',
36    }
37    power = {
38        'arm' : sysfs['arm'] + '/sensor_W',
39        'kfc' : sysfs['kfc'] + '/sensor_W',
40    }
41
42    def __init__(self, samples, period):
43        self.energy_proxy = {
44            'arm' : 0,
45            'kfc' : 0,
46        }
47        self.samples = samples
48        self.period = period
49
50    def sample(self, device):
51        f = open(self.power[device], 'r')
52        power = f.readline()
53        self.energy_proxy[device] += power
54        f.close()
55
56    def averagePower(self, device):
57        self.energy_proxy[device] = 0
58        for i in range(0, self.samples):
59            sample(self, device)
60            sleep(self.period / 1e6)
61        return self.energy_proxy[device] / self.samples
62
63
64def parseOptions():
65    global config
66
67    logging.debug('Parsing options')
68
69    try:
70        opts, args = getopt.getopt(sys.argv[1:], "p:s:", ["period=", "samples="])
71    except getopt.GetoptError as err:
72        # print help information and exit:
73        print str(err)
74        usage()
75        sys.exit(2)
76    output = None
77    verbose = False
78    for o, a in opts:
79        if o == "-v":
80            verbose = True
81        elif o in ("-p", "--period"):
82            config['period'] = int(a)
83        elif o in ("-s", "--samples"):
84            config['samples'] = int(a)
85        else:
86            assert False, "unhandled option"
87
88    logging.info('Sampler configured for {0:d} samples, evenry {1:.3f}ms'\
89        .format(config['samples'], config['period']/1000))
90
91def main():
92
93    parseOptions()
94
95    sampler = OdroidSampler(config['samples'], config['period'])
96
97    avg_power = amples.averagePower(config['device'])
98    logging.info('Average power: {0:f}'.format(avg_power))
99
100logging.basicConfig(
101    format='%(asctime)-9s %(levelname)-8s: %(message)s',
102    level=logging.DEBUG,
103    datefmt='%I:%M:%S')
104
105if __name__ == "__main__":
106        main()
107