From 03d8eb4eaf05e5f9929a765e04600dbcace8015f Mon Sep 17 00:00:00 2001 From: PA4WDH Date: Thu, 12 Sep 2024 13:34:54 +0200 Subject: Add cpufreq_get_long_int as generic function to get cpufreq data --- cpufreq.c | 8 ++++++++ cpufreq.h | 1 + cputemp2maxfreq.c | 15 +++++---------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cpufreq.c b/cpufreq.c index 842ef2c..ffe0269 100644 --- a/cpufreq.c +++ b/cpufreq.c @@ -69,3 +69,11 @@ int cpufreq_set_long_int(char *parameter,long int value,long int checkdelay) snprintf(buf,255,"%ld",value); return cpufreq_set_str(parameter,buf,checkdelay); } + +long int cpufreq_get_long_int(char *parameter) +{ + char sysfs_file[128]; + sprintf(sysfs_file,"/sys/devices/system/cpu/%s/cpufreq/%s",cpudata.cpulist[0],parameter); + return sysfs_read_long_int(sysfs_file); +} + diff --git a/cpufreq.h b/cpufreq.h index 9e4feca..8e8d74c 100644 --- a/cpufreq.h +++ b/cpufreq.h @@ -21,5 +21,6 @@ int cpufreq_set_str(char *parameter,char *value,long int checkdelay); int cpufreq_set_long_int(char *parameter,long int value,long int checkdelay); +long int cpufreq_get_long_int(char *parameter); #endif diff --git a/cputemp2maxfreq.c b/cputemp2maxfreq.c index 434fb70..d146158 100644 --- a/cputemp2maxfreq.c +++ b/cputemp2maxfreq.c @@ -92,7 +92,6 @@ int main(int argc,char **argv) struct s_sensor sensor; char *transition_latency_remark; char sensor_string[20]; - char sysfs_file[128]; argparse(argc,argv); config.logger("%s version %s, buildtime %s %s",config.name,version(),__DATE__,__TIME__); @@ -205,17 +204,13 @@ int main(int argc,char **argv) } // Get and validate CPU data - sprintf(sysfs_file,"/sys/devices/system/cpu/%s/cpufreq/cpuinfo_min_freq",cpudata.cpulist[0]); - cpudata.min_freq=sysfs_read_long_int(sysfs_file); - sprintf(sysfs_file,"/sys/devices/system/cpu/%s/cpufreq/cpuinfo_max_freq",cpudata.cpulist[0]); - cpudata.max_freq=sysfs_read_long_int(sysfs_file); - sprintf(sysfs_file,"/sys/devices/system/cpu/%s/cpufreq/scaling_max_freq",cpudata.cpulist[0]); - cpudata.scale_max=sysfs_read_long_int(sysfs_file); + cpudata.min_freq=cpufreq_get_long_int("cpuinfo_min_freq"); + cpudata.max_freq=cpufreq_get_long_int("cpuinfo_max_freq"); + cpudata.scale_max=cpufreq_get_long_int("scaling_max_freq"); if (config.transition_latency<0) { transition_latency_remark=transition_latency_remarks[0]; - sprintf(sysfs_file,"/sys/devices/system/cpu/%s/cpufreq/cpuinfo_transition_latency",cpudata.cpulist[0]); - cpudata.transition_latency=sysfs_read_long_int(sysfs_file); + cpudata.transition_latency=cpufreq_get_long_int("cpuinfo_transition_latency"); if (cpudata.transition_latency<1000) transition_latency_remark=transition_latency_remarks[2]; } else { transition_latency_remark=transition_latency_remarks[1]; @@ -275,7 +270,7 @@ int main(int argc,char **argv) while(1) { // Get new measurements - cpudata.cur_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq"); + cpudata.cur_freq=cpufreq_get_long_int("scaling_cur_freq"); if ((cpudata.cur_freqVALID_FREQ_MAX)) { config.logger("Invalid current frequency reported by CPU, exiting"); -- cgit v1.2.3