Skip to content

Commit fd242a7

Browse files
committed
Refactor disable_frequency_scaling to multiple methods with meaningful names
1 parent 977039d commit fd242a7

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

lib/cpu_config.rb

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,48 @@ def configure_for_benchmarking(turbo:)
1313

1414
# Disable Turbo Boost while running benchmarks. Maximize the CPU frequency.
1515
def disable_frequency_scaling(turbo:)
16-
# sudo requires the flag '-S' in order to take input from stdin
17-
if File.exist?('/sys/devices/system/cpu/intel_pstate') # Intel
18-
unless intel_no_turbo? || turbo
19-
BenchmarkRunner.check_call("sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo'")
20-
at_exit { BenchmarkRunner.check_call("sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo'", quiet: true) }
21-
end
22-
# Disabling Turbo Boost reduces the CPU frequency, so this should be run after that.
23-
BenchmarkRunner.check_call("sudo -S sh -c 'echo 100 > /sys/devices/system/cpu/intel_pstate/min_perf_pct'") unless intel_perf_100pct?
24-
elsif File.exist?('/sys/devices/system/cpu/cpufreq/boost') # AMD
25-
unless amd_no_boost? || turbo
26-
BenchmarkRunner.check_call("sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost'")
27-
at_exit { BenchmarkRunner.check_call("sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/cpufreq/boost'", quiet: true) }
28-
end
29-
BenchmarkRunner.check_call("sudo -S cpupower frequency-set -g performance") unless performance_governor?
16+
if intel_cpu?
17+
disable_intel_turbo unless turbo
18+
maximize_intel_frequency
19+
elsif amd_cpu?
20+
disable_amd_boost unless turbo
21+
set_performance_governor
3022
end
3123
end
3224

25+
def intel_cpu?
26+
File.exist?('/sys/devices/system/cpu/intel_pstate')
27+
end
28+
29+
def amd_cpu?
30+
File.exist?('/sys/devices/system/cpu/cpufreq/boost')
31+
end
32+
33+
def disable_intel_turbo
34+
return if intel_no_turbo?
35+
# sudo requires the flag '-S' in order to take input from stdin
36+
BenchmarkRunner.check_call("sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo'")
37+
at_exit { BenchmarkRunner.check_call("sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo'", quiet: true) }
38+
end
39+
40+
def maximize_intel_frequency
41+
return if intel_perf_100pct?
42+
# Disabling Turbo Boost reduces the CPU frequency, so this should be run after that.
43+
BenchmarkRunner.check_call("sudo -S sh -c 'echo 100 > /sys/devices/system/cpu/intel_pstate/min_perf_pct'")
44+
end
45+
46+
def disable_amd_boost
47+
return if amd_no_boost?
48+
# sudo requires the flag '-S' in order to take input from stdin
49+
BenchmarkRunner.check_call("sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost'")
50+
at_exit { BenchmarkRunner.check_call("sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/cpufreq/boost'", quiet: true) }
51+
end
52+
53+
def set_performance_governor
54+
return if performance_governor?
55+
BenchmarkRunner.check_call("sudo -S cpupower frequency-set -g performance")
56+
end
57+
3358
def intel_no_turbo?
3459
File.exist?('/sys/devices/system/cpu/intel_pstate/no_turbo') &&
3560
File.read('/sys/devices/system/cpu/intel_pstate/no_turbo').strip == '1'

0 commit comments

Comments
 (0)