56 size_t size =
sizeof(result);
57 if (sysctlbyname(
"machdep.cpu.brand_string", result, &size,
nullptr, 0) == 0)
61#elif defined(unix) || defined(__unix) || defined(__unix__)
62 static std::regex pattern(
"model name(.*): (.*)");
65 std::ifstream stream(
"/proc/cpuinfo");
66 while (getline(stream, line))
69 if (std::regex_match(line, matches, pattern))
74#elif defined(_WIN32) || defined(_WIN64)
76 LONG lError = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &hKeyProcessor);
77 if (lError != ERROR_SUCCESS)
81 auto key =
resource(hKeyProcessor, [](HKEY hKey) { RegCloseKey(hKey); });
83 CHAR pBuffer[_MAX_PATH] = { 0 };
84 DWORD dwBufferSize =
sizeof(pBuffer);
85 lError = RegQueryValueExA(key.get(),
"ProcessorNameString",
nullptr,
nullptr, (LPBYTE)pBuffer, &dwBufferSize);
86 if (lError != ERROR_SUCCESS)
89 return std::string(pBuffer);
91 #error Unsupported platform
135#if defined(__APPLE__)
137 size_t logical_size =
sizeof(logical);
138 if (sysctlbyname(
"hw.logicalcpu", &logical, &logical_size,
nullptr, 0) != 0)
142 size_t physical_size =
sizeof(physical);
143 if (sysctlbyname(
"hw.physicalcpu", &physical, &physical_size,
nullptr, 0) != 0)
146 return std::make_pair(logical, physical);
147#elif defined(unix) || defined(__unix) || defined(__unix__)
148 static std::regex pattern(
"core id(.*): (.*)");
153 std::ifstream stream(
"/proc/cpuinfo");
154 while (getline(stream, line))
157 if (std::regex_match(line, matches, pattern))
158 cores.insert(atoi(matches[2].str().c_str()));
161 size_t logical = cores.size();
162 long physical = sysconf(_SC_NPROCESSORS_ONLN);
163 return std::make_pair(logical, physical);
164#elif defined(_WIN32) || defined(_WIN64)
165 BOOL allocated = FALSE;
166 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pBuffer =
nullptr;
171 BOOL bResult = GetLogicalProcessorInformation(pBuffer, &dwLength);
172 if (bResult == FALSE)
174 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
176 if (pBuffer !=
nullptr)
178 pBuffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)std::malloc(dwLength);
179 if (pBuffer ==
nullptr)
180 return std::make_pair(-1, -1);
183 return std::make_pair(-1, -1);
189 std::pair<int, int> result(0, 0);
190 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pCurrent = pBuffer;
193 while (dwOffset +
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= dwLength)
195 switch (pCurrent->Relationship)
197 case RelationProcessorCore:
198 result.first += Internals::CountSetBits(pCurrent->ProcessorMask);
201 case RelationNumaNode:
203 case RelationProcessorPackage:
206 return std::make_pair(-1, -1);
208 dwOffset +=
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
216 #error Unsupported platform
222#if defined(__APPLE__)
223 uint64_t frequency = 0;
224 size_t frequency_size =
sizeof(frequency);
225 if (sysctlbyname(
"hw.cpufrequency", &frequency, &frequency_size,
nullptr, 0) == 0)
229 struct clockinfo clockrate;
230 size_t clockrate_size =
sizeof(clockrate);
231 if ((sysctlbyname(
"hw.tbfrequency", &frequency, &frequency_size, NULL, 0) == 0) && (sysctlbyname(
"kern.clockrate", &clockrate, &clockrate_size, NULL, 0) == 0))
232 return frequency * clockrate.hz;
235#elif defined(unix) || defined(__unix) || defined(__unix__)
236 static std::regex pattern(
"cpu MHz(.*): (.*)");
239 std::ifstream stream(
"/proc/cpuinfo");
240 while (getline(stream, line))
243 if (std::regex_match(line, matches, pattern))
244 return (int64_t)(atof(matches[2].str().c_str()) * 1000000);
248#elif defined(_WIN32) || defined(_WIN64)
250 long lError = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &hKeyProcessor);
251 if (lError != ERROR_SUCCESS)
255 auto key =
resource(hKeyProcessor, [](HKEY hKey) { RegCloseKey(hKey); });
258 DWORD dwBufferSize =
sizeof(DWORD);
259 lError = RegQueryValueExA(key.get(),
"~MHz",
nullptr,
nullptr, (LPBYTE)&dwMHz, &dwBufferSize);
260 if (lError != ERROR_SUCCESS)
263 return dwMHz * 1000000;
265 #error Unsupported platform