From 4ccafdc4fafcddaeaf68568de135cdf2a94c122c Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Tue, 10 Sep 2024 22:03:21 +0100 Subject: [PATCH] Refactor --- src/x86/cpuid.c | 52 ++++++++++++++++++++++----------------------- src/x86/cpuid.h | 2 +- src/x86/freq/freq.c | 10 ++++----- src/x86/freq/freq.h | 2 +- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/x86/cpuid.c b/src/x86/cpuid.c index cfe5f81..7281836 100644 --- a/src/x86/cpuid.c +++ b/src/x86/cpuid.c @@ -446,6 +446,23 @@ int32_t get_core_type(void) { } } +#ifdef __linux__ +// Gets the max frequency for estimating the peak performance +// and fills in the passed cpuInfo parameter. +void fill_frequency_info_pp(struct cpuInfo* cpu) { + int32_t unused; + int32_t *max_freq_pp_vec = malloc(sizeof(int32_t) * cpu->num_cpus); + struct cpuInfo* ptr = cpu; + + for (uint32_t i=0; i < cpu->num_cpus; i++) { + set_cpu_module(i, cpu->num_cpus, &unused); + + cpu->freq->max_pp = measure_frequency(cpu, max_freq_pp_vec); + ptr = ptr->next_cpu; + } +} +#endif + struct cpuInfo* get_cpu_info(void) { struct cpuInfo* cpu = emalloc(sizeof(struct cpuInfo)); cpu->peak_performance = -1; @@ -546,9 +563,7 @@ struct cpuInfo* get_cpu_info(void) { ptr->feat = get_features_info(ptr); ptr->arch = get_cpu_uarch(ptr); - // If accurate_pp is not requested, get it now. - if (!accurate_pp()) - ptr->freq = get_frequency_info(ptr, false, NULL); + ptr->freq = get_frequency_info(ptr); if (cpu->cpu_name == NULL && ptr == cpu) { // If we couldnt read CPU name from cpuid, infer it now @@ -569,21 +584,12 @@ struct cpuInfo* get_cpu_info(void) { if(ptr->topo == NULL) return cpu; } - // If accurate_pp is requested, we need to get the frequency - // after fetching the topology for all CPU modules (this information - // is required by get_frequency_info) - if (accurate_pp()) { - int32_t unused; - int32_t *max_freq_pp_vec = NULL; - ptr = cpu; - - for (uint32_t i=0; i < cpu->num_cpus; i++) { - set_cpu_module(i, cpu->num_cpus, &unused); - - ptr->freq = get_frequency_info(ptr, accurate_pp(), &max_freq_pp_vec); - ptr = ptr->next_cpu; - } - } +#ifdef __linux__ + // If accurate_pp is requested, we need to get the max frequency + // after fetching the topology for all CPU modules, since the topology + // is required by fill_frequency_info_pp + if (accurate_pp()) fill_frequency_info_pp(cpu); +#endif cpu->peak_performance = get_peak_performance(cpu, accurate_pp()); @@ -950,7 +956,7 @@ struct cache* get_cache_info(struct cpuInfo* cpu) { return cach; } -struct frequency* get_frequency_info(struct cpuInfo* cpu, bool accurate_pp, int32_t **max_freq_pp_vec) { +struct frequency* get_frequency_info(struct cpuInfo* cpu) { struct frequency* freq = emalloc(sizeof(struct frequency)); freq->measured = false; @@ -1021,14 +1027,6 @@ struct frequency* get_frequency_info(struct cpuInfo* cpu, bool accurate_pp, int3 #endif freq->max_pp = UNKNOWN_DATA; - #ifdef __linux__ - if(accurate_pp) - freq->max_pp = measure_frequency(cpu, max_freq_pp_vec); - #else - // Silence compiler warning - (void)(accurate_pp); - #endif - return freq; } diff --git a/src/x86/cpuid.h b/src/x86/cpuid.h index 1c324ec..a0a6a5c 100644 --- a/src/x86/cpuid.h +++ b/src/x86/cpuid.h @@ -5,7 +5,7 @@ struct cpuInfo* get_cpu_info(void); struct cache* get_cache_info(struct cpuInfo* cpu); -struct frequency* get_frequency_info(struct cpuInfo* cpu, bool accurate_pp, int32_t **max_freq_pp_vec); +struct frequency* get_frequency_info(struct cpuInfo* cpu); struct topology* get_topology_info(struct cpuInfo* cpu, struct cache* cach, int module); char* get_str_avx(struct cpuInfo* cpu); diff --git a/src/x86/freq/freq.c b/src/x86/freq/freq.c index 2d01e5c..caa0a43 100644 --- a/src/x86/freq/freq.c +++ b/src/x86/freq/freq.c @@ -102,23 +102,21 @@ void* measure_freq(void *freq_ptr) { return NULL; } -int32_t measure_frequency(struct cpuInfo* cpu, int32_t **max_freq_pp_vec_ptr) { +int32_t measure_frequency(struct cpuInfo* cpu, int32_t *max_freq_pp_vec) { if (cpu->hybrid_flag && cpu->module_id > 0) { // We have a hybrid architecture and we have already // measured the frequency for this module in a previous // call to this function, so now just return it. - return (*max_freq_pp_vec_ptr)[cpu->module_id]; + return max_freq_pp_vec[cpu->module_id]; } - *max_freq_pp_vec_ptr = malloc(sizeof(int32_t) * cpu->num_cpus); - int ret; int num_spaces; struct freq_thread* freq_struct = malloc(sizeof(struct freq_thread)); freq_struct->end = false; freq_struct->measure = false; freq_struct->cpu = cpu; - freq_struct->max_pp = *max_freq_pp_vec_ptr; + freq_struct->max_pp = max_freq_pp_vec; void* (*compute_function)(void*); @@ -190,5 +188,5 @@ int32_t measure_frequency(struct cpuInfo* cpu, int32_t **max_freq_pp_vec_ptr) { } printf("\r%*c", num_spaces, ' '); - return (*max_freq_pp_vec_ptr)[cpu->module_id]; + return max_freq_pp_vec[cpu->module_id]; } diff --git a/src/x86/freq/freq.h b/src/x86/freq/freq.h index da30607..cc6f98c 100644 --- a/src/x86/freq/freq.h +++ b/src/x86/freq/freq.h @@ -8,6 +8,6 @@ #define MEASURE_TIME_SECONDS 5 #define LOOP_ITERS 100000000 -int32_t measure_frequency(struct cpuInfo* cpu, int32_t **max_freq_pp_vec); +int32_t measure_frequency(struct cpuInfo* cpu, int32_t *max_freq_pp_vec); #endif