Refactor the governor request into an external process helper function

This commit is contained in:
Marc Di Luzio 2019-02-03 16:18:59 +00:00
parent 0f6c9a8a95
commit a395caeb48
6 changed files with 67 additions and 29 deletions

View File

@ -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 <stdlib.h>

View File

@ -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 <linux/limits.h>
@ -42,21 +40,15 @@ POSSIBILITY OF SUCH DAMAGE.
#include <unistd.h>
/**
* 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;
}

View File

@ -31,10 +31,5 @@ POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <stdbool.h>
/**
* 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);

View File

@ -164,6 +164,11 @@ void game_mode_free_gpu(GameModeGPUInfo **info)
*info = NULL;
}
//#include <linux/limits.h>
//#include <stdio.h>
//#include <sys/wait.h>
//#include <unistd.h>
/**
* 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;
}

View File

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

View File

@ -28,7 +28,7 @@ daemon_sources = [
'gamemode-gpu.c',
'daemonize.c',
'dbus_messaging.c',
'governors.c',
'external-helper.c',
'daemon_config.c',
]