clr_rapl.py 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#!/usr/bin/env python
#
# coolr rapl related codes
#
# This code requires the intel_powerclamp module.
#
# Contact: Kazutomo Yoshii <ky@anl.gov>
#

import os, sys, re, time, getopt

12
import clr_nodeinfo
13 14 15

class rapl_reader:
    dryrun = False
16

17
    rapldir='/sys/devices/virtual/powercap/intel-rapl'
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

    re_domain = re.compile('package-([0-9]+)(/\S+)?')

    def readint(self, fn):
        v = -1
        for retry in range(0,10):
            try:
                f = open( fn )
                v = int(f.readline())
                f.close()
            except:
                continue
        return v

    def writeint(self, fn, v):
        ret = False
        try:
            f = open(fn, 'w')
            f.write('%d' % v)
            f.close()
            ret = True
        except:
            ret = False
        return ret
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
    # 
    # e.g.,
    # intel-rapl:0/name
    # intel-rapl:0/intel-rapl:0:0/name
    # intel-rapl:0/intel-rapl:0:1/name
    def __init__ (self):
        self.dirs = {}
        self.max_energy_range_uj_d = {}

        if self.dryrun :
            return 

        self.init = False
        if not os.path.exists(self.rapldir):
            return
        self.init = True

        for d1 in os.listdir(self.rapldir):
            dn = "%s/%s" % (self.rapldir,d1)
            fn = dn + "/name"
            if os.access( fn , os.R_OK ) :
                f = open( fn)
                l = f.readline().strip()
                f.close()
                if re.search('package-[0-9]+', l):
                    self.dirs[l] = dn
                    pkg=l
                    for d2 in os.listdir("%s/%s" % (self.rapldir,d1) ):
                        dn = "%s/%s/%s" % (self.rapldir,d1,d2)
                        fn = dn + "/name"
                        if os.access( fn, os.R_OK ) :
                            f = open(fn)
                            l = f.readline().strip()
                            f.close
                            if re.search('core|dram', l):
                                self.dirs['%s/%s' % (pkg,l)] = dn


        for k in sorted(self.dirs.keys()):
            fn = self.dirs[k] + "/max_energy_range_uj"
            try:
                f = open( fn )
            except:
                print 'Unable to open', fn
                sys.exit(0)
            self.max_energy_range_uj_d[k] = int(f.readline())
            f.close()

        self.start_energy_counter()

    def initialized(self):
        return self.init

    def shortenkey(self,str):
        return str.replace('package-','p')

98 99
    #   for k in sorted(self.dirs.keys()):
    #      print k, self.max_energy_range_uj_d[k]
100 101 102 103 104 105 106 107 108 109 110 111 112


    def readenergy(self):
        if not self.init:
            return

        ret = {}
        ret['time'] = time.time()
        if self.dryrun:
            ret['package-0'] = readuptime()*1000.0*1000.0
            return ret
        for k in sorted(self.dirs.keys()):
            fn = self.dirs[k] + "/energy_uj"
113
            ret[k] = self.readint(fn)
114 115
        return ret

116 117 118 119 120 121 122
    # Read all possible power caps, except package 'short_term', which
    # will be supported later. This function is designed to be called
    # from a slow path. return a dict with long domain names as keys
    # and a value contains a dict with 'curW', 'maxW', 'enabled'


    def readpowerlimitall(self):
123 124 125 126 127 128 129 130
        if not self.init:
            return

        ret = {}
        if self.dryrun:
            ret['package-0'] = 100.0
            return ret
        for k in sorted(self.dirs.keys()):
131 132 133 134 135 136 137 138 139 140 141 142
            dvals = {}
            v = self.readint( self.dirs[k] + '/constraint_0_power_limit_uw' )
            dvals['curW'] = v * 1e-6  # uw to w

            v = self.readint( self.dirs[k] + '/constraint_0_max_power_uw' )
            dvals['maxW'] = v * 1e-6  # uw to w

            dvals['enabled'] = False
            v = self.readint( self.dirs[k] + '/enabled' )
            if v == 1:
                dvals['enabled'] = True
            ret[k] = dvals
