-
Notifications
You must be signed in to change notification settings - Fork 3
/
power_check.py
executable file
·434 lines (380 loc) · 18.5 KB
/
power_check.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env python3
"""
Convient power measurement script for the Jetson TX2/Tegra X2.
relevant docs: http://developer2.download.nvidia.com/embedded/L4T/r27_Release_v1.0/Docs/Tegra_Linux_Driver_Package_Release_Notes_R27.1.pdf
@author: Lukas Cavigelli ([email protected])
"""
import sys
sys.path.append("/home/omnia/.local/lib/python3.8/site-packages")
sys.path.append("/home/omnia/.local/lib/python3.8/site-packages/torchvision-0.13.0-py3.8-linux-aarch64.egg")
device_nodes = {
'jetson_tx2':[('module/main' , '0041', '0'),
('module/gpu' , '0040', '0'),
('module/ddr' , '0041', '2'),
('module/cpu' , '0041', '1'),
('module/soc' , '0040', '1'),
('module/wifi' , '0040', '2'),
('board/main' , '0042', '0'),
('board/5v0-io-sys' , '0042', '1'),
('board/3v3-sys' , '0042', '2'),
('board/3v3-io-sleep', '0043', '0'),
('board/1v8-io' , '0043', '1'),
('board/3v3-m.2' , '0043', '2'),
],
'jetson_nx':[('main', '0040','1'),
('cpu+gpu', '0040','2'),
('soc', '0040','3')
]
}
driver_dir = { 'jetson_tx2':'/sys/bus/i2c/drivers/ina3221x/0-0041/iio:device1/',
'jetson_nx': '/sys/bus/i2c/devices/7-0040/'
}
import os
# from jtop_logger import JtopLogger
# descr, i2c-addr, channel
# _nodes = [('module/main' , '0041', '0'),
# ('module/gpu' , '0040', '0'),
# ('module/ddr' , '0041', '2'),
# ('module/cpu' , '0041', '1'),
# ('module/soc' , '0040', '1'),
# ('module/wifi' , '0040', '2'),
# ('board/main' , '0042', '0'),
# ('board/5v0-io-sys' , '0042', '1'),
# ('board/3v3-sys' , '0042', '2'),
# ('board/3v3-io-sleep', '0043', '0'),
# ('board/1v8-io' , '0043', '1'),
# ('board/3v3-m.2' , '0043', '2'),
# ]
_valTypes = ['power', 'voltage', 'current']
_valTypesFull = ['power [mW]', 'voltage [mV]', 'current [mA]']
def getNodes(device='jetson_tx2'):
"""Returns a list of all power measurement nodes, each a
tuple of format (name, i2d-addr, channel)"""
assert device in device_nodes
return device_nodes[device]
def getNodesByName(nameList=['module/main']):
return [_nodes[[n[0] for n in _nodes].index(name)] for name in nameList]
def getDevice():
for dir in driver_dir:
if powerSensorsPresent(dir):
return dir
def powerSensorsPresent(device='jetson_tx2'):
"""Check whether we are on the TX2 platform/whether the sensors are present"""
return os.path.isdir(driver_dir[device])
def getPowerMode():
return os.popen("nvpmodel -q | grep 'Power Mode'").read()[15:-1]
def readValue(i2cAddr='0041', channel='0', valType='power',device='jetson_tx2'):
"""Reads a single value from the sensor"""
if device == 'jetson_tx2':
fname = '/sys/bus/i2c/drivers/ina3221x/0-%s/iio:device%s/in_%s%s_input' % (i2cAddr, i2cAddr[-1], valType, channel)
f = open(fname, 'r')
res = f.read()
f.close()
return res
elif device == 'jetson_nx':
res = {}
for valtype in ['voltage','current']:
val = 'in' if valtype=='voltage' else 'curr'
# fname = '/sys/class/hwmon/hwmon4/%s%s_input' % (val,channel)
fname='/sys/bus/i2c/drivers/ina3221/7-0040/hwmon/hwmon4/%s%s_input'% (val,channel)
f = open(fname, 'r')
res[valtype] = f.read()
f.close()
res['power'] = eval(res['voltage'])*eval(res['current'])/1000
return res[valType]
def getModulePower():
"""Returns the current power consumption of the entire module in mW."""
return float(readValue(i2cAddr='0041', channel='0', valType='power'))
def getAllValues(nodes,device):
"""Returns all values (power, voltage, current) for a specific set of nodes."""
return [[float(readValue(i2cAddr=node[1], channel=node[2], valType=valType,device=device)) for valType in _valTypes] for node in nodes]
def printFullReport(device):
"""Prints a full report, i.e. (power,voltage,current) for all measurement nodes."""
# from tabulate import tabulate
header = []
header.append('A description')
for vt in _valTypesFull:
header.append(vt)
resultTable = []
for descr, i2dAddr, channel in device_nodes[device]:
row = []
row.append(descr)
for valType in _valTypes:
row.append(readValue(i2cAddr=i2dAddr, channel=channel, valType=valType,device=device))
resultTable.append(row)
import pandas as pd
total = {}
for i, vt in enumerate(header):
if i<2: total.update({vt:[row[i] for row in resultTable]})
else: total.update({vt:[eval(row[i]) for row in resultTable]})
totalTable = pd.DataFrame(total)
print(totalTable) # print(tabulate(resultTable, header))
"""
def draw_csv_img(csv_path = '/home/omnia/power/power/power.csv', names = list(range(12)), filename = 'from_csv', Events=None):
from csv import reader
label = []
add_info = []
data = []
with open(csv_path,'r') as f:
fcsv = reader(f)
for row in fcsv:
line = list(row)
data.append(eval(line[2:]))
label.append(line[1])
add_info.append(eval(line[0]))
x = data[0]
y = data[1:]
for i in range(1,len(data)):
y.append(list(map(float,data[i])))
import numpy as np
x_s = []
y_a = [[] for i in range(len(y))]
y_t = [[] for i in range(len(y))]
for i in range(int(len(x)/6)):
x_s.append(i+3)
for j in range(len(y)):
part = y[j][i:i+6]
y_a[j].append(np.mean(part))
y_t[j].append(np.max(part))
if names == None:
names = [name for name, _, _ in [self._nodes[i] for i in [0,1,2,3,11]]]
label_names = ['System-wise', 'GPU', 'RAM', 'CPU', 'SSD']
styles = [':', '-', '-', '-', '-', '--']
else:
names = [name for name, _, _ in [self._nodes[i] for i in names]]
label_names = names
half = int((len(names)-1)/2)
styles = [":"] + ['-'] * half + ['-.'] * (len(names) - half) + ['--']
label_names.append('SUM')
import matplotlib.pyplot as plt
# total
for t in range(len(y)): plt.plot(x, y[t], label = ('%s (%.2f J)' % (label[t+1], (add_info[t+1]/1e3))), linestyle=styles[t])
plt.xlabel('time [s]')
plt.ylabel(_valTypesFull[_valTypes.index(valType)])
plt.grid(True)
ln = plt.legend(loc='center left', bbox_to_anchor=(1.04,0.5)) # ['%s (%.2f J)' % (name, enrgy/1e3) for name, enrgy in zip(label_names, energies)],
plt.title('%s trace (NVPModel: %s)' % (valType, os.popen("nvpmodel -q | grep 'Power Mode'").read()[15:-1]))
if Events is not None:
for t, _ in Events:
plt.axvline(x=t, color='black', linestyle='-.')
plt.savefig('power/%s/%s_total.png' % (filename, valType), bbox_extra_artists=(ln,), bbox_inches='tight')
plt.close()
"""
import threading
import time
class PowerLogger:
"""This is an asynchronous power logger.
Logging can be controlled using start(), stop().
Special events can be marked using recordEvent().
Results can be accessed through
"""
# def __init__(self, interval=0.01, nodes=device_nodes['jetson_tx2'], figsave=True, csvwrite = True):
def __init__(self, interval=0.01,figsave=True, csvwrite = True):
"""Constructs the power logger and sets a sampling interval (default: 0.01s)
and fixes which nodes are sampled (default: all of them)"""
self.interval = interval
self._startTime = -1
self.eventLog = []
self.dataLog = []
self.figsave = figsave
self.csvwrite = csvwrite
self.device = getDevice()
self._nodes = device_nodes[self.device]
def start(self):
"Starts the logging activity"""
#define the inner function called regularly by the thread to log the data
def threadFun():
#start next timer
self.start()
#log data
t = self._getTime() - self._startTime
self.dataLog.append((t, getAllValues(self._nodes,self.device)))
#ensure long enough sampling interval
t2 = self._getTime() - self._startTime
# assert(t2-t < self.interval)
#setup the timer and launch it
self._tmr = threading.Timer(self.interval, threadFun)
self._tmr.start()
if self._startTime < 0:
self._startTime = self._getTime()
def _getTime(self):
return time.clock_gettime(time.CLOCK_REALTIME)
def recordEvent(self, name):
"""Records a marker a specific event (with name)"""
t = self._getTime() - self._startTime
self.eventLog.append((t, name))
def stop(self):
"""Stops the logging activity"""
self._tmr.cancel()
def getDataTrace(self, nodeName='module/main', valType='power'):
# if getDevice() == 'jetson_nx': nodeName = 'main'
"""Return a list of sample values and time stamps for a specific measurement node and type"""
pwrVals = [itm[1][[n[0] for n in self._nodes].index(nodeName)][_valTypes.index(valType)]
for itm in self.dataLog]
timeVals = [itm[0] for itm in self.dataLog]
return timeVals, pwrVals
def showDataTraces(self, names=None,valType='power', showEvents=True, filename='test'):
"""creates a PyPlot figure showing all the measured power traces and event markers"""
device = getDevice()
if device == 'jetson_tx2':
if names == None:
names = [name for name, _, _ in [self._nodes[i] for i in [0,1,2,3,11]]]
label_names = ['System-wise', 'GPU', 'RAM', 'CPU', 'SSD']
styles = [':', '-', '-', '-', '-', '--']
else:
names = [name for name, _, _ in [self._nodes[i] for i in names]]
label_names = names
half = int((len(names)-1)/2)
styles = [":"] + ['-'] * half + ['-.'] * (len(names) - half) + ['--']
elif device == 'jetson_nx':
if names == None:
names = [name for name, _, _ in [node for node in self._nodes]]
label_names = ['System-wise', 'GPU+CPU+CV', 'SOC']
styles = [':', '-', '--']
else:
names = [name for name, _, _ in [self._nodes[i] for i in names]]
label_names = names
half = int((len(names)-1)/2)
styles = [":"] + ['-'] * half + ['-.'] * (len(names) - half) + ['--']
#prepare data to display
TPs = [self.getDataTrace(nodeName=name, valType=valType) for name in names]
Ts, _ = TPs[0]
Ps = [p for _, p in TPs]
if device == 'jetson_tx2':
Ps.append([Ps[1][i]+Ps[2][i]+Ps[3][i] for i in range(len(Ts))])
elif device == 'jetson_nx':
Ps.append([Ps[0][i]+Ps[1][i]+Ps[2][i] for i in range(len(Ts))])
import numpy as np
Ts_s = []
Ps_a = []
Ps_t = []
for i in range(int(len(Ts)/5)):
Ts_s.append(Ts[(5*i)+2])
for p in range(len(Ps)):
Ps_ap = []
Ps_tp = []
for i in range(int(len(Ts)/5)):
part = Ps[p][(5*i):(5*i)+5]
Ps_ap.append(np.mean(part))
Ps_tp.append(np.max(part))
Ps_a.append(Ps_ap)
Ps_t.append(Ps_tp)
energies = [self.getTotalEnergy(nodeName=nodeName) for nodeName in names]
if device == 'jetson_tx2':
energies.append(energies[1]+energies[2]+energies[3])
elif device == 'jetson_nx':
energies.append(energies[0]+energies[1]+energies[2])
# Ps = list(map(list, zip(*Ps))) # transpose list of lists
# label_names.append('SUM')
os.makedirs('results_test/power/%s' % (filename,) , exist_ok=True)
#draw figure
if self.figsave:
import matplotlib.pyplot as plt
# total
for t in range(len(label_names)): plt.plot(Ts, Ps[t], label = ('%s (%.2f J)' % (label_names[t], (energies[t]/1e3))), linestyle=styles[t])
plt.xlabel('time [s]')
plt.ylabel(_valTypesFull[_valTypes.index(valType)])
plt.grid(True)
ln = plt.legend(loc='center left', bbox_to_anchor=(1.04,0.5)) # ['%s (%.2f J)' % (name, enrgy/1e3) for name, enrgy in zip(label_names, energies)],
plt.title('%s trace (NVPModel: %s)' % (valType, os.popen("nvpmodel -q | grep 'Power Mode'").read()[15:-1]))
if showEvents:
for t, _ in self.eventLog:
plt.axvline(x=t, color='black', linestyle='-.')
plt.savefig('results_test/power/%s/%s_total.png' % (filename, valType), bbox_extra_artists=(ln,), bbox_inches='tight')
plt.close()
# Average
for t in range(len(label_names)): plt.plot(Ts_s, Ps_a[t], label = ('%s (%.2f J)' % (label_names[t], (energies[t]/1e3))), linestyle=styles[t])
plt.xlabel('time [s]')
plt.ylabel(_valTypesFull[_valTypes.index(valType)])
plt.grid(True)
ln = plt.legend(loc='center left', bbox_to_anchor=(1.04,0.5)) # ['%s (%.2f J)' % (name, enrgy/1e3) for name, enrgy in zip(label_names, energies)],
plt.title('%s trace (NVPModel: %s)' % (valType, os.popen("nvpmodel -q | grep 'Power Mode'").read()[15:-1]))
if showEvents:
for t, _ in self.eventLog:
plt.axvline(x=t, color='black', linestyle='-.')
plt.savefig('results_test/power/%s/%s_average.png' % (filename, valType), bbox_extra_artists=(ln,), bbox_inches='tight')
plt.close()
# Top
for t in range(len(label_names)): plt.plot(Ts_s, Ps_t[t], label = ('%s (%.2f J)' % (label_names[t], (energies[t]/1e3))), linestyle=styles[t])
plt.xlabel('time [s]')
plt.ylabel(_valTypesFull[_valTypes.index(valType)])
plt.grid(True)
ln = plt.legend(loc='center left', bbox_to_anchor=(1.04,0.5)) # ['%s (%.2f J)' % (name, enrgy/1e3) for name, enrgy in zip(label_names, energies)],
plt.title('%s trace (NVPModel: %s)' % (valType, os.popen("nvpmodel -q | grep 'Power Mode'").read()[15:-1]))
if showEvents:
for t, _ in self.eventLog:
plt.axvline(x=t, color='black', linestyle='-.')
plt.savefig('results_test/power/%s/%s_top.png' % (filename, valType), bbox_extra_artists=(ln,), bbox_inches='tight')
plt.close()
if self.csvwrite:
import csv
with open('results_test/power/%s/%s.csv' % (filename, valType),'w') as f:
csvf = csv.writer(f)
csvf.writerow(['time'] + label_names)
for i in range(len(Ts)):
csvf.writerow([Ts[i]] + [Ps[j][i] for j in range(len(label_names))])
csvf.writerow([0] + energies)
def showMostCommonPowerValue(self, nodeName='module/main', valType='power', numBins=100, filename='test'):
"""computes a histogram of power values and print most frequent bin"""
import numpy as np
_, pwrData = np.array(self.getDataTrace(nodeName=nodeName, valType=valType))
count, center = np.histogram(pwrData, bins=1) #int(len(pwrData)*5))
if self.figsave:
import matplotlib.pyplot as plt
plt.bar((center[:-1]+center[1:])/2.0, count, align='center')
plt.ylabel('Number of value')
plt.xlabel(_valTypesFull[_valTypes.index(valType)])
plt.grid(True)
plt.title('%s trace (NVPModel: %s)' % (valType, os.popen("nvpmodel -q | grep 'Power Mode'").read()[15:-1]))
os.makedirs('power/%s' % (filename,) , exist_ok=True)
plt.savefig('power/%s/%s_histogram.png' % (filename, valType))
plt.close()
maxProbVal = center[np.argmax(count)]#0.5*(center[np.argmax(count)] + center[np.argmax(count)+1])
print('max frequent %s bin value %s: %f' % (valType, (_valTypesFull[_valTypes.index(valType)])[-4:], maxProbVal))
if self.csvwrite:
import csv
with open('power/%s/%s_histogram.csv' % (filename, valType),'w') as f:
csvf = csv.writer(f)
csvf.writerow(['x axis (center)', 'y axis (count)'])
for i in range(len(count)):
csvf.writerow([center[i], count[i]])
def getTotalEnergy(self, nodeName='module/main', valType='power'):
"""Integrate the power consumption over time."""
timeVals, dataVals = self.getDataTrace(nodeName=nodeName, valType=valType)
assert(len(timeVals) == len(dataVals))
tPrev, wgtdSum = 0.0, 0.0
for t, d in zip(timeVals, dataVals):
wgtdSum += d*(t-tPrev)
tPrev = t
return wgtdSum
def getAveragePower(self, nodeName='module/main', valType='power'):
energy = self.getTotalEnergy(nodeName=nodeName, valType=valType)
timeVals, _ = self.getDataTrace(nodeName=nodeName, valType=valType)
return energy/timeVals[-1]
if __name__ == "__main__":
printFullReport(getDevice())
# print(getModulePower())
# pl = PowerLogger(interval=0.05, nodes=getNodesByName(['module/main', 'board/main']))
pl = PowerLogger(interval=0.05)
pl.start()
time.sleep(5)
print('5s IDLE time passed, start IO bench mark now!')
pl.recordEvent('started IO bench mark')
time.sleep(2)
pl.recordEvent('ding! 3s')
os.system('stress -c 12 -t 3')
time.sleep(1.5)
pl.recordEvent('ding! 2s')
os.system('stress -c 1 -t 2')
time.sleep(2)
pl.recordEvent('ding! 1s')
os.system('stress -c 2 -t 1')
time.sleep(1.5)
pl.stop()
pl.showDataTraces()
# pl.showDataTraces(valType='voltage')
# pl.showDataTraces(valType='current')
nodename = device_nodes[pl.device][0][0] # main
pl.showMostCommonPowerValue(nodename)
# pl.showMostCommonPowerValue(valType='voltage')
# pl.showMostCommonPowerValue(valType='current')