diff --git a/runtime/main.c b/runtime/main.c index e3428719..38bbb941 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -532,6 +532,10 @@ int main(int argc, char **argv) { // Timed guest waits (kernel.c block_inner) sleep in whole milliseconds; at the default 15.6 ms tick a 1 ms wait // costs a full tick. TOTK_TIMER_1MS=0 keeps the default for comparison. if (cfg_bool("TOTK_TIMER_1MS")) plat_timer_1ms(); + int power = plat_power_full(); + if (power >= 0) + rt_log("power: %s", power ? "OS power throttling off (EcoQoS, timer resolution in the background)" + : "OS power throttling: the opt-out was refused"); cfg = (RtConfig){.exefs = rt_default_exefs, .romfs = rt_default_romfs, .profile = rt_default_profile, .gpu = cfg_get("TOTK_GPU")}; portable_paths(&cfg); // command-line paths below still win diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 158601cb..38a840e3 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -481,6 +481,8 @@ uint64_t plat_counter_hz(void) { return 1000000000u; } // inherit it from the thread that creates them. void plat_timer_1ms(void) { prctl(PR_SET_TIMERSLACK, 1000ul, 0, 0, 0); } +int plat_power_full(void) { return -1; } + // clock_nanosleep wakes within the timer slack (50 us by default) of its deadline; the last PLAT_SPIN_NS are spent // yielding so the deadline is met closely, as on Windows. #define PLAT_SPIN_NS 100000u diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 97911d27..2d454d2a 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -99,6 +99,7 @@ uint64_t plat_now_ns(void); // monotonic, high resolution, arbitra uint64_t plat_counter(void); // the OS's raw monotonic counter (QPC on Windows), plat_counter_hz per second uint64_t plat_counter_hz(void); void plat_timer_1ms(void); // asks the OS for timed waits accurate to 1 ms or better (call before threads) +int plat_power_full(void); // opts the process out of OS power throttling: 1 done, 0 refused, -1 no such thing // t * num / den for a fixed num / den without a 128-bit division per call (__udivti3 took 5% of the game's running // samples through plat_now_ns and rt_ticks): t * q + (t * f >> 64) with q = num / den and f = (num % den) * 2^64 / den. // Monotonic in t and at most one below the exact quotient while t * (num % den) / den fits 64 bits. diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 5dda4538..d0e05af8 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -18,6 +18,13 @@ #include "platform.h" +// Hybrid laptops: the drivers read these exports of the executable when it starts and run it on the dedicated GPU. +// NVIDIA Optimus can otherwise hide its GPU from a program it put on the integrated one, whatever the Windows and NVIDIA +// settings say; AMD switchable graphics read the second one. TOTK_GPU_INDEX still picks another GPU. Savage Wind Recomp +// exports the same two (runtime/platform/windows.c). +__declspec(dllexport) DWORD NvOptimusEnablement = 1; +__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1; + _Static_assert(sizeof(PlatMutex) == sizeof(SRWLOCK), "PlatMutex holds an SRWLOCK"); _Static_assert(sizeof(PlatCond) == sizeof(CONDITION_VARIABLE), "PlatCond holds a CONDITION_VARIABLE"); @@ -319,6 +326,17 @@ uint64_t plat_counter_hz(void) { return qpc_freq(); } // The default timer tick is 15.6 ms; a 1 ms wait would cost a full tick. void plat_timer_1ms(void) { timeBeginPeriod(1); } +// Windows 11 runs the threads of a process it deems in the background at EcoQoS (on the E-cores of a hybrid Intel CPU, +// at a low clock) and ignores its timer resolution while its window is hidden. The game is always latency-bound, so it +// opts out of both. Windows before 10 1709 does not know the class: 0. +int plat_power_full(void) { + PROCESS_POWER_THROTTLING_STATE s = { + .Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION, + .ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED | PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION, + .StateMask = 0}; + return SetProcessInformation(GetCurrentProcess(), ProcessPowerThrottling, &s, sizeof s) ? 1 : 0; +} + static PlatScale qpc_ns; // set before main, so no thread reads it half written __attribute__((constructor)) static void qpc_ns_init(void) { qpc_ns = plat_scale(1000000000u, qpc_freq()); }