mirror of
https://github.com/Dr-Noob/cpufetch.git
synced 2026-03-25 07:50:40 +01:00
[v1.00][X86] Retrieve topology from udev when APIC fails (like #119)
This commit is contained in:
@@ -2,6 +2,48 @@
|
||||
#include "global.h"
|
||||
#include "cpu.h"
|
||||
|
||||
// https://www.kernel.org/doc/html/latest/core-api/cpu_hotplug.html
|
||||
int get_ncores_from_cpuinfo() {
|
||||
// Examples:
|
||||
// 0-271
|
||||
// 0-7
|
||||
// 0
|
||||
|
||||
int filelen;
|
||||
char* buf;
|
||||
if((buf = read_file(_PATH_CPUS_PRESENT, &filelen)) == NULL) {
|
||||
printWarn("read_file: %s: %s\n", _PATH_CPUS_PRESENT, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ncores;
|
||||
char* tmp1;
|
||||
if((tmp1 = strstr(buf, "-")) == NULL) {
|
||||
// file contains no - character, we assume that it contains 0,
|
||||
// which means that the CPU contains only one core
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
tmp1++;
|
||||
}
|
||||
char* tmp2 = strstr(buf, "\n");
|
||||
char ncores_str[filelen];
|
||||
memset(ncores_str, 0, sizeof(char) * filelen);
|
||||
memcpy(ncores_str, tmp1, tmp2-tmp1);
|
||||
|
||||
char* end;
|
||||
errno = 0;
|
||||
ncores = strtol(ncores_str, &end, 10) + 1;
|
||||
if(errno != 0) {
|
||||
printWarn("strtol: %s:\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
|
||||
return ncores;
|
||||
}
|
||||
|
||||
char* read_file(char* path, int* len) {
|
||||
int fd = open(path, O_RDONLY);
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#define _PATH_CACHE_L3 "/cache/index3"
|
||||
#define _PATH_CACHE_SIZE "/size"
|
||||
#define _PATH_CACHE_SHARED_MAP "/shared_cpu_map"
|
||||
#define _PATH_CPUS_PRESENT _PATH_SYS_SYSTEM _PATH_SYS_CPU "/present"
|
||||
|
||||
#define _PATH_FREQUENCY_MAX_LEN 100
|
||||
#define _PATH_CACHE_MAX_LEN 200
|
||||
@@ -36,5 +37,6 @@ long get_l1d_cache_size(uint32_t core);
|
||||
long get_l2_cache_size(uint32_t core);
|
||||
long get_l3_cache_size(uint32_t core);
|
||||
int get_num_caches_by_level(struct cpuInfo* cpu, uint32_t level);
|
||||
int get_ncores_from_cpuinfo();
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user