system76-tools

collection of utilities for system76 laptops
git clone https://git.parazyd.org/system76-tools
Log | Files | Refs | README | LICENSE

commit 739dbd3e5faf2b987081868b00e1b3d67867d8d9
parent 76da27febcd12f45edcdba4d2102ee67dcd07776
Author: parazyd <parazyd@dyne.org>
Date:   Fri, 21 Oct 2022 12:22:17 +0200

Minor code cleanups

Diffstat:
MMakefile | 2+-
Mcharge-thresholds.c | 31++++++++-----------------------
Mcommon.c | 44++++++++++++++------------------------------
Mcommon.h | 3+--
Mperf-profile.c | 179+++++++++++++++++++++++++++++++++----------------------------------------------
5 files changed, 98 insertions(+), 161 deletions(-)

diff --git a/Makefile b/Makefile @@ -22,7 +22,7 @@ all: $(SUID_BIN) $(BRIGHTNESSOBJ) $(CHARGETHRESHOLDSOBJ) $(PERFPROFILEOBJ): $(HDR) clean: - rm -f $(BRIGHTNESSOBJ) $(CHARGETHRESHOLDSOBJ) $(PERFPROFILEOBJ) + rm -f $(SUID_BIN) $(BRIGHTNESSOBJ) $(CHARGETHRESHOLDSOBJ) $(PERFPROFILEOBJ) install: all mkdir -p $(DESTDIR)$(PREFIX)/bin diff --git a/charge-thresholds.c b/charge-thresholds.c @@ -34,7 +34,6 @@ static void usage(void) int main(int argc, char *argv[]) { int start, end; - FILE *fd; ARGBEGIN { } ARGEND; @@ -53,33 +52,19 @@ int main(int argc, char *argv[]) end = 60; } else { usage(); - exit(1); + return 1; } /* Without this, setting start threshold may fail if the previous end - * threshold is higher - */ - if ((fd = fopen(END_FD, "w")) == NULL) - die("Could not open %s for writing:", END_FD); + * threshold is higher */ + if (write_oneshot_int(END_FD, 100)) + die("Could not open %s for writing:", END_FD); - fprintf(fd, "100"); - fclose(fd); - fd = NULL; + if (write_oneshot_int(START_FD, start)) + die("Could not open %s for writing:", START_FD); - if ((fd = fopen(START_FD, "w")) == NULL) - die("Could not open %s for writing:", START_FD); + if (write_oneshot_int(END_FD, end)) + die("Could not open %s for writing:", END_FD); - fprintf(fd, "%d", start); - fclose(fd); - fd = NULL; - - if ((fd = fopen(END_FD, "w")) == NULL) - die("Could not open %s for writing:", END_FD); - - fprintf(fd, "%d", end); - fclose(fd); - fd = NULL; - - printf("Thresholds set to: %d-%d\n", start, end); return 0; } diff --git a/common.c b/common.c @@ -21,35 +21,19 @@ void die(const char *fmt, ...) exit(1); } -void reverse(char *s) +int intlen(int n) { - int i, j; - char c; - - for (i = 0, j = strlen(s)-1; i<j; i++, j--) { - c = s[i]; - s[i] = s[j]; - s[j] = c; - } -} - -void itoa(int n, char *s) -{ - int i, sign; - - if ((sign = n) < 0) - n = -n; - - i = 0; - do { - s[i++] = n % 10 + '0'; - } while ((n /= 10) > 0); - - if (sign < 0) - s[i++] = '-'; - - s[i] = '\0'; - reverse(s); + if (n < 0) return intlen(-n) + 1; + if (n < 10) return 1; + if (n < 100) return 2; + if (n < 1000) return 3; + if (n < 10000) return 4; + if (n < 100000) return 5; + if (n < 1000000) return 6; + if (n < 10000000) return 7; + if (n < 100000000) return 8; + if (n < 1000000000) return 9; + return 10; } int write_oneshot_str(const char *path, const char *text) @@ -62,7 +46,7 @@ int write_oneshot_str(const char *path, const char *text) fprintf(fd, text); fclose(fd); - fprintf(stderr, "Wrote into %s: %s\n", path, text); + fprintf(stderr, "%s: %s\n", path, text); return 0; } @@ -76,6 +60,6 @@ int write_oneshot_int(const char *path, int value) fprintf(fd, "%d", value); fclose(fd); - fprintf(stderr, "Wrote into %s: %d\n", path, value); + fprintf(stderr, "%s: %d\n", path, value); return 0; } diff --git a/common.h b/common.h @@ -4,8 +4,7 @@ #define MIN(a, b) ((a) < (b) ? (a) : (b)) void die(const char *fmt, ...); -void itoa(int n, char *s); - +int intlen(int n); int write_oneshot_str(const char *path, const char *text); int write_oneshot_int(const char *path, int value); diff --git a/perf-profile.c b/perf-profile.c @@ -34,6 +34,11 @@ enum Profile { PERFORMANCE, }; +enum MType { + MIN, + MAX, +}; + static void usage(void) { die("usage: %s [-v] low-power|balanced|performance", argv0); @@ -50,80 +55,67 @@ static void set_max_lost_work(int secs) die("Could not open %s for writing:", DIRTY_WRITEBACK); } -static int get_frequency(int typ, int cpu) +static int get_frequency(enum MType typ, int cpu) { - /* typ is 0 for min, !0 for max */ - FILE *fd; - char *line = NULL, *path, *rem; - char ccpu[1]; /* Will break if cpu > 9 */ + const char *rem; + char *line = NULL, *path; size_t nread, len; int plen, ret; + FILE *fd; - if (typ) - rem = "/cpufreq/cpuinfo_max_freq"; - else + switch(typ) { + case MIN: rem = "/cpufreq/cpuinfo_min_freq"; + break; + case MAX: + rem = "/cpufreq/cpuinfo_max_freq"; + break; + } - - itoa(cpu, ccpu); - - plen = strlen(SYS_CPU_PREFIX) + strlen(rem) + 2; + plen = strlen(SYS_CPU_PREFIX) + strlen(rem) + intlen(cpu) + 1; path = malloc(plen); memset(path, 0, plen); - - path = strcat(path, SYS_CPU_PREFIX); - path = strcat(path, ccpu); - path = strcat(path, rem); + snprintf(path, plen, "%s%d%s", SYS_CPU_PREFIX, cpu, rem); if ((fd = fopen(path, "r")) == NULL) { free(path); - die("Could not open cpu%d min/max file for reading:", cpu); + die("Could not open cpu%d%s file for reading:", cpu, rem); } - free(path); - nread = getline(&line, &len, fd); (void)nread; ret = atoi(line); + + free(path); free(line); return ret; } -static void set_frequency(int typ, int cpu, int freq) +static void set_frequency(enum MType typ, int cpu, int freq) { - /* typ is 0 for min, !0 for max */ - char *path, *rem, *ccpu; + const char *rem; + char *path; int plen; - if (cpu > 9) { - ccpu = malloc(2); - memset(ccpu, 0, 2); - } else { - ccpu = malloc(1); - memset(ccpu, 0, 1); - } - - if (typ) - rem = "/cpufreq/scaling_max_freq"; - else + switch(typ) { + case MIN: rem = "/cpufreq/scaling_min_freq"; + break; + case MAX: + rem = "/cpufreq/scaling_max_freq"; + break; + } - itoa(cpu, ccpu); - plen = strlen(ccpu) + strlen(SYS_CPU_PREFIX) + strlen(rem); + plen = strlen(SYS_CPU_PREFIX) + strlen(rem) + intlen(cpu) + 1; path = malloc(plen); memset(path, 0, plen); - - path = strcat(path, SYS_CPU_PREFIX); - path = strcat(path, ccpu); - path = strcat(path, rem); - - free(ccpu); + snprintf(path, plen, "%s%d%s", SYS_CPU_PREFIX, cpu, rem); if (write_oneshot_int(path, freq)) { free(path); - die("Could not open cpu%d min/max file for writing:", cpu); + die("Could not open cpu%d%s file for writing:", cpu, rem); } free(path); @@ -131,32 +123,18 @@ static void set_frequency(int typ, int cpu, int freq) static void set_governor(int cpu, const char *governor) { - char *path, *ccpu; - char *rem = "/cpufreq/scaling_governor"; + const char *rem = "/cpufreq/scaling_governor"; + char *path; int plen; - if (cpu > 9) { - ccpu = malloc(2); - memset(ccpu, 0, 2); - } else { - ccpu = malloc(1); - memset(ccpu, 0, 1); - } - - itoa(cpu, ccpu); - plen = strlen(ccpu) + strlen(SYS_CPU_PREFIX) + strlen(rem); + plen = strlen(SYS_CPU_PREFIX) + strlen(rem) + intlen(cpu) + 1; path = malloc(plen); memset(path, 0, plen); - - path = strcat(path, SYS_CPU_PREFIX); - path = strcat(path, ccpu); - path = strcat(path, rem); - - free(ccpu); + snprintf(path, plen, "%s%d%s", SYS_CPU_PREFIX, cpu, rem); if (write_oneshot_str(path, governor)) { free(path); - die("Could not open cpu%d governor file:", cpu); + die("Could not write to cpu%d%s file:", cpu, rem); } free(path); @@ -164,9 +142,8 @@ static void set_governor(int cpu, const char *governor) static void cpufreq_set(enum Profile profile, int max_percent) { - int i, nproc; - int min, max; - char *governor; + const char *governor; + int i, nproc, min, max; /* We assume we have intel_pstate */ switch(profile) { @@ -193,8 +170,14 @@ static void cpufreq_set(enum Profile profile, int max_percent) } } -static void set_lowpower(void) +static void set_lowpower(int acpi_platform_supported) { + if (acpi_platform_supported) { + if (write_oneshot_str(ACPI_PLPR_PATH, "low-power")) + die("Could not open %s for writing:", ACPI_PLPR_PATH); + return; + } + set_max_lost_work(15); cpufreq_set(LOWPOWER, 50); @@ -209,8 +192,14 @@ static void set_lowpower(void) die("Could not open %s for writing:", PSTATE_NO_TURBO); } -static void set_balanced(void) +static void set_balanced(int acpi_platform_supported) { + if (acpi_platform_supported) { + if (write_oneshot_str(ACPI_PLPR_PATH, "balanced")) + die("Could not open %s for writing:", ACPI_PLPR_PATH); + return; + } + set_max_lost_work(15); cpufreq_set(BALANCED, 100); @@ -228,8 +217,14 @@ static void set_balanced(void) die("Could not open %s for writing:", PSTATE_NO_TURBO); } -static void set_performance(void) +static void set_performance(int acpi_platform_supported) { + if (acpi_platform_supported) { + if (write_oneshot_str(ACPI_PLPR_PATH, "performance")) + die("Could not open %s for writing:", ACPI_PLPR_PATH); + return; + } + set_max_lost_work(15); cpufreq_set(PERFORMANCE, 100); @@ -253,9 +248,6 @@ int main(int argc, char *argv[]) { int vflag = 0; int acpi_platform_supported = 0; - char *line = NULL; - size_t len, nread; - FILE *fd; ARGBEGIN { case 'v': @@ -263,21 +255,15 @@ int main(int argc, char *argv[]) break; default: usage(); - exit(1); } ARGEND; - if (!access(ACPI_PLPR_PATH, F_OK)) - acpi_platform_supported = 1; - - if (vflag) { - if (acpi_platform_supported) { - if ((fd = fopen(ACPI_PLPR_PATH, "r")) == NULL) - die("Could not open %s for reading:", ACPI_PLPR_PATH); - } else { - if ((fd = fopen(S76_POW_PROF, "r")) == NULL) - die("Could not open %s for reading:", S76_POW_PROF); - } + char *line = NULL; + size_t len, nread; + FILE *fd; + + if ((fd = fopen(S76_POW_PROF, "r")) == NULL) + die("Could not open %s for reading:", S76_POW_PROF); nread = getline(&line, &len, fd); fclose(fd); @@ -285,38 +271,21 @@ int main(int argc, char *argv[]) printf("Current profile: %s\n", line); free(line); - exit(0); + return 0; } if (argc != 1) usage(); - if (acpi_platform_supported) { - if ((fd = fopen(ACPI_PLPR_PATH, "w")) == NULL) - die("Could not open %s for writing:", ACPI_PLPR_PATH); - - if (!strcmp(argv[0], "low-power")) - fprintf(fd, "low-power"); - else if (!strcmp(argv[0], "balanced")) - fprintf(fd, "balanced"); - else if (!strcmp(argv[0], "performance")) - fprintf(fd, "performance"); - else { - fclose(fd); - usage(); - } - - fclose(fd); - printf("Platform profile set to: %s\n", argv[0]); - return 0; - } + if (!access(ACPI_PLPR_PATH, F_OK)) + acpi_platform_supported = 1; if (!strcmp(argv[0], "low-power")) - set_lowpower(); + set_lowpower(acpi_platform_supported); else if (!strcmp(argv[0], "balanced")) - set_balanced(); + set_balanced(acpi_platform_supported); else if (!strcmp(argv[0], "performance")) - set_performance(); + set_performance(acpi_platform_supported); else usage();