diff --git a/src/homemade_speedstep.c b/src/homemade_speedstep.c index b507979..e1a5583 100644 --- a/src/homemade_speedstep.c +++ b/src/homemade_speedstep.c @@ -4,11 +4,26 @@ #define VERSION "1.0" #define POLLING_TIME 1 -#define CURGOV "/sys/devices/system/cpu/cpufreq/policy0/scaling_governor" +#define THREADS 4 /* TODO: automatically figure out how many threads host system has */ #define FREQS "/sys/devices/system/cpu/cpufreq/policy0/scaling_available_frequencies" -#define MAXSPDPATH "/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq" +#define MAXSPDPREFIX "/sys/devices/system/cpu/cpu" +#define MAXSPDSUFFIX "/cpufreq/scaling_max_freq" #define CURTEMPPATH "/sys/devices/virtual/thermal/thermal_zone0/temp" +void changespeed(int threads, char** steps, int curstep){ + int i; + char filename[100]; + FILE* file; + for (i=0; i < threads; i++){ + sprintf(filename, MAXSPDPREFIX"%d"MAXSPDSUFFIX, i); + file = fopen(filename, "w"); + if (file != NULL){ + fprintf(file, "%s\n", steps[curstep]); + fclose(file); + } + } +} + int comparestrings(char *a, char *b){ int c = 0; while(a[c] != '\0' && b[c] != '\0'){ @@ -157,7 +172,7 @@ int main(int argc, char** argv){ free(freqsfilec); fclose(freqsfile); /* try to open scaling_maximum_frequency, exit on fail */ - FILE* curmaxfile = fopen(MAXSPDPATH, "r"); + FILE* curmaxfile = fopen(MAXSPDPREFIX"0"MAXSPDSUFFIX, "r"); if (curmaxfile == NULL){ fprintf(stderr, "error: couldn't open scaling_maximum_frequency\n"); fprintf(stderr, " check that you have support for cpufreq\n"); @@ -202,46 +217,19 @@ int main(int argc, char** argv){ /* check if current temp is higher than max and step down */ if (temperature >= maxtemp && curstep < stepc - 1){ curstep++; - curmaxfile = fopen(MAXSPDPATH, "w"); - if (curmaxfile == NULL){ - fprintf(stderr, "error: couldn't open scaling_maximum_frequency\n"); - fprintf(stderr, " check that you have support for cpufreq\n"); - fprintf(stderr, " in your kernel and that the program is\n"); - fprintf(stderr, " running as root\n"); - return 6; - } - fprintf(curmaxfile, "%s\n", steps[curstep]); - fclose(curmaxfile); + changespeed(THREADS, steps, curstep); timepassed = 0; } /* check if current temp is lower than min and step down */ if (temperature <= mintemp && curstep > 0){ curstep--; - curmaxfile = fopen(MAXSPDPATH, "w"); - if (curmaxfile == NULL){ - fprintf(stderr, "error: couldn't open scaling_maximum_frequency\n"); - fprintf(stderr, " check that you have support for cpufreq\n"); - fprintf(stderr, " in your kernel and that the program is\n"); - fprintf(stderr, " running as root\n"); - return 6; - } - fprintf(curmaxfile, "%s\n", steps[curstep]); - fclose(curmaxfile); + changespeed(THREADS, steps, curstep); timepassed = 0; } /* check if a minute passed without step changes and temp is below minmax average */ if (timepassed > 60 && temperature <= (mintemp+maxtemp)/2 && curstep > 0){ curstep--; - curmaxfile = fopen(MAXSPDPATH, "w"); - if (curmaxfile == NULL){ - fprintf(stderr, "error: couldn't open scaling_maximum_frequency\n"); - fprintf(stderr, " check that you have support for cpufreq\n"); - fprintf(stderr, " in your kernel and that the program is\n"); - fprintf(stderr, " running as root\n"); - return 6; - } - fprintf(curmaxfile, "%s\n", steps[curstep]); - fclose(curmaxfile); + changespeed(THREADS, steps, curstep); timepassed = 0; } free(char_temperature);