143 144
        return ret

145
    def diffenergy(self,e1,e2,shortenFlag=False): # e1 is prev and e2 is not
146 147 148
        ret = {}
        ret['time'] = e2['time'] - e1['time']
        for k in self.max_energy_range_uj_d:
149 150
            if shortenFlag:
                k = self.shortenkey(k)
151 152 153 154 155 156 157 158 159
            if e2[k]>=e1[k]:
                ret[k] = e2[k] - e1[k]
            else:
                ret[k] = (self.max_energy_range_uj_d[k]-e1[k]) + e2[k]
        return ret

    # calculate the average power from two energy values
    # e1 and e2 are the value returned from readenergy()
    # e1 should be sampled before e2
160
    def calcpower(self,e1,e2,shortenFlag=False): 
161 162 163 164 165 166 167 168 169 170
        ret = {}
        delta = e2['time'] - e1['time']  # assume 'time' never wrap around
        ret['delta']  = delta
        if self.dryrun:
            k = 'package-0'
            ret[k] = e2[k] - e1[k]
            ret[k] /= (1000.0*1000.0) # conv. [uW] to [W]
            return ret

        for k in self.max_energy_range_uj_d:
171 172
            if shortenFlag:
                k = self.shortenkey(k)
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
            if e2[k]>=e1[k]:
                ret[k] = e2[k] - e1[k]
            else:
                ret[k] = (self.max_energy_range_uj_d[k]-e1[k]) + e2[k]
            ret[k] /= delta
            ret[k] /= (1000.0*1000.0) # conv. [uW] to [W]
        return ret

    # this should be renamed to reset_...
    def start_energy_counter(self):
        if not self.initialized():
            return

        self.start_time_e = time.time()
        self.totalenergy = {}
        self.lastpower = {}

        e = self.readenergy()
        for k in sorted(e.keys()):
            if k != 'time':
                self.totalenergy[k] = 0.0
                self.lastpower[k] = 0.0
        self.prev_e = e

    # XXX: fix the total energy tracking later
    def read_energy_acc(self):
        if not self.initialized():
            return

        e = self.readenergy()

        de = self.diffenergy(self.prev_e, e)

        for k in sorted(e.keys()):
            if k != 'time':
                self.totalenergy[k] += de[k]
                self.lastpower[k] = de[k]/de['time']/1000.0/1000.0;
        self.prev_e = e

        return e

    def stop_energy_counter(self):
        if not self.initialized():
            return

        e = self.read_energy_acc()
        self.stop_time = time.time()

221
    def sample(self, accflag=False):
222 223 224 225 226 227 228 229 230 231 232
        if not self.initialized():
            return

        e = self.readenergy()

        de = self.diffenergy(self.prev_e, e)

        for k in sorted(e.keys()):
            if k != 'time':
                if accflag:
                    self.totalenergy[k] += de[k]
233
                self.lastpower[k] = de[k]/de['time']/1000.0/1000.0
234 235
        self.prev_e = e

236 237
        ret = dict()
        ret['energy'] = dict()
238 239
        for k in sorted(e.keys()):
            if k != 'time':
240
                ret['energy'][self.shortenkey(k)] = e[k]
241

242
        ret['power'] = dict()
243 244 245
        totalpower = 0.0
        for k in sorted(self.lastpower.keys()):
            if k != 'time':
246 247 248
                ret['power'][self.shortenkey(k)] = self.lastpower[k]
                # this is a bit ad hoc way to calculate the total.
                # needs to be fixed later
249 250
                if k.find("core") == -1:
                    totalpower += self.lastpower[k]
251
        ret['power']['total'] = totalpower
252

253
        ret['powercap'] = dict()
254
        rlimit = self.readpowerlimitall()
255
        for k in sorted(rlimit.keys()):
256
            ret['powercap'][self.shortenkey(k)] = rlimit[k]['curW']
257

258
        return ret
