Skip to content

Commit

Permalink
linux: parse /proc/cpuinfo to learn about CPU packages and cores
Browse files Browse the repository at this point in the history
  • Loading branch information
leahneukirchen authored and BenBE committed Jan 20, 2025
1 parent 7bf9966 commit 8266759
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
64 changes: 63 additions & 1 deletion linux/LinuxMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,59 @@ static void scanCPUFrequencyFromCPUinfo(LinuxMachine* this) {
}
}

#ifdef HAVE_SENSORS_SENSORS_H
static void LinuxMachine_fetchCPUTopologyFromCPUinfo(LinuxMachine* this) {
const Machine* super = &this->super;

FILE* file = fopen(PROCCPUINFOFILE, "r");
if (file == NULL)
return;

int cpuid = -1;
int coreid = -1;
int physicalid = -1;

int max_physicalid = -1;
int max_coreid = -1;

while (!feof(file)) {
char *buffer = String_readLine(file);
if (!buffer)
break;

if (buffer[0] == '\0') { /* empty line after each cpu */
if (cpuid >= 0 && (unsigned int)cpuid < super->existingCPUs) {
CPUData* cpuData = &(this->cpuData[cpuid + 1]);
cpuData->coreID = coreid;
cpuData->physicalID = physicalid;

if (coreid > max_coreid)
max_coreid = coreid;
if (physicalid > max_physicalid)
max_physicalid = physicalid;

cpuid = -1;
coreid = -1;
physicalid = -1;
}
} else if (String_startsWith(buffer, "processor")) {
sscanf(buffer, "processor : %d", &cpuid);
} else if (String_startsWith(buffer, "physical id")) {
sscanf(buffer, "physical id : %d", &physicalid);
} else if (String_startsWith(buffer, "core id")) {
sscanf(buffer, "core id : %d", &coreid);
}

free(buffer);
}

this->maxPhysicalID = max_physicalid;
this->maxCoreID = max_coreid;

fclose(file);
}
#endif

static void LinuxMachine_scanCPUFrequency(LinuxMachine* this) {
const Machine* super = &this->super;

Expand All @@ -640,7 +693,11 @@ void Machine_scan(Machine* super) {
LinuxMachine_scanCPUTime(this);

const Settings* settings = super->settings;
if (settings->showCPUFrequency)
if (settings->showCPUFrequency
#ifdef HAVE_SENSORS_SENSORS_H
|| settings->showCPUTemperature
#endif
)
LinuxMachine_scanCPUFrequency(this);

#ifdef HAVE_SENSORS_SENSORS_H
Expand Down Expand Up @@ -689,6 +746,11 @@ Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
// Initialize CPU count
LinuxMachine_updateCPUcount(this);

#ifdef HAVE_SENSORS_SENSORS_H
// Fetch CPU topology
LinuxMachine_fetchCPUTopologyFromCPUinfo(this);
#endif

return super;
}

Expand Down
8 changes: 8 additions & 0 deletions linux/LinuxMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ typedef struct CPUData_ {

#ifdef HAVE_SENSORS_SENSORS_H
double temperature;

int physicalID; /* different for each CPU socket */
int coreID; /* same for hyperthreading */
#endif

bool online;
Expand All @@ -74,6 +77,11 @@ typedef struct LinuxMachine_ {

CPUData* cpuData;

#ifdef HAVE_SENSORS_SENSORS_H
int maxPhysicalID;
int maxCoreID;
#endif

memory_t totalHugePageMem;
memory_t usedHugePageMem[HTOP_HUGEPAGE_COUNT];

Expand Down

0 comments on commit 8266759

Please sign in to comment.