Skip to content

Instantly share code, notes, and snippets.

@gomasy
Last active March 24, 2022 00:54
Show Gist options
  • Save gomasy/f974ada639aa08ef75086f214703c5f0 to your computer and use it in GitHub Desktop.
Save gomasy/f974ada639aa08ef75086f214703c5f0 to your computer and use it in GitHub Desktop.
CPU governor changer for Network UPS Tools
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
const char basepath[] = "/sys/devices/system/cpu/cpu%d/cpufreq/%s";
void update_governor(int cpu, char *gov) {
FILE *fp;
char path[64];
sprintf(path, basepath, cpu, "scaling_governor");
fp = fopen(path, "a");
if (!fp) {
return;
}
fwrite(gov, 1, 9, fp);
fclose(fp);
}
int main(int argc, char *argv[]) {
char *type;
int cpus = sysconf(_SC_NPROCESSORS_CONF);
if (!(type = getenv("NOTIFYTYPE"))) {
return 1;
}
for (int i = 0; i < cpus; i++) {
if (!strcmp(type, "ONLINE")) {
update_governor(i, "schedutil");
} else if (!strcmp(type, "ONBATT")) {
update_governor(i, "powersave");
} else {
return 1;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment