From a395caeb48e020c903a803b0c7d7746b3d27870e Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sun, 3 Feb 2019 16:18:59 +0000 Subject: [PATCH] Refactor the governor request into an external process helper function --- daemon/dbus_messaging.c | 1 - daemon/{governors.c => external-helper.c} | 22 ++++--------- daemon/{governors.h => external-helper.h} | 9 ++--- daemon/gamemode-gpu.c | 40 ++++++++++++++++++++++- daemon/gamemode.c | 22 ++++++++++--- daemon/meson.build | 2 +- 6 files changed, 67 insertions(+), 29 deletions(-) rename daemon/{governors.c => external-helper.c} (84%) rename daemon/{governors.h => external-helper.h} (90%) diff --git a/daemon/dbus_messaging.c b/daemon/dbus_messaging.c index c411e0a..f777612 100644 --- a/daemon/dbus_messaging.c +++ b/daemon/dbus_messaging.c @@ -34,7 +34,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "dbus_messaging.h" #include "daemonize.h" #include "gamemode.h" -#include "governors.h" #include "logging.h" #include diff --git a/daemon/governors.c b/daemon/external-helper.c similarity index 84% rename from daemon/governors.c rename to daemon/external-helper.c index 9836491..096317e 100644 --- a/daemon/governors.c +++ b/daemon/external-helper.c @@ -31,9 +31,7 @@ POSSIBILITY OF SUCH DAMAGE. #define _GNU_SOURCE -#include "governors.h" #include "config.h" -#include "governors-query.h" #include "logging.h" #include @@ -42,21 +40,15 @@ POSSIBILITY OF SUCH DAMAGE. #include /** - * Update the governors to the given argument, via pkexec + * Call an external process */ -bool set_governors(const char *value) +int run_external_process(const char *const *exec_args) { pid_t p; int status = 0; int ret = 0; int r = -1; - const char *const exec_args[] = { - "/usr/bin/pkexec", LIBEXECDIR "/cpugovctl", "set", value, NULL, - }; - - LOG_MSG("Requesting update of governor policy to %s\n", value); - if ((p = fork()) < 0) { LOG_ERROR("Failed to fork(): %s\n", strerror(errno)); return false; @@ -69,14 +61,14 @@ bool set_governors(const char *value) * http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html */ if ((r = execv(exec_args[0], (char *const *)exec_args)) != 0) { - LOG_ERROR("Failed to execute cpugovctl helper: %s %s\n", exec_args[1], strerror(errno)); + LOG_ERROR("Failed to execute external process: %s %s\n", exec_args[1], strerror(errno)); exit(EXIT_FAILURE); } _exit(EXIT_SUCCESS); } else { if (waitpid(p, &status, 0) < 0) { LOG_ERROR("Failed to waitpid(%d): %s\n", (int)p, strerror(errno)); - return false; + return -1; } /* i.e. sigsev */ if (!WIFEXITED(status)) { @@ -85,9 +77,9 @@ bool set_governors(const char *value) } if ((ret = WEXITSTATUS(status)) != 0) { - LOG_ERROR("Failed to update cpu governor policy\n"); - return false; + LOG_ERROR("External process failed\n"); + return -1; } - return true; + return 0; } diff --git a/daemon/governors.h b/daemon/external-helper.h similarity index 90% rename from daemon/governors.h rename to daemon/external-helper.h index f382f8f..9d51db8 100644 --- a/daemon/governors.h +++ b/daemon/external-helper.h @@ -31,10 +31,5 @@ POSSIBILITY OF SUCH DAMAGE. #pragma once -#include - -/** - * Update all governors to the given value. If this is NULL, restore the - * initial governor. - */ -bool set_governors(const char *value); +/* Run an external process and capture the return value */ +int run_external_process(const char *const *exec_args); diff --git a/daemon/gamemode-gpu.c b/daemon/gamemode-gpu.c index 223128b..f339d40 100644 --- a/daemon/gamemode-gpu.c +++ b/daemon/gamemode-gpu.c @@ -164,6 +164,11 @@ void game_mode_free_gpu(GameModeGPUInfo **info) *info = NULL; } +//#include +//#include +//#include +//#include + /** * Applies GPU optimisations when gamemode is active and removes them after */ @@ -173,7 +178,40 @@ int game_mode_apply_gpu(const GameModeGPUInfo *info, bool apply) if (!info) return 0; - /* TODO Call gpuclockctl set */ + /* + pid_t p; + int status = 0; + int ret = 0; + int r = -1; + + const char *const exec_args[] = { + "/usr/bin/pkexec", LIBEXECDIR "/gpuclockctl", "set", value, NULL, + }; + + LOG_MSG("Requesting new GPU settings %s\n", value); + + if ((p = fork()) < 0) { + LOG_ERROR("Failed to fork(): %s\n", strerror(errno)); + return false; + } else if (p == 0) { + if ((r = execv(exec_args[0], (char *const *)exec_args)) != 0) { + LOG_ERROR("Failed to execute cpugovctl helper: %s %s\n", exec_args[1], + strerror(errno)); exit(EXIT_FAILURE); + } + _exit(EXIT_SUCCESS); + } else { + if (waitpid(p, &status, 0) < 0) { + LOG_ERROR("Failed to waitpid(%d): %s\n", (int)p, strerror(errno)); + return false; + } + if (!WIFEXITED(status)) { + LOG_ERROR("Child process '%s' exited abnormally\n", exec_args[0]); + } + } + + if ((ret = WEXITSTATUS(status)) != 0) { + LOG_ERROR("Failed to update cpu governor policy\n"); + */ return 0; } diff --git a/daemon/gamemode.c b/daemon/gamemode.c index 2b80057..bdfdae9 100644 --- a/daemon/gamemode.c +++ b/daemon/gamemode.c @@ -32,10 +32,11 @@ POSSIBILITY OF SUCH DAMAGE. #define _GNU_SOURCE #include "gamemode.h" +#include "config.h" #include "daemon_config.h" #include "dbus_messaging.h" +#include "external-helper.h" #include "governors-query.h" -#include "governors.h" #include "helpers.h" #include "logging.h" @@ -197,8 +198,13 @@ static void game_mode_context_enter(GameModeContext *self) config_get_desired_governor(self->config, desired); const char *desiredGov = desired[0] != '\0' ? desired : "performance"; - /* set the governor to performance */ - if (!set_governors(desiredGov)) { + const char *const exec_args[] = { + "/usr/bin/pkexec", LIBEXECDIR "/cpugovctl", "set", desiredGov, NULL, + }; + + LOG_MSG("Requesting update of governor policy to %s\n", desiredGov); + if (run_external_process(exec_args) != 0) { + LOG_ERROR("Failed to update cpu governor policy\n"); /* if the set fails, clear the initial mode so we don't try and reset it back and fail * again, presumably */ memset(self->initial_cpu_mode, 0, sizeof(self->initial_cpu_mode)); @@ -238,7 +244,15 @@ static void game_mode_context_leave(GameModeContext *self) config_get_default_governor(self->config, defaultgov); const char *gov_mode = defaultgov[0] != '\0' ? defaultgov : self->initial_cpu_mode; - set_governors(gov_mode); + const char *const exec_args[] = { + "/usr/bin/pkexec", LIBEXECDIR "/cpugovctl", "set", gov_mode, NULL, + }; + + LOG_MSG("Requesting update of governor policy to %s\n", gov_mode); + if (run_external_process(exec_args) != 0) { + LOG_ERROR("Failed to update cpu governor policy\n"); + } + memset(self->initial_cpu_mode, 0, sizeof(self->initial_cpu_mode)); } diff --git a/daemon/meson.build b/daemon/meson.build index 9b79453..46e40a4 100644 --- a/daemon/meson.build +++ b/daemon/meson.build @@ -28,7 +28,7 @@ daemon_sources = [ 'gamemode-gpu.c', 'daemonize.c', 'dbus_messaging.c', - 'governors.c', + 'external-helper.c', 'daemon_config.c', ]