From 2b21326167ae1b879cec56b2b23b589340ab53de Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Sat, 7 Aug 2021 08:43:46 +0200 Subject: [PATCH] [v0.98][Refactoring] Use printWarn + strerror(errno) instead of perror. Use fallback in ppc in case total_cores cannot be retrieved --- src/arm/udev.c | 20 ++++++++++---------- src/common/udev.c | 9 +++------ src/ppc/ppc.c | 4 ++-- src/x86/apic.c | 5 +++-- src/x86/cpuid.c | 2 +- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/arm/udev.c b/src/arm/udev.c index 873edca..b52bb8a 100644 --- a/src/arm/udev.c +++ b/src/arm/udev.c @@ -27,7 +27,7 @@ int get_ncores_from_cpuinfo() { int filelen; char* buf; if((buf = read_file(_PATH_CPUS_PRESENT, &filelen)) == NULL) { - perror("open"); + printWarn("read_file: %s: %s\n", _PATH_CPUS_PRESENT, strerror(errno)); return UNKNOWN; } @@ -50,7 +50,7 @@ int get_ncores_from_cpuinfo() { errno = 0; ncores = strtol(ncores_str, &end, 10) + 1; if(errno != 0) { - perror("strtol"); + printWarn("strtol: %s:\n", strerror(errno)); return UNKNOWN; } @@ -68,7 +68,7 @@ long parse_cpuinfo_field(char* buf, char* field_str, int field_base) { errno = 0; long ret = strtol(tmp, &end, field_base); if(errno != 0) { - perror("strtol"); + printWarn("strtol: %s:\n", strerror(errno)); return -1; } @@ -81,7 +81,7 @@ uint32_t get_midr_from_cpuinfo(uint32_t core, bool* success) { char* buf; *success = true; if((buf = read_file(_PATH_CPUINFO, &filelen)) == NULL) { - perror("open"); + printWarn("read_file: %s: %s\n", _PATH_CPUINFO, strerror(errno)); *success = false; return 0; } @@ -108,35 +108,35 @@ uint32_t get_midr_from_cpuinfo(uint32_t core, bool* success) { long ret; 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; return 0; } cpu_implementer = (uint32_t) ret; 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; return 0; } cpu_architecture = (uint32_t) 0xF; // Why? 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; return 0; } cpu_variant = (uint32_t) ret; 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; return 0; } cpu_part = (uint32_t) ret; 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; return 0; } @@ -155,7 +155,7 @@ char* get_field_from_cpuinfo(char* CPUINFO_FIELD) { int filelen; char* buf; if((buf = read_file(_PATH_CPUINFO, &filelen)) == NULL) { - perror("open"); + printWarn("read_file: %s: %s:\n", _PATH_CPUINFO, strerror(errno)); return NULL; } diff --git a/src/common/udev.c b/src/common/udev.c index 3f72467..bfcf4da 100644 --- a/src/common/udev.c +++ b/src/common/udev.c @@ -44,8 +44,7 @@ long get_freq_from_file(char* path, bool hv_present) { errno = 0; long ret = strtol(buf, &end, 10); if(errno != 0) { - perror("strtol"); - printBug("Failed parsing '%s' file. Read data was: '%s'", path, buf); + printBug("strtol: %s", strerror(errno)); free(buf); return UNKNOWN_FREQ; } @@ -77,8 +76,7 @@ long get_cache_size_from_file(char* path) { errno = 0; long ret = strtol(buf, &end, 10); if(errno != 0) { - perror("strtol"); - printBug("Failed parsing '%s' file. Read data was: '%s'", path, buf); + printBug("strtol: %s", strerror(errno)); free(buf); return -1; } @@ -146,8 +144,7 @@ int get_num_caches_from_files(char** paths, int num_paths) { errno = 0; long ret = strtol(buf, &end, 16); if(errno != 0) { - perror("strtol"); - printBug("Failed parsing '%s' file. Read data was: '%s'", paths[i], buf); + printBug("strtol: %s", strerror(errno)); free(buf); return -1; } diff --git a/src/ppc/ppc.c b/src/ppc/ppc.c index 5fddf4c..3d8a12a 100644 --- a/src/ppc/ppc.c +++ b/src/ppc/ppc.c @@ -50,8 +50,8 @@ struct topology* get_topology_info(struct cache* cach) { // 1. Total cores detection if((topo->total_cores = sysconf(_SC_NPROCESSORS_ONLN)) == -1) { - perror("sysconf"); - return NULL; + printWarn("sysconf(_SC_NPROCESSORS_ONLN): %s", strerror(errno)); + topo->total_cores = 1; // fallback } // To find physical cores, we use topo->total_cores and core_ids diff --git a/src/x86/apic.c b/src/x86/apic.c index ad6c768..2cdf5ce 100644 --- a/src/x86/apic.c +++ b/src/x86/apic.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "apic.h" #include "cpuid_asm.h" @@ -84,7 +85,7 @@ bool bind_to_cpu(int cpu_id) { CPU_ZERO(¤tCPU); CPU_SET(cpu_id, ¤tCPU); if (sched_setaffinity (0, sizeof(currentCPU), ¤tCPU) == -1) { - perror("sched_setaffinity"); + printWarn("sched_setaffinity: %s", strerror(errno)); return false; } return true; @@ -93,7 +94,7 @@ bool bind_to_cpu(int cpu_id) { CPU_ZERO(¤tCPU); CPU_SET(cpu_id, ¤tCPU); 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 true; diff --git a/src/x86/cpuid.c b/src/x86/cpuid.c index 66bc1fa..4483ed0 100644 --- a/src/x86/cpuid.c +++ b/src/x86/cpuid.c @@ -435,7 +435,7 @@ struct topology* get_topology_info(struct cpuInfo* cpu, struct cache* cach) { topo->total_cores = info.dwNumberOfProcessors; #else 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 } #endif