Skip to content

Commit 69a4ee5

Browse files
committed
Extract CPU configuration logic into its own file
And add tests for it.
1 parent 69f4205 commit 69a4ee5

3 files changed

Lines changed: 407 additions & 70 deletions

File tree

lib/cpu_config.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
require_relative 'benchmark_runner'
2+
3+
# Manages CPU frequency and turbo boost configuration for benchmark consistency
4+
class CPUConfig
5+
class << self
6+
# Configure CPU for benchmarking: disable frequency scaling and verify settings
7+
def configure_for_benchmarking(turbo:)
8+
disable_frequency_scaling(turbo: turbo)
9+
check_pstate(turbo: turbo)
10+
end
11+
12+
private
13+
14+
# Disable Turbo Boost while running benchmarks. Maximize the CPU frequency.
15+
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?
30+
end
31+
end
32+
33+
def intel_no_turbo?
34+
File.exist?('/sys/devices/system/cpu/intel_pstate/no_turbo') &&
35+
File.read('/sys/devices/system/cpu/intel_pstate/no_turbo').strip == '1'
36+
end
37+
38+
def intel_perf_100pct?
39+
File.exist?('/sys/devices/system/cpu/intel_pstate/min_perf_pct') &&
40+
File.read('/sys/devices/system/cpu/intel_pstate/min_perf_pct').strip == '100'
41+
end
42+
43+
def amd_no_boost?
44+
File.exist?('/sys/devices/system/cpu/cpufreq/boost') &&
45+
File.read('/sys/devices/system/cpu/cpufreq/boost').strip == '0'
46+
end
47+
48+
def performance_governor?
49+
Dir.glob('/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor').all? do |governor|
50+
File.read(governor).strip == 'performance'
51+
end
52+
end
53+
54+
# Verify that CPU frequency settings have been configured correctly
55+
def check_pstate(turbo:)
56+
if File.exist?('/sys/devices/system/cpu/intel_pstate') # Intel
57+
unless turbo || intel_no_turbo?
58+
puts("You forgot to disable turbo:")
59+
puts(" sudo sh -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo'")
60+
exit(-1)
61+
end
62+
63+
unless intel_perf_100pct?
64+
puts("You forgot to set the min perf percentage to 100:")
65+
puts(" sudo sh -c 'echo 100 > /sys/devices/system/cpu/intel_pstate/min_perf_pct'")
66+
exit(-1)
67+
end
68+
elsif File.exist?('/sys/devices/system/cpu/cpufreq/boost') # AMD
69+
unless turbo || amd_no_boost?
70+
puts("You forgot to disable boost:")
71+
puts(" sudo sh -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost'")
72+
exit(-1)
73+
end
74+
75+
unless performance_governor?
76+
puts("You forgot to set the performance governor:")
77+
puts(" sudo cpupower frequency-set -g performance")
78+
exit(-1)
79+
end
80+
end
81+
end
82+
end
83+
end

run_benchmarks.rb

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require 'etc'
1212
require 'yaml'
1313
require_relative 'misc/stats'
14+
require_relative 'lib/cpu_config'
1415
require_relative 'lib/benchmark_runner'
1516
require_relative 'lib/table_formatter'
1617
require_relative 'lib/benchmark_filter'
@@ -20,71 +21,6 @@ def have_yjit?(ruby)
2021
ruby_version.downcase.include?("yjit")
2122
end
2223

23-
# Disable Turbo Boost while running benchmarks. Maximize the CPU frequency.
24-
def set_bench_config(turbo:)
25-
# sudo requires the flag '-S' in order to take input from stdin
26-
if File.exist?('/sys/devices/system/cpu/intel_pstate') # Intel
27-
unless intel_no_turbo? || turbo
28-
BenchmarkRunner.check_call("sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo'")
29-
at_exit { BenchmarkRunner.check_call("sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo'", quiet: true) }
30-
end
31-
# Disabling Turbo Boost reduces the CPU frequency, so this should be run after that.
32-
BenchmarkRunner.check_call("sudo -S sh -c 'echo 100 > /sys/devices/system/cpu/intel_pstate/min_perf_pct'") unless intel_perf_100pct?
33-
elsif File.exist?('/sys/devices/system/cpu/cpufreq/boost') # AMD
34-
unless amd_no_boost? || turbo
35-
BenchmarkRunner.check_call("sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost'")
36-
at_exit { BenchmarkRunner.check_call("sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/cpufreq/boost'", quiet: true) }
37-
end
38-
BenchmarkRunner.check_call("sudo -S cpupower frequency-set -g performance") unless performance_governor?
39-
end
40-
end
41-
42-
def check_pstate(turbo:)
43-
if File.exist?('/sys/devices/system/cpu/intel_pstate') # Intel
44-
unless turbo || intel_no_turbo?
45-
puts("You forgot to disable turbo:")
46-
puts(" sudo sh -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo'")
47-
exit(-1)
48-
end
49-
50-
unless intel_perf_100pct?
51-
puts("You forgot to set the min perf percentage to 100:")
52-
puts(" sudo sh -c 'echo 100 > /sys/devices/system/cpu/intel_pstate/min_perf_pct'")
53-
exit(-1)
54-
end
55-
elsif File.exist?('/sys/devices/system/cpu/cpufreq/boost') # AMD
56-
unless turbo || amd_no_boost?
57-
puts("You forgot to disable boost:")
58-
puts(" sudo sh -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost'")
59-
exit(-1)
60-
end
61-
62-
unless performance_governor?
63-
puts("You forgot to set the performance governor:")
64-
puts(" sudo cpupower frequency-set -g performance")
65-
exit(-1)
66-
end
67-
end
68-
end
69-
70-
def intel_no_turbo?
71-
File.read('/sys/devices/system/cpu/intel_pstate/no_turbo').strip == '1'
72-
end
73-
74-
def intel_perf_100pct?
75-
File.read('/sys/devices/system/cpu/intel_pstate/min_perf_pct').strip == '100'
76-
end
77-
78-
def amd_no_boost?
79-
File.read('/sys/devices/system/cpu/cpufreq/boost').strip == '0'
80-
end
81-
82-
def performance_governor?
83-
Dir.glob('/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor').all? do |governor|
84-
File.read(governor).strip == 'performance'
85-
end
86-
end
87-
8824
def mean(values)
8925
Stats.new(values).mean
9026
end
@@ -366,11 +302,7 @@ def run_benchmarks(ruby:, ruby_description:, categories:, name_filters:, out_pat
366302
end
367303
end
368304

369-
# Disable CPU frequency scaling
370-
set_bench_config(turbo: args.turbo)
371-
372-
# Check pstate status
373-
check_pstate(turbo: args.turbo)
305+
CPUConfig.configure_for_benchmarking(turbo: args.turbo)
374306

375307
# Create the output directory
376308
FileUtils.mkdir_p(args.out_path)

0 commit comments

Comments
 (0)