123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605 |
- #define _GNU_SOURCE
- #include "gamemode.h"
- #include "daemon_config.h"
- #include "governors-query.h"
- #include "governors.h"
- #include "logging.h"
- #include <linux/limits.h>
- #include <linux/sched.h>
- #include <pthread.h>
- #include <sched.h>
- #include <signal.h>
- #include <stdatomic.h>
- #include <string.h>
- #include <sys/resource.h>
- #include <sys/sysinfo.h>
- #include <systemd/sd-daemon.h>
- #ifndef SCHED_ISO
- #define SCHED_ISO 4
- #endif
- #define NICE_DEFAULT_PRIORITY -4
- typedef struct GameModeClient {
- pid_t pid;
- struct GameModeClient *next;
- char *executable;
- } GameModeClient;
- struct GameModeContext {
- pthread_rwlock_t rwlock;
- _Atomic int refcount;
- GameModeClient *client;
- GameModeConfig *config;
- char initial_cpu_mode[64];
-
- struct {
- pthread_t thread;
- bool running;
- pthread_mutex_t mutex;
- pthread_cond_t condition;
- } reaper;
- };
- static GameModeContext instance = { 0 };
- #define MAX_GAMES 256
- static volatile bool had_context_init = false;
- static GameModeClient *game_mode_client_new(pid_t pid);
- static void game_mode_client_free(GameModeClient *client);
- static bool game_mode_context_has_client(GameModeContext *self, pid_t client);
- static int game_mode_context_num_clients(GameModeContext *self);
- static void *game_mode_context_reaper(void *userdata);
- static void game_mode_context_enter(GameModeContext *self);
- static void game_mode_context_leave(GameModeContext *self);
- static char *game_mode_context_find_exe(pid_t pid);
- void game_mode_context_init(GameModeContext *self)
- {
- if (had_context_init) {
- LOG_ERROR("Context already initialised\n");
- return;
- }
- had_context_init = true;
- self->refcount = ATOMIC_VAR_INIT(0);
-
- memset(self->initial_cpu_mode, 0, sizeof(self->initial_cpu_mode));
-
- self->config = config_create();
- config_init(self->config);
- pthread_rwlock_init(&self->rwlock, NULL);
- pthread_mutex_init(&self->reaper.mutex, NULL);
- pthread_cond_init(&self->reaper.condition, NULL);
-
- self->reaper.running = true;
- if (pthread_create(&self->reaper.thread, NULL, game_mode_context_reaper, self) != 0) {
- FATAL_ERROR("Couldn't construct a new thread");
- }
- }
- void game_mode_context_destroy(GameModeContext *self)
- {
- if (!had_context_init) {
- return;
- }
-
- if (game_mode_context_num_clients(self) > 0) {
- game_mode_context_leave(self);
- }
- had_context_init = false;
- game_mode_client_free(self->client);
- self->reaper.running = false;
-
- pthread_mutex_lock(&self->reaper.mutex);
- pthread_cond_signal(&self->reaper.condition);
- pthread_mutex_unlock(&self->reaper.mutex);
-
- pthread_join(self->reaper.thread, NULL);
- pthread_cond_destroy(&self->reaper.condition);
- pthread_mutex_destroy(&self->reaper.mutex);
-
- config_destroy(self->config);
- pthread_rwlock_destroy(&self->rwlock);
- }
- static void game_mode_apply_scheduler(GameModeContext *self, pid_t client)
- {
- LOG_MSG("Setting scheduling policies...\n");
-
- long int renice = 0;
- config_get_renice_value(self->config, &renice);
- if ((renice < 1) || (renice > 20)) {
- LOG_ERROR("Renice value [%ld] defaulted to [%d].\n", renice, -NICE_DEFAULT_PRIORITY);
- renice = NICE_DEFAULT_PRIORITY;
- } else {
- renice = -renice;
- }
-
- if (getpriority(PRIO_PROCESS, (id_t)client) != 0) {
- LOG_ERROR("Client [%d] already reniced, ignoring.\n", client);
- } else if (setpriority(PRIO_PROCESS, (id_t)client, (int)renice)) {
- LOG_ERROR("Renicing client [%d] failed with error %d, ignoring.\n", client, errno);
- }
-
- char softrealtime[CONFIG_VALUE_MAX] = { 0 };
- config_get_soft_realtime(self->config, softrealtime);
-
- bool enable_softrealtime = (strcmp(softrealtime, "on") == 0) || (get_nprocs() > 3);
-
- if (!(strcmp(softrealtime, "off") == 0) && (enable_softrealtime)) {
- const struct sched_param p = { .sched_priority = 0 };
- if (sched_setscheduler(client, SCHED_ISO, &p)) {
- LOG_ERROR("Setting client [%d] to SCHED_ISO failed with error %d, ignoring.\n",
- client,
- errno);
- }
- } else {
- LOG_ERROR("Not using softrealtime, setting is '%s'.\n", softrealtime);
- }
- }
- static void game_mode_context_enter(GameModeContext *self)
- {
- LOG_MSG("Entering Game Mode...\n");
- sd_notifyf(0, "STATUS=%sGameMode is now active.%s\n", "\x1B[1;32m", "\x1B[0m");
- char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
- memset(scripts, 0, sizeof(scripts));
- config_get_gamemode_start_scripts(self->config, scripts);
- unsigned int i = 0;
- while (*scripts[i] != '\0' && i < CONFIG_LIST_MAX) {
- LOG_MSG("Executing script [%s]\n", scripts[i]);
- int err;
- if ((err = system(scripts[i])) != 0) {
-
- LOG_ERROR("Script [%s] failed with error %d\n", scripts[i], err);
- }
- i++;
- }
-
- const char *initial_state = get_gov_state();
- if (initial_state) {
-
- 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);
-
- char desired[CONFIG_VALUE_MAX] = { 0 };
- config_get_desired_governor(self->config, desired);
- const char *desiredGov = desired[0] != '\0' ? desired : "performance";
-
- if (!set_governors(desiredGov)) {
-
- memset(self->initial_cpu_mode, 0, sizeof(self->initial_cpu_mode));
- }
- }
- }
- 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->initial_cpu_mode[0] != '\0') {
-
- char defaultgov[CONFIG_VALUE_MAX] = { 0 };
- config_get_default_governor(self->config, defaultgov);
- const char *gov_mode = defaultgov[0] != '\0' ? defaultgov : self->initial_cpu_mode;
- set_governors(gov_mode);
- memset(self->initial_cpu_mode, 0, sizeof(self->initial_cpu_mode));
- }
- char scripts[CONFIG_LIST_MAX][CONFIG_VALUE_MAX];
- memset(scripts, 0, sizeof(scripts));
- config_get_gamemode_end_scripts(self->config, scripts);
- unsigned int i = 0;
- while (*scripts[i] != '\0' && i < CONFIG_LIST_MAX) {
- LOG_MSG("Executing script [%s]\n", scripts[i]);
- int err;
- if ((err = system(scripts[i])) != 0) {
-
- LOG_ERROR("Script [%s] failed with error %d\n", scripts[i], err);
- }
- i++;
- }
- }
- static void game_mode_context_auto_expire(GameModeContext *self)
- {
- bool removing = true;
- while (removing) {
- pthread_rwlock_rdlock(&self->rwlock);
- removing = false;
-
- for (GameModeClient *client = self->client; client; client = client->next) {
- if (kill(client->pid, 0) != 0) {
- LOG_MSG("Removing expired game [%i]...\n", client->pid);
- pthread_rwlock_unlock(&self->rwlock);
- game_mode_context_unregister(self, client->pid);
- removing = true;
- break;
- }
- }
- if (!removing) {
- pthread_rwlock_unlock(&self->rwlock);
- break;
- }
- }
- }
- static bool game_mode_context_has_client(GameModeContext *self, pid_t client)
- {
- bool found = false;
- pthread_rwlock_rdlock(&self->rwlock);
-
- for (GameModeClient *cl = self->client; cl; cl = cl->next) {
- if (cl->pid == client) {
- found = true;
- break;
- }
- }
- pthread_rwlock_unlock(&self->rwlock);
- return found;
- }
- static int game_mode_context_num_clients(GameModeContext *self)
- {
- return atomic_load(&self->refcount);
- }
- bool game_mode_context_register(GameModeContext *self, pid_t client)
- {
-
- GameModeClient *cl = NULL;
-
- if (game_mode_context_num_clients(self) + 1 > MAX_GAMES) {
- LOG_ERROR("Max games (%d) reached, not registering %d\n", MAX_GAMES, client);
- return false;
- }
- cl = game_mode_client_new(client);
- if (!cl) {
- fputs("OOM\n", stderr);
- return false;
- }
- cl->executable = game_mode_context_find_exe(client);
- if (game_mode_context_has_client(self, client)) {
- LOG_ERROR("Addition requested for already known process [%d]\n", client);
- goto error_cleanup;
- }
-
- if (!config_get_client_whitelisted(self->config, cl->executable)) {
- LOG_MSG("Client [%s] was rejected (not in whitelist)\n", cl->executable);
- goto error_cleanup;
- } else if (config_get_client_blacklisted(self->config, cl->executable)) {
- LOG_MSG("Client [%s] was rejected (in blacklist)\n", cl->executable);
- goto error_cleanup;
- }
-
- pthread_rwlock_wrlock(&self->rwlock);
- LOG_MSG("Adding game: %d [%s]\n", client, cl->executable);
-
- cl->next = self->client;
- self->client = cl;
- pthread_rwlock_unlock(&self->rwlock);
-
- if (atomic_fetch_add_explicit(&self->refcount, 1, memory_order_seq_cst) == 0) {
- game_mode_context_enter(self);
- }
-
- game_mode_apply_scheduler(self, client);
- return true;
- error_cleanup:
- game_mode_client_free(cl);
- return false;
- }
- bool game_mode_context_unregister(GameModeContext *self, pid_t client)
- {
- GameModeClient *cl = NULL;
- GameModeClient *prev = NULL;
- bool found = false;
-
- pthread_rwlock_wrlock(&self->rwlock);
- for (prev = cl = self->client; cl; cl = cl->next) {
- if (cl->pid != client) {
- prev = cl;
- continue;
- }
- LOG_MSG("Removing game: %d [%s]\n", client, cl->executable);
-
- found = true;
- prev->next = cl->next;
- if (cl == self->client) {
- self->client = cl->next;
- }
- cl->next = NULL;
- game_mode_client_free(cl);
- break;
- }
-
- pthread_rwlock_unlock(&self->rwlock);
- if (!found) {
- LOG_ERROR("Removal requested for unknown process [%d]\n", client);
- return false;
- }
-
- if (atomic_fetch_sub_explicit(&self->refcount, 1, memory_order_seq_cst) == 1) {
- game_mode_context_leave(self);
- }
- return true;
- }
- int game_mode_context_query_status(GameModeContext *self, pid_t client)
- {
- GameModeClient *cl = NULL;
- int ret = 0;
-
- if (atomic_load_explicit(&self->refcount, memory_order_seq_cst)) {
- ret++;
-
-
- pthread_rwlock_rdlock(&self->rwlock);
- for (cl = self->client; cl; cl = cl->next) {
- if (cl->pid != client) {
- continue;
- }
-
- ret++;
- break;
- }
-
- pthread_rwlock_unlock(&self->rwlock);
- }
- return ret;
- }
- static GameModeClient *game_mode_client_new(pid_t pid)
- {
- GameModeClient c = {
- .next = NULL,
- .pid = pid,
- };
- GameModeClient *ret = NULL;
- ret = calloc(1, sizeof(struct GameModeClient));
- if (!ret) {
- return NULL;
- }
- *ret = c;
- return ret;
- }
- static void game_mode_client_free(GameModeClient *client)
- {
- if (!client) {
- return;
- }
- if (client->next) {
- game_mode_client_free(client->next);
- }
- if (client->executable) {
- free(client->executable);
- }
- free(client);
- }
- static void *game_mode_context_reaper(void *userdata)
- {
-
- GameModeContext *self = userdata;
- long reaper_interval = 0.0f;
- config_get_reaper_thread_frequency(self->config, &reaper_interval);
- struct timespec ts = { 0, 0 };
- ts.tv_sec = time(NULL) + reaper_interval;
- while (self->reaper.running) {
-
- pthread_mutex_lock(&self->reaper.mutex);
- pthread_cond_timedwait(&self->reaper.condition, &self->reaper.mutex, &ts);
- pthread_mutex_unlock(&self->reaper.mutex);
-
- if (!self->reaper.running) {
- return NULL;
- }
-
- game_mode_context_auto_expire(self);
- ts.tv_sec = time(NULL) + reaper_interval;
- }
- return NULL;
- }
- GameModeContext *game_mode_context_instance()
- {
- return &instance;
- }
- static char *game_mode_context_find_exe(pid_t pid)
- {
- static char proc_path[PATH_MAX] = { 0 };
- if (snprintf(proc_path, sizeof(proc_path), "/proc/%d/exe", pid) < 0) {
- LOG_ERROR("Unable to find executable for PID %d: %s\n", pid, strerror(errno));
- return NULL;
- }
-
- return realpath(proc_path, NULL);
- }
|