clr_hwmon.py 6.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
#!/usr/bin/env python
#
# coolr hwmon related codes
#
# This code requires the coretemp driver for temperature reading
#
# Contact: Kazutomo Yoshii <ky@anl.gov>
#

import re, os, sys
import numpy as np
from clr_nodeinfo import *

class coretemp_reader :
    def parse_pkgtemp(self,fn):
        retval = -1
        try:
            f = open(fn , "r")
        except:
            return retval
        l = f.readline()
        m = re.search('Physical id ([0-9]+)', l )
        if m:
            retval=int(m.group(1))
        f.close()
        return retval

    def parse_coretemp(self,fn):
        retval = -1
        try:
            f = open(fn , "r")
        except:
            return retval
        l = f.readline()
        m = re.search('Core ([0-9]+)', l )
        if m:
            retval=int(m.group(1))
        f.close()
        return retval

    hwmondir = '/sys/class/hwmon/'

    class coretempinfo:
        def __init__(self):
            self.dir = ''
            self.coretempfns = {} # use coreid as key
            self.pkgtempfn = ''

    def __init__ (self):
        self.outputpercore(True)

        self.coretemp = {} # use pkgid as  key
        for d1 in os.listdir(self.hwmondir):
            # try to check see if 'name' contains 'coretemp'
            tmpdir = "%s%s" % (self.hwmondir,d1)
            drivername = readbuf("%s/name" % tmpdir).rstrip()
            if not drivername == "coretemp":
                continue

            pkgid = -1
            coretempfns = {}
            pkgtempfn = ''
            # parse all temp*_label files
            for d2 in os.listdir( tmpdir ):
                m = re.search( 'temp([0-9]+)_label', d2 )
                if m:
                    tempid=int(m.group(1))
                    coreid = self.parse_coretemp("%s/%s" % (tmpdir, d2))
                    if coreid >= 0 :
                        coretempfns[coreid] = "%s/temp%d_input" % (tmpdir, tempid)
                    else: # possibly pkgid
                        pkgtempfn = "%s/temp%d_input" % (tmpdir, tempid)
                        pkgid = self.parse_pkgtemp("%s/%s" % (tmpdir, d2))
                        if pkgid < 0 :
                            print 'unlikely: ', pkgtempfn



            cti = self.coretempinfo()
            cti.dir = tmpdir
            cti.coretempfns = coretempfns
            cti.pkgtempfn = pkgtempfn

            if pkgid < 0: # assume a single socket machine
                self.coretemp[0] = cti
            else:
                self.coretemp[pkgid] = cti

    def readtempall(self):
        ctemp = self.coretemp
        ret = {}
        for pkgid in sorted(ctemp.keys()):
            temps = {}
            if os.access(ctemp[pkgid].pkgtempfn, os.R_OK):
                val = int(readbuf(ctemp[pkgid].pkgtempfn))/1000
                temps['pkg'] = val
            for c in sorted(ctemp[pkgid].coretempfns.keys()):
                if os.access(ctemp[pkgid].coretempfns[c], os.R_OK):
                    val = int(readbuf(ctemp[pkgid].coretempfns[c]))/1000
                    temps[c] = val
            ret[pkgid] = temps
        return ret

    def outputpercore(self,flag=True):
        self.percore=flag

    def sample_and_json(self,node = ""):
        temp = self.readtempall()
        # constructing a json output
        s  = '{"sample":"temp","time":%.3f' \
            % (time.time())
        if len(node) > 0:
            s += ',"node":"%s"' % node
        for p in sorted(temp.keys()):
            s += ',"p%d":{' % p

            pstat = self.getpkgstats(temp, p)

            s += '"mean":%.2lf ' % pstat[0]
            s += ',"std":%.2lf ' % pstat[1]
            s += ',"min":%.2lf ' % pstat[2]
            s += ',"max":%.2lf ' % pstat[3]

            if self.percore:
                for c in sorted(temp[p].keys()):
                    s += ',"%s":%d' % (c, temp[p][c])
            s += '}'
        s += '}'

        return s

    def getmaxcoretemp(self, temps):
        vals = []
        for pkgid in temps.keys():
            for c in temps[pkgid].keys():
                vals.append(temps[pkgid][c])
        return np.max(vals)

    def getpkgstats(self, temps, pkgid):
        vals = []
        for c in temps[pkgid].keys():
            vals.append(temps[pkgid][c])
        return [np.mean(vals), np.std(vals), np.min(vals), np.max(vals)]
                    
    def readpkgtemp(self):
        fn = "%s_input" % self.pkgtempfns[pkgid].pkgfn
        f = open(fn) 
        v = int(f.readline())/1000.0
        f.close()
        return v

    def readcoretemp(self,pkgid):
        t = []
        for fnbase in self.pkgtempfns[pkgid].corefns:
            fn = "%s_input" % fnbase
            if not os.access( fn, os.R_OK ):
                continue  # cpu may become offline
            f = open(fn) 
            v = int(f.readline())/1000.0
            f.close()
            t.append(v)
        return t


class acpi_power_meter_reader :
    # add a nicer detection routine later
    def __init__(self):
        self.init = False
        fn = '/sys/class/hwmon/hwmon0/device/power1_average'
        if os.path.exists(fn):
            self.init = True

    def initialized(self):
        return self.init

    def read(self):
        if not self.init:
            return -1
            
        retval=-1

        fn = '/sys/class/hwmon/hwmon0/device/power1_average'
        try:
            f = open(fn , "r")
        except:
            return retval

        l = f.readline()
        retval = int(l) * 1e-6 # uW to W
        f.close()
        return retval

    def sample_and_json(self, node=""):
        if not self.init:
            return ''

        pwr = self.read()
        buf = '{"sample":"acpi", "time":%.3f' % time.time()
        if len(node) > 0:
            buf += ',"node":"%s"' % node
        buf += ',"power":%.2lf}' % pwr

        return buf


if __name__ == '__main__':

    acpipwr = acpi_power_meter_reader()

    if acpipwr.initialized():
        print acpipwr.sample_and_json('testnode')

    ctr = coretemp_reader()
    ctr.outputpercore(False)

    temp = ctr.readtempall()

    for p in sorted(temp.keys()):
        print 'pkg%d:' % p,
        for c in sorted(temp[p].keys()):
            print "%s=%d " % (c, temp[p][c]),
        print

    for i in range(0,3):
        s = ctr.sample_and_json()
        print s
        time.sleep(1)

    # measure the time to read all temp
    # note: reading temp on other core triggers an IPI, 
    # so reading temp frequency will icreate the CPU load
    print 'Measuring readtime() and getmaxcoretemp ...'
    for i in range(0,3):
        a = time.time()
        temp = ctr.readtempall()
        maxcoretemp = ctr.getmaxcoretemp(temp)
        b = time.time()
        print '  %.1f msec, maxcoretemp=%d' % ((b-a)*1000.0, maxcoretemp),

        for p in sorted(temp.keys()):
            s = ctr.getpkgstats(temp, p)
            print ' mean=%.2lf std=%.2lf min=%.1lf max=%.1lf' % \
                (s[0], s[1], s[2], s[3]),

        print

        time.sleep(1)

    print