diff options
-rw-r--r-- | LICENSE | 2 | ||||
-rw-r--r-- | coreupdown.c | 74 | ||||
-rw-r--r-- | coreupdownd.1 | 16 | ||||
-rw-r--r-- | coreupdownd.conf.5 | 6 |
4 files changed, 88 insertions, 10 deletions
@@ -1,6 +1,6 @@ ISC License -© 2024 Mattias Andrée <maandree@kth.se> +© 2024 Mattias Andrée <m@maandree.se> Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above diff --git a/coreupdown.c b/coreupdown.c index 500839f..283a1de 100644 --- a/coreupdown.c +++ b/coreupdown.c @@ -19,11 +19,11 @@ char *argv0; static volatile sig_atomic_t caught_sighup = 0; -#define coreup() coreupdown(ULONG_MAX) -#define coredown() coreupdown(1) +#define coreup(POSTCOMMAND) coreupdown(ULONG_MAX, (POSTCOMMAND)) +#define coredown(POSTCOMMAND) coreupdown(1, (POSTCOMMAND)) static int -coreupdown(unsigned long int count) +coreupdown(unsigned long int count, const char *postcommand) { char path[sizeof("/sys/devices/system/cpu/cpu/online") + 3 * sizeof(count)]; unsigned long int i; @@ -49,7 +49,14 @@ coreupdown(unsigned long int count) } close(fd); } - return -(have_failure && !have_success); + + if (have_failure && !have_success) + return -1; + + if (postcommand) + system(postcommand); + + return 0; } static void @@ -70,6 +77,8 @@ daemon_usage(const char *command) "[-t coredown-cpu-usage-time-consistency-threshold] " "[-U coreup-cpu-usage-threshold] " "[-u coredown-cpu-usage-threshold] " + "[-X post-coreup-command] " + "[-x post-coredown-command] " "[-ef]\n", command); } @@ -99,7 +108,7 @@ oneshot_main(int argc, char **argv, unsigned long int count) argc--; } - return -coreupdown(count); + return -coreupdown(count, NULL); } static int @@ -506,9 +515,11 @@ load_configurations(const char *file, int optional, unsigned int *coreup_threshold_cpu, unsigned int *coredown_threshold_cpu, unsigned int *coreup_threshold_time, unsigned int *coredown_threshold_time, unsigned int *coreup_cooldown, unsigned int *coredown_cooldown, - unsigned int *daemon_interval_sec, unsigned long int *daemon_interval_nsec) + unsigned int *daemon_interval_sec, unsigned long int *daemon_interval_nsec, + char **post_coreup_command, char **post_coredown_command) { unsigned int *uintp; + char **strp; unsigned long int tmp; int fd; FILE *f; @@ -582,8 +593,38 @@ load_configurations(const char *file, int optional, *uintp = (unsigned int)tmp; continue; + not_uint: + if (OPTION("post-coreup-command")) + strp = post_coreup_command; + else if (OPTION("post-coredown-command")) + strp = post_coredown_command; + else + goto not_str; + s = &s[n]; + while (isspace(*s)) + s++; + if (*s != '=') + goto invalid; + s++; + while (isspace(*s)) + s++; + p = rstrip(s); + pc = *p; + *p = 0; + + if (strp) { + *strp = strdup(s); + if (!*strp) { + fprintf(stderr, "%s: strdup: %s\n", argv0, strerror(errno)); + exit(1); + } + } + + continue; + + not_str: if (!OPTION("check-interval")) goto invalid; @@ -672,6 +713,7 @@ daemon_main(int argc, char **argv, int orig_argc, char **orig_argv, int reexeced unsigned int coreup_cooldown, coredown_cooldown; unsigned int daemon_interval_sec, *uintp; unsigned long int daemon_interval_nsec, tmp; + char *post_coreup_command = NULL, *post_coredown_command = NULL; int have_coreup_threshold_cpu = 0, have_coredown_threshold_cpu = 0; int have_coreup_threshold_time = 0, have_coredown_threshold_time = 0; int have_coreup_cooldown = 0, have_coredown_cooldown = 0; @@ -721,6 +763,18 @@ restart: have_daemon_interval = 1; break; + case 'X': + post_coreup_command = ARGNULL(); + if (!post_coreup_command) + goto usage; + break; + + case 'x': + post_coredown_command = ARGNULL(); + if (!post_coredown_command) + goto usage; + break; + case 'e': keep_stderr = 1; break; @@ -788,7 +842,9 @@ restart: have_coreup_cooldown ? NULL : &coreup_cooldown, have_coredown_cooldown ? NULL : &coredown_cooldown, have_daemon_interval ? NULL : &daemon_interval_sec, - have_daemon_interval ? NULL : &daemon_interval_nsec); + have_daemon_interval ? NULL : &daemon_interval_nsec, + post_coreup_command ? NULL : &post_coreup_command, + post_coredown_command ? NULL : &post_coredown_command); if (coreup_threshold_cpu > 100) { fprintf(stderr, "%s: CPU usage threshold for coreup was above 100, setting to 100\n", argv0); @@ -860,7 +916,7 @@ restart: down_time = 0; if (up_time++ >= coreup_threshold_time) { cooldown = coreup_cooldown; - if (coreup()) + if (coreup(post_coreup_command)) return 1; up_time = 0; } @@ -868,7 +924,7 @@ restart: up_time = 0; if (down_time++ >= coredown_threshold_time) { cooldown = coredown_cooldown; - if (coredown()) + if (coredown(post_coredown_command)) return 1; down_time = 0; } diff --git a/coreupdownd.1 b/coreupdownd.1 index f366464..7d59db3 100644 --- a/coreupdownd.1 +++ b/coreupdownd.1 @@ -11,6 +11,8 @@ coreupdownd - Dynamically enable and disable CPU's to only use one during low CP .RI "[-t " coredown-cpu-usage-time-consistency-threshold ] .RI "[-U " coreup-cpu-usage-threshold ] .RI "[-u " coredown-cpu-usage-threshold ] +.RI "[-X " post-coreup-command ] +.RI "[-x " post-coredown-command ] [-ef] .SH DESCRIPTION The @@ -89,6 +91,20 @@ daemon may enable all CPU's. .BI "-u " coredown-cpu-usage-threshold The required CPU usage (integer upper bound) before the daemon may disable all CPU's except the main core. +.TP +.BI "-X " post-coreup-command +Run +.I post-coreup-command +in +.BR sh (1) +after each time all CPU's are enabled. +.TP +.BI "-x " post-coredown-command +Run +.I post-coredown-command +in +.BR sh (1) +after each time all non-main CPU's are disabled. .SH OPERANDS No operands are supported. .SH STDIN diff --git a/coreupdownd.conf.5 b/coreupdownd.conf.5 index 3e8759c..961de41 100644 --- a/coreupdownd.conf.5 +++ b/coreupdownd.conf.5 @@ -49,6 +49,12 @@ coredown-cooldown-time .TP * check-interval +.TP +* +post-coreup-command +.TP +* +post-coredown-command .SH NOTES None. .SH SEE ALSO |