Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/cpu_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ def maximize_frequency
def check_pstate(turbo:)
# Override in subclasses
end

# A quiet sudo --non-interactive call that on failure, retries asking for input
def sudo_prefer_quiet(command)
# For compatibility, we use short flags older sudo versions understand.
# -n is --non-interactive, -S is --stdin
result = BenchmarkRunner.check_call("sudo -n #{command}", quiet: true, raise_error: false)
unless result[:success]
result = BenchmarkRunner.check_call("sudo -S #{command}")
end
result
end
end

# Intel CPU configuration
Expand All @@ -67,7 +78,7 @@ class IntelCPUConfig < CPUConfig
def disable_turbo_boost
# sudo requires the flag '-S' in order to take input from stdin
BenchmarkRunner.check_call("sudo -S sh -c 'echo #{TURBO_DISABLED_VALUE} > #{NO_TURBO_PATH}'")
at_exit { BenchmarkRunner.check_call("sudo -S sh -c 'echo 0 > #{NO_TURBO_PATH}'", quiet: true) }
at_exit { sudo_prefer_quiet("sh -c 'echo 0 > #{NO_TURBO_PATH}'") }
end

def maximize_frequency
Expand Down Expand Up @@ -114,7 +125,7 @@ class AMDCPUConfig < CPUConfig
def disable_turbo_boost
# sudo requires the flag '-S' in order to take input from stdin
BenchmarkRunner.check_call("sudo -S sh -c 'echo #{TURBO_DISABLED_VALUE} > #{BOOST_PATH}'")
at_exit { BenchmarkRunner.check_call("sudo -S sh -c 'echo #{TURBO_ENABLED_VALUE} > #{BOOST_PATH}'", quiet: true) }
at_exit { sudo_prefer_quiet("sh -c 'echo #{TURBO_ENABLED_VALUE} > #{BOOST_PATH}'") }
end

def maximize_frequency
Expand Down
20 changes: 14 additions & 6 deletions test/cpu_config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,17 @@

# Verify at_exit block restores Intel turbo settings
cleanup_commands = []
BenchmarkRunner.stub :check_call, ->(cmd, **opts) { cleanup_commands << { cmd: cmd, opts: opts } } do
check_call_stub = ->(cmd, **opts) do
cleanup_commands << { cmd: cmd, opts: opts }
{ success: true }
end
BenchmarkRunner.stub :check_call, check_call_stub do
at_exit_block.call
end

assert_equal 1, cleanup_commands.length, "at_exit block should call check_call once"
assert_equal "sudo -S sh -c 'echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo'", cleanup_commands[0][:cmd]
assert_equal({ quiet: true }, cleanup_commands[0][:opts])
assert_equal "sudo -n sh -c 'echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo'", cleanup_commands[0][:cmd]
assert_equal({ quiet: true, raise_error: false }, cleanup_commands[0][:opts])
end

it 'exits when Intel turbo is not disabled and turbo flag is false' do
Expand Down Expand Up @@ -293,13 +297,17 @@

# Verify at_exit block restores AMD boost settings
cleanup_commands = []
BenchmarkRunner.stub :check_call, ->(cmd, **opts) { cleanup_commands << { cmd: cmd, opts: opts } } do
check_call_stub = ->(cmd, **opts) do
cleanup_commands << { cmd: cmd, opts: opts }
{ success: true }
end
BenchmarkRunner.stub :check_call, check_call_stub do
at_exit_block.call
end

assert_equal 1, cleanup_commands.length, "at_exit block should call check_call once"
assert_equal "sudo -S sh -c 'echo 1 > /sys/devices/system/cpu/cpufreq/boost'", cleanup_commands[0][:cmd]
assert_equal({ quiet: true }, cleanup_commands[0][:opts])
assert_equal "sudo -n sh -c 'echo 1 > /sys/devices/system/cpu/cpufreq/boost'", cleanup_commands[0][:cmd]
assert_equal({ quiet: true, raise_error: false }, cleanup_commands[0][:opts])
end

it 'exits when AMD boost is not disabled and turbo flag is false' do
Expand Down
Loading