Skip to content

Commit 5cdf694

Browse files
authored
Fix CPU affinity (#4119)
1 parent ed3bb71 commit 5cdf694

File tree

2 files changed

+57
-44
lines changed

2 files changed

+57
-44
lines changed

Client/core/CCore.cpp

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,43 +1769,44 @@ void CCore::OnPostColorFilterRender()
17691769

17701770
void CCore::ApplyCoreInitSettings()
17711771
{
1772-
#if (_WIN32_WINNT >= _WIN32_WINNT_LONGHORN) // Windows Vista
1773-
bool bValue;
1774-
CVARS_GET("process_dpi_aware", bValue);
1772+
#if (_WIN32_WINNT >= _WIN32_WINNT_LONGHORN)
1773+
const auto aware = CVARS_GET_VALUE<bool>("process_dpi_aware");
17751774

1776-
if (bValue)
1777-
{
1778-
// Minimum supported client for the function below is Windows Vista
1779-
// See also: https://technet.microsoft.com/en-us/evalcenter/dn469266(v=vs.90)
1775+
if (aware)
17801776
SetProcessDPIAware();
1781-
}
17821777
#endif
17831778

1784-
if (int revision = GetApplicationSettingInt("reset-settings-revision"); revision < 21486)
1785-
{
1786-
// Force users with default skin to the 2023 version by replacing "Default" with "Default 2023".
1787-
// The GUI skin "Default 2023" was introduced in commit 2d9e03324b07e355031ecb3263477477f1a91399.
1788-
std::string currentSkinName;
1789-
CVARS_GET("current_skin", currentSkinName);
1779+
const auto revision = GetApplicationSettingInt("reset-settings-revision");
17901780

1791-
if (currentSkinName == "Default")
1792-
{
1793-
CVARS_SET("current_skin", "Default 2023");
1794-
}
1781+
if (revision >= 21486)
1782+
return;
17951783

1796-
SetApplicationSettingInt("reset-settings-revision", 21486);
1797-
}
1784+
const auto skin = CVARS_GET_VALUE<std::string>("current_skin");
1785+
1786+
if (skin == "Default")
1787+
CVARS_SET("current_skin", "Default 2023");
17981788

1799-
// Set process settings
1800-
HANDLE currProc = GetCurrentProcess();
1789+
SetApplicationSettingInt("reset-settings-revision", 21486);
18011790

1802-
// Process priority
1803-
int PriorityClassList[] = {NORMAL_PRIORITY_CLASS, ABOVE_NORMAL_PRIORITY_CLASS, HIGH_PRIORITY_CLASS};
1804-
SetPriorityClass(currProc, PriorityClassList[CVARS_GET_VALUE<int>("process_priority") % 3]);
1791+
const auto process = GetCurrentProcess();
1792+
const int priorities[] = {NORMAL_PRIORITY_CLASS, ABOVE_NORMAL_PRIORITY_CLASS, HIGH_PRIORITY_CLASS};
1793+
const auto priority = CVARS_GET_VALUE<int>("process_priority") % 3;
1794+
1795+
SetPriorityClass(process, priorities[priority]);
1796+
1797+
const auto affinity = CVARS_GET_VALUE<bool>("process_cpu_affinity");
1798+
1799+
if (!affinity)
1800+
return;
1801+
1802+
DWORD_PTR mask;
1803+
DWORD_PTR sys;
1804+
const auto result = GetProcessAffinityMask(process, &mask, &sys);
1805+
1806+
if (!result)
1807+
return;
18051808

1806-
// Process CPU affinity
1807-
if (CVARS_GET_VALUE<bool>("process_cpu_affinity"))
1808-
SetProcessAffinityMask(currProc, 1 << 0);
1809+
SetProcessAffinityMask(process, mask & ~1);
18091810
}
18101811

18111812
//

Client/core/CSettings.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,17 +3242,23 @@ void CSettings::LoadData()
32423242
CVARS_GET("process_cpu_affinity", bVar);
32433243
m_pProcessAffinityCheckbox->SetSelected(bVar);
32443244

3245-
DWORD_PTR affinityMask = 0;
3246-
if (bVar)
3247-
affinityMask = 1 << 0; // CPU 0 only
3245+
DWORD_PTR mask;
3246+
DWORD_PTR sys;
3247+
3248+
const auto process = GetCurrentProcess();
3249+
const auto result = GetProcessAffinityMask(process, &mask, &sys);
3250+
3251+
if (bVar && result)
3252+
{
3253+
SetProcessAffinityMask(process, mask & ~1);
3254+
}
32483255
else
32493256
{
3250-
SYSTEM_INFO sysInfo;
3251-
GetSystemInfo(&sysInfo);
3257+
SYSTEM_INFO info;
32523258

3253-
affinityMask = (1 << sysInfo.dwNumberOfProcessors) - 1; // All cores (default)
3259+
GetSystemInfo(&info);
3260+
SetProcessAffinityMask(process, (1 << info.dwNumberOfProcessors) - 1);
32543261
}
3255-
SetProcessAffinityMask(GetCurrentProcess(), affinityMask);
32563262

32573263
// Update build type
32583264
CVARS_GET("update_build_type", iVar);
@@ -3651,20 +3657,26 @@ void CSettings::SaveData()
36513657
CScreenShot::SetPhotoSavingInsideDocuments(photoSaving);
36523658

36533659
// Process CPU Affinity
3654-
bool cpuAffinity = m_pProcessAffinityCheckbox->GetSelected();
3655-
CVARS_SET("process_cpu_affinity", cpuAffinity);
3660+
const auto affinity = m_pProcessAffinityCheckbox->GetSelected();
3661+
CVARS_SET("process_cpu_affinity", affinity);
36563662

3657-
DWORD_PTR affinityMask = 0;
3658-
if (cpuAffinity)
3659-
affinityMask = 1 << 0; // CPU 0 only
3663+
DWORD_PTR mask;
3664+
DWORD_PTR sys;
3665+
3666+
const auto process = GetCurrentProcess();
3667+
const auto result = GetProcessAffinityMask(process, &mask, &sys);
3668+
3669+
if (affinity && result)
3670+
{
3671+
SetProcessAffinityMask(process, mask & ~1);
3672+
}
36603673
else
36613674
{
3662-
SYSTEM_INFO sysInfo;
3663-
GetSystemInfo(&sysInfo);
3675+
SYSTEM_INFO info;
36643676

3665-
affinityMask = (1 << sysInfo.dwNumberOfProcessors) - 1; // All cores (default)
3677+
GetSystemInfo(&info);
3678+
SetProcessAffinityMask(process, (1 << info.dwNumberOfProcessors) - 1);
36663679
}
3667-
SetProcessAffinityMask(GetCurrentProcess(), affinityMask);
36683680

36693681
// Debug setting
36703682
if (CGUIListItem* pSelected = m_pDebugSettingCombo->GetSelectedItem())

0 commit comments

Comments
 (0)