gamemode-ioprio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "gamemode.h"
  28. #include "common-helpers.h"
  29. #include "common-logging.h"
  30. #include "gamemode-config.h"
  31. #include <dirent.h>
  32. #include <sys/syscall.h>
  33. /**
  34. * Define the syscall interface in Linux because it is missing from glibc
  35. */
  36. #ifndef IOPRIO_BITS
  37. #define IOPRIO_BITS (16)
  38. #endif
  39. #ifndef IOPRIO_CLASS_SHIFT
  40. #define IOPRIO_CLASS_SHIFT (13)
  41. #endif
  42. #ifndef IOPRIO_PRIO_MASK
  43. #define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
  44. #endif
  45. #ifndef IOPRIO_PRIO_CLASS
  46. #define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT)
  47. #endif
  48. #ifndef IOPRIO_PRIO_DATA
  49. #define IOPRIO_PRIO_DATA(mask) ((mask)&IOPRIO_PRIO_MASK)
  50. #endif
  51. #ifndef IOPRIO_PRIO_VALUE
  52. #define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
  53. #endif
  54. enum {
  55. IOPRIO_CLASS_NONE,
  56. IOPRIO_CLASS_RT,
  57. IOPRIO_CLASS_BE,
  58. IOPRIO_CLASS_IDLE,
  59. };
  60. enum {
  61. IOPRIO_WHO_PROCESS = 1,
  62. IOPRIO_WHO_PGRP,
  63. IOPRIO_WHO_USER,
  64. };
  65. static inline int ioprio_set(int which, int who, int ioprio)
  66. {
  67. return (int)syscall(SYS_ioprio_set, which, who, ioprio);
  68. }
  69. static inline int ioprio_get(int which, int who)
  70. {
  71. return (int)syscall(SYS_ioprio_get, which, who);
  72. }
  73. /**
  74. * Get the i/o priorities
  75. */
  76. int game_mode_get_ioprio(const pid_t client)
  77. {
  78. int ret = ioprio_get(IOPRIO_WHO_PROCESS, client);
  79. if (ret == -1) {
  80. LOG_ERROR("Failed to get ioprio value for [%d] with error %s\n", client, strerror(errno));
  81. ret = IOPRIO_DONT_SET;
  82. }
  83. /* We support only IOPRIO_CLASS_BE as IOPRIO_CLASS_RT required CAP_SYS_ADMIN */
  84. return IOPRIO_PRIO_DATA(ret);
  85. }
  86. /**
  87. * Apply io priorities
  88. *
  89. * This tries to change the io priority of the client to a value specified
  90. * and can possibly reduce lags or latency when a game has to load assets
  91. * on demand.
  92. */
  93. void game_mode_apply_ioprio(const GameModeContext *self, const pid_t client, int expected)
  94. {
  95. if (expected == IOPRIO_DONT_SET)
  96. /* Silently bail if fed a don't set (invalid) */
  97. return;
  98. GameModeConfig *config = game_mode_config_from_context(self);
  99. /* read configuration "ioprio" (0..7) */
  100. int ioprio = (int)config_get_ioprio_value(config);
  101. /* Special value to simply not set the value */
  102. if (ioprio == IOPRIO_DONT_SET)
  103. return;
  104. LOG_MSG("Setting ioprio value...\n");
  105. /* If fed the default, we'll try and reset the value back */
  106. if (expected != IOPRIO_DEFAULT) {
  107. expected = (int)ioprio;
  108. ioprio = IOPRIO_DEFAULT;
  109. }
  110. /* Open the tasks dir for the client */
  111. char tasks[128];
  112. snprintf(tasks, sizeof(tasks), "/proc/%d/task", client);
  113. DIR *client_task_dir = opendir(tasks);
  114. if (client_task_dir == NULL) {
  115. LOG_ERROR("Could not inspect tasks for client [%d]! Skipping ioprio optimisation.\n",
  116. client);
  117. return;
  118. }
  119. /* Iterate for all tasks of client process */
  120. struct dirent *tid_entry;
  121. while ((tid_entry = readdir(client_task_dir)) != NULL) {
  122. /* Skip . and .. */
  123. if (tid_entry->d_name[0] == '.')
  124. continue;
  125. /* task name is the name of the file */
  126. int tid = atoi(tid_entry->d_name);
  127. int current = game_mode_get_ioprio(tid);
  128. if (current == IOPRIO_DONT_SET) {
  129. /* Couldn't get the ioprio value
  130. * This could simply mean that the thread exited before fetching the ioprio
  131. * So we should continue
  132. */
  133. } else if (current != expected) {
  134. /* Don't try and adjust the ioprio value if the value we got doesn't match default */
  135. LOG_ERROR("Skipping ioprio on client [%d,%d]: ioprio was (%d) but we expected (%d)\n",
  136. client,
  137. tid,
  138. current,
  139. expected);
  140. } else {
  141. /*
  142. * For now we only support IOPRIO_CLASS_BE
  143. * IOPRIO_CLASS_RT requires CAP_SYS_ADMIN but should be possible with a polkit process
  144. */
  145. int p = ioprio;
  146. ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, ioprio);
  147. if (ioprio_set(IOPRIO_WHO_PROCESS, tid, ioprio) != 0) {
  148. /* This could simply mean the thread is gone now, as above */
  149. LOG_ERROR(
  150. "Setting client [%d,%d] IO priority to (%d) failed with error %d, ignoring.\n",
  151. client,
  152. tid,
  153. p,
  154. errno);
  155. }
  156. }
  157. }
  158. closedir(client_task_dir);
  159. }