diff --git a/README.md b/README.md index 2c52b46..28a292d 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,8 @@ You must provide the temperature at which you wish to step down the maximum freq `homemade_speedstep ` +**Important**: max temp and min temp must be passed as 5 digit integers, like so: 82ÂșC = 82000 + --- **Recomendation**: add a service to your init script that runs this program at startup in the background so you don't need to keep a terminal emulator open running it. diff --git a/src/homemade_speedstep.c b/src/homemade_speedstep.c index 8dbc3f5..3385123 100644 --- a/src/homemade_speedstep.c +++ b/src/homemade_speedstep.c @@ -1,6 +1,8 @@ #include #include +#include +#define POLLING_TIME 1 #define CURGOV "/sys/devices/system/cpu/cpufreq/policy0/scaling_governor" #define FREQS "/sys/devices/system/cpu/cpufreq/policy0/scaling_available_frequencies" #define MAXSPDPATH "/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq" @@ -113,9 +115,15 @@ int main(int argc, char** argv){ fprintf(stderr, " see homemade_speedstep --help\n"); return 1; } - /* set maxtemp and mintemp TODO: check for integer and difference */ + /* set maxtemp and mintemp TODO: check for integer */ const int maxtemp = stringtoint(argv[1], stringlength(argv[1])); const int mintemp = stringtoint(argv[2], stringlength(argv[2])); + /* check that the values are, at the very least, different + and that max is higher than min */ + if (maxtemp <= mintemp){ + fprintf(stderr, "error: maxtemp must be higher than mintemp\n"); + return 2; + } /* try to open scaling_current_governor, exit on fail */ FILE* govfile = fopen(CURGOV, "r"); if (govfile == NULL){ @@ -124,7 +132,7 @@ int main(int argc, char** argv){ fprintf(stderr, " and the userspace governor in your kernel\n"); fprintf(stderr, " also check that you're running this program\n"); fprintf(stderr, " as root\n"); - return 2; + return 3; } /* assign content of govfile to a temporary char array */ char* gov = malloc(sizeof(char) * 20); @@ -135,7 +143,7 @@ int main(int argc, char** argv){ /* free memory and close file */ free(gov); fclose(govfile); - return 3; + return 4; } /* free memory and close file */ free(gov); @@ -147,7 +155,7 @@ int main(int argc, char** argv){ 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 4; + return 5; } /* assign content of freqsfile to a temporary char array */ char* freqsfilec = malloc(sizeof(char) * 200); @@ -170,7 +178,7 @@ int main(int argc, char** argv){ 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 5; + return 6; } /* assign content of curmaxfile to a temporary char array */ char *curmaxfreq = malloc(sizeof(char) * 20); @@ -193,7 +201,7 @@ int main(int argc, char** argv){ 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; + return 7; } FILE* curtempfile; /* from here onwards an infinite while loop should start */ @@ -201,22 +209,39 @@ int main(int argc, char** argv){ char* temperature = malloc(sizeof(char) * 8); getfilec(curtempfile, temperature); newlinetonullbyte(temperature); + /* check if current temp is higher than max and step down */ if (stringtoint(temperature, stringlength(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); } + /* check if current temp is lower than min and step down */ if (stringtoint(temperature, stringlength(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); } free(temperature); fclose(curtempfile); + sleep(POLLING_TIME); } /* cleanup for graceful exit */ free(steps);