-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathframework.h
188 lines (153 loc) · 5.77 KB
/
framework.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <memory>
#include <thread>
#include <algorithm>
#include <vector>
//#include "uv.h"
static constexpr unsigned MaxLogicalProcessorsPerGroup =
std::numeric_limits<KAFFINITY>::digits;
bool IsNUMA() noexcept
{
ULONG HighestNodeNumber;
return !(!GetNumaHighestNodeNumber(&HighestNodeNumber) || HighestNodeNumber == 0);
}
int __g_ProcGroupCount = 0;
int __g_ProcLogicalThreadCount = 0;
int __g_ProcSelectedForThread = 0;
PVOID* __g_VirtAllocBuffers = NULL;
int calcLogicalGroups()
{
DWORD length = 0;
GetLogicalProcessorInformationEx(RelationAll, nullptr, &length);
std::unique_ptr<void, void (*)(void*)> buffer(std::malloc(length), std::free);
auto* mem = reinterpret_cast<unsigned char*>(buffer.get());
GetLogicalProcessorInformationEx(RelationAll, reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(mem), &length);
DWORD i = 0;
while (i < length) {
const auto* proc = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(mem + i);
return proc->Processor.GroupCount;
}
}
// This implementation supports both conventional single-cpu PC configurations
// and multi-cpu system on NUMA (Non-uniform_memory_access) architecture
// Modified by GermanAizek
unsigned int GetLogicalThreadCount() noexcept
{
DWORD length = 0;
const auto single_cpu_concurrency = []() noexcept -> unsigned int {
SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwNumberOfProcessors;
};
if (GetLogicalProcessorInformationEx(RelationAll, nullptr, &length) != FALSE) {
return single_cpu_concurrency();
}
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
return single_cpu_concurrency();
}
std::unique_ptr<void, void (*)(void*)> buffer(std::malloc(length), std::free);
if (!buffer) {
return single_cpu_concurrency();
}
auto* mem = reinterpret_cast<unsigned char*>(buffer.get());
if (GetLogicalProcessorInformationEx(
RelationAll, reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(mem), &length) == false) {
return single_cpu_concurrency();
}
DWORD i = 0;
unsigned int concurrency = 0;
while (i < length) {
const auto* proc = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(mem + i);
if (proc->Relationship == RelationProcessorCore) {
for (WORD group = 0; group < proc->Processor.GroupCount; ++group) {
for (KAFFINITY mask = proc->Processor.GroupMask[group].Mask; mask != 0; mask >>= 1) {
concurrency += mask & 1;
}
}
}
i += proc->Size;
}
return concurrency;
}
void setThreadAffinityAllGroupCores(HANDLE handle) noexcept
{
/* After Windows 10 22H2, include Windows 11 not needed set thread affinity */
if (*(DWORD*)(0x7FFE0000 + 0x260) <= 19045) /* Windows 10 22H2 */
{
//SetThreadIdealProcessor(handle, MAXIMUM_PROCESSORS);
SetThreadAffinityMask(handle, (1 << __g_ProcLogicalThreadCount) - 1);
// HACK: set thread affinity for main thread
//SetThreadIdealProcessor(GetCurrentThread(), MAXIMUM_PROCESSORS);
SetThreadAffinityMask(GetCurrentThread(), (1 << __g_ProcLogicalThreadCount) - 1);
}
//MessageBoxW(NULL, L"Crash", L"NUMAYei", MB_OK);
}
void setThreadParallelAllNUMAGroups(HANDLE handle) noexcept
{
auto nodes = std::make_unique<GROUP_AFFINITY[]>(__g_ProcGroupCount);
for (int i = 0; i < __g_ProcGroupCount; ++i)
{
nodes[i].Group = i;
nodes[i].Mask = (ULONG_PTR(1) << (__g_ProcLogicalThreadCount / __g_ProcGroupCount)) - 1;
}
SetThreadSelectedCpuSetMasks(handle, nodes.get(), __g_ProcGroupCount);
// parallel threads using global var scheduler
/* not effective
PROCESSOR_NUMBER proc;
proc.Group = __g_ProcSelectedForThread;
proc.Number = __g_ProcLogicalThreadCount / __g_ProcGroupCount;
//MessageBoxW(NULL, std::to_wstring(__g_ProcLogicalThreadCount).c_str(), L"NUMAYei", MB_OK);
SetThreadIdealProcessorEx(handle, &proc, NULL);
*/
if (__g_ProcSelectedForThread < __g_ProcGroupCount)
++__g_ProcSelectedForThread;
else
__g_ProcSelectedForThread = 0;
}
LPVOID virtualAllocNUMA(HANDLE hProcess, LPVOID lpAddress, SIZE_T dwSize)
{
//
// Allocate array of pointers to memory blocks.
//
__g_VirtAllocBuffers = (PVOID*)malloc(sizeof(PVOID) * __g_ProcLogicalThreadCount);
if (__g_VirtAllocBuffers == NULL)
{
//_putts(_T("Allocating array of buffers failed"));
//goto Exit;
}
ZeroMemory(__g_VirtAllocBuffers, sizeof(PVOID) * __g_ProcLogicalThreadCount);
//
// For each processor, get its associated NUMA node and allocate some memory from it.
//
for (UINT i = 0; i < __g_ProcLogicalThreadCount; ++i)
{
UCHAR NodeNumber;
if (!GetNumaProcessorNode(i, &NodeNumber))
{
//assert(false);
}
auto data = VirtualAllocExNuma(hProcess, lpAddress, dwSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE, NodeNumber);
__g_VirtAllocBuffers[i] = data;
// full memory call below will touch every page in the buffer, faulting them into our working set.
FillMemory(data, dwSize, 'x');
}
return __g_VirtAllocBuffers[__g_ProcLogicalThreadCount]; // TODO: it will be necessary to test which block allocated memory is better to return back
}
BOOL virtualFreeNUMA()
{
if (__g_VirtAllocBuffers != NULL)
{
for (UINT i = 0; i < __g_ProcLogicalThreadCount; ++i)
{
if (__g_VirtAllocBuffers[i] != NULL)
{
VirtualFree(__g_VirtAllocBuffers[i], 0, MEM_RELEASE);
}
}
free(__g_VirtAllocBuffers);
return TRUE;
}
return FALSE;
}