Skip to content

Commit 77b8998

Browse files
authored
Add CPU affinity setting & fix process related settings (#4096)
Fix #4088
1 parent 61a0e19 commit 77b8998

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

Client/core/CClientVariables.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ void CClientVariables::LoadDefaults()
357357
DEFAULT("discord_rpc_share_data", false); // Consistent Rich Presence data sharing
358358
DEFAULT("discord_rpc_share_data_firsttime", false); // Display the user data sharing consent dialog box - for the first time
359359
DEFAULT("browser_enable_gpu", true); // Enable GPU in CEF? (allows stuff like WebGL to function)
360+
DEFAULT("process_cpu_affinity", true); // Set CPU 0 affinity to improve game performance and fix the known issue in single-threaded games
360361

361362
if (!Exists("locale"))
362363
{

Client/core/CCore.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,17 @@ void CCore::ApplyCoreInitSettings()
17951795

17961796
SetApplicationSettingInt("reset-settings-revision", 21486);
17971797
}
1798+
1799+
// Set process settings
1800+
HANDLE currProc = GetCurrentProcess();
1801+
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]);
1805+
1806+
// Process CPU affinity
1807+
if (CVARS_GET_VALUE<bool>("process_cpu_affinity"))
1808+
SetProcessAffinityMask(currProc, 1 << 0);
17981809
}
17991810

18001811
//

Client/core/CSettings.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,12 @@ void CSettings::CreateGUI()
12071207
m_pPhotoSavingCheckbox->AutoSize(NULL, 20.0f);
12081208
vecTemp.fY += fLineHeight;
12091209

1210+
// Process affinity
1211+
m_pProcessAffinityCheckbox = reinterpret_cast<CGUICheckBox*>(pManager->CreateCheckBox(pTabAdvanced, _("Set CPU 0 affinity to improve game performance"), true));
1212+
m_pProcessAffinityCheckbox->SetPosition(CVector2D(vecTemp.fX, vecTemp.fY));
1213+
m_pProcessAffinityCheckbox->AutoSize(nullptr, 20.0f);
1214+
vecTemp.fY += fLineHeight;
1215+
12101216
// Auto updater section label
12111217
m_pAdvancedUpdaterLabel = reinterpret_cast<CGUILabel*>(pManager->CreateLabel(pTabAdvanced, _("Auto updater")));
12121218
m_pAdvancedUpdaterLabel->SetPosition(CVector2D(vecTemp.fX - 10.0f, vecTemp.fY));
@@ -3228,6 +3234,22 @@ void CSettings::LoadData()
32283234
CVARS_GET("photosaving", bVar);
32293235
m_pPhotoSavingCheckbox->SetSelected(bVar);
32303236

3237+
// Process CPU Affinity
3238+
CVARS_GET("process_cpu_affinity", bVar);
3239+
m_pProcessAffinityCheckbox->SetSelected(bVar);
3240+
3241+
DWORD_PTR affinityMask = 0;
3242+
if (bVar)
3243+
affinityMask = 1 << 0; // CPU 0 only
3244+
else
3245+
{
3246+
SYSTEM_INFO sysInfo;
3247+
GetSystemInfo(&sysInfo);
3248+
3249+
affinityMask = (1 << sysInfo.dwNumberOfProcessors) - 1; // All cores (default)
3250+
}
3251+
SetProcessAffinityMask(GetCurrentProcess(), affinityMask);
3252+
32313253
// Update build type
32323254
CVARS_GET("update_build_type", iVar);
32333255
if (iVar == 0 || iVar == 1)
@@ -3624,6 +3646,22 @@ void CSettings::SaveData()
36243646
CVARS_SET("photosaving", photoSaving);
36253647
CScreenShot::SetPhotoSavingInsideDocuments(photoSaving);
36263648

3649+
// Process CPU Affinity
3650+
bool cpuAffinity = m_pProcessAffinityCheckbox->GetSelected();
3651+
CVARS_SET("process_cpu_affinity", cpuAffinity);
3652+
3653+
DWORD_PTR affinityMask = 0;
3654+
if (cpuAffinity)
3655+
affinityMask = 1 << 0; // CPU 0 only
3656+
else
3657+
{
3658+
SYSTEM_INFO sysInfo;
3659+
GetSystemInfo(&sysInfo);
3660+
3661+
affinityMask = (1 << sysInfo.dwNumberOfProcessors) - 1; // All cores (default)
3662+
}
3663+
SetProcessAffinityMask(GetCurrentProcess(), affinityMask);
3664+
36273665
// Debug setting
36283666
if (CGUIListItem* pSelected = m_pDebugSettingCombo->GetSelectedItem())
36293667
{

Client/core/CSettings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class CSettings
216216
CGUICheckBox* m_pWin8ColorCheckBox;
217217
CGUICheckBox* m_pWin8MouseCheckBox;
218218
CGUICheckBox* m_pPhotoSavingCheckbox;
219+
CGUICheckBox* m_pProcessAffinityCheckbox;
219220
CGUILabel* m_pUpdateBuildTypeLabel;
220221
CGUIComboBox* m_pUpdateBuildTypeCombo;
221222
CGUILabel* m_pUpdateAutoInstallLabel;

0 commit comments

Comments
 (0)