#include #include #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" #define CURTEMPPATH "/sys/devices/virtual/thermal/thermal_zone0/temp" int comparestrings(char *a, char *b){ int c = 0; while(a[c] != '\0' && b[c] != '\0'){ if(a[c] != b[c]){ return 1; } c++; } return 0; } int getfilec(FILE *file, char arr[]){ int c, i; i = 0; while((c = fgetc(file)) != EOF){ arr[i] = c; i++; } arr[i] = '\0'; return i; } int powerof(int base, int exponent){ int i, result; if (exponent == 0){ return result = 1; } result = base; for(i = 0; i < exponent - 1; i++){ result *= base; } return result; } int stringtoint(char number[], int length){ int j, result; result = 0; j = 0; while(length - 1 >= 0){ result += ((int)number[length - 1] - '0') * (powerof(10, j)); j++; length--; } return result; } int stringlength(char string[]){ int i = 0; while(string[i] != '\0'){ i++; } return i; } int splitstr(char** dest, char line[], int index1, int index2){ int offset = index1; while (line[index1] != ' ' && index1 < stringlength(line)){ index1++; } char* p = malloc(sizeof (char)*index1); int i; for(i = 0; i < index1 - offset; i++){ p[i] = line[i + offset]; } p[i] = '\0'; dest[index2] = p; return index1 + 1; } int countblanks(char line[]){ int i = 0; int j = 0; while(line[i] != '\0'){ if (line[i] == ' '){ j++; } i++; } return j; } void newlinetonullbyte(char string[]){ int i = 0; while (string[i] != '\0'){ if (string[i] == '\n'){ string[i] = '\0'; break; } i++; } } int main(int argc, char** argv){ /* check for no arguments or help */ if (argc == 1 || comparestrings(argv[1], "-h") == 0 || comparestrings(argv[1], "--help") == 0){ fprintf(stderr, "usage: homemade_speedstep \n"); fprintf(stderr, " homemade_speedstep [-h | --help]\n"); fprintf(stderr, " -h | --help prints out this help message\n"); return 0; } /* check for correct ammount of arguments */ if (argc != 3){ fprintf(stderr, "error: wrong ammount of arguments given\n"); fprintf(stderr, " see homemade_speedstep --help\n"); return 1; } /* set maxtemp and mintemp TODO: check for integer and difference */ const int maxtemp = stringtoint(argv[1], stringlength(argv[1])); const int mintemp = stringtoint(argv[2], stringlength(argv[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 2; } /* 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 3; } /* 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){ fprintf(stderr, "error: couldn't open scaling_available_frequencies\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 4; } /* assign content of freqsfile to a temporary char array */ char* freqsfilec = malloc(sizeof(char) * 200); getfilec(freqsfile, freqsfilec); int stepc = countblanks(freqsfilec); /* create a char ** array with pointers to freq steps */ char ** steps = malloc(sizeof(char*) * stepc); int index = 0; int i; for(i = 0; i < stepc; i++){ index = splitstr(steps, freqsfilec, index, i); } /* free memory and close file */ free(freqsfilec); fclose(freqsfile); /* try to open scaling_maximum_frequency, exit on fail */ FILE* curmaxfile = fopen(MAXSPDPATH, "r"); 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 5; } /* assign content of curmaxfile to a temporary char array */ char *curmaxfreq = malloc(sizeof(char) * 20); getfilec(curmaxfile, curmaxfreq); newlinetonullbyte(curmaxfreq); /* figure out what step we're on */ int curstep = -1; for(i = 0; i < stepc; i++){ if (comparestrings(steps[i], curmaxfreq) == 0){ curstep = i; break; } } /* free memory and close file */ free(curmaxfreq); fclose(curmaxfile); /* check if we didn't find our current step, exit on fail */ if (curstep == -1){ fprintf(stderr, "error: couldn't figure out the current 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; } 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); if (stringtoint(temperature, stringlength(temperature)) >= maxtemp && curstep < stepc - 1){ curstep++; curmaxfile = fopen(MAXSPDPATH, "w"); fprintf(curmaxfile, "%s\n", steps[curstep]); fclose(curmaxfile); } if (stringtoint(temperature, stringlength(temperature)) <= mintemp && curstep > 0){ curstep--; curmaxfile = fopen(MAXSPDPATH, "w"); fprintf(curmaxfile, "%s\n", steps[curstep]); fclose(curmaxfile); } free(temperature); fclose(curtempfile); } /* cleanup for graceful exit */ free(steps); fclose(curtempfile); return 0; }