forked from intel/CommsPowerManagement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsst_bf.py
executable file
·579 lines (486 loc) · 17 KB
/
sst_bf.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause
# Copyright(c) 2019 Intel Corporation
"""SST-BF enable/disable"""
from __future__ import print_function
import os
import sys
import time
import re
import struct
import argparse
import subprocess
import textwrap
# raw_input() is only available in python 2.
try:
raw_input
except NameError:
raw_input = input
DRV_FILE = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_driver"
BASE_FILE = "/sys/devices/system/cpu/cpu%d/cpufreq/base_frequency"
CPU_MAX_FILE = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
CPU_MIN_FILE = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq"
MAX_FILE = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq"
MIN_FILE = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq"
MSR_FILE = "/dev/cpu/0/msr"
CPU_COUNT = 0
ONLINE_CORES = []
SCRIPT_VERSION = "1.3"
# Read a 64-byte value from an MSR through the sysfs interface.
# Returns an 8-byte binary packed string.
def __rdmsr(core, msr):
try:
msr_filename = os.path.join("/dev/cpu", str(core), "msr")
with open(msr_filename, "rb") as msr_file:
msr_file.seek(msr, os.SEEK_SET)
regstr = msr_file.read(8)
return regstr
except:
print("Could not read from MSR 0x%x on core %i" % (msr, core))
raise
# Writes a 64-byte value to an MSR through the sysfs interface.
# Expects an 8-byte binary packed string in regstr.
def __wrmsr(core, msr, regstr):
try:
msr_filename = os.path.join("/dev/cpu", str(core), "msr")
with open(msr_filename, "wb") as msr_file:
msr_file.seek(msr)
msr_file.write(regstr)
except IOError:
print("Could not write to MSR 0x%x on core %i" % (msr, core))
raise
# Read the HWP_ENABLED MSR
def get_hwp_enabled():
"""Read the HWP_ENABLED MSR."""
# rdmsr returns 8 bytes of packed binary data
regstr = __rdmsr(0, 0x770)
# Unpack the 8 bytes into array of unsigned chars
msr_bytes = struct.unpack('BBBBBBBB', regstr)
enabled = msr_bytes[0]
return enabled
# Read the TURBO_MODE_ENABLED from package
def get_turbo_disabled():
"""Read if the Package TURBO is disabled"""
# rdmsr returns 8 bytes of packed binary data
regstr = __rdmsr(0, 0x1A0)
# Unpack the 8 bytes into array of unsigned chars
msr_bytes = struct.unpack('BBBBBBBB', regstr)
disabled = msr_bytes[4] & 0x40
return disabled
# Get the CPU base frequencey from the PLATFORM_INFO MSR
def get_cpu_base_frequency():
"""Get the CPU base frequencey from the PLATFORM_INFO MSR."""
regstr = __rdmsr(0, 0xCE) # MSR_PLATFORM_INFO
# Unpack the 8 bytes into array of unsigned chars
msr_bytes = struct.unpack('BBBBBBBB', regstr)
# Byte 1 contains the max non-turbo frequecy
freq_p1 = msr_bytes[1]*100
return freq_p1
# Set package UNCORE frequency
def __set_uncore(freq):
freq = int(freq)/100
regstr = struct.pack('BBBBBBBB', freq, freq, 0, 0, 0, 0, 0, 0)
__wrmsr(0, 0x620, regstr)
__wrmsr(CPU_COUNT-1, 0x620, regstr)
time.sleep(0.1)
# get package UNCORE frequency
def __get_uncore():
regstr = __rdmsr(0, 0x621)
msr_bytes = struct.unpack('BBBBBBBB', regstr)
return int(msr_bytes[0]*100)
# get the online status of a core
def __is_online(core):
online_filename = "/sys/devices/system/cpu/cpu{}/online".format(core)
try:
with open(online_filename) as online_file:
return int(online_file.readline()) == 1
except IOError:
# If the file is not found, then the core is online
return True
# Get the CPU max frequency
def get_cpu_max_frequency(core):
""" Get the CPU max frequency"""
max_filename = "/sys/devices/system/cpu/cpu" + str(core) + "/cpufreq/cpuinfo_max_freq"
try:
with open(max_filename, 'r') as max_file:
maximum = int(max_file.readline().strip("\n"))
except IOError:
print("WARNING: cpuinfo_max_freq sysfs entry not found")
maximum = 0
return maximum
# Get the CPU min frequencey
def get_cpu_min_frequency(core):
""" Get the CPU min frequency"""
min_filename = "/sys/devices/system/cpu/cpu" + str(core) + "/cpufreq/cpuinfo_min_freq"
try:
with open(min_filename, 'r') as min_file:
minimum = int(min_file.readline().strip("\n"))
except IOError:
print("WARNING: cpuinfo_min_freq sysfs entry not found")
minimum = 0
return minimum
# Get the scaling max frequency
def get_scaling_max_frequency(core):
""" Get the scaling max frequency"""
max_filename = "/sys/devices/system/cpu/cpu" + str(core) + "/cpufreq/scaling_max_freq"
try:
with open(max_filename, 'r') as max_file:
maximum = int(max_file.readline().strip("\n"))
except IOError:
print("WARNING: scaling_max_freq sysfs entry not found")
maximum = 0
return maximum
# Get the scaling min frequencey
def get_scaling_min_frequency(core):
""" Get the scaling min frequency"""
min_filename = "/sys/devices/system/cpu/cpu" + str(core) + "/cpufreq/scaling_min_freq"
try:
with open(min_filename, 'r') as min_file:
minimum = int(min_file.readline().strip("\n"))
except IOError:
print("WARNING: scaling_min_freq sysfs entry not found")
minimum = 0
return minimum
# Get the SST-BF base frequencey
def get_sst_bf_frequency(core):
""" Get the SST-BF frequency"""
base_filename = "/sys/devices/system/cpu/cpu" + str(core) + "/cpufreq/base_frequency"
try:
with open(base_filename, 'r') as base_file:
base = int(base_file.readline().strip("\n")) // 1000
except IOError:
print("WARNING: base_frequency sysfs entry not found")
base = FREQ_P1
return base
def get_issbf_cpu_freqs():
"""Get the SST-BF frequencies."""
p1_high = 0
p1_normal = 0
for core in ONLINE_CORES:
base = get_sst_bf_frequency(core)
if p1_high == 0:
p1_high = base
if p1_normal == 0:
p1_normal = base
if base > p1_high:
p1_high = base
return (p1_high, p1_normal)
def check_driver():
"""check the name of the P-STATE driver"""
driver = "none"
try:
with open(DRV_FILE, 'r') as drv_file:
driver = drv_file.readline().strip("\n")
except IOError:
print()
print("ERROR: No pstate driver file found.")
print(" Are P-States enabled in the system BIOS?")
print()
return 0
if driver == "intel_pstate":
return 1
print("Invalid Driver : [" + driver + "]")
return 0
# Set max core frequency
def set_max_cpu_freq(maxfreq, core):
"""Set the core's max cpu frequency."""
max_filename = "/sys/devices/system/cpu/cpu" + str(core) + "/cpufreq/scaling_max_freq"
try:
with open(max_filename, 'w') as max_file:
max_file.write(str(maxfreq))
except IOError:
print("WARNING: cannot set core %s to %s" % (str(core), str(maxfreq)))
# Set min core frequency
def set_min_cpu_freq(minfreq, core):
"""Set the core's min cpu frequency."""
min_filename = "/sys/devices/system/cpu/cpu" + str(core) + "/cpufreq/scaling_min_freq"
try:
with open(min_filename, 'w') as min_file:
min_file.write(str(minfreq))
except IOError:
print("WARNING: cannot set core %s to %s" % (str(core), str(minfreq)))
# Set core governor
def set_core_governor(governor, core):
"""Set the core's power governor"""
min_filename = "/sys/devices/system/cpu/cpu" + str(core) + "/cpufreq/scaling_governor"
try:
with open(min_filename, 'w') as min_file:
min_file.write(governor)
except IOError:
print("WARNING: cannot write to scaling_governor sysfs file")
def getcpu_count():
"""Get the number of cores in the system."""
cpus = os.listdir("/sys/devices/system/cpu")
regex = re.compile(r'cpu[0-9]')
cpus = list(filter(regex.search, cpus))
return len(cpus)
def set_sst_bf(mode):
"""Enable SST_BF mode"""
print("CPU Count = " + str(CPU_COUNT))
for core in ONLINE_CORES:
set_core_governor("powersave", core)
set_max_cpu_freq(FREQ_P0*1000, core)
base = get_sst_bf_frequency(core)
if mode == 0:
set_min_cpu_freq(base*1000, core)
set_max_cpu_freq(base*1000, core)
elif mode == 1:
set_min_cpu_freq(FREQ_P1*1000, core)
set_max_cpu_freq(FREQ_P1*1000, core)
query_sst_bf()
def reverse_sst_bf():
"""Reverse SST-BF mode"""
print("CPU Count = " + str(CPU_COUNT))
for core in ONLINE_CORES:
maximum = get_cpu_max_frequency(core)
set_max_cpu_freq(maximum, core)
minimum = get_cpu_min_frequency(core)
set_min_cpu_freq(minimum, core)
query_sst_bf()
def sst_bf_enabled():
prev = None
for core in ONLINE_CORES:
base = get_sst_bf_frequency(core)
if not prev:
prev = base
continue
if prev != base:
return True # we found at least one core that's different, bail out
return False # all cores appear to be the same
def query_sst_bf():
""""Show information on sst-bf frequencies."""
print("CPUs = " + str(CPU_COUNT))
freq_p1 = get_cpu_base_frequency()
print("Base = " + str(freq_p1))
p1_high = 0
print(" |------sysfs-------|")
print("Core | base max min |")
print("-----|------------------|")
for core in ONLINE_CORES:
base = get_sst_bf_frequency(core)
maximum = get_scaling_max_frequency(core)
minimum = get_scaling_min_frequency(core)
print(str(core).rjust(4) + " | " + \
str(base).rjust(4) + " " + \
str(int(maximum//1000)).rjust(4) + " " + \
str(int(minimum//1000)).rjust(4) + " |")
if base > freq_p1:
p1_high = p1_high + 1
print("-----|------------------|")
print("We have " + str(p1_high) + " high priority cores according to sysfs base_frequency.")
def list_sst_bf_cores():
"""Short, comma-separated list of bf cores."""
freq_p1 = get_cpu_base_frequency()
cores = []
for core in ONLINE_CORES:
base = get_sst_bf_frequency(core)
if base > freq_p1:
cores.append(core)
print(*cores, sep=",")
# Coremask may be bigger than 64-bit number, but Python 2 and 3 handle big numbers differently
# Python 2 has 'long' type while Python 3 uses 'int'.
try:
coremask = long(0)
except NameError:
coremask = int(0)
for core in cores:
coremask = coremask + (2**core)
print(hex(coremask).rstrip('L'))
def list_sst_bf_normal_cores():
"""Short, comma-separated list of bf normal cores."""
freq_p1 = get_cpu_base_frequency()
cores = []
for core in ONLINE_CORES:
base = get_sst_bf_frequency(core)
if base <= freq_p1:
cores.append(core)
print(*cores, sep=",")
# Coremask may be bigger than 64-bit number, but Python 2 and 3 handle big numbers differently
# Python 2 has 'long' type while Python 3 uses 'int'.
try:
coremask = long(0)
except NameError:
coremask = int(0)
for core in cores:
coremask = coremask + (2**core)
print(hex(coremask).rstrip('L'))
def print_banner():
"""Print script banner."""
print("----------------------------------------------------------")
print("SST-BF SETUP SCRIPT v" + SCRIPT_VERSION)
print("----------------------------------------------------------")
def __print_wrap(opt, text):
wrapper = textwrap.TextWrapper()
lines = wrapper.wrap(text)
first = 1
for line in lines:
if first == 1:
print("[%s] %s" % (opt, line))
first = 0
else:
print(" %s" % line)
def __print_help():
print("")
print_banner()
print("")
__print_wrap('s', HELP_TEXT_S_LONG)
__print_wrap('m', HELP_TEXT_M_LONG)
__print_wrap('r', HELP_TEXT_R_LONG)
print("[i] %s" % HELP_TEXT_I)
print("[l] %s" % HELP_TEXT_L)
print("[n] %s" % HELP_TEXT_N)
print("[v] %s" % HELP_TEXT_V)
print("[h] %s" % HELP_TEXT_H)
print(" Run script with no arguments for interactive menu")
print(" Run script with -h for help on the supported arguments")
def show_version():
"""print out the script version."""
print(SCRIPT_VERSION)
def do_menu():
"""Show the main menu to the user."""
print("")
print_banner()
print("")
print("[s] %s" % HELP_TEXT_S)
print("[m] %s" % HELP_TEXT_M)
print("[r] %s" % HELP_TEXT_R)
print("[i] %s" % HELP_TEXT_I)
print("[l] %s" % HELP_TEXT_L)
print("[n] %s" % HELP_TEXT_N)
print("[v] %s" % HELP_TEXT_V)
print("[h] %s" % HELP_TEXT_H)
print("")
print("[q] Exit Script")
print("----------------------------------------------------------")
text = raw_input("Option: ")
if text == "s":
set_sst_bf(0)
elif text == "m":
set_sst_bf(1)
elif text == "r":
reverse_sst_bf()
elif text == "i":
query_sst_bf()
elif text == "l":
list_sst_bf_cores()
elif text == "n":
list_sst_bf_normal_cores()
elif text == "v":
show_version()
elif text == "h":
__print_help()
elif text == "q":
sys.exit(0)
else:
print("")
print("Unknown Option")
#
# Do some prerequesite checks.
#
try:
open(MSR_FILE, "r")
except IOError:
print("ERROR: Need the 'msr' kernel module")
print("Please run 'modprobe msr'")
sys.exit(1)
if get_turbo_disabled() > 0:
print("ERROR: Turbo Boost not enabled in BIOS. Exiting.")
sys.exit(1)
if get_hwp_enabled() == 0:
print("ERROR: HWP not enabled in BIOS. Exiting.")
sys.exit(1)
if check_driver() == 0:
sys.exit(1)
CPU_COUNT = getcpu_count()
ONLINE_CORES = list(filter(__is_online, range(0, CPU_COUNT)))
FREQ_P1 = get_cpu_base_frequency()
BASE = get_sst_bf_frequency(0)
if BASE == FREQ_P1:
print("ERROR: No High Priority cores found. Is SST-BF enabled in BIOS?")
print(" (base_frequency equals p1 frequency)")
sys.exit(-1)
if not sst_bf_enabled():
print("No High Priority Cores found. ")
print("Please ensure compatible BIOS and Kernel versions are being used.")
sys.exit(-1)
FREQ_P0 = get_cpu_max_frequency(0) // 1000
FREQ_P1N = get_cpu_min_frequency(0) // 1000
(FREQ_P1_HIGH, FREQ_P1_NORMAL) = get_issbf_cpu_freqs()
SCRIPT_NAME = sys.argv[0]
# Set up the parser arguments, along with their help text. The help text
# contains the actual min/max/base frequencies from the local machine for
# extra clarity when using the script.
# The HELP_TEXT variables are also used later for the interactive menu.
PARSER = argparse.ArgumentParser(description="Configure SST-BF frequencies")
HELP_TEXT_S = "Set SST-BF config (set min/max to %s/%s and %s/%s)" % \
(str(FREQ_P1_HIGH),
str(FREQ_P1_HIGH),
str(FREQ_P1_NORMAL),
str(FREQ_P1_NORMAL))
HELP_TEXT_S_LONG = "Set SST-BF config. Set high priority cores to %s" \
" minimum and %s maximum, and set normal priority" \
" cores to %s minimum and %s maximum." % \
(str(FREQ_P1_HIGH),
str(FREQ_P1_HIGH),
str(FREQ_P1_NORMAL),
str(FREQ_P1_NORMAL))
PARSER.add_argument('-s', action="store_true", help=HELP_TEXT_S_LONG)
HELP_TEXT_M = "Set P1 on all cores (set min/max to %s/%s)" % \
(str(FREQ_P1),
str(FREQ_P1))
HELP_TEXT_M_LONG = "Set P1 on all cores. Set all cores to %s" \
" minimum and %s maximum." % \
(str(FREQ_P1),
str(FREQ_P1))
PARSER.add_argument('-m', action="store_true", help=HELP_TEXT_M_LONG)
HELP_TEXT_R = "Revert cores to min/Turbo (set min/max to %s/%s)" % \
(str(FREQ_P1N),
str(FREQ_P0))
HELP_TEXT_R_LONG = "Revert cores to minimum/Turbo. Set all cores to %s" \
" minimum and %s maximum." % \
(str(FREQ_P1N),
str(FREQ_P0))
PARSER.add_argument('-r', action="store_true", help=HELP_TEXT_R_LONG)
HELP_TEXT_I = "Show current SST-BF frequency information"
PARSER.add_argument('-i', action="store_true", help=HELP_TEXT_I)
HELP_TEXT_L = "List High Priority cores"
PARSER.add_argument('-l', action="store_true", help=HELP_TEXT_L)
HELP_TEXT_N = "List Normal Priority cores"
PARSER.add_argument('-n', action="store_true", help=HELP_TEXT_N)
HELP_TEXT_U = "Set UNCORE frequency, e.g. -u 1800 sets to 1.8GHz"
PARSER.add_argument('-u', type=int, help=HELP_TEXT_U)
HELP_TEXT_V = "Show script version"
PARSER.add_argument('-v', action="store_true", help=HELP_TEXT_V)
HELP_TEXT_H = "Print additional help on menu options"
ARGS = PARSER.parse_args()
if ARGS.s:
set_sst_bf(0)
sys.exit(0)
if ARGS.m:
set_sst_bf(1)
sys.exit(0)
if ARGS.r:
reverse_sst_bf()
sys.exit(0)
if ARGS.i:
query_sst_bf()
sys.exit(0)
if ARGS.l:
list_sst_bf_cores()
sys.exit(0)
if ARGS.n:
list_sst_bf_normal_cores()
sys.exit(0)
if ARGS.u:
__set_uncore(ARGS.u)
UNCORE_FREQ = __get_uncore()
print("Uncore frequency now running at %dMHz" % UNCORE_FREQ)
sys.exit(0)
if ARGS.v:
show_version()
sys.exit(0)
while 1:
do_menu()
print("")
raw_input("Press enter to continue ... ")
print("")