forked from andikleen/pmu-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tl_cpu.py
216 lines (203 loc) · 7.62 KB
/
tl_cpu.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
# Copyright (c) 2012-2020, Intel Corporation
# Author: Andi Kleen
#
# This program is free software; you can redistribute it and/or modify it
# under the terms and conditions of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# toplev CPU detection
#
# Environment variables to override (mainly for testing):
# FORCECPU=cpu Force CPU type (e.g. skl). Also --force-cpu
# FORCECOUNTERS=n Force number of generic counters
# FORCEHT=0/1 Force SMT mode
# HYPERVISOR=0/1 Force hypervisor mode (also --force-hypervisor)
# CPUINFO=file Read cpu information from file instead of /proc/cpuinfo. Also --force-cpuinfo
# REDUCED_COUNTERS=n Reduce counters by n
#
from collections import defaultdict, Counter
import os
import re
import glob
modelid_map = {
(0x8e, ): "KBLR",
(0x9e, ): "CFL",
}
cpus_8gpc = set(["icl", "tgl", "icx"])
def num_offline_cpus():
cpus = glob.glob("/sys/devices/system/cpu/cpu[0-9]*/online")
offline = 0
for fn in cpus:
with open(fn, "r") as f:
if int(f.read()) == 0:
offline += 1
return offline
def reduced_counters():
rc = os.getenv("REDUCED_COUNTERS")
if rc:
return int(rc)
val = 1
fn = "/sys/devices/cpu/allow_tsx_force_abort"
if os.path.exists(fn):
with open(fn, "r") as f:
val = int(f.read())
return 1 if val == 0 else 0
class Env(object):
def __init__(self):
self.forcecpu = os.getenv("FORCECPU")
self.forcecounters = os.getenv("FORCECOUNTERS")
self.forceht = os.getenv("FORCEHT")
self.hypervisor = os.getenv("HYPERVISOR")
self.cpuinfo = os.getenv("CPUINFO")
class CPU(object):
"""Detect the CPU."""
# overrides for easy regression tests
def force_cpu(self, known_cpus):
force = self.env.forcecpu
if not force:
return False
self.cpu = None
for i in known_cpus:
if force == i[0]:
self.cpu = i[0]
break
if self.cpu is None:
print("Unknown FORCECPU ",force)
return True
def force_counters(self):
cnt = self.env.forcecounters
if cnt:
self.counters = { "cpu": int(cnt) }
def force_ht(self):
ht = self.env.forceht
if ht:
self.ht = int(ht)
return True
return False
def __init__(self, known_cpus, nocheck, env):
self.env = env
self.model = 0
self.cpu = None
self.realcpu = "simple"
self.ht = False
self.counters = None
self.has_tsx = False
self.hypervisor = False
self.force_hypervisor = False
if self.env.hypervisor:
self.hypervisor = True
self.force_hypervisor = True
self.freq = 0.0
self.threads = 0
forced_cpu = self.force_cpu(known_cpus)
forced_ht = self.force_ht()
cores = Counter()
sockets = Counter()
self.coreids = defaultdict(list)
self.cputocore = {}
self.cputothread = {}
self.sockettocpus = defaultdict(list)
self.cputosocket = {}
self.allcpus = []
self.step = 0
self.name = ""
cpuinfo = self.env.cpuinfo
if cpuinfo is None:
cpuinfo = "/proc/cpuinfo"
with open(cpuinfo, "r") as f:
seen = set()
for l in f:
n = l.split()
if len(n) < 3:
continue
if n[0] == 'processor':
seen.add("processor")
cpunum = int(n[2])
self.allcpus.append(cpunum)
elif (n[0], n[2]) == ("vendor_id", "GenuineIntel"):
seen.add("vendor_id")
elif (len(n) > 3 and
(n[0], n[1], n[3]) == ("cpu", "family", "6")):
seen.add("cpu family")
elif (n[0], n[1]) == ("model", ":"):
seen.add("model")
self.model = int(n[2])
elif (n[0], n[1]) == ("model", "name"):
seen.add("model name")
m = re.search(r"@ (\d+\.\d+)GHz", l)
if m:
self.freq = float(m.group(1))
self.name = " ".join(n[3:])
elif (n[0], n[1]) == ("physical", "id"):
physid = int(n[3])
sockets[physid] += 1
self.sockettocpus[physid].append(cpunum)
self.cputosocket[cpunum] = physid
elif (n[0], n[1]) == ("core", "id"):
coreid = int(n[3])
key = (physid, coreid,)
cores[key] += 1
self.threads = max(self.threads, cores[key])
if self.threads > 1 and not forced_ht:
self.ht = True
self.coreids[key].append(cpunum)
self.cputocore[cpunum] = key
self.cputothread[cpunum] = self.coreids[key].index(cpunum)
elif n[0] == "flags":
seen.add("flags")
self.has_tsx = "rtm" in n
if "hypervisor" in n:
self.hypervisor = True
elif n[0] == "stepping":
seen.add("stepping")
self.step = int(n[2])
if len(seen) >= 7:
for i in known_cpus:
if self.model in i[1] or (self.model, self.step) in i[1]:
self.realcpu = i[0]
if not forced_cpu:
self.cpu = i[0]
break
self.force_counters()
self.limit4_counters = { "cpu": "none" }
self.standard_counters = { "cpu": ("0,1,2,3",) }
if self.cpu == "slm":
newcounters = { "cpu": 2 }
self.standard_counters = { "cpu": ("0,1",) }
# when running in a hypervisor always assume worst case HT in on
# also when CPUs are offline assume SMT is on
elif self.ht or self.hypervisor or (num_offline_cpus() > 0 and not nocheck) or self.cpu in cpus_8gpc:
if self.cpu in cpus_8gpc or (self.cpu == "simple" and self.realcpu in cpus_8gpc):
newcounters = {"cpu": 8 }
self.standard_counters = { "cpu": ("0,1,2,3,4,5,6,7", "0,1,2,3", ) }
self.limit4_counters = { "cpu": "0,1,2,3" }
else:
newcounters = { "cpu": 4 }
else:
newcounters = { "cpu": 8 }
if self.counters is None:
self.counters = newcounters
if not nocheck and not self.env.forcecounters:
for j in ("cpu", "cpu_core", "cpu_atom"):
if j in self.counters:
self.counters[j] -= reduced_counters()
if self.cpu in cpus_8gpc:
self.standard_counters = { "cpu": ("0,1,2,3,4,5,6,7", "0,1,2,3", ) }
self.limit4_counters = { "cpu": "0,1,2,3" }
try:
self.pmu_name = open("/sys/devices/cpu/caps/pmu_name").read().strip()
except IOError:
self.pmu_name = None
self.sockets = len(sockets.keys())
self.modelid = None
mid = (self.model,)
self.true_name = self.cpu
if mid in modelid_map:
self.modelid = modelid_map[mid]
self.true_name = self.modelid.lower()
# XXX match steppings here too