- [Sys-clk-OC] Partial rewrite, general performance & stability improvement

- [ReverseNX-RT] Notify user if profile is synced with sys-clk
This commit is contained in:
KazushiM
2021-12-16 23:04:44 +08:00
parent 12aab58198
commit e6b608fa43
15 changed files with 348 additions and 457 deletions

View File

@@ -8,7 +8,6 @@
* --------------------------------------------------------------------------
*/
#define FORCE_ALL_HANDHELD_MODES_TO_USE_DOCK_CLOCK
#include <nxExt.h>
#include "errors.h"
#include "clock_manager.h"
@@ -46,6 +45,7 @@ ClockManager::ClockManager()
this->context = new SysClkContext;
this->context->applicationId = 0;
this->context->profile = SysClkProfile_Handheld;
this->context->realProfile = SysClkProfile_Handheld;
this->context->enabled = false;
for(unsigned int i = 0; i < SysClkModule_EnumMax; i++)
{
@@ -60,8 +60,10 @@ ClockManager::ClockManager()
this->oc = new SysClkOcExtra;
this->oc->systemCoreBoostCPU = false;
this->oc->systemCoreCheckStuck = false;
this->oc->tickWaitTimeMs = 500;
this->oc->systemCoreBoostThreshold = 100;
this->oc->reverseNXToolMode = ReverseNX_NotFound;
this->oc->reverseNXRTMode = ReverseNX_NotFound;
this->oc->tickWaitTimeMs = 0;
this->oc->maxMEMFreq = 1600'000'000;
// this->oc->systemCoreStuckCount = 0;
}
@@ -75,98 +77,18 @@ ClockManager::~ClockManager()
bool ClockManager::IsCpuBoostMode()
{
std::uint32_t confId = this->context->perfConfId;
if(confId == 0x92220009 || confId == 0x9222000A)
return true;
else
return false;
return (confId == 0x92220009 || confId == 0x9222000A);
}
SysClkProfile ClockManager::ReverseNXProfile(bool ForceDock)
bool ClockManager::IsReverseNXEnabled()
{
RealProfile = Clocks::GetCurrentProfile();
switch(RealProfile)
{
case SysClkProfile_HandheldChargingOfficial:
#ifdef FORCE_ALL_HANDHELD_MODES_TO_USE_DOCK_CLOCK
case SysClkProfile_HandheldChargingUSB:
case SysClkProfile_HandheldCharging:
case SysClkProfile_Handheld:
#endif
if (ForceDock)
return SysClkProfile_Docked;
else
return RealProfile;
case SysClkProfile_Docked:
if (ForceDock)
return SysClkProfile_Docked;
else
return FileUtils::IsDownclockDockEnabled() ? SysClkProfile_HandheldChargingOfficial : SysClkProfile_Docked;
default:
return RealProfile;
}
return (this->oc->reverseNXRTMode || this->oc->reverseNXToolMode);
}
void ClockManager::checkReverseNXTool()
bool ClockManager::IsReverseNXDocked()
{
char ReverseNXToolAsm[] = "_ZN2nn2oe18GetPerformanceModeEv.asm64"; // Checking one asm64 file is enough
char ReverseNXToolAsmPath[128];
uint8_t flag = 0;
snprintf(ReverseNXToolAsmPath, sizeof ReverseNXToolAsmPath, "/SaltySD/patches/%s", ReverseNXToolAsm);
FILE *readReverseNXToolAsm;
readReverseNXToolAsm = fopen(ReverseNXToolAsmPath, "rb");
// Enforce mode globally: Enabled
if(readReverseNXToolAsm != NULL)
{
checkReverseNXToolAsm(readReverseNXToolAsm, &flag);
switch(flag)
{
case 1:
FileUtils::LogLine("[mgr] ReverseNX-Tool patches detected: Enforce Handheld globally");
this->context->profile = ReverseNXProfile(false);
isDockedReverseNX = false;
isEnabledReverseNX = true;
isEnabledReverseNXTool = true;
break;
case 2:
FileUtils::LogLine("[mgr] ReverseNX-Tool patches detected: Enforce Docked globally");
this->context->profile = ReverseNXProfile(true);
isDockedReverseNX = true;
isEnabledReverseNX = true;
isEnabledReverseNXTool = true;
break;
}
}
else
{
snprintf(ReverseNXToolAsmPath, sizeof ReverseNXToolAsmPath, "/SaltySD/patches/%016lX/%s", this->context->applicationId, ReverseNXToolAsm);
readReverseNXToolAsm = fopen(ReverseNXToolAsmPath, "rb");
// Found game-specific setting
if(readReverseNXToolAsm != NULL)
{
checkReverseNXToolAsm(readReverseNXToolAsm, &flag);
switch(flag)
{
case 1:
FileUtils::LogLine("[mgr] ReverseNX-Tool patches detected: Force Handheld in %016lX", this->context->applicationId);
this->context->profile = ReverseNXProfile(false);
isDockedReverseNX = false;
isEnabledReverseNX = true;
isEnabledReverseNXTool = true;
break;
case 2:
FileUtils::LogLine("[mgr] ReverseNX-Tool patches detected: Force Docked in %016lX", this->context->applicationId);
this->context->profile = ReverseNXProfile(true);
isDockedReverseNX = true;
isEnabledReverseNX = true;
isEnabledReverseNXTool = true;
break;
default:
isEnabledReverseNXTool = false;
}
}
}
return (this->oc->reverseNXRTMode == ReverseNX_Docked
|| this->oc->reverseNXToolMode == ReverseNX_Docked);
}
void ClockManager::SetRunning(bool running)
@@ -186,16 +108,16 @@ uint32_t ClockManager::GetHz(SysClkModule module)
/* Temp override setting */
hz = this->context->overrideFreqs[module];
/* Per-Game setting */
if (!hz)
hz = this->config->GetAutoClockHz(this->context->applicationId, module, this->context->profile);
/* Global setting */
if (!hz)
hz = this->config->GetAutoClockHz(0xA111111111111111, module, this->context->profile);
/* Adjust hz if ReverseNX is enabled */
if (!hz && isEnabledReverseNX)
/* Per-Game setting */
if (!hz)
hz = this->config->GetAutoClockHz(this->context->applicationId, module, this->context->profile);
/* Return pre-set hz if ReverseNX is enabled, downclock is disabled when realProfile == Docked */
if (!hz && IsReverseNXEnabled())
{
switch(module)
{
@@ -203,15 +125,13 @@ uint32_t ClockManager::GetHz(SysClkModule module)
hz = 1020'000'000;
break;
case SysClkModule_GPU:
if (!isDockedReverseNX && ((FileUtils::IsDownclockDockEnabled() && RealProfile == SysClkProfile_Docked)
|| RealProfile != SysClkProfile_Docked))
if (!IsReverseNXDocked() && this->context->realProfile != SysClkProfile_Docked)
hz = 460'800'000;
else
hz = 768'000'000;
break;
case SysClkModule_MEM:
if (!isDockedReverseNX && ((FileUtils::IsDownclockDockEnabled() && RealProfile == SysClkProfile_Docked)
|| RealProfile != SysClkProfile_Docked))
if (!IsReverseNXDocked() && this->context->realProfile != SysClkProfile_Docked)
hz = 1331'200'000;
else
hz = 1600'000'000;
@@ -223,20 +143,25 @@ uint32_t ClockManager::GetHz(SysClkModule module)
if (hz)
{
hz = Clocks::GetNearestHz(module, isEnabledReverseNX ? RealProfile : this->context->profile, hz);
/* Considering realProfile frequency limit */
hz = Clocks::GetNearestHz(module, this->context->realProfile, hz);
/* Ignore if MEM Max Hz > 1600 MHz */
if (module == SysClkModule_MEM && hz == 1600'000'000 && this->context->freqs[module] >= hz)
if (module == SysClkModule_MEM && hz == 1600'000'000)
{
return 0;
/* Return maxMemFreq */
if (this->context->freqs[module] > this->oc->maxMEMFreq)
this->oc->maxMEMFreq = this->context->freqs[module];
return this->oc->maxMEMFreq;
}
}
if (module == SysClkModule_CPU)
{
if (this->oc->systemCoreBoostCPU && hz < MAX_CPU)
return MAX_CPU;
if (this->oc->systemCoreBoostCPU && hz < CPU_BOOST_FREQ)
return CPU_BOOST_FREQ;
else if (!hz)
/* Prevent crash when hz = 0 in SetHz(0), trigger RefreshContext() and Tick() */
return 1020'000'000;
}
@@ -255,14 +180,12 @@ void ClockManager::Tick()
if (hz && hz != this->context->freqs[module])
{
if (IsCpuBoostMode())
// Skip setting CPU or GPU clocks in CpuBoostMode if CPU <= 1963.5MHz or GPU >= 76.8MHz
if (IsCpuBoostMode() && ((module == SysClkModule_CPU && hz <= CPU_BOOST_FREQ) || module == SysClkModule_GPU))
{
// Skip setting CPU or GPU clocks in CpuBoostMode if CPU <= 1963.5MHz or GPU >= 76.8MHz
if ((module == SysClkModule_CPU && hz <= MAX_CPU) || module == SysClkModule_GPU)
{
continue;
}
continue;
}
FileUtils::LogLine("[mgr] %s clock set : %u.%u Mhz", Clocks::GetModuleName((SysClkModule)module, true), hz/1000000, hz/100000 - hz/1000000*10);
Clocks::SetHz((SysClkModule)module, hz);
this->context->freqs[module] = hz;
@@ -275,36 +198,37 @@ void ClockManager::WaitForNextTick()
{
/* Self-check system core (#3) usage via idleticks at intervals (Not enabled at higher CPU freq or without charger) */
this->oc->tickWaitTimeMs = this->GetConfig()->GetConfigValue(SysClkConfigValue_PollingIntervalMs);
uint64_t tickWaitTimeNs = this->oc->tickWaitTimeMs * 1000000ULL;
if ( this->context->enabled
&& this->context->profile != SysClkProfile_Handheld
&& this->context->freqs[SysClkModule_CPU] <= MAX_CPU)
if ( FileUtils::IsBoostEnabled()
&& this->context->realProfile != SysClkProfile_Handheld
&& this->context->enabled
&& this->context->freqs[SysClkModule_CPU] <= CPU_BOOST_FREQ)
{
uint64_t systemCoreIdleTickPrev = 0, systemCoreIdleTickNext = 0;
svcGetInfo(&systemCoreIdleTickPrev, InfoType_IdleTickCount, INVALID_HANDLE, 3);
svcSleepThread(this->oc->tickWaitTimeMs * 1000000ULL);
svcSleepThread(tickWaitTimeNs);
svcGetInfo(&systemCoreIdleTickNext, InfoType_IdleTickCount, INVALID_HANDLE, 3);
/* Convert idletick to load% */
/* Convert idletick to free% */
/* If CPU core usage is 0%, then idletick = 19'200'000 per sec */
uint64_t systemCoreIdleTick = systemCoreIdleTickNext - systemCoreIdleTickPrev;
uint64_t freeIdleTick = 19'200 * this->oc->tickWaitTimeMs;
uint8_t freePerc = systemCoreIdleTick / (freeIdleTick / 100);
bool systemCoreBoostCPU_PrevState = this->oc->systemCoreBoostCPU;
switch (this->context->profile)
uint8_t systemCoreBoostFreeThreshold = 5;
switch (this->context->realProfile)
{
case SysClkProfile_HandheldChargingOfficial:
case SysClkProfile_Docked:
this->oc->systemCoreBoostThreshold = systemCoreBoostCPU_PrevState ? 6 : 10;
systemCoreBoostFreeThreshold = 10;
break;
default: // Unofficial charger
this->oc->systemCoreBoostThreshold = systemCoreBoostCPU_PrevState ? 3 : 5;
default:
break;
}
this->oc->systemCoreBoostCPU = (freePerc <= this->oc->systemCoreBoostThreshold);
bool systemCoreBoostCPU_PrevState = this->oc->systemCoreBoostCPU;
this->oc->systemCoreBoostCPU = (freePerc <= systemCoreBoostFreeThreshold);
if (systemCoreBoostCPU_PrevState && !this->oc->systemCoreBoostCPU)
{
@@ -312,12 +236,12 @@ void ClockManager::WaitForNextTick()
}
else if (!systemCoreBoostCPU_PrevState && this->oc->systemCoreBoostCPU)
{
Clocks::SetHz(SysClkModule_CPU, MAX_CPU);
Clocks::SetHz(SysClkModule_CPU, CPU_BOOST_FREQ);
}
}
else
{
svcSleepThread(this->oc->tickWaitTimeMs * 1000000ULL);
svcSleepThread(tickWaitTimeNs);
}
/* Signal that systemCore is not busy */
@@ -334,7 +258,7 @@ void ClockManager::WaitForNextTick()
{
svcSleepThread(clkMgr->oc->tickWaitTimeMs * 1000000ULL);
if (clkMgr->context->freqs[SysClkModule_CPU] >= clkMgr->MAX_CPU)
if (clkMgr->context->freqs[SysClkModule_CPU] >= clkMgr->CPU_BOOST_FREQ)
continue;
// Core #0,1,2 will check if Core#3 is stuck (threshold count = 2*3 = 6)
@@ -343,7 +267,7 @@ void ClockManager::WaitForNextTick()
{
// Signal that current core will take over to boost CPU and wait some time to recheck
clkMgr->oc->systemCoreStuckCount = -6;
Clocks::SetHz(SysClkModule_CPU, clkMgr->MAX_CPU);
Clocks::SetHz(SysClkModule_CPU, clkMgr->CPU_BOOST_FREQ);
}
}
}*/
@@ -370,61 +294,107 @@ void ClockManager::WaitForNextTick()
threadClose(&this->t_CheckSystemCoreStuck_2);
}*/
void ClockManager::checkReverseNXToolAsm(FILE* readFile, uint8_t* flag)
SysClkProfile ClockManager::ReverseNXProfileHandler()
{
// Magic Value
uint8_t Docked[0x10] = {0xE0, 0x03, 0x00, 0x32, 0xC0, 0x03, 0x5F, 0xD6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t Handheld[0x10] = {0x00, 0x00, 0xA0, 0x52, 0xC0, 0x03, 0x5F, 0xD6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uint8_t filebuffer[0x10] = {0};
fread(&filebuffer, 1, 16, readFile);
uint8_t cmpresult = 0;
cmpresult = memcmp(filebuffer, Docked, sizeof(Docked));
if (cmpresult != 0)
if (!IsReverseNXEnabled())
{
cmpresult = memcmp(filebuffer, Handheld, sizeof(Handheld));
if (cmpresult != 0)
*flag = 0; // Set to default
else
*flag = 1; // Handheld
return this->context->realProfile;
}
else
*flag = 2; // Docked
fclose(readFile);
if (IsReverseNXDocked())
{
return SysClkProfile_Docked;
}
if (// !IsReversedNXDocked() &&
this->context->realProfile == SysClkProfile_Docked)
{
return SysClkProfile_HandheldChargingOfficial;
}
return this->context->realProfile;
}
void ClockManager::checkReverseNXRT(bool recheckReverseNX, uint8_t* flag)
ReverseNXMode ClockManager::ReverseNXFileHandler(bool checkTool, const char* filePath)
{
FILE* readReverseNXRTConf = fopen(FILE_REVERSENX_RT_CONF_PATH, "rb");
if (readReverseNXRTConf != NULL)
FILE *readFile;
readFile = fopen(filePath, "rb");
if (!readFile)
{
uint8_t ReverseNXRTConfArr[9];
fread(ReverseNXRTConfArr, 9, 1, readReverseNXRTConf);
fclose(readReverseNXRTConf);
remove(FILE_REVERSENX_RT_CONF_PATH);
uint8_t currentTid[8];
for(int i = 0; i < 8; i++)
currentTid[i] = this->context->applicationId >> 8*(7-i);
uint8_t cmpresult = memcmp(currentTid, ReverseNXRTConfArr, sizeof(currentTid));
if (cmpresult == 0)
*flag = ReverseNXRTConfArr[8]; // 1: Handheld, 2: Docked, 3: Reset
else
*flag = 0; // 0: Not applicable
return ReverseNX_NotFound;
}
else if (recheckReverseNX)
*flag = prevReverseNXRT; // Use previous state when profile changes
else
*flag = 0;
if (checkTool)
{
uint64_t magicDocked = 0xD65F03C0320003E0;
uint64_t magicHandheld = 0xD65F03C052A00000;
uint64_t readBuffer = 0;
fread(&readBuffer, 1, sizeof(readBuffer), readFile);
fclose(readFile);
if (!memcmp(&readBuffer, &magicDocked, sizeof(readBuffer)))
return ReverseNX_Docked;
if (!memcmp(&readBuffer, &magicHandheld, sizeof(readBuffer)))
return ReverseNX_Handheld;
}
else // checkRT
{
uint64_t tidBuffer = 0;
uint8_t ctrlBuffer = 0;
fread(&tidBuffer, 1, sizeof(tidBuffer), readFile);
fread(&ctrlBuffer, 1, sizeof(ctrlBuffer), readFile);
fclose(readFile);
remove(filePath);
if (!memcmp(&this->context->applicationId, &tidBuffer, sizeof(tidBuffer)))
{
/* If user toggles -RT overlay, mode set by -Tool remains invalid until the game restarts */
this->oc->reverseNXToolMode = ReverseNX_NotValid;
ReverseNXMode getMode = static_cast<ReverseNXMode>(ctrlBuffer);
this->oc->reverseNXRTMode = getMode;
return getMode;
}
}
return ReverseNX_NotValid;
}
void ClockManager::CheckReverseNXTool()
{
bool shouldCheckReverseNXTool = FileUtils::IsReverseNXSyncEnabled() && FileUtils::ExistReverseNXTool();
if (!shouldCheckReverseNXTool)
return;
ReverseNXMode getMode = ReverseNX_NotValid;
if (this->context->applicationId != PROCESS_MANAGEMENT_QLAUNCH_TID)
{
const char asmFileName[] = "_ZN2nn2oe18GetPerformanceModeEv.asm64"; // Checking one asm64 file is enough
char asmFilePath[128];
/* Check global override */
snprintf(asmFilePath, sizeof(asmFilePath), "/SaltySD/patches/%s", asmFileName);
getMode = ReverseNXFileHandler(true, asmFilePath);
if (!getMode)
{
/* Check per-game override */
snprintf(asmFilePath, sizeof(asmFilePath), "/SaltySD/patches/%016lX/%s", this->context->applicationId, asmFileName);
getMode = ReverseNXFileHandler(true, asmFilePath);
}
}
this->oc->reverseNXToolMode = getMode;
}
bool ClockManager::RefreshContext()
{
bool hasChanged = false;
bool shouldAdjustProfile = false;
bool enabled = this->GetConfig()->Enabled();
if(enabled != this->context->enabled)
@@ -438,81 +408,42 @@ bool ClockManager::RefreshContext()
if (applicationId != this->context->applicationId)
{
FileUtils::LogLine("[mgr] TitleID change: %016lX", applicationId);
prevReverseNXRT = 0; // Reset ReverseNX-RT previous state when Title ID changes
this->context->applicationId = applicationId;
hasChanged = true;
shouldAdjustProfile = true;
if (FileUtils::IsReverseNXSyncEnabled() && (FileUtils::IsReverseNXToolExist() || recheckReverseNX))
{
// A new game starts or the real profile changes, then we need to check if ReverseNXTool patches are applied
isEnabledReverseNX = false;
// Check if ReverseNXTool patches are applied
if (applicationId != PROCESS_MANAGEMENT_QLAUNCH_TID)
this->checkReverseNXTool();
}
/* Clear ReverseNX-RT state and recheck -Tool patches*/
this->oc->reverseNXRTMode = ReverseNX_SystemDefault;
CheckReverseNXTool();
}
if (FileUtils::IsReverseNXSyncEnabled() && (!tickCheckReverseNXRT || recheckReverseNX))
{
uint8_t flag = 0;
checkReverseNXRT(recheckReverseNX, &flag);
switch(flag)
{
case 1:
FileUtils::LogLine("[mgr] ReverseNX-RT detected: Enforce Handheld Mode");
this->context->profile = ReverseNXProfile(false);
prevReverseNXRT = flag;
isEnabledReverseNX = true;
isDockedReverseNX = false;
hasChanged = true;
break;
case 2:
FileUtils::LogLine("[mgr] ReverseNX-RT detected: Enforce Docked Mode");
this->context->profile = ReverseNXProfile(true);
prevReverseNXRT = flag;
isEnabledReverseNX = true;
isDockedReverseNX = true;
hasChanged = true;
break;
case 3:
FileUtils::LogLine("[mgr] ReverseNX-RT disabled: Reset to System-controlled Mode and recheck ReverseNX-Tool");
RealProfile = Clocks::GetCurrentProfile();
this->context->profile = RealProfile;
prevReverseNXRT = 0;
isEnabledReverseNX = false;
isDockedReverseNX = false;
hasChanged = true;
if (this->context->applicationId != PROCESS_MANAGEMENT_QLAUNCH_TID)
this->checkReverseNXTool();
break;
case 0:
if (recheckReverseNX && isEnabledReverseNXTool && this->context->applicationId != PROCESS_MANAGEMENT_QLAUNCH_TID)
this->checkReverseNXTool();
break;
}
tickCheckReverseNXRT = (std::uint32_t)( 1'000 / this->oc->tickWaitTimeMs ) + 1;
}
tickCheckReverseNXRT--;
if (recheckReverseNX)
recheckReverseNX = false;
SysClkProfile profile = Clocks::GetCurrentProfile();
if (profile != this->context->profile && !isEnabledReverseNX)
if (profile != this->context->realProfile)
{
FileUtils::LogLine("[mgr] Profile change: %s", Clocks::GetProfileName(profile, true));
this->context->profile = profile;
this->context->realProfile = profile;
hasChanged = true;
shouldAdjustProfile = true;
}
if (profile != RealProfile && isEnabledReverseNX)
/* Check ReverseNX-RT once per tick */
{
FileUtils::LogLine("[mgr] Profile change: %s, recheck ReverseNX", Clocks::GetProfileName(profile, true));
this->context->profile = profile;
RealProfile = profile;
hasChanged = true;
recheckReverseNX = true;
ReverseNXMode getMode = ReverseNXFileHandler(false, FILE_REVERSENX_RT_CONF_PATH);
if (getMode)
{
this->oc->reverseNXRTMode = getMode;
shouldAdjustProfile = true;
}
if (getMode == ReverseNX_RTResetToDefault)
{
this->oc->reverseNXRTMode = ReverseNX_SystemDefault;
}
if (shouldAdjustProfile)
{
this->context->profile = ReverseNXProfileHandler();
}
}
/* Update PerformanceConfigurationId */