mirror of
https://github.com/Dr-Noob/cpufetch.git
synced 2026-03-25 07:50:40 +01:00
[v0.98][Refactoring] Use printWarn + strerror(errno) instead of perror. Use fallback in ppc in case total_cores cannot be retrieved
This commit is contained in:
@@ -27,7 +27,7 @@ int get_ncores_from_cpuinfo() {
|
|||||||
int filelen;
|
int filelen;
|
||||||
char* buf;
|
char* buf;
|
||||||
if((buf = read_file(_PATH_CPUS_PRESENT, &filelen)) == NULL) {
|
if((buf = read_file(_PATH_CPUS_PRESENT, &filelen)) == NULL) {
|
||||||
perror("open");
|
printWarn("read_file: %s: %s\n", _PATH_CPUS_PRESENT, strerror(errno));
|
||||||
return UNKNOWN;
|
return UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ int get_ncores_from_cpuinfo() {
|
|||||||
errno = 0;
|
errno = 0;
|
||||||
ncores = strtol(ncores_str, &end, 10) + 1;
|
ncores = strtol(ncores_str, &end, 10) + 1;
|
||||||
if(errno != 0) {
|
if(errno != 0) {
|
||||||
perror("strtol");
|
printWarn("strtol: %s:\n", strerror(errno));
|
||||||
return UNKNOWN;
|
return UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ long parse_cpuinfo_field(char* buf, char* field_str, int field_base) {
|
|||||||
errno = 0;
|
errno = 0;
|
||||||
long ret = strtol(tmp, &end, field_base);
|
long ret = strtol(tmp, &end, field_base);
|
||||||
if(errno != 0) {
|
if(errno != 0) {
|
||||||
perror("strtol");
|
printWarn("strtol: %s:\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,7 +81,7 @@ uint32_t get_midr_from_cpuinfo(uint32_t core, bool* success) {
|
|||||||
char* buf;
|
char* buf;
|
||||||
*success = true;
|
*success = true;
|
||||||
if((buf = read_file(_PATH_CPUINFO, &filelen)) == NULL) {
|
if((buf = read_file(_PATH_CPUINFO, &filelen)) == NULL) {
|
||||||
perror("open");
|
printWarn("read_file: %s: %s\n", _PATH_CPUINFO, strerror(errno));
|
||||||
*success = false;
|
*success = false;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -108,35 +108,35 @@ uint32_t get_midr_from_cpuinfo(uint32_t core, bool* success) {
|
|||||||
long ret;
|
long ret;
|
||||||
|
|
||||||
if ((ret = parse_cpuinfo_field(tmp, CPUINFO_CPU_IMPLEMENTER_STR, 16)) < 0) {
|
if ((ret = parse_cpuinfo_field(tmp, CPUINFO_CPU_IMPLEMENTER_STR, 16)) < 0) {
|
||||||
printf("Failed parsing cpu_implementer\n");
|
printBug("get_midr_from_cpuinfo: Failed parsing cpu_implementer\n");
|
||||||
*success = false;
|
*success = false;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
cpu_implementer = (uint32_t) ret;
|
cpu_implementer = (uint32_t) ret;
|
||||||
|
|
||||||
if ((ret = parse_cpuinfo_field(tmp, CPUINFO_CPU_ARCHITECTURE_STR, 10)) < 0) {
|
if ((ret = parse_cpuinfo_field(tmp, CPUINFO_CPU_ARCHITECTURE_STR, 10)) < 0) {
|
||||||
printf("Failed parsing cpu_architecture\n");
|
printBug("get_midr_from_cpuinfo: Failed parsing cpu_architecture\n");
|
||||||
*success = false;
|
*success = false;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
cpu_architecture = (uint32_t) 0xF; // Why?
|
cpu_architecture = (uint32_t) 0xF; // Why?
|
||||||
|
|
||||||
if ((ret = parse_cpuinfo_field(tmp, CPUINFO_CPU_VARIANT_STR, 16)) < 0) {
|
if ((ret = parse_cpuinfo_field(tmp, CPUINFO_CPU_VARIANT_STR, 16)) < 0) {
|
||||||
printf("Failed parsing cpu_variant\n");
|
printBug("get_midr_from_cpuinfo: Failed parsing cpu_variant\n");
|
||||||
*success = false;
|
*success = false;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
cpu_variant = (uint32_t) ret;
|
cpu_variant = (uint32_t) ret;
|
||||||
|
|
||||||
if ((ret = parse_cpuinfo_field(tmp, CPUINFO_CPU_PART_STR, 16)) < 0) {
|
if ((ret = parse_cpuinfo_field(tmp, CPUINFO_CPU_PART_STR, 16)) < 0) {
|
||||||
printf("Failed parsing cpu_part\n");
|
printBug("get_midr_from_cpuinfo: Failed parsing cpu_part\n");
|
||||||
*success = false;
|
*success = false;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
cpu_part = (uint32_t) ret;
|
cpu_part = (uint32_t) ret;
|
||||||
|
|
||||||
if ((ret = parse_cpuinfo_field(tmp, CPUINFO_CPU_REVISION_STR, 10)) < 0) {
|
if ((ret = parse_cpuinfo_field(tmp, CPUINFO_CPU_REVISION_STR, 10)) < 0) {
|
||||||
printf("Failed parsing cpu_revision\n");
|
printBug("get_midr_from_cpuinfo: Failed parsing cpu_revision\n");
|
||||||
*success = false;
|
*success = false;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -155,7 +155,7 @@ char* get_field_from_cpuinfo(char* CPUINFO_FIELD) {
|
|||||||
int filelen;
|
int filelen;
|
||||||
char* buf;
|
char* buf;
|
||||||
if((buf = read_file(_PATH_CPUINFO, &filelen)) == NULL) {
|
if((buf = read_file(_PATH_CPUINFO, &filelen)) == NULL) {
|
||||||
perror("open");
|
printWarn("read_file: %s: %s:\n", _PATH_CPUINFO, strerror(errno));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,8 +44,7 @@ long get_freq_from_file(char* path, bool hv_present) {
|
|||||||
errno = 0;
|
errno = 0;
|
||||||
long ret = strtol(buf, &end, 10);
|
long ret = strtol(buf, &end, 10);
|
||||||
if(errno != 0) {
|
if(errno != 0) {
|
||||||
perror("strtol");
|
printBug("strtol: %s", strerror(errno));
|
||||||
printBug("Failed parsing '%s' file. Read data was: '%s'", path, buf);
|
|
||||||
free(buf);
|
free(buf);
|
||||||
return UNKNOWN_FREQ;
|
return UNKNOWN_FREQ;
|
||||||
}
|
}
|
||||||
@@ -77,8 +76,7 @@ long get_cache_size_from_file(char* path) {
|
|||||||
errno = 0;
|
errno = 0;
|
||||||
long ret = strtol(buf, &end, 10);
|
long ret = strtol(buf, &end, 10);
|
||||||
if(errno != 0) {
|
if(errno != 0) {
|
||||||
perror("strtol");
|
printBug("strtol: %s", strerror(errno));
|
||||||
printBug("Failed parsing '%s' file. Read data was: '%s'", path, buf);
|
|
||||||
free(buf);
|
free(buf);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -146,8 +144,7 @@ int get_num_caches_from_files(char** paths, int num_paths) {
|
|||||||
errno = 0;
|
errno = 0;
|
||||||
long ret = strtol(buf, &end, 16);
|
long ret = strtol(buf, &end, 16);
|
||||||
if(errno != 0) {
|
if(errno != 0) {
|
||||||
perror("strtol");
|
printBug("strtol: %s", strerror(errno));
|
||||||
printBug("Failed parsing '%s' file. Read data was: '%s'", paths[i], buf);
|
|
||||||
free(buf);
|
free(buf);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,8 +50,8 @@ struct topology* get_topology_info(struct cache* cach) {
|
|||||||
|
|
||||||
// 1. Total cores detection
|
// 1. Total cores detection
|
||||||
if((topo->total_cores = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
|
if((topo->total_cores = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
|
||||||
perror("sysconf");
|
printWarn("sysconf(_SC_NPROCESSORS_ONLN): %s", strerror(errno));
|
||||||
return NULL;
|
topo->total_cores = 1; // fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
// To find physical cores, we use topo->total_cores and core_ids
|
// To find physical cores, we use topo->total_cores and core_ids
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#include "apic.h"
|
#include "apic.h"
|
||||||
#include "cpuid_asm.h"
|
#include "cpuid_asm.h"
|
||||||
@@ -84,7 +85,7 @@ bool bind_to_cpu(int cpu_id) {
|
|||||||
CPU_ZERO(¤tCPU);
|
CPU_ZERO(¤tCPU);
|
||||||
CPU_SET(cpu_id, ¤tCPU);
|
CPU_SET(cpu_id, ¤tCPU);
|
||||||
if (sched_setaffinity (0, sizeof(currentCPU), ¤tCPU) == -1) {
|
if (sched_setaffinity (0, sizeof(currentCPU), ¤tCPU) == -1) {
|
||||||
perror("sched_setaffinity");
|
printWarn("sched_setaffinity: %s", strerror(errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -93,7 +94,7 @@ bool bind_to_cpu(int cpu_id) {
|
|||||||
CPU_ZERO(¤tCPU);
|
CPU_ZERO(¤tCPU);
|
||||||
CPU_SET(cpu_id, ¤tCPU);
|
CPU_SET(cpu_id, ¤tCPU);
|
||||||
if(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset_t), ¤tCPU) == -1) {
|
if(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset_t), ¤tCPU) == -1) {
|
||||||
perror("cpuset_setaffinity");
|
printWarn("cpuset_setaffinity: %s", strerror(errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -435,7 +435,7 @@ struct topology* get_topology_info(struct cpuInfo* cpu, struct cache* cach) {
|
|||||||
topo->total_cores = info.dwNumberOfProcessors;
|
topo->total_cores = info.dwNumberOfProcessors;
|
||||||
#else
|
#else
|
||||||
if((topo->total_cores = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
|
if((topo->total_cores = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
|
||||||
perror("sysconf");
|
printWarn("sysconf(_SC_NPROCESSORS_ONLN): %s", strerror(errno));
|
||||||
topo->total_cores = topo->logical_cores; // fallback
|
topo->total_cores = topo->logical_cores; // fallback
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user