diff --git a/CHANGELOG b/CHANGELOG index 998665b..a556c27 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +1.2 + +Store the initial governor state on mode enter + 1.1 Cascaded config file loading diff --git a/daemon/gamemode.c b/daemon/gamemode.c index 53d509c..05239fb 100644 --- a/daemon/gamemode.c +++ b/daemon/gamemode.c @@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "gamemode.h" #include "daemon_config.h" +#include "governors-query.h" #include "governors.h" #include "logging.h" @@ -60,7 +61,7 @@ struct GameModeContext { GameModeConfig *config; /**refcount = ATOMIC_VAR_INIT(0); + /* clear the initial string */ + memset(self->initial_cpu_mode, 0, sizeof(self->initial_cpu_mode)); + /* Initialise the config */ self->config = config_create(); config_init(self->config); - /* Read current governer state before setting up any message handling */ - update_initial_gov_state(); - LOG_MSG("governor is set to [%s]\n", get_initial_governor()); - pthread_rwlock_init(&self->rwlock, NULL); pthread_mutex_init(&self->reaper.mutex, NULL); pthread_cond_init(&self->reaper.condition, NULL); @@ -176,8 +176,20 @@ static void game_mode_context_enter(GameModeContext *self) i++; } - if (!self->performance_mode && set_governors("performance")) { - self->performance_mode = true; + /* Read the initial governor state so we can revert it correctly */ + const char *initial_state = get_gov_state(); + if (initial_state) { + /* store the initial cpu governor mode */ + strncpy(self->initial_cpu_mode, initial_state, sizeof(self->initial_cpu_mode) - 1); + self->initial_cpu_mode[sizeof(self->initial_cpu_mode) - 1] = '\0'; + LOG_MSG("governor was initially set to [%s]\n", initial_state); + + /* set the governor to performance */ + if (!set_governors("performance")) { + /* 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)); + } } } @@ -192,8 +204,10 @@ static void game_mode_context_leave(GameModeContext *self) LOG_MSG("Leaving Game Mode...\n"); sd_notifyf(0, "STATUS=%sGameMode is currently deactivated.%s\n", "\x1B[1;36m", "\x1B[0m"); - if (self->performance_mode && set_governors(NULL)) { - self->performance_mode = false; + /* Reset the governer state back to initial */ + if (self->initial_cpu_mode[0] != '\0') { + set_governors(self->initial_cpu_mode); + memset(self->initial_cpu_mode, 0, sizeof(self->initial_cpu_mode)); } char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX]; diff --git a/daemon/governors-query.c b/daemon/governors-query.c index 3faa97f..1d037b1 100644 --- a/daemon/governors-query.c +++ b/daemon/governors-query.c @@ -99,8 +99,9 @@ int fetch_governors(char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH]) */ const char *get_gov_state(void) { - /* Cached primary governor state */ + /* Persistent governor state */ static char governor[64] = { 0 }; + memset(governor, 0, sizeof(governor)); /* State for all governors */ char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = { { 0 } }; diff --git a/daemon/governors.c b/daemon/governors.c index 7ced40d..9836491 100644 --- a/daemon/governors.c +++ b/daemon/governors.c @@ -41,16 +41,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -static const char *initial = NULL; - -/** - * Cache the governor state as seen at startup - */ -void update_initial_gov_state(void) -{ - initial = get_gov_state(); -} - /** * Update the governors to the given argument, via pkexec */ @@ -61,12 +51,11 @@ bool set_governors(const char *value) int ret = 0; int r = -1; - const char *const govern = value ? value : initial; const char *const exec_args[] = { - "/usr/bin/pkexec", LIBEXECDIR "/cpugovctl", "set", govern, NULL, + "/usr/bin/pkexec", LIBEXECDIR "/cpugovctl", "set", value, NULL, }; - LOG_MSG("Requesting update of governor policy to %s\n", govern); + LOG_MSG("Requesting update of governor policy to %s\n", value); if ((p = fork()) < 0) { LOG_ERROR("Failed to fork(): %s\n", strerror(errno)); @@ -102,11 +91,3 @@ bool set_governors(const char *value) return true; } - -/** - * Return the cached governor seen at startup - */ -const char *get_initial_governor(void) -{ - return initial; -} diff --git a/daemon/governors.h b/daemon/governors.h index 4ba019a..f382f8f 100644 --- a/daemon/governors.h +++ b/daemon/governors.h @@ -33,16 +33,6 @@ POSSIBILITY OF SUCH DAMAGE. #include -/** - * Store initial governor so we can use it again - */ -void update_initial_gov_state(void); - -/** - * Return the governer set in update_initial_gov_state - */ -const char *get_initial_governor(void); - /** * Update all governors to the given value. If this is NULL, restore the * initial governor.