1
0

gamemode-sched.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Copyright (c) 2017-2025, Feral Interactive and the GameMode contributors
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Feral Interactive nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #define _GNU_SOURCE
  27. #include "gamemode.h"
  28. #include "common-logging.h"
  29. #include "gamemode-config.h"
  30. #include <dirent.h>
  31. #include <sched.h>
  32. #include <sys/resource.h>
  33. #include <sys/sysinfo.h>
  34. /* SCHED_ISO may not be defined as it is a reserved value not yet
  35. * implemented in official kernel sources, see linux/sched.h.
  36. */
  37. #ifndef SCHED_ISO
  38. #define SCHED_ISO 4
  39. #endif
  40. /**
  41. * Apply scheduling policies
  42. *
  43. * This tries to change the scheduler of the client to soft realtime mode
  44. * available in some kernels as SCHED_ISO. It also tries to adjust the nice
  45. * level. If some of each fail, ignore this and log a warning.
  46. */
  47. #define RENICE_INVALID -128 /* Special value to store invalid value */
  48. int game_mode_get_renice(const pid_t client)
  49. {
  50. /* Clear errno as -1 is a regitimate return */
  51. errno = 0;
  52. int priority = getpriority(PRIO_PROCESS, (id_t)client);
  53. if (priority == -1 && errno) {
  54. LOG_ERROR("getprority(PRIO_PROCESS, %d) failed : %s\n", client, strerror(errno));
  55. return RENICE_INVALID;
  56. }
  57. return -priority;
  58. }
  59. /* If expected is 0 then we try to apply our renice, otherwise, we try to remove it */
  60. void game_mode_apply_renice(const GameModeContext *self, const pid_t client, int expected)
  61. {
  62. if (expected == RENICE_INVALID)
  63. /* Silently bail if fed an invalid value */
  64. return;
  65. GameModeConfig *config = game_mode_config_from_context(self);
  66. /*
  67. * read configuration "renice" (1..20)
  68. */
  69. long int renice = config_get_renice_value(config);
  70. if (renice == 0) {
  71. return;
  72. }
  73. /* Invert the renice value */
  74. renice = -renice;
  75. /* When expected is non-zero, we should try and remove the renice only if it doesn't match the
  76. * expected value */
  77. if (expected != 0) {
  78. expected = (int)renice;
  79. renice = 0;
  80. }
  81. /* Open the tasks dir for the client */
  82. char tasks[128];
  83. snprintf(tasks, sizeof(tasks), "/proc/%d/task", client);
  84. DIR *client_task_dir = opendir(tasks);
  85. if (client_task_dir == NULL) {
  86. LOG_ERROR("Could not inspect tasks for client [%d]! Skipping ioprio optimisation.\n",
  87. client);
  88. return;
  89. }
  90. /* Iterate for all tasks of client process */
  91. struct dirent *tid_entry;
  92. while ((tid_entry = readdir(client_task_dir)) != NULL) {
  93. /* Skip . and .. */
  94. if (tid_entry->d_name[0] == '.')
  95. continue;
  96. /* task name is the name of the file */
  97. int tid = atoi(tid_entry->d_name);
  98. /* Clear errno as -1 is a regitimate return */
  99. errno = 0;
  100. int prio = getpriority(PRIO_PROCESS, (id_t)tid);
  101. if (prio == -1 && errno) {
  102. /* Process may well have ended */
  103. LOG_ERROR("getpriority failed for client [%d,%d] with error: %s\n",
  104. client,
  105. tid,
  106. strerror(errno));
  107. } else if (prio != expected) {
  108. /*
  109. * Don't adjust priority if it does not match the expected value
  110. * ie. Another process has changed it, or it began non-standard
  111. */
  112. LOG_ERROR("Refused to renice client [%d,%d]: prio was (%d) but we expected (%d)\n",
  113. client,
  114. tid,
  115. prio,
  116. expected);
  117. } else if (setpriority(PRIO_PROCESS, (id_t)tid, (int)renice)) {
  118. LOG_HINTED(ERROR,
  119. "Failed to renice client [%d,%d], ignoring error condition: %s\n",
  120. " -- Your user may not have permission to do this. Please read the docs\n"
  121. " -- to learn how to adjust the pam limits.\n",
  122. client,
  123. tid,
  124. strerror(errno));
  125. }
  126. }
  127. closedir(client_task_dir);
  128. }
  129. void game_mode_apply_scheduling(const GameModeContext *self, const pid_t client)
  130. {
  131. GameModeConfig *config = game_mode_config_from_context(self);
  132. /*
  133. * read configuration "softrealtime" (on, off, auto)
  134. */
  135. char softrealtime[CONFIG_VALUE_MAX] = { 0 };
  136. config_get_soft_realtime(config, softrealtime);
  137. /*
  138. * Enable unconditionally or auto-detect soft realtime usage,
  139. * auto detection is based on observations where dual-core CPU suffered
  140. * priority inversion problems with the graphics driver thus running
  141. * slower as a result, so enable only with more than 3 cores.
  142. */
  143. bool enable_softrealtime = (strcmp(softrealtime, "on") == 0) ||
  144. ((strcmp(softrealtime, "auto") == 0) && (get_nprocs() > 3));
  145. /*
  146. * Actually apply the scheduler policy if not explicitly turned off
  147. */
  148. if (enable_softrealtime) {
  149. const struct sched_param p = { .sched_priority = 0 };
  150. if (sched_setscheduler(client, SCHED_ISO | SCHED_RESET_ON_FORK, &p)) {
  151. const char *hint = "";
  152. HINT_ONCE_ON(
  153. errno == EPERM,
  154. hint,
  155. " -- The error indicates that you may be running a resource management\n"
  156. " -- daemon managing your game launcher and it leaks lower scheduling\n"
  157. " -- classes into the games. This is likely a bug in the management daemon\n"
  158. " -- and not a bug in GameMode, it should be reported upstream.\n"
  159. " -- If unsure, please also look here:\n"
  160. " -- https://github.com/FeralInteractive/gamemode/issues/68\n");
  161. HINT_ONCE_ON(
  162. errno == EINVAL,
  163. hint,
  164. " -- The error indicates that your kernel may not support this. If you\n"
  165. " -- don't know what SCHED_ISO means, you can safely ignore this. If you\n"
  166. " -- expected it to work, ensure you're running a kernel with MuQSS or\n"
  167. " -- PDS scheduler.\n"
  168. " -- For further technical reading on the topic start here:\n"
  169. " -- https://lwn.net/Articles/720227/\n");
  170. LOG_ERROR(
  171. "Failed setting client [%d] into SCHED_ISO mode, ignoring error condition: %s\n"
  172. "%s",
  173. client,
  174. strerror(errno),
  175. hint);
  176. }
  177. }
  178. }