[client] cpuinfo: implement CPU socket count for Linux

This commit is contained in:
Quantum 2022-01-07 03:06:49 -05:00 committed by Geoffrey McRae
parent 194241c5a3
commit a40a964b30

View File

@ -37,15 +37,14 @@ bool lgCPUInfo(char * model, size_t modelSize, int * procs, int * cores,
return false; return false;
} }
int socketCount = 0;
if (procs) if (procs)
*procs = 0; *procs = 0;
if (cores) if (cores)
*cores = 0; *cores = 0;
if (sockets)
*sockets = 1;
char buffer[1024]; char buffer[1024];
while (fgets(buffer, sizeof(buffer), cpuinfo)) while (fgets(buffer, sizeof(buffer), cpuinfo))
{ {
@ -65,13 +64,20 @@ bool lgCPUInfo(char * model, size_t modelSize, int * procs, int * cores,
model = NULL; 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, ": "); const char * num = strstr(buffer, ": ");
if (num) if (num)
{ {
*cores = atoi(num + 2); int id = atoi(num + 2);
cores = NULL; if (id >= socketCount)
socketCount = id + 1;
} }
} }
@ -82,6 +88,12 @@ bool lgCPUInfo(char * model, size_t modelSize, int * procs, int * cores,
} }
done: done:
if (sockets)
*sockets = socketCount;
if (cores)
*cores *= socketCount;
fclose(cpuinfo); fclose(cpuinfo);
return true; return true;
} }