gamemode-sched.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Copyright (c) 2017-2019, Feral Interactive
  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 "daemon_config.h"
  28. #include "gamemode.h"
  29. #include "logging.h"
  30. #include <errno.h>
  31. #include <sched.h>
  32. #include <string.h>
  33. #include <sys/resource.h>
  34. #include <sys/sysinfo.h>
  35. /* SCHED_ISO may not be defined as it is a reserved value not yet
  36. * implemented in official kernel sources, see linux/sched.h.
  37. */
  38. #ifndef SCHED_ISO
  39. #define SCHED_ISO 4
  40. #endif
  41. /**
  42. * Apply scheduling policies
  43. *
  44. * This tries to change the scheduler of the client to soft realtime mode
  45. * available in some kernels as SCHED_ISO. It also tries to adjust the nice
  46. * level. If some of each fail, ignore this and log a warning.
  47. *
  48. * We don't need to store the current values because when the client exits,
  49. * everything will be good: Scheduling is only applied to the client and
  50. * its children.
  51. */
  52. void game_mode_apply_renice(const GameModeContext *self, const pid_t client)
  53. {
  54. GameModeConfig *config = game_mode_config_from_context(self);
  55. /*
  56. * read configuration "renice" (1..20)
  57. */
  58. long int renice = config_get_renice_value(config);
  59. if (renice == 0) {
  60. return;
  61. } else if ((renice < 1) || (renice > 20)) {
  62. LOG_ONCE(ERROR, "Configured renice value '%ld' is invalid, will not renice.\n", renice);
  63. return;
  64. } else {
  65. renice = -renice;
  66. }
  67. /*
  68. * don't adjust priority if it was already adjusted
  69. */
  70. if (getpriority(PRIO_PROCESS, (id_t)client) != 0) {
  71. LOG_ERROR("Refused to renice client [%d]: already reniced\n", client);
  72. } else if (setpriority(PRIO_PROCESS, (id_t)client, (int)renice)) {
  73. LOG_HINTED(ERROR,
  74. "Failed to renice client [%d], ignoring error condition: %s\n",
  75. " -- Your user may not have permission to do this. Please read the docs\n"
  76. " -- to learn how to adjust the pam limits.\n",
  77. client,
  78. strerror(errno));
  79. }
  80. }
  81. void game_mode_apply_scheduling(const GameModeContext *self, const pid_t client)
  82. {
  83. GameModeConfig *config = game_mode_config_from_context(self);
  84. /*
  85. * read configuration "softrealtime" (on, off, auto)
  86. */
  87. char softrealtime[CONFIG_VALUE_MAX] = { 0 };
  88. config_get_soft_realtime(config, softrealtime);
  89. /*
  90. * Enable unconditionally or auto-detect soft realtime usage,
  91. * auto detection is based on observations where dual-core CPU suffered
  92. * priority inversion problems with the graphics driver thus running
  93. * slower as a result, so enable only with more than 3 cores.
  94. */
  95. bool enable_softrealtime = (strcmp(softrealtime, "on") == 0) ||
  96. ((strcmp(softrealtime, "auto") == 0) && (get_nprocs() > 3));
  97. /*
  98. * Actually apply the scheduler policy if not explicitly turned off
  99. */
  100. if (enable_softrealtime) {
  101. const struct sched_param p = { .sched_priority = 0 };
  102. if (sched_setscheduler(client, SCHED_ISO | SCHED_RESET_ON_FORK, &p)) {
  103. const char *hint = "";
  104. HINT_ONCE_ON(
  105. errno == EPERM,
  106. hint,
  107. " -- The error indicates that you may be running a resource management\n"
  108. " -- daemon managing your game launcher and it leaks lower scheduling\n"
  109. " -- classes into the games. This is likely a bug in the management daemon\n"
  110. " -- and not a bug in GameMode, it should be reported upstream.\n"
  111. " -- If unsure, please also look here:\n"
  112. " -- https://github.com/FeralInteractive/gamemode/issues/68\n");
  113. HINT_ONCE_ON(
  114. errno == EINVAL,
  115. hint,
  116. " -- The error indicates that your kernel may not support this. If you\n"
  117. " -- don't know what SCHED_ISO means, you can safely ignore this. If you\n"
  118. " -- expected it to work, ensure you're running a kernel with MuQSS or\n"
  119. " -- PDS scheduler.\n"
  120. " -- For further technical reading on the topic start here:\n"
  121. " -- https://lwn.net/Articles/720227/\n");
  122. LOG_ERROR(
  123. "Failed setting client [%d] into SCHED_ISO mode, ignoring error condition: %s\n"
  124. "%s",
  125. client,
  126. strerror(errno),
  127. hint);
  128. }
  129. }
  130. }