-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpurr_tb.py
166 lines (146 loc) · 5.53 KB
/
purr_tb.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
#!/usr/bin/python3
######################################################################
# purr_tb.py : A utility for validating whether the sum of the
# values of the PURR register across the active threads
# of cores over a time iterval matches the number of
# timebase ticks for that interval.
#
# Authors:
# Kamalesh Babulal <[email protected]>
# Gautham R. Shenoy <[email protected]>
#
# (C) Copyright IBM Corp, 2019
######################################################################
import os, time, subprocess, getopt, sys
online_cpus=[]
PURR={}
PURR_new={}
cores_list = {}
###################################################
# Computes the list of online CPUs
###################################################
def get_online_cpus():
with open('/proc/cpuinfo', 'r') as f:
for line in f.readlines():
if line.startswith('processor'):
cpu = int(line.split()[2])
online_cpus.append(cpu)
########################################################################
# Returns a string of online threads of the core @tl from the cores_map
########################################################################
def stringify(tl):
retstring='['
for t in tl:
if int(t) in online_cpus:
retstring = retstring + "%3d," %(int(t))
last_comma_id=retstring.rfind(',')
retstring = retstring[0:last_comma_id] + "]"
return retstring
########################################################################
# Determines the map of active cores to their constituent threads
# from the ppc64_cpu utility
#
# The map is saved in the global variable cores_list{}
########################################################################
def get_core_map():
cores_str = str(subprocess.check_output('ppc64_cpu --info', shell=True)).split('\n')
active_cores_list = map(lambda c:c.replace('*', ''),
filter(lambda c:c.find('*') >= 0, cores_str))
idx = 0
for core in active_cores_list:
if (core.find('Core') == -1):
continue
tmp = core.split(':')[1]
tmp = ' '.join(tmp.split())
tmp = tmp.split('\'')[0].replace('\n', '').split(" ")
cores_list[idx] = tmp
idx = idx + 1
########################################################################
# Prints the following banner:
# =========================================
# Core delta tb(apprx) delta purr
# =========================================
########################################################################
def print_banner():
banner = ""
header = ""
if len(cores_list) == 0:
return
banner = "Core"
mystring = "Core%02d %s" %(0, stringify(cores_list[0]))
paddinglen = len(mystring) - len(banner)
padding = "".join([' ' for i in range(0, paddinglen)])
banner = banner + padding + "\t%s\t\t%s\t" %("delta tb(apprx)", "delta purr")
header = "".join(['=' for i in range(0, len(banner) + 16)])
print(header)
print(banner)
print(header)
########################################################################
# For a CPU @c, read the PURR value from sysfs
########################################################################
def read_purr(c):
cpu = str(c)
with open('/sys/devices/system/cpu/cpu'+cpu+'/purr') as f:
purr = int(f.readline().rstrip('\n'), 16)
return purr
def help():
print('monitor.py -i <interval seconds> -s <samples count>\n')
########################################################################
# Parse commandline options and update the value of samples and
# interval
########################################################################
def parse_cmdline():
samples=10
interval=1 #seconds
try:
options, others = getopt.getopt(
sys.argv[1:],
'hi:s:',
['interval=',
'samples='])
except getopt.GetoptError as err:
help()
sys.exit(1)
for opt, arg in options:
if opt in ('-i', '--interval'):
interval = int(arg)
elif opt in ('-s', '--samples'):
samples = int(arg)
elif opt in ('-h', '--help'):
help()
sys.exit(0)
return interval, samples
#####################################################################
# For each time interval, compute the sum of purr increments for each
# online thread in each active core and print the sum of purr
# increments in comparison with the tb ticks elapsed in that interval.
#
# Expectation:
# sum of purr increments in an active core == elapsed tb-ticks
#####################################################################
def validate():
for i in range(0, samples):
old_sec = int(time.time())
for cpu in online_cpus:
PURR[cpu] = read_purr(cpu)
time.sleep(interval)
now_sec = int(time.time())
delta_sec = now_sec - old_sec
delta_tb = delta_sec * 512000000
for cpu in online_cpus:
PURR_new[cpu] = read_purr(cpu)
for key,value in cores_list.items():
purr_total = 0
for cpustr in value:
cpu = int (cpustr)
if cpu not in online_cpus:
continue
purr_total += PURR_new[cpu] - PURR[cpu]
print("core%02d %s\t%d\t\t%d\t" % (key, stringify(cores_list[key]), delta_tb, purr_total))
print("")
interval,samples=parse_cmdline()
get_online_cpus()
get_core_map()
print_banner()
validate()
sys.exit(0)