removed unnecesary gov check, simplified number of function calls, added timepassed check

This commit is contained in:
celso 2022-11-19 22:39:03 -03:00
parent a4290beb57
commit b83902efb9
1 changed files with 26 additions and 32 deletions

View File

@ -124,30 +124,6 @@ int main(int argc, char** argv){
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){
fprintf(stderr, "error: couldn't open scaling_current_governor\n");
fprintf(stderr, " check that you have support for cpufreq\n");
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 3;
}
/* assign content of govfile to a temporary char array */
char* gov = malloc(sizeof(char) * 20);
getfilec(govfile, gov);
/* check content of scaling_current_governor and exit on failure */
if (comparestrings(gov, "userspace\n") != 0){
fprintf(stderr, "error: userspace governor not set\n");
/* free memory and close file */
free(gov);
fclose(govfile);
return 4;
}
/* free memory and close file */
free(gov);
fclose(govfile);
/* try to open scaling_available_frequencies, exit on fail */
FILE *freqsfile = fopen(FREQS, "r");
if (freqsfile == NULL){
@ -203,15 +179,17 @@ int main(int argc, char** argv){
fprintf(stderr, " running as root\n");
return 7;
}
int timepassed = 0;
FILE* curtempfile;
/* from here onwards an infinite while loop should start */
while((curtempfile = fopen(CURTEMPPATH, "r")) != NULL){
char* temperature = malloc(sizeof(char) * 8);
getfilec(curtempfile, temperature);
newlinetonullbyte(temperature);
timepassed++;
char* char_temperature = malloc(sizeof(char) * 8);
getfilec(curtempfile, char_temperature);
newlinetonullbyte(char_temperature);
int temperature = stringtoint(temperature, stringlength(temperature));
/* check if current temp is higher than max and step down */
if (stringtoint(temperature, stringlength(temperature)) >= maxtemp
&& curstep < stepc - 1){
if (tempreature >= maxtemp && curstep < stepc - 1){
curstep++;
curmaxfile = fopen(MAXSPDPATH, "w");
if (curmaxfile == NULL){
@ -223,10 +201,10 @@ int main(int argc, char** argv){
}
fprintf(curmaxfile, "%s\n", steps[curstep]);
fclose(curmaxfile);
timepassed = 0;
}
/* check if current temp is lower than min and step down */
if (stringtoint(temperature, stringlength(temperature)) <= mintemp
&& curstep > 0){
if (temperature <= mintemp && curstep > 0){
curstep--;
curmaxfile = fopen(MAXSPDPATH, "w");
if (curmaxfile == NULL){
@ -238,8 +216,24 @@ int main(int argc, char** argv){
}
fprintf(curmaxfile, "%s\n", steps[curstep]);
fclose(curmaxfile);
timepassed = 0;
}
free(temperature);
/* check if a minute passed without step changes and temp is below minmax average */
if (timepassed > 60 && temperature <= (mintemp+maxtemp)/2 && 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);
timepassed = 0;
}
free(char_temperature);
fclose(curtempfile);
sleep(POLLING_TIME);
}