Refactor to store the initial governor state when gamemode is entered

Rather than when gamemoded is started. As see in Issue #52.

	A small refactor here to ensure things stay consistent, especially
	to stop storing a pointer from get_gov_state that might have had
	it's memory changed at some point, confusingly.
This commit is contained in:
Marc Di Luzio 2018-05-15 16:28:33 +01:00
parent d9072607d9
commit 1c38a6047e
5 changed files with 31 additions and 41 deletions

View File

@ -1,3 +1,7 @@
1.2
Store the initial governor state on mode enter
1.1
Cascaded config file loading

View File

@ -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; /**<Pointer to config object */
bool performance_mode; /**<Only updates when we can */
char initial_cpu_mode[64]; /**<Only updates when we can */
/* Reaper control */
struct {
@ -99,14 +100,13 @@ void game_mode_context_init(GameModeContext *self)
had_context_init = true;
self->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];

View File

@ -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 } };

View File

@ -41,16 +41,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <sys/wait.h>
#include <unistd.h>
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;
}

View File

@ -33,16 +33,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <stdbool.h>
/**
* 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.