From 9a27f3d44b1e630569eaf294aada5c4a91c1edf4 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Sun, 9 Jul 2023 13:51:05 +0100 Subject: [PATCH] [*] Lower idle CPU usage (idle utilization regression) --- Include/Aurora/Runtime.hpp | 1 + Source/AuProcAddresses.NT.cpp | 28 +++++++++++++++++++++++----- Source/AuProcAddresses.NT.hpp | 1 + Source/AuRTEntrypoint.cpp | 9 +++++++-- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/Include/Aurora/Runtime.hpp b/Include/Aurora/Runtime.hpp index 3ff62b58..f7648fad 100644 --- a/Include/Aurora/Runtime.hpp +++ b/Include/Aurora/Runtime.hpp @@ -351,6 +351,7 @@ namespace Aurora AuUInt8 uSpinLoopPowerA { 7 }; // Nudgable spinloop power. This is our local userland niceness factor; where 1 << n is the amount of yield instructions to stall for bool bEnableAggressiveScheduling { false }; + bool bEnableAgrSchedulingRatelimit { true }; bool bPreferNt51XpMutexesOver8 { false }; // Fun Fact: Undocumented Windows XP APIs are still better than whatever the fuck shit fest they sharted out under Windows Vista and maybe 8.1 bool bPerferNt51XpCondvarsOver8 { false }; // Wth the former set of apis, we are still nothing more than a futex intended for nothing more than x86 bittestandset with undefined }; // bahviour on the higher bits, and we're crippled by some annoying thread switch function. Windows Vista superseded the dumb kernel-io diff --git a/Source/AuProcAddresses.NT.cpp b/Source/AuProcAddresses.NT.cpp index dcc0fb22..33ee3e1c 100644 --- a/Source/AuProcAddresses.NT.cpp +++ b/Source/AuProcAddresses.NT.cpp @@ -9,6 +9,8 @@ namespace Aurora { + static bool gShouldResPathDoNothing {}; + void InitNTAddresses() { #if defined(AURORA_PLATFORM_WIN32) @@ -121,23 +123,39 @@ namespace Aurora !pNtWaitForKeyedEvent; gUseNativeWaitCondvar = (pWaitOnAddress && - !gRuntimeConfig.threadingConfig.bPerferNt51XpCondvarsOver8 && - (pRtlWaitOnAddress || AuBuild::kCurrentPlatform != AuBuild::EPlatform::ePlatformWin32)) || - !pNtWaitForKeyedEvent; + !gRuntimeConfig.threadingConfig.bPerferNt51XpCondvarsOver8 && + (pRtlWaitOnAddress || AuBuild::kCurrentPlatform != AuBuild::EPlatform::ePlatformWin32)) || + !pNtWaitForKeyedEvent; gUseNativeWaitSemapahore = bool(pWaitOnAddress); } + void Win32DropInit() + { + gShouldResPathDoNothing = + (AuBuild::kCurrentPlatform != AuBuild::EPlatform::ePlatformWin32) || + (!gRuntimeConfig.threadingConfig.bEnableAggressiveScheduling && + AuSwInfo::IsWindows10OrGreater()); + } + void Win32DropSchedulerResolution() { + static AuUInt uCounter {}; ULONG ullActualResolution {}; - if (!gRuntimeConfig.threadingConfig.bEnableAggressiveScheduling && - AuSwInfo::IsWindows10OrGreater()) + if (gShouldResPathDoNothing) { return; } + if (gRuntimeConfig.threadingConfig.bEnableAgrSchedulingRatelimit) + { + if (!((uCounter++) & 128)) + { + return; + } + } + if (pZwSetTimerResolution) { auto uRet = pZwSetTimerResolution(1, true, &ullActualResolution); diff --git a/Source/AuProcAddresses.NT.hpp b/Source/AuProcAddresses.NT.hpp index 25ae75e2..c4c9a850 100644 --- a/Source/AuProcAddresses.NT.hpp +++ b/Source/AuProcAddresses.NT.hpp @@ -200,5 +200,6 @@ namespace Aurora inline bool gUseNativeWaitCondvar {}; inline bool gUseNativeWaitSemapahore {}; + void Win32DropInit(); void Win32DropSchedulerResolution(); } \ No newline at end of file diff --git a/Source/AuRTEntrypoint.cpp b/Source/AuRTEntrypoint.cpp index 05ab03fe..961da1db 100644 --- a/Source/AuRTEntrypoint.cpp +++ b/Source/AuRTEntrypoint.cpp @@ -58,9 +58,8 @@ static void Init() gRuntimeRunLevel = 1; tlsHackIsMainThread = true; -#if defined(AURORA_PLATFORM_WIN32) - Aurora::Win32DropSchedulerResolution(); +#if defined(AURORA_PLATFORM_WIN32) Aurora::Extensions::Win32::InitDarkMode(); #endif @@ -73,6 +72,12 @@ static void Init() Aurora::Threading::InitSleep(); Aurora::Process::InitProcess(); Aurora::SWInfo::InitSwInfo(); + +#if defined(AURORA_PLATFORM_WIN32) + Aurora::Win32DropInit(); + Aurora::Win32DropSchedulerResolution(); +#endif + Aurora::Threading::Threads::InitThreading(); Aurora::Exit::InitExit(); Aurora::IO::Net::InitNetworking();