Skip to content

Commit

Permalink
perf boost mode in its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
eggrobin committed Dec 29, 2024
1 parent b1b7221 commit 704e429
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 136 deletions.
146 changes: 10 additions & 136 deletions nanobenchmarks/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

#include <Windows.h>
#include <Powersetting.h>

#include <algorithm>
#include <iostream>
#include <iomanip>
Expand All @@ -15,9 +12,14 @@
#include <intrin.h>
#include <emmintrin.h>

#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "base/cpuid.hpp"
#include "numerics/cbrt.hpp"
#include "testing_utilities/statistics.hpp"
#include "nanobenchmarks/disable_perf_boost_mode.hpp"

using namespace principia::nanobenchmarks::_disable_perf_boost_mode;

#define BENCHMARK_CALLING_CONVENTION

Expand Down Expand Up @@ -185,124 +187,9 @@ BENCHMARK_FUNCTION_WITH_NAME(
BENCHMARK_FUNCTION_WITH_NAME("Cbrt",
principia::numerics::_cbrt::Cbrt);

std::pair<DWORD, DWORD> ReadPerfBoostModeACDC(
GUID const* const active_power_scheme) {
#define CASE(value) \
case value: \
return #value
auto perf_boost_mode_to_string = [](DWORD mode) -> std::string {
switch (mode) {
CASE(PROCESSOR_PERF_BOOST_MODE_DISABLED);
CASE(PROCESSOR_PERF_BOOST_MODE_ENABLED);
CASE(PROCESSOR_PERF_BOOST_MODE_AGGRESSIVE);
CASE(PROCESSOR_PERF_BOOST_MODE_EFFICIENT_ENABLED);
CASE(PROCESSOR_PERF_BOOST_MODE_EFFICIENT_AGGRESSIVE);
CASE(PROCESSOR_PERF_BOOST_MODE_AGGRESSIVE_AT_GUARANTEED);
CASE(PROCESSOR_PERF_BOOST_MODE_EFFICIENT_AGGRESSIVE_AT_GUARANTEED);
default:
return std::to_string(mode);
}
};
#undef CASE
DWORD perf_boost_mode_ac;
DWORD perf_boost_mode_dc;
DWORD perf_boost_mode_size = sizeof(perf_boost_mode_ac);
CHECK_EQ(PowerReadACValue(nullptr,
active_power_scheme,
&GUID_PROCESSOR_SETTINGS_SUBGROUP,
&GUID_PROCESSOR_PERF_BOOST_MODE,
nullptr,
reinterpret_cast<LPBYTE>(&perf_boost_mode_ac),
&perf_boost_mode_size),
ERROR_SUCCESS)
<< perf_boost_mode_size;
CHECK_EQ(PowerReadDCValue(nullptr,
active_power_scheme,
&GUID_PROCESSOR_SETTINGS_SUBGROUP,
&GUID_PROCESSOR_PERF_BOOST_MODE,
nullptr,
reinterpret_cast<LPBYTE>(&perf_boost_mode_dc),
&perf_boost_mode_size),
ERROR_SUCCESS)
<< perf_boost_mode_size;
std::println(std::cout,
"PERF_BOOST_MODE AC={} ({})",
perf_boost_mode_ac,
perf_boost_mode_to_string(perf_boost_mode_ac));
std::println(std::cout,
"PERF_BOOST_MODE DC={} ({})",
perf_boost_mode_ac,
perf_boost_mode_to_string(perf_boost_mode_dc));
return {
perf_boost_mode_ac, perf_boost_mode_dc
};
}

int __cdecl main(int argc, const char** argv) {
SYSTEM_POWER_STATUS power_status;
CHECK(GetSystemPowerStatus(&power_status));
std::println(std::cout,
"ACLineStatus={} ({})",
power_status.ACLineStatus,
power_status.ACLineStatus == 0 ? "Offline"
: power_status.ACLineStatus == 1 ? "Online"
: "Unknown");
GUID* active_power_scheme;
CHECK_EQ(PowerGetActiveScheme(nullptr, &active_power_scheme), ERROR_SUCCESS);

// powercfg /query scheme_current sub_processor PERFBOOSTMODE

auto const [perf_boost_mode_ac, perf_boost_mode_dc] =
ReadPerfBoostModeACDC(active_power_scheme);
std::println(std::cout, "Disabling perf boost mode…");
std::println(
std::cout,
R"(If interrupted, restore with
POWERCFG /SETACVALUEINDEX SCHEME_CURRENT SUB_PROCESSOR PERFBOOSTMODE {}
POWERCFG /SETDCVALUEINDEX SCHEME_CURRENT SUB_PROCESSOR PERFBOOSTMODE {})",
perf_boost_mode_ac,
perf_boost_mode_dc);
CHECK_EQ(PowerWriteACValueIndex(nullptr,
active_power_scheme,
&GUID_PROCESSOR_SETTINGS_SUBGROUP,
&GUID_PROCESSOR_PERF_BOOST_MODE,
PROCESSOR_PERF_BOOST_MODE_DISABLED),
ERROR_SUCCESS);
CHECK_EQ(PowerWriteDCValueIndex(nullptr,
active_power_scheme,
&GUID_PROCESSOR_SETTINGS_SUBGROUP,
&GUID_PROCESSOR_PERF_BOOST_MODE,
PROCESSOR_PERF_BOOST_MODE_DISABLED),
ERROR_SUCCESS);
auto const [updated_perf_boost_mode_ac, updated_perf_boost_mode_dc] =
ReadPerfBoostModeACDC(active_power_scheme);
CHECK_EQ(updated_perf_boost_mode_ac, PROCESSOR_PERF_BOOST_MODE_DISABLED);
CHECK_EQ(updated_perf_boost_mode_dc, PROCESSOR_PERF_BOOST_MODE_DISABLED);

{
DWORD perf_boost_mode_ac;
DWORD perf_boost_mode_dc;
DWORD perf_boost_mode_size = sizeof(perf_boost_mode_ac);
CHECK_EQ(PowerReadACValue(nullptr,
active_power_scheme,
&GUID_PROCESSOR_SETTINGS_SUBGROUP,
&GUID_PROCESSOR_PERF_BOOST_MODE,
nullptr,
reinterpret_cast<LPBYTE>(&perf_boost_mode_ac),
&perf_boost_mode_size),
ERROR_SUCCESS)
<< perf_boost_mode_size;
CHECK_EQ(PowerReadDCValue(nullptr,
active_power_scheme,
&GUID_PROCESSOR_SETTINGS_SUBGROUP,
&GUID_PROCESSOR_PERF_BOOST_MODE,
nullptr,
reinterpret_cast<LPBYTE>(&perf_boost_mode_dc),
&perf_boost_mode_size),
ERROR_SUCCESS)
<< perf_boost_mode_size;
}

int __cdecl main(int argc, char** argv) {
absl::ParseCommandLine(argc, argv);
PerfBoostModeDisabler perf_boost_mode_disabled;
distribution::quantile_definitions = {1000, 100, 10, 4, 2};
std::cout << principia::base::_cpuid::CPUVendorIdentificationString() << " "
<< principia::base::_cpuid::ProcessorBrandString() << "\nFeatures:"
Expand Down Expand Up @@ -355,27 +242,14 @@ int __cdecl main(int argc, const char** argv) {
return a * benchmark(f) + b;
};
for (auto const& [name, f] : function_registry) {
auto const result = benchmark(f);
std::cout << (std::ranges::contains(std::views::keys(reference_functions),
f)
? "R"
: " ");
std::vprint_unicode(std::cout,
"{:>" + std::to_string(name_width + 1) + "}",
std::make_format_args(name));
std::cout << a * benchmark(f) + b << "\n";
std::cout << a * result + b << "\n";
}
std::println(std::cout, "Restoring perf boost mode…");
CHECK_EQ(PowerWriteACValueIndex(nullptr,
active_power_scheme,
&GUID_PROCESSOR_SETTINGS_SUBGROUP,
&GUID_PROCESSOR_PERF_BOOST_MODE,
perf_boost_mode_ac),
ERROR_SUCCESS);
CHECK_EQ(PowerWriteDCValueIndex(nullptr,
active_power_scheme,
&GUID_PROCESSOR_SETTINGS_SUBGROUP,
&GUID_PROCESSOR_PERF_BOOST_MODE,
perf_boost_mode_dc),
ERROR_SUCCESS);
ReadPerfBoostModeACDC(active_power_scheme);
}

Check warning on line 255 in nanobenchmarks/main.cpp

View workflow job for this annotation

GitHub Actions / check-cpp

whitespace/ending_newline

Could not find a newline character at the end of the file.
4 changes: 4 additions & 0 deletions nanobenchmarks/nanobenchmarks.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="disable_perf_boost_mode.cpp" />
</ItemGroup>
<ItemGroup>
<MASM Include="reference_functions.asm" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="disable_perf_boost_mode.hpp" />
</ItemGroup>
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
</ImportGroup>
Expand Down

0 comments on commit 704e429

Please sign in to comment.