int main (int argn, char** argv) { int cpuid; int freq; int numCPUs = 0; char* gov; char* gpath = malloc(100); char* fpath = malloc(100); if (argn < 3 || argn > 4) { fprintf(stderr, "Usage: %s <processorID> <frequency> [<governor>] \n",argv[0]); exit(EXIT_FAILURE); } cpuid = atoi(argv[1]); numCPUs = get_numCPUs(); if (cpuid < 0 || cpuid > numCPUs) { fprintf(stderr, "CPU %d not a valid CPU ID. Range from 0 to %d.\n",cpuid,numCPUs); exit(EXIT_FAILURE); } freq = atoi(argv[2]); if (freq <= 0) { fprintf(stderr, "Frequency must be greater than 0.\n"); exit(EXIT_FAILURE); } if (argn == 4) { gov = argv[3]; if ((strncmp(gov,"ondemand",12)) && (strncmp(gov,"performance",12))) { fprintf(stderr, "Invalid governor %s!\n",gov); return (EXIT_FAILURE); } snprintf(gpath, 60, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", cpuid); FILE* f = fopen(gpath, "w"); if (f == NULL) { fprintf(stderr, "Unable to open path for writing\n"); return (EXIT_FAILURE); } fprintf(f,"%s",gov); fclose(f); return(EXIT_SUCCESS); } snprintf(gpath, 60, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", cpuid); snprintf(fpath, 60, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_setspeed", cpuid); FILE* f = fopen(gpath, "w"); if (f == NULL) { fprintf(stderr, "Unable to open path for writing\n"); return (EXIT_FAILURE); } fprintf(f,"userspace"); fclose(f); f = fopen(fpath, "w"); if (f == NULL) { fprintf(stderr, "Unable to open path for writing\n"); return (EXIT_FAILURE); } fprintf(f,"%d",freq); fclose(f); return(EXIT_SUCCESS); }
/* ##### MAIN FUNCTION DEFINITION ################## */ int main (int argn, char** argv) { int i = 0; int tmp; int cpuid; int freq = 0; int numCPUs = 0; char* gov; char* gpath = malloc(100); char* fpath = malloc(100); if (argn < 3 || argn > 4) { fprintf(stderr, "Usage: %s <processorID> <frequency> [<governor>] \n",argv[0]); free(gpath); free(fpath); exit(EXIT_FAILURE); } cpuid = atoi(argv[1]); numCPUs = get_numCPUs(); if (cpuid < 0 || cpuid > numCPUs) { fprintf(stderr, "CPU %d not a valid CPU ID. Range from 0 to %d.\n", cpuid, numCPUs); free(gpath); free(fpath); exit(EXIT_FAILURE); } freq = atoi(argv[2]); if (freq <= 0) { fprintf(stderr, "Frequency must be greater than 0.\n"); free(gpath); free(fpath); exit(EXIT_FAILURE); } if (argn == 4) { FILE* f; gov = argv[3]; if ((strncmp(gov,"ondemand",8) != 0) && (strncmp(gov,"performance",11) != 0) && (strncmp(gov,"conservative",12) != 0) && (strncmp(gov,"powersave",9) != 0)) { fprintf(stderr, "Invalid governor %s!\n",gov); free(gpath); free(fpath); return (EXIT_FAILURE); } for (i=0; i<2; i++) { snprintf(fpath, 99, "/sys/devices/system/cpu/cpu%d/cpufreq/%s", cpuid, getfiles[i]); f = fopen(fpath, "r"); if (f == NULL) { fprintf(stderr, "Unable to open path %s for writing\n", fpath); free(gpath); free(fpath); return (EXIT_FAILURE); } tmp = fread(fpath, 100, sizeof(char), f); freq = atoi(fpath); fclose(f); snprintf(fpath, 99, "/sys/devices/system/cpu/cpu%d/cpufreq/%s", cpuid, setfiles[i]); f = fopen(fpath, "w"); if (f == NULL) { fprintf(stderr, "Unable to open path %s for writing\n",fpath); free(gpath); free(fpath); return (EXIT_FAILURE); } fprintf(f,"%d",freq); fclose(f); } snprintf(gpath, 99, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", cpuid); f = fopen(gpath, "w"); if (f == NULL) { fprintf(stderr, "Unable to open path %s for writing\n", gpath); free(gpath); free(fpath); return (EXIT_FAILURE); } fprintf(f,"%s",gov); fclose(f); free(gpath); free(fpath); return(EXIT_SUCCESS); } snprintf(gpath, 99, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", cpuid); FILE* f = fopen(gpath, "w"); if (f == NULL) { fprintf(stderr, "Unable to open path %s for writing\n", gpath); free(gpath); free(fpath); return (EXIT_FAILURE); } if ((argn == 4) && ((strncmp(argv[3],"ondemand",8) == 0) || (strncmp(argv[3],"performance",11) == 0) || (strncmp(argv[3],"conservative",12) == 0) || (strncmp(argv[3],"powersave",9) == 0))) { fprintf(f, "%s", argv[3]); tmp = 1; } else { fprintf(f, "%s", "userspace"); tmp = 3; } fclose(f); for (i=0;i<tmp;i++) { snprintf(fpath, 99, "/sys/devices/system/cpu/cpu%d/cpufreq/%s", cpuid, setfiles[i]); f = fopen(fpath, "w"); if (f == NULL) { fprintf(stderr, "Unable to open path %s for writing\n",fpath); free(gpath); free(fpath); return (EXIT_FAILURE); } fprintf(f,"%d",freq); fclose(f); } free(gpath); free(fpath); return(EXIT_SUCCESS); }