@@ -9,9 +9,9 @@ def configure_for_benchmarking(turbo:)
99 end
1010
1111 def build
12- if File . exist? ( '/sys/devices/system/cpu/intel_pstate' )
12+ if File . exist? ( IntelCPUConfig :: PSTATE_DIR )
1313 IntelCPUConfig . new
14- elsif File . exist? ( '/sys/devices/system/cpu/cpufreq/boost' )
14+ elsif File . exist? ( AMDCPUConfig :: BOOST_PATH )
1515 AMDCPUConfig . new
1616 else
1717 NullCPUConfig . new
@@ -56,79 +56,92 @@ def check_pstate(turbo:)
5656
5757# Intel CPU configuration
5858class IntelCPUConfig < CPUConfig
59+ PSTATE_DIR = '/sys/devices/system/cpu/intel_pstate'
60+ NO_TURBO_PATH = "#{ PSTATE_DIR } /no_turbo"
61+ MIN_PERF_PCT_PATH = "#{ PSTATE_DIR } /min_perf_pct"
62+ TURBO_DISABLED_VALUE = '1'
63+ FREQUENCY_MAXIMIZED_VALUE = '100'
64+
5965 private
6066
6167 def disable_turbo_boost
6268 # sudo requires the flag '-S' in order to take input from stdin
63- BenchmarkRunner . check_call ( "sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo '" )
64- at_exit { BenchmarkRunner . check_call ( "sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo '" , quiet : true ) }
69+ BenchmarkRunner . check_call ( "sudo -S sh -c 'echo #{ TURBO_DISABLED_VALUE } > #{ NO_TURBO_PATH } '" )
70+ at_exit { BenchmarkRunner . check_call ( "sudo -S sh -c 'echo 0 > #{ NO_TURBO_PATH } '" , quiet : true ) }
6571 end
6672
6773 def maximize_frequency
6874 # Disabling Turbo Boost reduces the CPU frequency, so this should be run after that.
69- BenchmarkRunner . check_call ( "sudo -S sh -c 'echo 100 > /sys/devices/system/cpu/intel_pstate/min_perf_pct '" )
75+ BenchmarkRunner . check_call ( "sudo -S sh -c 'echo #{ FREQUENCY_MAXIMIZED_VALUE } > #{ MIN_PERF_PCT_PATH } '" )
7076 end
7177
7278 def turbo_disabled?
73- @turbo_disabled ||= File . exist? ( '/sys/devices/system/cpu/intel_pstate/no_turbo' ) &&
74- File . read ( '/sys/devices/system/cpu/intel_pstate/no_turbo' ) . strip == '1'
79+ @turbo_disabled ||= File . exist? ( NO_TURBO_PATH ) &&
80+ File . read ( NO_TURBO_PATH ) . strip == TURBO_DISABLED_VALUE
7581 end
7682
7783 def frequency_maximized?
78- @frequency_maximized ||= File . exist? ( '/sys/devices/system/cpu/intel_pstate/min_perf_pct' ) &&
79- File . read ( '/sys/devices/system/cpu/intel_pstate/min_perf_pct' ) . strip == '100'
84+ @frequency_maximized ||= File . exist? ( MIN_PERF_PCT_PATH ) &&
85+ File . read ( MIN_PERF_PCT_PATH ) . strip == FREQUENCY_MAXIMIZED_VALUE
8086 end
8187
8288 def check_pstate ( turbo :)
8389 unless turbo || turbo_disabled?
8490 puts ( "You forgot to disable turbo:" )
85- puts ( " sudo sh -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo '" )
91+ puts ( " sudo sh -c 'echo #{ TURBO_DISABLED_VALUE } > #{ NO_TURBO_PATH } '" )
8692 exit ( -1 )
8793 end
8894
8995 unless frequency_maximized?
9096 puts ( "You forgot to set the min perf percentage to 100:" )
91- puts ( " sudo sh -c 'echo 100 > /sys/devices/system/cpu/intel_pstate/min_perf_pct '" )
97+ puts ( " sudo sh -c 'echo #{ FREQUENCY_MAXIMIZED_VALUE } > #{ MIN_PERF_PCT_PATH } '" )
9298 exit ( -1 )
9399 end
94100 end
95101end
96102
97103# AMD CPU configuration
98104class AMDCPUConfig < CPUConfig
105+ CPUFREQ_DIR = '/sys/devices/system/cpu/cpufreq'
106+ BOOST_PATH = "#{ CPUFREQ_DIR } /boost"
107+ SCALING_GOVERNOR_GLOB = '/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor'
108+ TURBO_DISABLED_VALUE = '0'
109+ TURBO_ENABLED_VALUE = '1'
110+ PERFORMANCE_GOVERNOR = 'performance'
111+
99112 private
100113
101114 def disable_turbo_boost
102115 # sudo requires the flag '-S' in order to take input from stdin
103- BenchmarkRunner . check_call ( "sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost '" )
104- at_exit { BenchmarkRunner . check_call ( "sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/cpufreq/boost '" , quiet : true ) }
116+ BenchmarkRunner . check_call ( "sudo -S sh -c 'echo #{ TURBO_DISABLED_VALUE } > #{ BOOST_PATH } '" )
117+ at_exit { BenchmarkRunner . check_call ( "sudo -S sh -c 'echo #{ TURBO_ENABLED_VALUE } > #{ BOOST_PATH } '" , quiet : true ) }
105118 end
106119
107120 def maximize_frequency
108121 BenchmarkRunner . check_call ( "sudo -S cpupower frequency-set -g performance" )
109122 end
110123
111124 def turbo_disabled?
112- @turbo_disabled ||= File . exist? ( '/sys/devices/system/cpu/cpufreq/boost' ) &&
113- File . read ( '/sys/devices/system/cpu/cpufreq/boost' ) . strip == '0'
125+ @turbo_disabled ||= File . exist? ( BOOST_PATH ) &&
126+ File . read ( BOOST_PATH ) . strip == TURBO_DISABLED_VALUE
114127 end
115128
116129 def frequency_maximized?
117- @frequency_maximized ||= Dir . glob ( '/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor' ) . all? do |governor |
118- File . read ( governor ) . strip == 'performance'
130+ @frequency_maximized ||= Dir . glob ( SCALING_GOVERNOR_GLOB ) . all? do |governor |
131+ File . read ( governor ) . strip == PERFORMANCE_GOVERNOR
119132 end
120133 end
121134
122135 def check_pstate ( turbo :)
123136 unless turbo || turbo_disabled?
124137 puts ( "You forgot to disable boost:" )
125- puts ( " sudo sh -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost '" )
138+ puts ( " sudo sh -c 'echo #{ TURBO_DISABLED_VALUE } > #{ BOOST_PATH } '" )
126139 exit ( -1 )
127140 end
128141
129142 unless frequency_maximized?
130143 puts ( "You forgot to set the performance governor:" )
131- puts ( " sudo cpupower frequency-set -g performance " )
144+ puts ( " sudo cpupower frequency-set -g #{ PERFORMANCE_GOVERNOR } " )
132145 exit ( -1 )
133146 end
134147 end
0 commit comments