Skip to content

Instantly share code, notes, and snippets.

@LightningStalker
Created August 27, 2017 03:49
Show Gist options
  • Select an option

  • Save LightningStalker/4a2bcc2d2555c36b430853d23ae433d6 to your computer and use it in GitHub Desktop.

Select an option

Save LightningStalker/4a2bcc2d2555c36b430853d23ae433d6 to your computer and use it in GitHub Desktop.
Sets the frequency governor for all CPUs on the system
/*Compile with gcc -Wall -s gov.c -o gov
gov by The Lightning Stalker 8/26/2017
pops open cpufreq-set for each CPU and sets governor*/
#include <stdio.h>
#include <string.h>
#define NUMCPUS 12 // number of CPUs on the system ($ lscpu)
int main(int argc, char *argv[])
{
char command[33] = "cpufreq-set -c ## -g powersave\0\0\0";
char governor[12] = "powersave\0\0\0"; // default governor is powersave
char numcpu[3] = "00\0";
FILE *stream;
if ( argc >= 2 ) strcpy(governor, "performance\0"); // CLI options were passed
strcpy(&command[21], governor);
for(int cpu = 0; cpu < NUMCPUS; cpu++)
{
sprintf(numcpu, "%02i", cpu);
// put 2 chars from numberstr into filename a position 15
strncpy(&command[15], numcpu, 2); // don't copy the trailing null
//printf("%s\n", command); // display command for debugging
stream = popen(command, "r"); // run cpufreq-set as read
// and ignore output
}
if ( pclose(stream) != 0 ) puts("Something went wrong. Are you root?");
else printf("All CPUs set to \'%s\' governor\n", governor);
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment