12 #if defined(__APPLE__)
13 #include <sys/sysctl.h>
14 #elif defined(unix) || defined(__unix) || defined(__unix__)
18 #elif defined(_WIN32) || defined(_WIN64)
27 #if defined(_WIN32) || defined(_WIN64)
30 DWORD CountSetBits(ULONG_PTR pBitMask)
32 DWORD dwLeftShift =
sizeof(ULONG_PTR) * 8 - 1;
33 DWORD dwBitSetCount = 0;
34 ULONG_PTR pBitTest = (ULONG_PTR)1 << dwLeftShift;
36 for (DWORD i = 0; i <= dwLeftShift; ++i)
38 dwBitSetCount += ((pBitMask & pBitTest) ? 1 : 0);
52 #if defined(__APPLE__)
54 size_t size =
sizeof(result);
55 if (sysctlbyname(
"machdep.cpu.brand_string", result, &size,
nullptr, 0) == 0)
59 #elif defined(unix) || defined(__unix) || defined(__unix__)
60 static std::regex pattern(
"model name(.*): (.*)");
63 std::ifstream stream(
"/proc/cpuinfo");
64 while (getline(stream, line))
67 if (std::regex_match(line, matches, pattern))
72 #elif defined(_WIN32) || defined(_WIN64)
74 LONG lError = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &hKeyProcessor);
75 if (lError != ERROR_SUCCESS)
79 auto key =
resource(hKeyProcessor, [](HKEY hKey) { RegCloseKey(hKey); });
81 CHAR pBuffer[_MAX_PATH] = { 0 };
82 DWORD dwBufferSize =
sizeof(pBuffer);
83 lError = RegQueryValueExA(key.get(),
"ProcessorNameString",
nullptr,
nullptr, (LPBYTE)pBuffer, &dwBufferSize);
84 if (lError != ERROR_SUCCESS)
87 return std::string(pBuffer);
89 #error Unsupported platform
95 #if defined(__APPLE__)
97 size_t logical_size =
sizeof(logical);
98 if (sysctlbyname(
"hw.logicalcpu", &logical, &logical_size,
nullptr, 0) != 0)
102 #elif defined(unix) || defined(__unix) || defined(__unix__)
103 long processors = sysconf(_SC_NPROCESSORS_ONLN);
105 #elif defined(_WIN32) || defined(_WIN64)
108 return si.dwNumberOfProcessors;
110 #error Unsupported platform
126 #if defined(__APPLE__)
128 size_t logical_size =
sizeof(logical);
129 if (sysctlbyname(
"hw.logicalcpu", &logical, &logical_size,
nullptr, 0) != 0)
133 size_t physical_size =
sizeof(physical);
134 if (sysctlbyname(
"hw.physicalcpu", &physical, &physical_size,
nullptr, 0) != 0)
137 return std::make_pair(logical, physical);
138 #elif defined(unix) || defined(__unix) || defined(__unix__)
139 long processors = sysconf(_SC_NPROCESSORS_ONLN);
140 return std::make_pair(processors, processors);
141 #elif defined(_WIN32) || defined(_WIN64)
142 BOOL allocated = FALSE;
143 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pBuffer =
nullptr;
148 BOOL bResult = GetLogicalProcessorInformation(pBuffer, &dwLength);
149 if (bResult == FALSE)
151 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
153 if (pBuffer !=
nullptr)
155 pBuffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)std::malloc(dwLength);
156 if (pBuffer ==
nullptr)
157 return std::make_pair(-1, -1);
160 return std::make_pair(-1, -1);
166 std::pair<int, int> result(0, 0);
167 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pCurrent = pBuffer;
170 while (dwOffset +
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= dwLength)
172 switch (pCurrent->Relationship)
174 case RelationProcessorCore:
175 result.first += Internals::CountSetBits(pCurrent->ProcessorMask);
178 case RelationNumaNode:
180 case RelationProcessorPackage:
183 return std::make_pair(-1, -1);
185 dwOffset +=
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
193 #error Unsupported platform
199 #if defined(__APPLE__)
200 uint64_t frequency = 0;
201 size_t size =
sizeof(frequency);
202 if (sysctlbyname(
"hw.cpufrequency", &frequency, &size,
nullptr, 0) == 0)
206 #elif defined(unix) || defined(__unix) || defined(__unix__)
207 static std::regex pattern(
"cpu MHz(.*): (.*)");
210 std::ifstream stream(
"/proc/cpuinfo");
211 while (getline(stream, line))
214 if (std::regex_match(line, matches, pattern))
215 return (int64_t)(atof(matches[2].str().c_str()) * 1000000);
219 #elif defined(_WIN32) || defined(_WIN64)
221 long lError = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &hKeyProcessor);
222 if (lError != ERROR_SUCCESS)
226 auto key =
resource(hKeyProcessor, [](HKEY hKey) { RegCloseKey(hKey); });
229 DWORD dwBufferSize =
sizeof(DWORD);
230 lError = RegQueryValueExA(key.get(),
"~MHz",
nullptr,
nullptr, (LPBYTE)&dwMHz, &dwBufferSize);
231 if (lError != ERROR_SUCCESS)
234 return dwMHz * 1000000;
236 #error Unsupported platform
243 return (cores.first != cores.second);
static int LogicalCores()
CPU logical cores count.
static bool HyperThreading()
Is CPU Hyper-Threading enabled?
static int Affinity()
CPU affinity count.
static std::string Architecture()
CPU architecture string.
static std::pair< int, int > TotalCores()
CPU total cores count.
static int PhysicalCores()
CPU physical cores count.
static int64_t ClockSpeed()
CPU clock speed in Hz.
CPU management definition.
C++ Common project definitions.
auto resource(T handle, TCleaner cleaner)
Resource smart cleaner pattern.
Resource smart cleaner pattern definition.