diff options
author | PA4WDH | 2023-05-20 17:17:43 +0200 |
---|---|---|
committer | PA4WDH | 2023-05-20 17:17:43 +0200 |
commit | 15d9095122c0d4f9a4dab05fb0e84d84c624e1b4 (patch) | |
tree | c6edcc0b2efe1dc68577d57de9de74ae4bf8cdd4 /cputemp2maxfreq.c | |
parent | Initial commit, POC shell script (diff) | |
download | cputemp2maxfreq-15d9095122c0d4f9a4dab05fb0e84d84c624e1b4.tar.gz cputemp2maxfreq-15d9095122c0d4f9a4dab05fb0e84d84c624e1b4.tar.bz2 cputemp2maxfreq-15d9095122c0d4f9a4dab05fb0e84d84c624e1b4.zip |
Translate POC to C code0.1
Diffstat (limited to 'cputemp2maxfreq.c')
-rw-r--r-- | cputemp2maxfreq.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/cputemp2maxfreq.c b/cputemp2maxfreq.c new file mode 100644 index 0000000..1760996 --- /dev/null +++ b/cputemp2maxfreq.c @@ -0,0 +1,75 @@ +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include "sysfs.h" +#include "cpufreq.h" +#include "debug.h" + +struct s_cpudata { + long int min_freq; + long int max_freq; + long int cur_freq; + long int cur_temp; + long int scale_max; +}; + +struct s_config { + char *governor; + long int max_temp; + char *temp_input; + long int freq_step; + long int fallback_freq; + unsigned int interval; +}; + +struct s_cpudata cpudata; +struct s_config config={"conservative",70000,"/sys/devices/virtual/thermal/thermal_zone0/temp",100000,2000000,10}; + +int main() +{ + long int diff; + long int newfreq; + + printf("Governor: %s\nTemp input: %s\n",config.governor,config.temp_input); + + if (cpufreq_set_str("scaling_governor",config.governor,0)!=0) + { + printf("Failed to set governow\n"); + exit(1); + } + + cpudata.min_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/cpuinfo_min_freq"); + cpudata.max_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/cpuinfo_max_freq"); + cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq"); + + while(1) + { + cpudata.cur_freq=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq"); + cpudata.cur_temp=sysfs_read_long_int(config.temp_input); + + + DEBUG1_MAIN("Data: %ld %ld %ld %ld %ld\n",cpudata.cur_temp,config.max_temp,cpudata.max_freq,cpudata.scale_max,cpudata.cur_freq); + + if ((cpudata.cur_temp<config.max_temp) && (cpudata.scale_max<cpudata.max_freq)) + { + diff=(config.max_temp-cpudata.cur_temp)/1000; + newfreq=cpudata.scale_max+(config.freq_step*diff); + if (newfreq>cpudata.max_freq) newfreq=cpudata.max_freq; + DEBUG1_MAIN("Increase to %ld\n",newfreq); + cpufreq_set_long_int("scaling_max_freq",newfreq,100); + cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq"); + } + + if ((cpudata.cur_temp>config.max_temp) && (cpudata.scale_max>cpudata.min_freq)) + { + diff=(cpudata.cur_temp-config.max_temp)/1000; + newfreq=cpudata.scale_max-(config.freq_step*diff); + if (newfreq<cpudata.min_freq) newfreq=cpudata.min_freq; + DEBUG1_MAIN("Decrease to %ld\n",newfreq); + cpufreq_set_long_int("scaling_max_freq",newfreq,100); + cpudata.scale_max=sysfs_read_long_int("/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq"); + } + + sleep(config.interval); + } +} |