From d4cadbd807686673ef8cd9e2424c1ef2936512ca Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Fri, 5 Jul 2024 08:32:11 +0100 Subject: [PATCH] [v1.05] Move bind_to_cpu from x86-specific to global (merging measure-freq #220) --- src/common/global.c | 39 +++++++++++++++++++++++++++++++++++++++ src/common/global.h | 3 +++ src/x86/apic.c | 28 ---------------------------- src/x86/apic.h | 4 ---- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/common/global.c b/src/common/global.c index 1b1b87d..96c91af 100644 --- a/src/common/global.c +++ b/src/common/global.c @@ -1,3 +1,14 @@ +#ifdef _WIN32 + #define NOMINMAX + #include +#elif defined __linux__ + #define _GNU_SOURCE + #include +#elif defined __FreeBSD__ + #include + #include +#endif + #include #include #include @@ -199,6 +210,34 @@ void* erealloc(void *ptr, size_t size) { return newptr; } +#ifndef __APPLE__ +bool bind_to_cpu(int cpu_id) { + #ifdef _WIN32 + HANDLE process = GetCurrentProcess(); + DWORD_PTR processAffinityMask = 1 << cpu_id; + return SetProcessAffinityMask(process, processAffinityMask); + #elif defined __linux__ + cpu_set_t currentCPU; + CPU_ZERO(¤tCPU); + CPU_SET(cpu_id, ¤tCPU); + if (sched_setaffinity (0, sizeof(currentCPU), ¤tCPU) == -1) { + printWarn("sched_setaffinity: %s", strerror(errno)); + return false; + } + return true; + #elif defined __FreeBSD__ + cpuset_t currentCPU; + CPU_ZERO(¤tCPU); + CPU_SET(cpu_id, ¤tCPU); + if(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset_t), ¤tCPU) == -1) { + printWarn("cpuset_setaffinity: %s", strerror(errno)); + return false; + } + return true; + #endif +} +#endif + void print_version(FILE *restrict stream) { #ifdef GIT_FULL_VERSION fprintf(stream, "cpufetch %s (%s %s)\n", GIT_FULL_VERSION, OS_STR, ARCH_STR); diff --git a/src/common/global.h b/src/common/global.h index 69ead1d..8e333fc 100644 --- a/src/common/global.h +++ b/src/common/global.h @@ -19,6 +19,9 @@ char *strremove(char *str, const char *sub); void* emalloc(size_t size); void* ecalloc(size_t nmemb, size_t size); void* erealloc(void *ptr, size_t size); +#ifndef __APPLE__ +bool bind_to_cpu(int cpu_id); +#endif void print_version(FILE *restrict stream); #endif diff --git a/src/x86/apic.c b/src/x86/apic.c index 1757110..2892468 100644 --- a/src/x86/apic.c +++ b/src/x86/apic.c @@ -72,34 +72,6 @@ uint32_t get_apic_id(bool x2apic_id) { } } -#ifndef __APPLE__ -bool bind_to_cpu(int cpu_id) { - #ifdef _WIN32 - HANDLE process = GetCurrentProcess(); - DWORD_PTR processAffinityMask = 1 << cpu_id; - return SetProcessAffinityMask(process, processAffinityMask); - #elif defined __linux__ - cpu_set_t currentCPU; - CPU_ZERO(¤tCPU); - CPU_SET(cpu_id, ¤tCPU); - if (sched_setaffinity (0, sizeof(currentCPU), ¤tCPU) == -1) { - printWarn("sched_setaffinity: %s", strerror(errno)); - return false; - } - return true; - #elif defined __FreeBSD__ - cpuset_t currentCPU; - CPU_ZERO(¤tCPU); - CPU_SET(cpu_id, ¤tCPU); - if(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset_t), ¤tCPU) == -1) { - printWarn("cpuset_setaffinity: %s", strerror(errno)); - return false; - } - return true; - #endif -} -#endif - #ifdef __linux__ int get_total_cores_module(int total_cores, int module) { int total_modules = 2; diff --git a/src/x86/apic.h b/src/x86/apic.h index 0f79995..c104ea5 100644 --- a/src/x86/apic.h +++ b/src/x86/apic.h @@ -17,10 +17,6 @@ struct apic { bool get_topology_from_apic(struct cpuInfo* cpu, struct topology* topo); uint32_t is_smt_enabled_amd(struct topology* topo); -#ifndef __APPLE__ -bool bind_to_cpu(int cpu_id); -#endif - #ifdef __linux__ int get_total_cores_module(int total_cores, int module); #endif