/* * (c) Bernd 'Aard' Wachter, * You may use and redistribute this file under the terms and conditions * of the GNU GPL v2 * * Install: * - Compile with cc cpu.c -o cpu * - Copy cpu into some directory in your searchpath (e.g. /usr/bin) * - Create links named clo and chi pointing to cpu * * Usage: * - call cpu to view the current governor and cpu freq * - call clo or cpu lo to set the powersave governor * - call chi or cpu hi to set the performance governor * */ #include #define GV_PATH "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor" #define FQ_PATH "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq" void msg(char *msg){ write(1,msg,strlen(msg)); } int wrgov(char *gov){ int fd; if ((fd=open(GV_PATH, O_WRONLY))==-1){ msg("Cannot open governor file\n"); return -1; } write(fd,gov,strlen(gov)); if (close(fd)==-1) { msg("Problem closing governor file\n"); return -1; } return 0; } int rdgov(char *gov){ int fd,i; char buf[512]; if ((fd=open(gov, O_RDONLY))==-1){ msg("Cannot open governor file\n"); return -1; } if ((i=read(fd,buf,512))==-1){ msg("Error while reading file\n"); return -1; } buf[i]='\0'; msg(buf); if (close(fd)==-1) { msg("Problem closing governor file\n"); return -1; } } int main(int argc, char** argv) { setuid(geteuid()); if (argv[1]==0) argv[1]=""; if (!strcmp(argv[0],"clo") || !strcmp(argv[1],"lo")){ msg("Setting cpu to lo\n"); if (wrgov("powersave")==-1) return -1; } else if (!strcmp(argv[0],"chi") || !strcmp(argv[1],"hi")){ msg("Setting cpu to hi\n"); if (wrgov("performance")==-1) return -1; } else { msg("Current governor: "); if (rdgov(GV_PATH)==-1) return -1; msg("Current freq: "); if (rdgov(FQ_PATH)==-1) return -1; } }