-
Notifications
You must be signed in to change notification settings - Fork 1
/
clr_amperf.py
executable file
·153 lines (122 loc) · 3.64 KB
/
clr_amperf.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
#!/usr/bin/env python3
#
# reading the APERF and MPERF register via the amperf sysfs provided
# by the coolrfreq driver
#
# This code requires the coolrfreq driver
#
# Contact: Kazutomo Yoshii <[email protected]>
#
import re, os, sys, time
import numpy as np
from clr_nodeinfo import *
class amperf_reader :
def __init__ (self):
self.ct = cputopology()
self.init = False
if os.path.exists('/sys/devices/system/cpu/cpu0/amperf'):
self.init = True
else:
print('Warning: the amperf sysfs is unavailable')
self.samples = [ None, None ]
self.sidx = 0
self.sample()
time.sleep(.1)
self.sample()
def read(self):
if not self.init:
return None
ret = {}
for p in sorted(self.ct.pkgcpus.keys()):
perpkg = {}
for cpuid in self.ct.pkgcpus[p]:
fn = "/sys/devices/system/cpu/cpu%d/amperf" % cpuid
with open(fn, 'r') as f:
t = f.readline().split()
vals = []
for v in t:
vals.append(int(v))
perpkg['c%d' % cpuid] = vals
ret['p%d' % p] = perpkg
ret['time'] = time.time()
return ret
def sample(self):
if not self.init:
return None
self.samples[self.sidx] = self.read()
if self.sidx == 0:
self.sidx = 1
else:
self.sidx = 0
def firstidx(self):
return self.sidx
def secondidx(self):
if self.sidx == 0:
return 1
return 0
def getdiff(self):
if not self.init:
return None
d = {}
f = self.samples[self.firstidx()]
s = self.samples[self.secondidx()]
for kp in list(f.keys()):
if kp == 'time':
d['time'] = s[kp] - f[kp]
else:
tmp = {}
for kc in list(f[kp].keys()):
vals = []
for i in range(0,2):
vals.append(s[kp][kc][i] - f[kp][kc][i])
tmp[kc] = vals
d[kp] = tmp
return d
def getavgGHz(self, d):
if not self.init:
return None
ret = {}
dt = d['time']
for kp in list(d.keys()):
if kp != 'time':
tmp = {}
for kc in list(d[kp].keys()):
# calc avgMHz delta_aperf / delta_t
tmp[kc] = d[kp][kc][0] / dt * 1e-9
ret[kp] = tmp
return ret
def getpkgstats(self, p):
if not self.init:
return None
ret = {}
for k in list(p.keys()):
tmp = []
for c in list(p[k].keys()):
tmp.append(p[k][c])
ret[k] = [np.mean(tmp), np.std(tmp), np.min(tmp), np.max(tmp)]
return ret
def sample_and_json(self):
if not self.init:
return ''
self.sample()
d = self.getdiff()
f = self.getavgGHz(d)
s = self.getpkgstats(f)
cnt = 0
buf = '{"sample":"freq",'
for kp in list(f.keys()):
if cnt > 0:
buf += ','
cnt += 1
buf += '"%s":{' % kp
buf += '"mean":%.3lf,"std":%.3lf' % (s[kp][0], s[kp][1])
for kc in list(f[kp].keys()):
buf += ',"%s":%.3lf' % (kc,f[kp][kc])
buf += '}'
buf += '}'
return buf
if __name__ == '__main__':
amp = amperf_reader()
while True:
print(amp.sample_and_json())
time.sleep(.5)