fixed max_freq changes only applying to one thread

This commit is contained in:
celso 2022-11-21 12:15:32 -03:00
parent 0b8ee4b5f6
commit fab416fbb4
1 changed files with 21 additions and 33 deletions

View File

@ -4,11 +4,26 @@
#define VERSION "1.0" #define VERSION "1.0"
#define POLLING_TIME 1 #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 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" #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 comparestrings(char *a, char *b){
int c = 0; int c = 0;
while(a[c] != '\0' && b[c] != '\0'){ while(a[c] != '\0' && b[c] != '\0'){
@ -157,7 +172,7 @@ int main(int argc, char** argv){
free(freqsfilec); free(freqsfilec);
fclose(freqsfile); fclose(freqsfile);
/* try to open scaling_maximum_frequency, exit on fail */ /* try to open scaling_maximum_frequency, exit on fail */
FILE* curmaxfile = fopen(MAXSPDPATH, "r"); FILE* curmaxfile = fopen(MAXSPDPREFIX"0"MAXSPDSUFFIX, "r");
if (curmaxfile == NULL){ if (curmaxfile == NULL){
fprintf(stderr, "error: couldn't open scaling_maximum_frequency\n"); fprintf(stderr, "error: couldn't open scaling_maximum_frequency\n");
fprintf(stderr, " check that you have support for cpufreq\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 */ /* check if current temp is higher than max and step down */
if (temperature >= maxtemp && curstep < stepc - 1){ if (temperature >= maxtemp && curstep < stepc - 1){
curstep++; curstep++;
curmaxfile = fopen(MAXSPDPATH, "w"); changespeed(THREADS, steps, curstep);
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; timepassed = 0;
} }
/* check if current temp is lower than min and step down */ /* check if current temp is lower than min and step down */
if (temperature <= mintemp && curstep > 0){ if (temperature <= mintemp && curstep > 0){
curstep--; curstep--;
curmaxfile = fopen(MAXSPDPATH, "w"); changespeed(THREADS, steps, curstep);
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; timepassed = 0;
} }
/* check if a minute passed without step changes and temp is below minmax average */ /* check if a minute passed without step changes and temp is below minmax average */
if (timepassed > 60 && temperature <= (mintemp+maxtemp)/2 && curstep > 0){ if (timepassed > 60 && temperature <= (mintemp+maxtemp)/2 && curstep > 0){
curstep--; curstep--;
curmaxfile = fopen(MAXSPDPATH, "w"); changespeed(THREADS, steps, curstep);
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; timepassed = 0;
} }
free(char_temperature); free(char_temperature);