8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-22 18:43:02 +01:00

Improvement #7634 : Include Performance Cores only in default affinity mask

This commit is contained in:
Vlad Khorsun 2023-06-16 13:24:00 +03:00
parent 990cf01c7a
commit d41cc85523
3 changed files with 66 additions and 0 deletions

View File

@ -358,6 +358,10 @@ namespace os_utils
#endif // WIN_NT
#ifdef WIN_NT
void setDefaultAffinity();
#endif
class CtrlCHandler
{
public:

View File

@ -29,6 +29,7 @@
#include "../common/classes/array.h"
#include "../common/classes/init.h"
#include "../common/classes/GenericMap.h"
#include "../common/gdsassert.h"
#include "../common/os/guid.h"
#include "../common/os/os_utils.h"
@ -560,6 +561,65 @@ void getUniqueFileId(HANDLE fd, UCharBuffer& id)
sizeof(file_info.nFileIndexLow));
}
void setDefaultAffinity()
{
HANDLE hCurrProc = GetCurrentProcess();
DWORD_PTR procMask, sysMask, newMask;
GetProcessAffinityMask(hCurrProc, &procMask, &sysMask);
// Don't change affinity if it was set by caller process
if (procMask != sysMask)
return;
newMask = sysMask;
DWORD len = 0;
if (!GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &len))
{
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return;
}
HalfStaticArray<UCHAR, 1024> buff;
auto p = buff.getBuffer(len);
auto const end = p + len;
auto pSLPI = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(p);
if (!GetLogicalProcessorInformationEx(RelationProcessorCore, pSLPI, &len))
return;
// Efficiency Class to Cores map
GenericMap<NonPooledPair<int, DWORD_PTR>> effClassMasks;
while (p < end)
{
if (pSLPI->Relationship == RelationProcessorCore)
{
DWORD_PTR coreMask = 0;
for (size_t i = 0; i < pSLPI->Processor.GroupCount; i++)
coreMask |= pSLPI->Processor.GroupMask[i].Mask;
DWORD_PTR* pMask = effClassMasks.get(pSLPI->Processor.EfficiencyClass);
if (!pMask)
effClassMasks.put(pSLPI->Processor.EfficiencyClass, coreMask);
else
*pMask |= coreMask;
}
p += pSLPI->Size;
pSLPI = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(p);
}
// Exclude efficient cores, i.e. cores with lowest efficient class
if (effClassMasks.count() > 1)
newMask &= ~(*effClassMasks.begin()).second;
if (newMask && newMask != procMask)
SetProcessAffinityMask(hCurrProc, newMask);
}
/// class CtrlCHandler

View File

@ -219,6 +219,8 @@ int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE /*hPrevInst*/, LPSTR lpszArgs,
const DWORD_PTR affinity = Config::getCpuAffinityMask();
if (affinity)
SetProcessAffinityMask(GetCurrentProcess(), affinity);
else
os_utils::setDefaultAffinity();
protocol_inet[0] = 0;