/
gamemode.c
670 lines (564 loc) · 18.4 KB
/
gamemode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*
Copyright (c) 2017-2018, Feral Interactive
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Feral Interactive nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#define _GNU_SOURCE
#include "gamemode.h"
#include "daemon_config.h"
#include "governors-query.h"
#include "governors.h"
#include "ioprio.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/param.h>
#include <sys/resource.h>
#include <sys/sysinfo.h>
#include <systemd/sd-daemon.h>
/* SCHED_ISO may not be defined as it is a reserved value not yet
* implemented in official kernel sources, see linux/sched.h.
*/
#ifndef SCHED_ISO
#define SCHED_ISO 4
#endif
/* Priority to renice the process to.
*/
#define NICE_DEFAULT_PRIORITY -4
/* Value clamping helper.
*/
#define CLAMP(lbound, ubound, value) MIN(MIN(lbound, ubound), MAX(MAX(lbound, ubound), value))
/**
* The GameModeClient encapsulates the remote connection, providing a list
* form to contain the pid and credentials.
*/
typedef struct GameModeClient {
pid_t pid; /**< Process ID */
struct GameModeClient *next; /**<Next client in the list */
char *executable; /**<Process executable */
} GameModeClient;
struct GameModeContext {
pthread_rwlock_t rwlock; /**<Guard access to the client list */
_Atomic int refcount; /**<Allow cycling the game mode */
GameModeClient *client; /**<Pointer to first client */
GameModeConfig *config; /**<Pointer to config object */
char initial_cpu_mode[64]; /**<Only updates when we can */
/* Reaper control */
struct {
pthread_t thread;
bool running;
pthread_mutex_t mutex;
pthread_cond_t condition;
} reaper;
};
static GameModeContext instance = { 0 };
/* Maximum number of concurrent processes we'll sanely support */
#define MAX_GAMES 256
/**
* Protect against signals
*/
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);
/* 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);
pthread_rwlock_init(&self->rwlock, NULL);
pthread_mutex_init(&self->reaper.mutex, NULL);
pthread_cond_init(&self->reaper.condition, NULL);
/* Get the reaper thread going */
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;
}
/* Leave game mode now */
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;
/* We might be stuck waiting, so wake it up again */
pthread_mutex_lock(&self->reaper.mutex);
pthread_cond_signal(&self->reaper.condition);
pthread_mutex_unlock(&self->reaper.mutex);
/* Join the thread as soon as possible */
pthread_join(self->reaper.thread, NULL);
pthread_cond_destroy(&self->reaper.condition);
pthread_mutex_destroy(&self->reaper.mutex);
/* Destroy the config object */
config_destroy(self->config);
pthread_rwlock_destroy(&self->rwlock);
}
/**
* Apply scheduling policies
*
* This tries to change the scheduler of the client to soft realtime mode
* available in some kernels as SCHED_ISO. It also tries to adjust the nice
* level. If some of each fail, ignore this and log a warning.
*
* We don't need to store the current values because when the client exits,
* everything will be good: Scheduling is only applied to the client and
* its children.
*/
static void game_mode_apply_scheduler(GameModeContext *self, pid_t client)
{
LOG_MSG("Setting scheduling policies...\n");
/*
* read configuration "renice" (1..20)
*/
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;
}
/*
* don't adjust priority if it was already adjusted
*/
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 (your user may not have "
"permission to do this).\n",
client,
errno);
}
/*
* read configuration "softrealtime" (on, off, auto)
*/
char softrealtime[CONFIG_VALUE_MAX] = { 0 };
config_get_soft_realtime(self->config, softrealtime);
/*
* Enable unconditionally or auto-detect soft realtime usage,
* auto detection is based on observations where dual-core CPU suffered
* priority inversion problems with the graphics driver thus running
* slower as a result, so enable only with more than 3 cores.
*/
bool enable_softrealtime = (strcmp(softrealtime, "on") == 0) || (get_nprocs() > 3);
/*
* Actually apply the scheduler policy if not explicitly turned off
*/
if (!(strcmp(softrealtime, "off") == 0) && (enable_softrealtime)) {
const struct sched_param p = { .sched_priority = 0 };
if (sched_setscheduler(client, SCHED_ISO | SCHED_RESET_ON_FORK, &p)) {
LOG_ERROR(
"Setting client [%d] to SCHED_ISO failed with error %d, ignoring (your "
"kernel may not support this).\n",
client,
errno);
}
} else {
LOG_ERROR("Not using softrealtime, setting is '%s'.\n", softrealtime);
}
}
/**
* Apply io priorities
*
* This tries to change the io priority of the client to a value specified
* and can possibly reduce lags or latency when a game has to load assets
* on demand.
*/
static void game_mode_apply_ioprio(GameModeContext *self, pid_t client)
{
LOG_MSG("Setting scheduling policies...\n");
/*
* read configuration "ioprio" (0..7)
*/
int ioprio = 0;
config_get_ioprio_value(self->config, &ioprio);
if (IOPRIO_RESET_DEFAULT == ioprio) {
LOG_MSG("IO priority will be reset to default behavior (based on CPU priority).\n");
ioprio = 0;
} else if (IOPRIO_DONT_SET == ioprio) {
return;
} else {
/* maybe clamp the value */
int invalid_ioprio = ioprio;
ioprio = CLAMP(0, 7, ioprio);
if (ioprio != invalid_ioprio)
LOG_ERROR("IO priority value %d invalid, clamping to %d\n", invalid_ioprio, ioprio);
/* We support only IOPRIO_CLASS_BE as IOPRIO_CLASS_RT required CAP_SYS_ADMIN */
ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, ioprio);
}
/*
* Actually apply the io priority
*/
int c = IOPRIO_PRIO_CLASS(ioprio), p = IOPRIO_PRIO_DATA(ioprio);
if (ioprio_set(IOPRIO_WHO_PROCESS, client, ioprio) == 0) {
if (0 == ioprio)
LOG_MSG("Resetting client [%d] IO priority.\n", client);
else
LOG_MSG("Setting client [%d] IO priority to (%d,%d).\n", client, c, p);
} else {
LOG_ERROR("Setting client [%d] IO priority to (%d,%d) failed with error %d, ignoring\n",
client,
c,
p,
errno);
}
}
/**
* Pivot into game mode.
*
* This is only possible after game_mode_context_init has made a GameModeContext
* usable, and should always be followed by a game_mode_context_leave.
*/
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 the failure, but this is not fatal */
LOG_ERROR("Script [%s] failed with error %d\n", scripts[i], err);
}
i++;
}
/* 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);
/* Choose the desired governor */
char desired[CONFIG_VALUE_MAX] = { 0 };
config_get_desired_governor(self->config, desired);
const char *desiredGov = desired[0] != '\0' ? desired : "performance";
/* set the governor to performance */
if (!set_governors(desiredGov)) {
/* 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));
}
}
}
/**
* Pivot out of game mode.
*
* Should only be called after both init and game_mode_context_enter have
* been performed.
*/
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");
/* Reset the governer state back to initial */
if (self->initial_cpu_mode[0] != '\0') {
/* Choose the governor to reset to, using the config to override */
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 the failure, but this is not fatal */
LOG_ERROR("Script [%s] failed with error %d\n", scripts[i], err);
}
i++;
}
}
/**
* Automatically expire all dead processes
*
* This has to take special care to ensure thread safety and ensuring that our
* pointer is never cached incorrectly.
*/
static void game_mode_context_auto_expire(GameModeContext *self)
{
bool removing = true;
while (removing) {
pthread_rwlock_rdlock(&self->rwlock);
removing = false;
/* Each time we hit an expired game, start the loop back */
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;
}
}
}
/**
* Determine if the client is already known to the context
*/
static bool game_mode_context_has_client(GameModeContext *self, pid_t client)
{
bool found = false;
pthread_rwlock_rdlock(&self->rwlock);
/* Walk all clients and find a matching pid */
for (GameModeClient *cl = self->client; cl; cl = cl->next) {
if (cl->pid == client) {
found = true;
break;
}
}
pthread_rwlock_unlock(&self->rwlock);
return found;
}
/**
* Helper to grab the current number of clients we know about
*/
static int game_mode_context_num_clients(GameModeContext *self)
{
return atomic_load(&self->refcount);
}
bool game_mode_context_register(GameModeContext *self, pid_t client)
{
/* Construct a new client if we can */
GameModeClient *cl = NULL;
/* Cap the total number of active clients */
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;
}
/* Check our blacklist and whitelist */
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;
}
/* Begin a write lock now to insert our new client at list start */
pthread_rwlock_wrlock(&self->rwlock);
LOG_MSG("Adding game: %d [%s]\n", client, cl->executable);
/* Update the list */
cl->next = self->client;
self->client = cl;
pthread_rwlock_unlock(&self->rwlock);
/* First add, init */
if (atomic_fetch_add_explicit(&self->refcount, 1, memory_order_seq_cst) == 0) {
game_mode_context_enter(self);
}
/* Apply scheduler policies */
game_mode_apply_scheduler(self, client);
/* Apply io priorities */
game_mode_apply_ioprio(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;
/* Requires locking. */
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 it */
found = true;
prev->next = cl->next;
if (cl == self->client) {
self->client = cl->next;
}
cl->next = NULL;
game_mode_client_free(cl);
break;
}
/* Unlock here, potentially yielding */
pthread_rwlock_unlock(&self->rwlock);
if (!found) {
LOG_ERROR("Removal requested for unknown process [%d]\n", client);
return false;
}
/* When we hit bottom then end the game mode */
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;
/*
* Check the current refcount on gamemode, this equates to whether gamemode is active or not,
* see game_mode_context_register and game_mode_context_unregister
*/
if (atomic_load_explicit(&self->refcount, memory_order_seq_cst)) {
ret++;
/* Check if the current client is registered */
/* Requires locking. */
pthread_rwlock_rdlock(&self->rwlock);
for (cl = self->client; cl; cl = cl->next) {
if (cl->pid != client) {
continue;
}
/* Found it */
ret++;
break;
}
/* Unlock here, potentially yielding */
pthread_rwlock_unlock(&self->rwlock);
}
return ret;
}
/**
* Construct a new GameModeClient for the given process ID
*
* This is deliberately OOM safe
*/
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;
}
/**
* Free a client and the next element in the list.
*/
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);
}
/**
* We continuously run until told otherwise.
*/
static void *game_mode_context_reaper(void *userdata)
{
/* Stack, not allocated, won't disappear. */
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) {
/* Wait for condition */
pthread_mutex_lock(&self->reaper.mutex);
pthread_cond_timedwait(&self->reaper.condition, &self->reaper.mutex, &ts);
pthread_mutex_unlock(&self->reaper.mutex);
/* Highly possible the main thread woke us up to exit */
if (!self->reaper.running) {
return NULL;
}
/* Expire remaining entries */
game_mode_context_auto_expire(self);
ts.tv_sec = time(NULL) + reaper_interval;
}
return NULL;
}
GameModeContext *game_mode_context_instance()
{
return &instance;
}
/**
* Attempt to locate the exe for the process.
* We might run into issues if the process is running under an odd umask.
*/
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;
}
/* Allocate the realpath if possible */
return realpath(proc_path, NULL);
}