forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexperiment_powertoplogger.py
More file actions
213 lines (181 loc) · 6.21 KB
/
Copy pathexperiment_powertoplogger.py
File metadata and controls
213 lines (181 loc) · 6.21 KB
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
import csv
import datetime
import os
import psutil
import re
import sys
import time
def isSudo():
return os.geteuid() == 0
if not isSudo():
print(f'\nPlease run the script as sudo:\n\tsudo python3 {os.path.basename(__file__)}\n')
sys.exit()
# Send a command to the linux terminal
def terminal(cmd):
return os.popen(cmd).read()
# def stopPowertops():
# terminal('sudo pkill powertop')
def startPowerTop():
if isPowerTopRunning(): return
exists = terminal('which powertop') != ''
if not exists:
print('\nPowerTOP does not exist. Installing...')
terminal('sudo apt-get install powertop')
print('\nPlease run "sudo powertop --calibrate" before proceeding.')
sys.exit()
terminal('sudo gnome-terminal -- powertop')
def isPowerTopRunning():
process = terminal('ps -A | grep powertop')
return process != ''
def getPowerTopInfo():
powertopVersion = ''
kernelVersion = ''
systemName = ''
cpuInformation = ''
osInformation = ''
powerUsageBaseline = '0'
powerUsageBaselineUnit = ''
powerUsage = '0'
powerUsageUnit = ''
signaturesPowerUsage = '0'
signaturesPowerUnit = ''
if not isPowerTopRunning(): return {
'powertopVersion': powertopVersion,
'kernelVersion': kernelVersion,
'systemName': systemName,
'cpuInformation': cpuInformation,
'osInformation': osInformation,
'powerUsage': powerUsage,
'powerUsageUnit': powerUsageUnit,
'powerUsageBaseline': powerUsageBaseline,
'powerUsageBaselineUnit': powerUsageBaselineUnit,
'signaturesPowerUsage': signaturesPowerUsage,
'signaturesPowerUnit': signaturesPowerUnit
}
powertopPath = os.path.expanduser('~/powertop.csv')
print(f'Creating {powertopPath}')
temp = open(powertopPath, 'w+')
temp.close()
output = terminal('sudo powertop --time=1 --csv=' + powertopPath)
print('\n\n\nPowertop output: "' + output + '"')
_powertopVersion = terminal(f'cat {powertopPath} | grep "PowerTOP Version;"')
match = re.match('PowerTOP Version;([^;\n]+)', _powertopVersion)
if match is not None:
powertopVersion = match.group(1)
_kernelVersion = terminal(f'cat {powertopPath} | grep "Kernel Version;"')
match = re.match('Kernel Version;([^;\n]+)', _kernelVersion)
if match is not None:
kernelVersion = match.group(1)
_systemName = terminal(f'cat {powertopPath} | grep "System Name;"')
match = re.match('System Name;([^;\n]+)', _systemName)
if match is not None:
systemName = match.group(1)
_cpuInformation = terminal(f'cat {powertopPath} | grep "CPU Information;"')
match = re.match('CPU Information;([^;\n]+)', _cpuInformation)
if match is not None:
cpuInformation = match.group(1)
_osInformation = terminal(f'cat {powertopPath} | grep "OS Information;"')
match = re.match('OS Information;([^;\n]+)', _osInformation)
if match is not None:
osInformation = match.group(1)
_powerUsage = terminal(f'cat {powertopPath} | grep "The battery reports a discharge rate of:"')
match = re.match('The battery reports a discharge rate of:\s*([0-9\.]+)([^;\n]+)', _powerUsage)
if match is not None:
powerUsage = match.group(1)
powerUsageUnit = match.group(2)
_powerUsageBaseline = terminal(f'cat {powertopPath} | grep "The system baseline power is estimated at:"')
match = re.match('The system baseline power is estimated at:\s*([0-9\.]+)([^;\n]+)', _powerUsageBaseline)
if match is not None:
powerUsageBaseline = match.group(1)
powerUsageBaselineUnit = match.group(2)
_signaturesUsage = terminal(f'cat {powertopPath} | grep "] ./Signatures.o ; "').strip()
match = re.match(r'[^\]]+\] \./Signatures.o ; +([0-9\.]+) ([a-zA-Z]+)', _signaturesUsage)
if match is not None:
signaturesPowerUsage = match.group(1)
signaturesPowerUnit = match.group(2)
return {
'powertopVersion': powertopVersion,
'kernelVersion': kernelVersion,
'systemName': systemName,
'cpuInformation': cpuInformation,
'osInformation': osInformation,
'powerUsage': powerUsage,
'powerUsageUnit': powerUsageUnit.replace(' ', ''),
'powerUsageBaseline': powerUsageBaseline,
'powerUsageBaselineUnit': powerUsageBaselineUnit.replace(' ', ''),
'signaturesPowerUsage': signaturesPowerUsage,
'signaturesPowerUnit': signaturesPowerUnit
}
startPowerTop()
outputFile = open('loggedPowerOutput.csv', 'w+')
line = ''
line += 'Timestamp,'
line += 'Timestamp (s),'
line += 'CPU %,'
line += 'CPU Frequency,'
line += 'Virtual Memory %,'
line += 'Virtual Memory,'
line += 'Swap Memory %,'
line += 'Swap Memory,'
line += 'Disk Usage %,'
line += 'Disk Usage,'
line += 'Powertop Version,'
line += 'Kernel Version,'
line += 'System Name,'
line += 'CPU Stats,'
line += 'OS Stats,'
line += 'Power Usage,'
line += 'Unit,'
line += 'Baseline Power,'
line += 'Unit,'
line += 'Signatures Power,'
line += 'Signatures Unit,'
outputFile.write(line + '\n')
print(getPowerTopInfo())
sampleCounter = 1
while True:
time.sleep(5)
try:
now = datetime.datetime.now()
seconds = (now - datetime.datetime(1970, 1, 1)).total_seconds()
powerStats = getPowerTopInfo()
line = ''
line += str(now) + ','
line += str(seconds) + ','
# CPU, memory, disk usage logger
cpu = psutil.cpu_percent()
cpu_f = 0
try:
cpu_f = psutil.cpu_freq().current
except: pass
v_mem_p = psutil.virtual_memory().percent
v_mem = psutil.virtual_memory().used
s_mem_p = psutil.swap_memory().percent
s_mem = psutil.swap_memory().used
disk_usage_p = psutil.disk_usage('/').percent
disk_usage = psutil.disk_usage('/').used
line += str(cpu) + ','
line += str(cpu_f) + ','
line += str(v_mem_p) + ','
line += str(v_mem) + ','
line += str(s_mem_p) + ','
line += str(s_mem) + ','
line += str(disk_usage_p) + ','
line += str(disk_usage) + ','
# Power logger
line += str(powerStats['powertopVersion']) + ','
line += str(powerStats['kernelVersion']) + ','
line += str(powerStats['systemName']) + ','
line += str(powerStats['cpuInformation']) + ','
line += str(powerStats['osInformation']) + ','
line += str(powerStats['powerUsage']) + ','
line += str(powerStats['powerUsageUnit']) + ','
line += str(powerStats['powerUsageBaseline']) + ','
line += str(powerStats['powerUsageBaselineUnit']) + ','
line += str(powerStats['signaturesPowerUsage']) + ','
line += str(powerStats['signaturesPowerUnit']) + ','
outputFile.write(line + '\n')
print(f'Logged power sample #{sampleCounter}')
sampleCounter += 1
except KeyboardInterrupt:
break