sensor.py 2.32 KB
Newer Older
1 2 3 4 5 6 7
"""Sensor Module:
    provide the core functionalities related to measuring power, energy,
    temperature and other information about the local node, using our internal
    version of the coolr code.

    This module should be the only one interfacing with coolr.
"""
8
from __future__ import print_function
9
import time
10 11 12 13 14 15
import coolr
import coolr.clr_rapl
import coolr.clr_hwmon
import coolr.clr_nodeinfo
import coolr.clr_cpufreq
import coolr.clr_misc
16 17 18 19 20 21


class SensorManager:
    """Performs sensor reading and basic data aggregation."""

    def __init__(self):
22
        self.nodeconfig = coolr.clr_nodeinfo.nodeconfig()
23
        self.nodename = self.nodeconfig.nodename
24 25 26 27 28 29 30 31 32
        self.cputopology = coolr.clr_nodeinfo.cputopology()
        self.coretemp = coolr.clr_hwmon.coretemp_reader()
        self.rapl = coolr.clr_rapl.rapl_reader()

    def start(self):
        self.rapl.start_energy_counter()

    def stop(self):
        self.rapl.stop_energy_counter()
33 34

    def do_update(self):
35 36 37
        machine_info = dict()
        machine_info['energy'] = self.rapl.sample(accflag=True)
        machine_info['temperature'] = self.coretemp.sample()
38
        machine_info['time'] = time.time()
39
        return machine_info
40 41 42 43 44 45

    def get_powerlimits(self):
        pl = self.rapl.get_powerlimits()
        # only return enabled domains
        return {k: pl[k] for k in pl if pl[k]['enabled']}

46
    def set_powerlimit(self, domain, value):
47
        self.rapl.set_powerlimit(value, domain)
48 49 50

    def calc_difference(self, start, end):
        diff = dict()
51 52 53 54 55 56 57
        for k in start.keys():
            if k not in ['time']:
                start[k.replace('p', 'package-')] = start[k]
                start.pop(k)
                end[k.replace('p', 'package-')] = end[k]
                end.pop(k)

58
        # Calculate energy difference
59
        diff['energy'] = self.rapl.diffenergy(start, end)
60 61 62 63 64 65 66 67 68
        # Update time elapsed
        diff['time'] = diff['energy']['time']
        # Remove 'time' field returned by function
        diff['energy'].pop('time')
        # Convert uJ to J
        diff['energy'] = {k: diff['energy'][k]/(1000000.0) for k in
                          diff['energy']}

        # Calculate power difference
69
        diff['power'] = self.rapl.calcpower(start, end)
70 71 72 73
        # Remove 'delta' field returned by function
        diff['power'].pop('delta')

        return diff