|
2 | 2 |
|
3 | 3 | # Manages CPU frequency and turbo boost configuration for benchmark consistency |
4 | 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 |
| 5 | + # Configure CPU for benchmarking: disable frequency scaling and verify settings |
| 6 | + def self.configure_for_benchmarking(turbo:) |
| 7 | + new.configure_for_benchmarking(turbo: turbo) |
| 8 | + end |
11 | 9 |
|
12 | | - private |
| 10 | + def initialize |
| 11 | + @intel_cpu = File.exist?('/sys/devices/system/cpu/intel_pstate') |
| 12 | + @amd_cpu = File.exist?('/sys/devices/system/cpu/cpufreq/boost') |
| 13 | + end |
13 | 14 |
|
14 | | - # Disable Turbo Boost while running benchmarks. Maximize the CPU frequency. |
15 | | - def disable_frequency_scaling(turbo:) |
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 |
22 | | - end |
23 | | - end |
| 15 | + def configure_for_benchmarking(turbo:) |
| 16 | + disable_frequency_scaling(turbo: turbo) |
| 17 | + check_pstate(turbo: turbo) |
| 18 | + end |
24 | 19 |
|
25 | | - def intel_cpu? |
26 | | - File.exist?('/sys/devices/system/cpu/intel_pstate') |
27 | | - end |
| 20 | + private |
28 | 21 |
|
29 | | - def amd_cpu? |
30 | | - File.exist?('/sys/devices/system/cpu/cpufreq/boost') |
| 22 | + # Disable Turbo Boost while running benchmarks. Maximize the CPU frequency. |
| 23 | + def disable_frequency_scaling(turbo:) |
| 24 | + if intel_cpu? |
| 25 | + disable_intel_turbo unless turbo |
| 26 | + maximize_intel_frequency |
| 27 | + elsif amd_cpu? |
| 28 | + disable_amd_boost unless turbo |
| 29 | + set_performance_governor |
31 | 30 | end |
| 31 | + end |
32 | 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 |
| 33 | + def intel_cpu? |
| 34 | + @intel_cpu |
| 35 | + end |
39 | 36 |
|
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 |
| 37 | + def amd_cpu? |
| 38 | + @amd_cpu |
| 39 | + end |
45 | 40 |
|
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 |
| 41 | + def disable_intel_turbo |
| 42 | + return if intel_no_turbo? |
| 43 | + # sudo requires the flag '-S' in order to take input from stdin |
| 44 | + BenchmarkRunner.check_call("sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo'") |
| 45 | + at_exit { BenchmarkRunner.check_call("sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo'", quiet: true) } |
| 46 | + end |
52 | 47 |
|
53 | | - def set_performance_governor |
54 | | - return if performance_governor? |
55 | | - BenchmarkRunner.check_call("sudo -S cpupower frequency-set -g performance") |
56 | | - end |
| 48 | + def maximize_intel_frequency |
| 49 | + return if intel_perf_100pct? |
| 50 | + # Disabling Turbo Boost reduces the CPU frequency, so this should be run after that. |
| 51 | + BenchmarkRunner.check_call("sudo -S sh -c 'echo 100 > /sys/devices/system/cpu/intel_pstate/min_perf_pct'") |
| 52 | + end |
57 | 53 |
|
58 | | - def intel_no_turbo? |
59 | | - File.exist?('/sys/devices/system/cpu/intel_pstate/no_turbo') && |
60 | | - File.read('/sys/devices/system/cpu/intel_pstate/no_turbo').strip == '1' |
61 | | - end |
| 54 | + def disable_amd_boost |
| 55 | + return if amd_no_boost? |
| 56 | + # sudo requires the flag '-S' in order to take input from stdin |
| 57 | + BenchmarkRunner.check_call("sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost'") |
| 58 | + at_exit { BenchmarkRunner.check_call("sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/cpufreq/boost'", quiet: true) } |
| 59 | + end |
62 | 60 |
|
63 | | - def intel_perf_100pct? |
64 | | - File.exist?('/sys/devices/system/cpu/intel_pstate/min_perf_pct') && |
65 | | - File.read('/sys/devices/system/cpu/intel_pstate/min_perf_pct').strip == '100' |
66 | | - end |
| 61 | + def set_performance_governor |
| 62 | + return if performance_governor? |
| 63 | + BenchmarkRunner.check_call("sudo -S cpupower frequency-set -g performance") |
| 64 | + end |
| 65 | + |
| 66 | + def intel_no_turbo? |
| 67 | + File.exist?('/sys/devices/system/cpu/intel_pstate/no_turbo') && |
| 68 | + File.read('/sys/devices/system/cpu/intel_pstate/no_turbo').strip == '1' |
| 69 | + end |
| 70 | + |
| 71 | + def intel_perf_100pct? |
| 72 | + File.exist?('/sys/devices/system/cpu/intel_pstate/min_perf_pct') && |
| 73 | + File.read('/sys/devices/system/cpu/intel_pstate/min_perf_pct').strip == '100' |
| 74 | + end |
67 | 75 |
|
68 | | - def amd_no_boost? |
69 | | - File.exist?('/sys/devices/system/cpu/cpufreq/boost') && |
70 | | - File.read('/sys/devices/system/cpu/cpufreq/boost').strip == '0' |
| 76 | + def amd_no_boost? |
| 77 | + File.exist?('/sys/devices/system/cpu/cpufreq/boost') && |
| 78 | + File.read('/sys/devices/system/cpu/cpufreq/boost').strip == '0' |
| 79 | + end |
| 80 | + |
| 81 | + def performance_governor? |
| 82 | + Dir.glob('/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor').all? do |governor| |
| 83 | + File.read(governor).strip == 'performance' |
71 | 84 | end |
| 85 | + end |
72 | 86 |
|
73 | | - def performance_governor? |
74 | | - Dir.glob('/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor').all? do |governor| |
75 | | - File.read(governor).strip == 'performance' |
| 87 | + # Verify that CPU frequency settings have been configured correctly |
| 88 | + def check_pstate(turbo:) |
| 89 | + if intel_cpu? |
| 90 | + unless turbo || intel_no_turbo? |
| 91 | + puts("You forgot to disable turbo:") |
| 92 | + puts(" sudo sh -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo'") |
| 93 | + exit(-1) |
| 94 | + end |
| 95 | + |
| 96 | + unless intel_perf_100pct? |
| 97 | + puts("You forgot to set the min perf percentage to 100:") |
| 98 | + puts(" sudo sh -c 'echo 100 > /sys/devices/system/cpu/intel_pstate/min_perf_pct'") |
| 99 | + exit(-1) |
| 100 | + end |
| 101 | + elsif amd_cpu? |
| 102 | + unless turbo || amd_no_boost? |
| 103 | + puts("You forgot to disable boost:") |
| 104 | + puts(" sudo sh -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost'") |
| 105 | + exit(-1) |
76 | 106 | end |
77 | | - end |
78 | 107 |
|
79 | | - # Verify that CPU frequency settings have been configured correctly |
80 | | - def check_pstate(turbo:) |
81 | | - if File.exist?('/sys/devices/system/cpu/intel_pstate') # Intel |
82 | | - unless turbo || intel_no_turbo? |
83 | | - puts("You forgot to disable turbo:") |
84 | | - puts(" sudo sh -c 'echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo'") |
85 | | - exit(-1) |
86 | | - end |
87 | | - |
88 | | - unless intel_perf_100pct? |
89 | | - puts("You forgot to set the min perf percentage to 100:") |
90 | | - puts(" sudo sh -c 'echo 100 > /sys/devices/system/cpu/intel_pstate/min_perf_pct'") |
91 | | - exit(-1) |
92 | | - end |
93 | | - elsif File.exist?('/sys/devices/system/cpu/cpufreq/boost') # AMD |
94 | | - unless turbo || amd_no_boost? |
95 | | - puts("You forgot to disable boost:") |
96 | | - puts(" sudo sh -c 'echo 0 > /sys/devices/system/cpu/cpufreq/boost'") |
97 | | - exit(-1) |
98 | | - end |
99 | | - |
100 | | - unless performance_governor? |
101 | | - puts("You forgot to set the performance governor:") |
102 | | - puts(" sudo cpupower frequency-set -g performance") |
103 | | - exit(-1) |
104 | | - end |
| 108 | + unless performance_governor? |
| 109 | + puts("You forgot to set the performance governor:") |
| 110 | + puts(" sudo cpupower frequency-set -g performance") |
| 111 | + exit(-1) |
105 | 112 | end |
106 | 113 | end |
107 | 114 | end |
|
0 commit comments