-
Notifications
You must be signed in to change notification settings - Fork 2
/
powerusagerunner.py
253 lines (195 loc) · 6.05 KB
/
powerusagerunner.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
import io
import argparse
import os
import json
import time
import psutil
import subprocess
from threading import Thread
def powerparser():
parser = argparse.ArgumentParser(
description='Continuously poll power usage and battery usage'
'measurements and save them for post-processing.'
)
parser.add_argument('--no-battery-usage', '-nbu', action='store_true', default=False,
help='If set, battery usage will not be measured (measured by default).')
parser.add_argument('--no-power-usage', '-npu', action='store_true', default=False,
help='If set, power usage will not be measured (measured by deafult).')
parser.add_argument('--no-ram-power-usage', '-nrpu', action='store_true', default=False,
help='If set, RAM power consumption will not be estimated. This polls much'
'more than power and battery usage estimators (once every 5 seconds).' )
parser.add_argument('--poll-interval', type=float, default=60,
help='Minimum interval is 1 minute with SRUMUtil from powercfg.exe. If '
'there are holes in the data, consider decreasing this interval '
'(default is 60 seconds).')
parser.add_argument('--ram-poll-interval', type=float, default=5,
help='Interval at which RAM usage should be polled at '
'(default is 5 seconds).')
parser.add_argument('--output', type=str, required=True,
help='Location to store output.')
return parser
class SRUMUtilPoller(Thread):
def __init__(self, args, stopper):
Thread.__init__(self)
self.stopper = stopper
if not args:
return
self.powerusage = not args['no_battery_usage']
self.batteryusage = not args['no_power_usage']
self.output = args['output']
self.poll_interval = args['poll_interval']
def get_curr_file_count(self, filename):
return len([
f
for f in os.listdir(self.output)
if os.path.isfile(f) and (filename in f or f in filename)
])
def run(self):
while not self.stopper.is_stop_set():
currtime = str(int(time.time()))
if self.powerusage:
# Call powercfg.exe /SRUMUTIL
retries = 5
success = False
while not success and retries >= 0:
try:
command = ['powercfg.exe', '/SRUMUTIL', '/CSV', '/OUTPUT']
command.append('srumutil' + currtime + '.csv')
subprocess.check_call(command)
success = True
except:
print('retrying...')
retries -= 1
time.sleep(5)
continue
if self.batteryusage:
# Call powercfg.exe /BATTERYREPORT
command = ['powercfg.exe', '/BATTERYREPORT', '/OUTPUT']
command.append('batteryreport' + currtime + '.html')
subprocess.check_call(command)
if self.stopper.is_stop_set():
break
time.sleep(self.poll_interval)
class RAMPoller(Thread):
def __init__(self, args, stopper):
Thread.__init__(self)
self.stopper = stopper
self.current_memory = 0
self.rampowerusage = not args['no_ram_power_usage']
self.output = args['output']
self.ram_poll_interval = args['ram_poll_interval']
def get_memory(self):
tmp = psutil.virtual_memory()
self.current_memory = tmp.used >> 20
return self.current_memory
def get_ram_power_usage(self):
# Estimated by the usage of 0.1 mW/Gb from
# here: https://ieeexplore.ieee.org/document/8310256
# Note that mW signifies Joules/second.
return (self.current_memory/1024) * 0.1
def run(self):
ramfile = os.path.join(self.output, 'ram_usage.csv')
f = io.open(ramfile, 'w')
# Write header
try:
f.write('CurrentTime,RAMUsage,RAMPowerConsumption\n')
def write_current_estimates():
f.write(','.join([
str(time.time()),
str(self.get_memory()),
str(self.get_ram_power_usage())
]) + '\n')
write_current_estimates()
while not self.stopper.is_stop_set():
if self.rampowerusage:
# Write them to the file
print('Writing RAM usage entry...')
write_current_estimates()
f.flush()
if self.stopper.is_stop_set():
break
time.sleep(self.ram_poll_interval)
f.close()
except:
f.close()
raise
class DaemonStopper(object):
def __init__(self):
self.stop = False
def is_stop_set(self):
return self.stop
def stop(self):
self.stop = True
def reset(self):
self.stop = False
def main():
parser = powerparser()
args = parser.parse_args()
args = dict(vars(args))
currdir = os.getcwd()
outputdir = args['output']
if not os.path.exists(outputdir):
os.mkdir(outputdir)
outputdir = os.path.abspath(outputdir)
usagedir = os.path.join(outputdir, 'usagerunfrom' + str(int(time.time())))
baselinedir = os.path.join(usagedir, 'baseline')
testdir = os.path.join(usagedir, 'test')
os.mkdir(usagedir)
os.mkdir(baselinedir)
os.mkdir(testdir)
starttime = time.time() - 3600*5
with open(os.path.join(usagedir, 'config.json'), 'w+') as f:
json.dump({
'starttime': starttime,
'args': args
}, f)
# Get the baseline
print("When ready, press enter to start collecting the baseline.")
print("It is important to do nothing during this stage to obtain")
print("baseline data that measures default power consumption.")
input()
print("Gathering baseline for 5 minutes...")
os.chdir(baselinedir)
args['output'] = baselinedir
stopper = DaemonStopper()
runner = SRUMUtilPoller(args, stopper)
ramrunner = RAMPoller(args, stopper)
runner.daemon = True
ramrunner.daemon = True
try:
ramrunner.start()
runner.start()
except:
stopper.stop()
raise
return
# Sleep for 5 minutes
time.sleep(300)
print("Done gathering baseline. Reseting runner daemons...")
runner.output = testdir
ramrunner.output = testdir
teststarttime = time.time() - 3600*5
with open(os.path.join(usagedir, 'config.json'), 'w+') as f:
json.dump({
'starttime': starttime,
'teststarttime': teststarttime,
'args': args
}, f)
os.chdir(testdir)
print("Test gathering started...you may start experiments now.")
print("Press q/Q to quit the program")
val = ''
while val not in ('q', 'Q'):
try:
input()
except Exception as e:
print(e)
continue
print("Stopping...please wait.")
stopper.stop()
runner.join()
RAMRUNNER.join()
os.chdir(currdir)
print("Done.")
if __name__=="__main__":
main()