From 7f0dc81ec3b15f6324467ba959021290bc59a585 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Tue, 9 Apr 2024 22:29:11 +0100 Subject: [PATCH] [*] Rejuvenate AuMemory::SwapLock --- Source/AuProcAddresses.NT.cpp | 106 +++++++++++++---------------- Source/AuProcAddresses.UNIX.cpp | 13 +++- Source/AuProcAddresses.hpp | 4 ++ Source/Memory/AuMemorySwapLock.cpp | 99 ++++++++++++++++++--------- 4 files changed, 126 insertions(+), 96 deletions(-) diff --git a/Source/AuProcAddresses.NT.cpp b/Source/AuProcAddresses.NT.cpp index 0674d42f..1945d18e 100644 --- a/Source/AuProcAddresses.NT.cpp +++ b/Source/AuProcAddresses.NT.cpp @@ -501,6 +501,42 @@ namespace Aurora && ::IsProcessorFeaturePresent(PF_FASTFAIL_AVAILABLE) #endif ; + + if (gShouldResPathDoNothing) + { + return; + } + + #if defined(PROCESS_POWER_THROTTLING_CURRENT_VERSION) + if (pSetProcessInformation && + AuSwInfo::IsWindows10OrGreater()) + { + static AuInitOnceSmall gInitOnce; + + if (AuThreading::InitOnceLocker::TryLock(&gInitOnce)) + { + PROCESS_POWER_THROTTLING_STATE powerThrottling {}; + + powerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION; + powerThrottling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED; + powerThrottling.StateMask = 0; + + gShouldResPathDoNothing = bool(pSetProcessInformation(GetCurrentProcess(), + ProcessPowerThrottling, + &powerThrottling, + sizeof(powerThrottling))); + + powerThrottling.ControlMask = PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION; + powerThrottling.StateMask = 0; + gShouldResPathDoNothing &= bool(pSetProcessInformation(GetCurrentProcess(), + ProcessPowerThrottling, + &powerThrottling, + sizeof(powerThrottling))); + + AuThreading::InitOnceLocker::Finish(&gInitOnce); + } /* else no-wait. intentionally nop*/ + } + #endif } void Win32DropSchedulerResolution() @@ -525,66 +561,6 @@ namespace Aurora } } - #if defined(PROCESS_POWER_THROTTLING_CURRENT_VERSION) - if (pSetProcessInformation && - AuSwInfo::IsWindows10OrGreater()) - { - static AuInitOnceSmall gInitOnce; - - // Imagine paying any amount of money for a computer above Ivan's ewaste shitbox standards, just for Microsoft to tell you your non-portable platform isn't EcOnOmIcaLLY scheduling tasks EffiCiENT enough. - // Unless otherwise stated, piss off. - // Why would any platform make us opt into being able to use our own hardware to its' full potential? This is stupid. - // Microshit cant even write a memory management subsystem that provides free pages during phases of low resource consumption; and now, we have to trust them to give us high priority scheduling? - // (low resource consumption -> read: RAM [ (UNUSED HUGE FILE) (UNUSED HUGE FILE) (UNUSED HUGE FILE) (UNUSED HUGE FILE) (UNUSED HUGE FILE) (HUGE FILE) (FREE SPACE) (FREE SPACE) (YOU) ], - // SWAP: [ (UNUSED HUGE FILE) (UNUSED HUGE FILE) (UNUSED HUGE FILE) (UNUSED HUGE FILE) (UNUSED HUGE FILE) (HUGE FILE) (HEAP BACKUP LOL) (SOME EXTRA SWAP SPACE YOU WONT BE ABLE TO USE) ] ) - // I'll let you know when we devolve into writing worthless chrome_helper.exe (42) processes allegedly doing some sandboxing of perceived value. Until then, let's assume our processes aren't *literal* retards staring into the abyss all day. - // - // Disable via: gRuntimeConfig.threadingConfig.bEnableAggressiveScheduling (def: false - unless defacto high perf application under xp-win7; use envvar AURORA_IS_NETBOOK to opt out) - // - // Update (2024/03): Fuck off; I'm doubling down on this messaging. - // Read into the absolute state of this, bio: "Using my 80,000 hours to evolve how anyone with an idea can apply technology to solve real problems." - // https://github.com/microsoft/WindowsAppSDK/discussions/2525 - // https://github.com/dotnet/runtime/issues/66417 - // Microsoft is planning on using data from https://app.electricitymaps.com/map, such that if you live in an area with "high climate impact of electricity," they're going to start shifting workloads to e-cores by default. - // To reiterate, fuck off CurryBlows 11 and fuck you SoySoft. - // - // Reece, [16/03/2024 18:11] - // `Imagine paying any amount of money for a computer above Ivan's ewaste shitbox standards, just for Microsoft to tell you [...]` - // [...] that MuH EnerGy GrId is ChArGinG toO mAnY TeSlAs of KarEns - // [...] TeXas CanT GeT ThEir ShiT ToGetTheR - // [...] CaLIiFoRnyanya is turning into a third world shithole with rolling blackouts worse than north korea (best korea - now with 100% less kpop slavery) - // - // Wake me when those soy infested over privileged retards commit to going one year without air conditioning and the mexicans' ball gargling service to go - // - // Update (2024/03) again: - // https://stackoverflow.com/questions/77182958/windows-11-application-timing-becomes-uneven-when-backgrounded - // It's already causing the general development community grief. bEnableAggressiveScheduling should always be enabled for high perf applications. - // "Under Windows 7, 8 or 10, it performed well. Under Windows 11, we have a new problem" - I stand by my other comments in the codebase that users should upgrade to Windows 7 - LTSC 10 - - if (AuThreading::InitOnceLocker::TryLock(&gInitOnce)) - { - PROCESS_POWER_THROTTLING_STATE powerThrottling {}; - - powerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION; - powerThrottling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED; - powerThrottling.StateMask = 0; - pSetProcessInformation(GetCurrentProcess(), - ProcessPowerThrottling, - &powerThrottling, - sizeof(powerThrottling)); - - powerThrottling.ControlMask = PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION; - powerThrottling.StateMask = 0; - pSetProcessInformation(GetCurrentProcess(), - ProcessPowerThrottling, - &powerThrottling, - sizeof(powerThrottling)); - - AuThreading::InitOnceLocker::Finish(&gInitOnce); - } /* else no-wait. intentionally nop*/ - } - #endif - if (pZwSetTimerResolution) { auto uRet = pZwSetTimerResolution(1, true, &ullActualResolution); @@ -821,4 +797,14 @@ namespace Aurora { VirtualFree(pBuffer, 0, MEM_RELEASE); } + + bool SysMemoryLockPages(const void *pVoid, AuUInt uLength) + { + return VirtualLock((void *)pVoid, uLength); + } + + bool SysMemoryUnlockPages(const void *pVoid, AuUInt uLength) + { + return VirtualUnlock((void *)pVoid, uLength); + } } \ No newline at end of file diff --git a/Source/AuProcAddresses.UNIX.cpp b/Source/AuProcAddresses.UNIX.cpp index 2751b684..7a0ac35e 100755 --- a/Source/AuProcAddresses.UNIX.cpp +++ b/Source/AuProcAddresses.UNIX.cpp @@ -44,9 +44,6 @@ namespace Aurora void PosixTerminate() { - #if 0 - PosixShutup(); - #endif ::killpg(0, SIGKILL); } @@ -211,4 +208,14 @@ namespace Aurora uLength = AuMemory::RoundPageUp(uLength); ::munmap(pBuffer, uLength); } + + bool SysMemoryLockPages(const void *pVoid, AuUInt uLength) + { + return ::mlock(pVoid, uLength) == 0; + } + + bool SysMemoryUnlockPages(const void *pVoid, AuUInt uLength) + { + return ::munlock(pVoid, uLength) == 0; + } } \ No newline at end of file diff --git a/Source/AuProcAddresses.hpp b/Source/AuProcAddresses.hpp index d193ea59..b23c7ffa 100644 --- a/Source/AuProcAddresses.hpp +++ b/Source/AuProcAddresses.hpp @@ -59,6 +59,10 @@ namespace Aurora void *SysAllocateLarge(AuUInt uLength); void SysAllocateFree(void *pBuffer, AuUInt uLength); + + bool SysMemoryLockPages(const void *pVoid, AuUInt uLength); + + bool SysMemoryUnlockPages(const void *pVoid, AuUInt uLength); } #if defined(AURORA_IS_MODERNNT_DERIVED) || defined(AURORA_IS_POSIX_DERIVED) diff --git a/Source/Memory/AuMemorySwapLock.cpp b/Source/Memory/AuMemorySwapLock.cpp index cb5b885c..f36f7d43 100644 --- a/Source/Memory/AuMemorySwapLock.cpp +++ b/Source/Memory/AuMemorySwapLock.cpp @@ -10,34 +10,50 @@ #include "AuMemorySwapLock.hpp" #include -#if defined(AURORA_IS_LINUX_DERIVED) - #include -#endif - namespace Aurora::Memory::SwapLock { + static AuPair ToPagePair(AuPair addressRange) + { + auto uBase = AuPageRound(addressRange.first, AuUInt(AuHwInfo::gPageSize)); + auto uLength = AuPageRoundUp(addressRange.second + (addressRange.first - uBase), AuUInt(AuHwInfo::gPageSize)); + return AuMakePair(uBase, uLength); + } + AUKN_SYM bool Lock(const AuList> &addressRanges) { - for (const auto &addressRange : addressRanges) + for (AU_ITERATE_N(i, addressRanges.size())) { - auto base = AuPageRound(addressRange.first, AuUInt(AuHwInfo::gPageSize)); - auto length = AuPageRoundUp(addressRange.second + (addressRange.first - base), AuUInt(AuHwInfo::gPageSize)); + bool bBypass {}; + AuUInt uBase, uLength; - #if defined(AURORA_IS_MODERNNT_DERIVED) - if (!VirtualLock(AuReinterpretCast(base), length)) + AuTupleTie(uBase, uLength) = ToPagePair(addressRanges[i]); + + for (AU_ITERATE_N(z, i)) { - SysPushErrorHAL("Couldn't lock memory 0x{:x} {}", base, length); + AuUInt uBase2, uLength2; + + AuTupleTie(uBase2, uLength2) = ToPagePair(addressRanges[z]); + + if (uBase < uBase2) + { + continue; + } + + if ((uBase + uLength) > (uBase2 + uLength2)) + { + continue; + } + + bBypass = true; + break; + } + + if (!bBypass && + !SysMemoryLockPages(AuReinterpretCast(uBase), uLength)) + { + SysPushErrorHAL("Couldn't lock memory 0x{:x} {}", uBase, uLength); return false; } - #endif - - #if defined(AURORA_IS_POSIX_DERIVED) - if (mlock(AuReinterpretCast(base), length) != 0) - { - SysPushErrorHAL("Couldn't lock memory 0x{:x} {}", base, length); - return false; - } - #endif } return true; @@ -45,26 +61,43 @@ namespace Aurora::Memory::SwapLock AUKN_SYM bool Unlock(const AuList> &addressRanges) { - for (const auto &addressRange : addressRanges) + bool bRet { true }; + + for (AU_ITERATE_N(i, addressRanges.size())) { - auto base = AuPageRound(addressRange.first, AuUInt(AuHwInfo::gPageSize)); - auto length = AuPageRoundUp(addressRange.second + (addressRange.first - base), AuUInt(AuHwInfo::gPageSize)); + bool bBypass {}; + AuUInt uBase, uLength; - #if defined(AURORA_IS_MODERNNT_DERIVED) - if (!VirtualUnlock(AuReinterpretCast(base), length)) - { - SysPushErrorHAL("Couldn't unlock memory 0x{:x} {}", base, length); - } - #endif + AuTupleTie(uBase, uLength) = ToPagePair(addressRanges[i]); - #if defined(AURORA_IS_POSIX_DERIVED) - if (munlock(AuReinterpretCast(base), length) != 0) + for (AU_ITERATE_N(z, i)) { - SysPushErrorHAL("Couldn't unlock memory 0x{:x} {}", base, length); + AuUInt uBase2, uLength2; + + AuTupleTie(uBase2, uLength2) = ToPagePair(addressRanges[z]); + + if (uBase < uBase2) + { + continue; + } + + if ((uBase + uLength) > (uBase2 + uLength2)) + { + continue; + } + + bBypass = true; + break; + } + + if (!bBypass && + !SysMemoryUnlockPages(AuReinterpretCast(uBase), uLength)) + { + SysPushErrorHAL("Couldn't unlock memory 0x{:x} {}", uBase, uLength); + bRet = false; } - #endif } - return true; + return bRet; } } \ No newline at end of file