-
Notifications
You must be signed in to change notification settings - Fork 1
/
coolrs.py
executable file
·346 lines (287 loc) · 9.18 KB
/
coolrs.py
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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
#!/usr/bin/env python3
#
# coolr monitoring library
#
# the following kernel modules must be installed
#
# - coretemp
# - intel_powerclamp
#
# the following kernel modules are optional
#
# - acpi_power_meter
# - cpustat (coolr-hpc)
#
# written by Kazutomo Yoshii <[email protected]>
#
import os, sys, re, time
import numpy as np
import subprocess
import threading
import signal
# local
import keypress
import clr_rapl
import clr_hwmon
import clr_nodeinfo
#import clr_amperf
import clr_cpufreq
from clr_misc import *
#
#
debuglevel=0
#
#
import getopt
def usage():
print('')
print('Usage: coolrmon.py [options] cmd args..')
print('')
print('[options]')
print('')
print('--runtests : run internal test funcs')
print('-C or --cooldown temp : wait until the max coretemp gets below temp')
print('')
class coolrmon_tracer:
def sample_acpi(self, label):
if self.acpi.initialized():
s = self.acpi.sample_and_json(node=self.nc.nodename)
self.logger(s)
def sample_freq(self,label):
#s = self.amp.sample_and_json()
if self.freq.init:
s = self.freq.sample_and_json(node=self.nc.nodename)
self.logger(s)
def sample_temp(self,label):
s = self.ctr.sample_and_json(node=self.nc.nodename)
self.logger(s)
def sample_energy(self, label):
accflag = False
if label == 'run' :
accflag = True
s = self.rapl.sample_and_json(label, accflag,\
node=self.nc.nodename)
self.logger(s)
def cooldown(self,label):
if self.cooldowntemp < 0:
return
while True:
self.sample_energy(label)
self.sample_temp(label)
# currently use only maxcoretemp
# XXX: readtempall() is called from sample_temp()
# possible to optimize this
temp = self.ctr.readtempall()
maxcoretemp = self.ctr.getmaxcoretemp(temp)
if maxcoretemp < self.cooldowntemp:
break
time.sleep(self.intervalsec)
def setcooldowntemp(self,v):
self.cooldowntemp = v
def setinterval(self,isec):
self.intervalsec = isec
def setbeacon(self):
self.beacon = True
self.ctr.outputpercore(False)
self.freq.outputpercore(False)
def setlogger(self,func):
self.logger = func
def defaultlogger(self,str):
print(str)
def __init__ (self):
# default values
self.cooldowntemp = 45 # depend on arch
#self.output = sys.stdout
self.intervalsec = 1
#XXX: demo
self.beacon = False
self.logger = self.defaultlogger
# instantiate class
self.nc = clr_nodeinfo.nodeconfig()
self.ct = clr_nodeinfo.cputopology()
self.samples = []
self.ctr = clr_hwmon.coretemp_reader()
self.samples.append("temp") # XXX: fix coretemp_reader.init
self.rapl = clr_rapl.rapl_reader()
if self.rapl.init:
self.samples.append("energy")
#self.amp = clr_amperf.amperf_reader()
self.freq = clr_cpufreq.cpufreq_reader()
if self.freq.init:
self.samples.append("freq")
self.acpi = clr_hwmon.acpi_power_meter_reader()
if self.acpi.init:
self.samples.append("acpi")
def shownodeinfo(self):
s = '{"nodeinfo":"%s"' % self.nc.nodename
s += ',"kernelversion":"%s"' % self.nc.version
s += ',"cpumodel":%d' % self.nc.cpumodel
s += ',"memoryKB":%d' % self.nc.memoryKB
s += ',"freqdriver":"%s"' % self.nc.freqdriver
s += ',"samples":[%s]' % \
','.join(map((lambda s:'"%s"'%s), self.samples))
ncpus = len(self.ct.onlinecpus)
s += ',"ncpus":%d' % ncpus
npkgs = len(list(self.ct.pkgcpus.keys()))
s += ',"npkgs":%d' % npkgs
for p in sorted(self.ct.pkgcpus.keys()):
s += ',"pkg%d":[' % p
s += ','.join(map(str,self.ct.pkgcpus[p]))
s += ']'
for p in sorted(self.ct.pkgcpus.keys()):
s += ',"pkg%dphyid":[' % p
phyid = []
for cpuid in self.ct.pkgcpus[p]:
phyid.append(self.ct.cpu2coreid[cpuid][1])
s += ','.join(map(str,phyid))
s += ']'
s += ',"nnodes":%d' % len(list(self.ct.nodecpus.keys()))
for n in sorted(self.ct.nodecpus.keys()):
s += ',"node%d":[' % n
s += ','.join(map(str,self.ct.nodecpus[n]))
s += ']'
if self.rapl.initialized():
s += ',"max_energy_uj":{'
firstitem = True
for p in sorted(self.ct.pkgcpus.keys()):
k = 'package-%d' % p # XXX: double check both 'topology' and rapldriver use the same numbering scheme.
if firstitem :
firstitem = False
else:
s += ','
s += '"p%d":%d' % (p, self.rapl.max_energy_range_uj_d[k])
s += '}'
s += '}'
self.logger(s)
def tee(self,argv):
proc = subprocess.Popen(argv, stdout=subprocess.PIPE )
while True:
l = proc.stdout.readline()
if not l:
break
print('stdout:', l, end=' ')
def sigh(signal, frame):
self.kp.disable()
sys.exit(1)
def run(self, argv):
self.shownodeinfo()
self.start_time0 = time.time()
s = '{"starttime":%.3f}' % self.start_time0
self.logger(s)
if len(argv) == 0:
signal.signal(signal.SIGINT, self.sigh)
signal.signal(signal.SIGTERM, self.sigh)
self.kp = keypress.keypress()
self.kp.enable()
self.rapl.start_energy_counter()
while True:
self.sample_temp('run')
self.sample_energy('run')
self.sample_freq('run')
self.sample_acpi('run')
time.sleep(self.intervalsec)
if self.kp.available():
if self.kp.readkey() == 'q':
break
self.rapl.stop_energy_counter()
s = self.rapl.total_energy_json()
self.logger(s)
self.kp.disable()
return
self.cooldown('cooldown')
# spawn a process and start the sampling thread
t = threading.Thread(target=self.tee, args=[argv])
t.start()
self.rapl.start_energy_counter()
while True:
#if not t.isAlive():
if not t.is_alive():
break
self.sample_temp('run')
self.sample_energy('run')
self.sample_freq('run')
self.sample_acpi('run')
time.sleep(self.intervalsec)
self.rapl.stop_energy_counter()
s = self.rapl.total_energy_json()
self.logger(s)
self.cooldown('cooldown')
class log2file:
def __init__(self, fn):
try:
self.f = open(fn, 'w')
except:
print('Error: failed to open', fn)
self.f = sys.stdout
def logger(self,str):
self.f.write(str + '\n')
# XXX: tentative
class log2beacon:
def __init__(self, cmd, topic):
self.cmd = cmd
self.topic = topic
def logger(self,str):
c = [self.cmd, self.topic, "%s" % str]
try:
subprocess.call(c)
except:
print('Error: failed to publish:', c)
time.sleep(5)
if debuglevel > 0:
print('Debug:', c)
if __name__ == '__main__':
shortopt = "hC:i:o:"
longopt = ['runtests', 'cooldown=', 'interval=', 'output=', 'sample', 'info', 'output2beacon=', 'debug=' ]
try:
opts, args = getopt.getopt(sys.argv[1:],
shortopt, longopt)
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(1)
cmd = ''
tr = coolrmon_tracer()
for o, a in opts:
if o in ('-h'):
usage()
sys.exit(0)
elif o in ("--debug"):
debuglevel=int(a)
elif o in ("-C", "--cooldown"):
tr.setcooldowntemp(int(a))
elif o in ("-i", "--interval"):
tr.setinterval(float(a))
elif o in ("-o", "--output"):
l = log2file(a)
tr.setlogger(l.logger)
elif o in ("--output2beacon"):
cmd, topic = a.split(':')
if len(topic) == 0:
print('Specify topic after :')
sys.exit(1)
l = log2beacon(cmd, topic)
tr.setlogger(l.logger)
tr.setbeacon()
elif o in ("--sample"):
tr.sample_temp('sample')
tr.sample_energy('sample')
tr.sample_freq('sample')
tr.sample_acpi('sample')
sys.exit(0)
elif o in ("--info"):
tr.shownodeinfo()
sys.exit(0)
else:
print('Unknown:', o, a)
sys.exit(1)
#
#
# XXX: beacon is ad hoc impl for the demo
if tr.beacon:
while True:
tr.sample_temp('run')
tr.sample_freq('run')
time.sleep(tr.intervalsec)
else:
tr.run(args)
sys.exit(0)