From 66aca8aa35bc0b07c754d9ad95c808e4d91f58f1 Mon Sep 17 00:00:00 2001 From: PA4WDH Date: Sat, 24 Aug 2024 16:07:11 +0200 Subject: Add multi-CPU support --- cpulist.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 cpulist.c (limited to 'cpulist.c') diff --git a/cpulist.c b/cpulist.c new file mode 100644 index 0000000..307638d --- /dev/null +++ b/cpulist.c @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "debug.h" +#include "sysfs.h" +#include "cputemp2maxfreq.h" + +extern struct s_config config; +extern struct s_cpudata cpudata; + +// Add a CPU to the list +void cpulist_add(char *cpu) +{ + cpudata.cpulist_len++; + cpudata.cpulist=reallocarray(cpudata.cpulist,cpudata.cpulist_len,sizeof(char *)); + cpudata.cpulist[cpudata.cpulist_len-1]=malloc(strlen(cpu)+1); + strcpy(cpudata.cpulist[cpudata.cpulist_len-1],cpu); + cpudata.cpulist[cpudata.cpulist_len-1][strlen(cpu)]=0; +} + +// Validate if we found a file named cpu[0-9][0-9][0-9] +int cpulist_is_cpu(char *name) +{ + if (strncmp(name,"cpu",3)!=0) return 0; + if ((name[3]<'0') || (name[3]>'9')) return 0; + if (name[4]==0) return 1; + + if ((name[4]<'0') || (name[4]>'9')) return 0; + if (name[5]==0) return 1; + + if ((name[5]<'0') || (name[5]>'9')) return 0; + if (name[6]==0) return 1; + + return 0; +} + +// Set a cpufreq parameter to a value +int cpulist_find_cpus() +{ + DIR *cpudir; + struct dirent *cpu_dirent; + char sysfs_file[128]; + int package; + + DEBUG1_CPULIST("Started\n") + + if (cpudata.cpulist!=NULL) + { + DEBUG1_CPULIST("cpulist already set, free list first\n"); + return -1; + } + cpudata.cpulist_len=0; + +// Open the CPU directory in sysfs + cpudir=opendir("/sys/devices/system/cpu"); + if (cpudir==NULL) + { + DEBUG1_CPULIST("Unable to find CPU's\n"); + return -1; + } + + cpu_dirent=readdir(cpudir); + while(cpu_dirent!=NULL) + { + +// Check if it's a CPU and it belongs to the correct physical package + if (cpulist_is_cpu(cpu_dirent->d_name)) + { + DEBUG2_CPULIST("Found CPU %s\n",cpu_dirent->d_name); + snprintf(sysfs_file,128,"/sys/devices/system/cpu/%s/topology/physical_package_id",cpu_dirent->d_name); + package=sysfs_read_long_int(sysfs_file); + DEBUG2_CPULIST("CPU Belongs to package id %d\n",package); + if ((package==config.cpu) || (config.cpu<0)) + { + DEBUG2_CPULIST("Add to list\n"); + cpulist_add(cpu_dirent->d_name); + } else { + DEBUG2_CPULIST("Skip this CPU\n"); + } + } + + cpu_dirent=readdir(cpudir); + } + closedir(cpudir); + + DEBUG1_CPULIST("Found %d CPU's\n",config.cpulist_len); + if (cpudata.cpulist_len==0) + { + config.logger("Warning: Found no CPU's beloning to physical CPU %d",config.cpu); + } + + return 0; +} + +void cpulist_free() +{ + int count; + if (cpudata.cpulist==NULL) return; + + for(count=0;count