259 260 261 262 263 264 265 266 267 268 269 270 271 272

    def total_energy_json(self):
        if not self.initialized():
            return ''

        dt = self.stop_time - self.start_time_e
        # constructing a json output
        e = self.totalenergy
        s  = '{"total":"energy","difftime":%f' % (dt)
        for k in sorted(e.keys()):
            if k != 'time':
                s += ',"%s":%d' % (self.shortenkey(k), e[k])
        s += '}'
        return s
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409



    def conv_long2short(self, n):
        m = self.re_domain.match(n)
        sn = ''
        if m:
            pkgid = int(m.group(1))
            sn = 'p%d' % (pkgid)

            if m.group(2):
                subname = m.group(2)[1:]
                sn += subname
        return sn

    #
    # APIs for power capping
    #

    def get_powerdomains(self):
        return self.readpowerlimitall().keys

    def get_powerlimits(self):
        return self.readpowerlimitall()

    def _set_powerlimit(self, rrdir, newval, id = 0):
        fn = rrdir + '/constraint_%d_power_limit_uw' % id
        uw = newval * 1e6
        try:
            f = open(fn, 'w')
        except:
            print 'Failed to update:', fn, '(root privilege is required)'
            return
        f.write('%d' % uw)
        f.close()

    def set_powerlimit(self, newval, dom):
        l = self.dirs[dom]
        self._set_powerlimit(l, newval)

    def set_powerlimit_pkg(self, newval):
        rlims = self.readpowerlimitall()
        for k in rlims.keys():
            if re.findall('package-[0-9]$', k):
                self._set_powerlimit(self.dirs[k], newval)

    # Implement the following method
    # is_enabled_rapl(), enable_rapl(), disable_rapl()
    # convshort2long
    # pkgid2cpuids, cpuid2pkgid

def usage():
    print 'clr_rapl.py [options]'
    print ''
    print '--show [pkgid]:   show the current setting'
    print '--limitp val: set the limit to all packages'
    print '         [pkgid:]powerval e.g., 140, 1:120'
    print ''
    print 'If no option is specified, run the test pattern.'
    print ''

def test_conv():
    l = ['package-1', 'package-3/dram',  'package-2/core']

    for s in l:
        rr.conv_long2short(s)

def report_powerlimits():
    l = rr.get_powerlimits()
    for k in l.keys():
        if l[k]['enabled']:
            print k, 'curW:', l[k]['curW'], 'maxW:', l[k]['maxW']

def run_powercap_testbench():
    # hard-coded for Haswell E5-2699v2 dual socket
    print rr.get_powerdomains()
    report_powerlimits()

    w = 10
    rr.set_powerlimit_pkg(120)
    time.sleep(w)
    rr.set_powerlimit_pkg(80)
    time.sleep(w)
    rr.set_powerlimit(130, 'package-1')
    time.sleep(w)
    rr.set_powerlimit_pkg(145)


if __name__ == '__main__':
    rr = rapl_reader()

    if not rr.initialized():
        print 'Error: No intel rapl sysfs found'
        sys.exit(1)

    shortopt = "h"
    longopt = ['getpd', 'getplim', 'setplim=', 'show', 'limitp=', 'testbench' ]
    try:
        opts, args = getopt.getopt(sys.argv[1:], 
                                   shortopt, longopt)

    except getopt.GetoptError, err:
        print err
        usage()
        sys.exit(1)

    for o, a in opts:
        if o in ('-h'):
            usage()
            sys.exit(0)
        elif o in ("--testbench"):
            print 'Start: testbench'
            run_powercap_testbench()
            print 'Stop: testbench'
            sys.exit(0)
        elif o in ("--getpd"):
            print rr.get_powerdomains()
            sys.exit(0)
        elif o in ("--getplim", "--show"):
            report_powerlimits()
            sys.exit(0)
        elif o in ("--setplim", "--limitp"):
            v = float(a)
            rr.set_powerlimit_pkg(v)
            report_powerlimits()
            sys.exit(0)

    rr.start_energy_counter()
    for i in range(0,3):
        time.sleep(1)
        s = rr.sample_and_json(accflag=True)
        print s
    rr.stop_energy_counter()
    s = rr.total_energy_json()
    print s

    sys.exit(0)