123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #define _GNU_SOURCE
- #include "gamemode.h"
- #include "common-helpers.h"
- #include "common-logging.h"
- #include "gamemode-config.h"
- #include <dirent.h>
- #include <sys/syscall.h>
- #ifndef IOPRIO_BITS
- #define IOPRIO_BITS (16)
- #endif
- #ifndef IOPRIO_CLASS_SHIFT
- #define IOPRIO_CLASS_SHIFT (13)
- #endif
- #ifndef IOPRIO_PRIO_MASK
- #define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
- #endif
- #ifndef IOPRIO_PRIO_CLASS
- #define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT)
- #endif
- #ifndef IOPRIO_PRIO_DATA
- #define IOPRIO_PRIO_DATA(mask) ((mask)&IOPRIO_PRIO_MASK)
- #endif
- #ifndef IOPRIO_PRIO_VALUE
- #define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
- #endif
- enum {
- IOPRIO_CLASS_NONE,
- IOPRIO_CLASS_RT,
- IOPRIO_CLASS_BE,
- IOPRIO_CLASS_IDLE,
- };
- enum {
- IOPRIO_WHO_PROCESS = 1,
- IOPRIO_WHO_PGRP,
- IOPRIO_WHO_USER,
- };
- static inline int ioprio_set(int which, int who, int ioprio)
- {
- return (int)syscall(SYS_ioprio_set, which, who, ioprio);
- }
- static inline int ioprio_get(int which, int who)
- {
- return (int)syscall(SYS_ioprio_get, which, who);
- }
- int game_mode_get_ioprio(const pid_t client)
- {
- int ret = ioprio_get(IOPRIO_WHO_PROCESS, client);
- if (ret == -1) {
- LOG_ERROR("Failed to get ioprio value for [%d] with error %s\n", client, strerror(errno));
- ret = IOPRIO_DONT_SET;
- }
-
- return IOPRIO_PRIO_DATA(ret);
- }
- void game_mode_apply_ioprio(const GameModeContext *self, const pid_t client, int expected)
- {
- if (expected == IOPRIO_DONT_SET)
-
- return;
- GameModeConfig *config = game_mode_config_from_context(self);
-
- int ioprio = (int)config_get_ioprio_value(config);
-
- if (ioprio == IOPRIO_DONT_SET)
- return;
- LOG_MSG("Setting ioprio value...\n");
-
- if (expected != IOPRIO_DEFAULT) {
- expected = (int)ioprio;
- ioprio = IOPRIO_DEFAULT;
- }
-
- char tasks[128];
- snprintf(tasks, sizeof(tasks), "/proc/%d/task", client);
- DIR *client_task_dir = opendir(tasks);
- if (client_task_dir == NULL) {
- LOG_ERROR("Could not inspect tasks for client [%d]! Skipping ioprio optimisation.\n",
- client);
- return;
- }
-
- struct dirent *tid_entry;
- while ((tid_entry = readdir(client_task_dir)) != NULL) {
-
- if (tid_entry->d_name[0] == '.')
- continue;
-
- int tid = atoi(tid_entry->d_name);
- int current = game_mode_get_ioprio(tid);
- if (current == IOPRIO_DONT_SET) {
-
- } else if (current != expected) {
-
- LOG_ERROR("Skipping ioprio on client [%d,%d]: ioprio was (%d) but we expected (%d)\n",
- client,
- tid,
- current,
- expected);
- } else {
-
- int p = ioprio;
- ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, ioprio);
- if (ioprio_set(IOPRIO_WHO_PROCESS, tid, ioprio) != 0) {
-
- LOG_ERROR(
- "Setting client [%d,%d] IO priority to (%d) failed with error %d, ignoring.\n",
- client,
- tid,
- p,
- errno);
- }
- }
- }
- closedir(client_task_dir);
- }
|