Skip to content

Commit 7d0a5a1

Browse files
committed
Detecting physical cores in GNU/Linux
1 parent b1562cf commit 7d0a5a1

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

source/benchmark/system.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,22 @@ std::pair<int, int> System::CpuTotalCores()
194194

195195
return std::make_pair(logical, physical);
196196
#elif defined(unix) || defined(__unix) || defined(__unix__)
197-
long processors = sysconf(_SC_NPROCESSORS_ONLN);
198-
return std::make_pair(processors, processors);
197+
static std::regex pattern("core id(.*): (.*)");
198+
199+
std::set<int> cores;
200+
201+
std::string line;
202+
std::ifstream stream("/proc/cpuinfo");
203+
while (getline(stream, line))
204+
{
205+
std::smatch matches;
206+
if (std::regex_match(line, matches, pattern))
207+
cores.insert(atoi(matches[2].str().c_str()));
208+
}
209+
210+
size_t logical = cores.size();
211+
long physical = sysconf(_SC_N2PROCESSORS_ONLN);
212+
return std::make_pair(logical, physical);
199213
#elif defined(_WIN32) || defined(_WIN64)
200214
BOOL allocated = FALSE;
201215
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION pBuffer = nullptr;

0 commit comments

Comments
 (0)