From a40a964b3026d4df431b5eb4eca6da4c1ca9decc Mon Sep 17 00:00:00 2001 From: Quantum Date: Fri, 7 Jan 2022 03:06:49 -0500 Subject: [PATCH] [client] cpuinfo: implement CPU socket count for Linux --- common/src/platform/linux/cpuinfo.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/common/src/platform/linux/cpuinfo.c b/common/src/platform/linux/cpuinfo.c index 67fdccac..94306cf8 100644 --- a/common/src/platform/linux/cpuinfo.c +++ b/common/src/platform/linux/cpuinfo.c @@ -37,15 +37,14 @@ bool lgCPUInfo(char * model, size_t modelSize, int * procs, int * cores, return false; } + int socketCount = 0; + if (procs) *procs = 0; if (cores) *cores = 0; - if (sockets) - *sockets = 1; - char buffer[1024]; while (fgets(buffer, sizeof(buffer), cpuinfo)) { @@ -65,13 +64,20 @@ bool lgCPUInfo(char * model, size_t modelSize, int * procs, int * cores, model = NULL; } - else if (cores && strncmp(buffer, "cpu cores", 9) == 0) + else if (cores && *cores == 0 && strncmp(buffer, "cpu cores", 9) == 0) + { + const char * num = strstr(buffer, ": "); + if (num) + *cores = atoi(num + 2); + } + else if (strncmp(buffer, "physical id", 11) == 0) { const char * num = strstr(buffer, ": "); if (num) { - *cores = atoi(num + 2); - cores = NULL; + int id = atoi(num + 2); + if (id >= socketCount) + socketCount = id + 1; } } @@ -82,6 +88,12 @@ bool lgCPUInfo(char * model, size_t modelSize, int * procs, int * cores, } done: + if (sockets) + *sockets = socketCount; + + if (cores) + *cores *= socketCount; + fclose(cpuinfo); return true; }