123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #define _GNU_SOURCE
- #include "daemon_config.h"
- #include "gamemode.h"
- #include "helpers.h"
- #include "logging.h"
- #include <errno.h>
- #include <sys/syscall.h>
- #include <unistd.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);
- }
- void game_mode_apply_ioprio(const GameModeContext *self, const pid_t client)
- {
- GameModeConfig *config = game_mode_config_from_context(self);
- LOG_MSG("Setting scheduling policies...\n");
-
- int ioprio = (int)config_get_ioprio_value(config);
- 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 {
-
- int invalid_ioprio = ioprio;
- ioprio = CLAMP(0, 7, ioprio);
- if (ioprio != invalid_ioprio)
- LOG_ONCE(ERROR,
- "IO priority value %d invalid, clamping to %d\n",
- invalid_ioprio,
- ioprio);
-
- ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, ioprio);
- }
-
- 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);
- }
- }
|