From 050735537217b83e18197915674cfbff67485b9e Mon Sep 17 00:00:00 2001 From: Dr-Noob Date: Mon, 26 Aug 2024 12:31:18 +0100 Subject: [PATCH] [v1.06][X86] Bugfix: set affinity in --accurate-pp There are cases where measure_frequency is called after binding the process to a specific core via bind_to_cpu (e.g., when iterating over modules in hybrid architectures). Thus, in measure_frequency we must set the affinity of the newly created threads, ensuring they are binded to the right core. --- src/x86/freq/freq.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/x86/freq/freq.c b/src/x86/freq/freq.c index def701b..615b66a 100644 --- a/src/x86/freq/freq.c +++ b/src/x86/freq/freq.c @@ -116,8 +116,25 @@ int64_t measure_frequency(struct cpuInfo* cpu) { } pthread_t* compute_th = malloc(sizeof(pthread_t) * cpu->topo->total_cores); + cpu_set_t cpus; + pthread_attr_t attr; + if ((ret = pthread_attr_init(&attr)) != 0) { + printErr("pthread_attr_init: %s", strerror(ret)); + return -1; + } + for(int i=0; i < cpu->topo->total_cores; i++) { - ret = pthread_create(&compute_th[i], NULL, compute_function, NULL); + // We might have called bind_to_cpu previously, binding the threads + // to a specific core, so now we must make sure we run the new thread + // on the correct core. + CPU_ZERO(&cpus); + CPU_SET(i, &cpus); + if ((ret = pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpus)) != 0) { + printErr("pthread_attr_setaffinity_np: %s", strerror(ret)); + return -1; + } + + ret = pthread_create(&compute_th[i], &attr, compute_function, NULL); if(ret != 0) { fprintf(stderr, "Error creating thread\n");