1
0
mirror of https://github.com/Dejvino/pinephone-toolkit.git synced 2024-11-23 15:03:20 +00:00

Add cpu-sleep

This commit is contained in:
Dejvino 2020-05-30 21:13:26 +02:00
parent 7802ec0c8d
commit 523d304260
3 changed files with 103 additions and 2 deletions

View File

@ -24,6 +24,17 @@ $ pptk-led set red 0
$ pptk-led set flash 255 $ pptk-led set flash 255
``` ```
### cpu-sleep
Puts secondary CPUs to sleep to save energy. Wakes them up to gain more CPU power.
```
$ pptk-cpu-sleep enable
$ pptk-cpu-sleep get
enabled
$ pptk-cpu-sleep disable
$ pptk-cpu-sleep get
disabled
```
## Build & Install ## Build & Install
``` ```
$ meson build $ meson build

View File

@ -1,5 +1,8 @@
project('PinePhone Toolkit', 'c') project('PinePhone Toolkit', 'c')
install_suid = ['rwsr-xr-x', 0, 0]
executable('pptk-backlight', ['src/backlight.c'], executable('pptk-backlight', ['src/backlight.c'],
install: true, install_mode: ['rwsr-xr-x', 0, 0]) install: true, install_mode: install_suid)
executable('pptk-led', ['src/led.c'], executable('pptk-led', ['src/led.c'],
install: true, install_mode: ['rwsr-xr-x', 0, 0]) install: true, install_mode: install_suid)
executable('pptk-cpu-sleep', ['src/cpu-sleep.c'],
install: true, install_mode: install_suid)

87
src/cpu-sleep.c Normal file
View File

@ -0,0 +1,87 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static char *argv0 = "./cpu-sleep";
static char *cpu_file_template = "/sys/devices/system/cpu/cpu%d/online";
static int cpu_min = 1;
static int cpu_max = 3;
void print_usage()
{
printf("usage: %s COMMAND\n", argv0);
printf(" COMMAND = {get, enable, disable}\n");
printf(" get ... prints the current sleep state: 'enabled' or 'disabled'\n");
printf(" enable ... enables sleep mode\n");
printf(" disable ... disables sleep mode\n");
}
void print_usage_and_fail()
{
print_usage();
exit(EXIT_FAILURE);
}
void command_get()
{
char filename[256];
sprintf(filename, cpu_file_template, 1);
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "Cannot open file '%s' for reading.\n", filename);
exit(EXIT_FAILURE);
}
int buffer_length = 8;
char buffer[buffer_length];
fgets(buffer, buffer_length, fp);
fclose(fp);
if (strcmp(buffer, "1\n") == 0) {
printf("disabled\n");
} else {
printf("enabled\n");
}
}
void command_set(int value)
{
if (setuid(0)) {
fprintf(stderr, "Cannot set root permissions via setuid(0).\n");
exit(EXIT_FAILURE);
}
for (int cpu = cpu_min; cpu <= cpu_max; cpu++) {
char filename[256];
sprintf(filename, cpu_file_template, cpu);
FILE *fp = fopen(filename, "w+");
if (fp == NULL) {
fprintf(stderr, "Cannot open file '%s' for writing.\n", filename);
exit(EXIT_FAILURE);
}
fprintf(fp, "%d\n", value);
fclose(fp);
}
}
int main(int argc, char *argv[])
{
if (argc < 1) {
print_usage_and_fail();
}
argv0 = argv[0];
if (argc != 2) {
print_usage_and_fail();
}
char *command = argv[1];
if (strcmp(command, "get") == 0) {
command_get();
} else if (strcmp(command, "enable") == 0) {
command_set(0);
} else if (strcmp(command, "disable") == 0) {
command_set(1);
} else {
print_usage_and_fail();
}
exit(EXIT_SUCCESS);
}