gamemode-ioprio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "helpers.h"
  30. #include "logging.h"
  31. #include <errno.h>
  32. #include <sys/syscall.h>
  33. #include <unistd.h>
  34. /**
  35. * Define the syscall interface in Linux because it is missing from glibc
  36. */
  37. #ifndef IOPRIO_BITS
  38. #define IOPRIO_BITS (16)
  39. #endif
  40. #ifndef IOPRIO_CLASS_SHIFT
  41. #define IOPRIO_CLASS_SHIFT (13)
  42. #endif
  43. #ifndef IOPRIO_PRIO_MASK
  44. #define IOPRIO_PRIO_MASK ((1UL << IOPRIO_CLASS_SHIFT) - 1)
  45. #endif
  46. #ifndef IOPRIO_PRIO_CLASS
  47. #define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT)
  48. #endif
  49. #ifndef IOPRIO_PRIO_DATA
  50. #define IOPRIO_PRIO_DATA(mask) ((mask)&IOPRIO_PRIO_MASK)
  51. #endif
  52. #ifndef IOPRIO_PRIO_VALUE
  53. #define IOPRIO_PRIO_VALUE(class, data) (((class) << IOPRIO_CLASS_SHIFT) | data)
  54. #endif
  55. enum {
  56. IOPRIO_CLASS_NONE,
  57. IOPRIO_CLASS_RT,
  58. IOPRIO_CLASS_BE,
  59. IOPRIO_CLASS_IDLE,
  60. };
  61. enum {
  62. IOPRIO_WHO_PROCESS = 1,
  63. IOPRIO_WHO_PGRP,
  64. IOPRIO_WHO_USER,
  65. };
  66. static inline int ioprio_set(int which, int who, int ioprio)
  67. {
  68. return (int)syscall(SYS_ioprio_set, which, who, ioprio);
  69. }
  70. static inline int ioprio_get(int which, int who)
  71. {
  72. return (int)syscall(SYS_ioprio_get, which, who);
  73. }
  74. /**
  75. * Get the i/o priorities
  76. */
  77. int game_mode_get_ioprio(const pid_t client)
  78. {
  79. int ret = ioprio_get(IOPRIO_WHO_PROCESS, client);
  80. if (ret == -1) {
  81. LOG_ERROR("Failed to get ioprio value for [%d] with error %s\n", client, strerror(errno));
  82. ret = IOPRIO_DONT_SET;
  83. }
  84. /* We support only IOPRIO_CLASS_BE as IOPRIO_CLASS_RT required CAP_SYS_ADMIN */
  85. return IOPRIO_PRIO_DATA(ret);
  86. }
  87. /**
  88. * Apply io priorities
  89. *
  90. * This tries to change the io priority of the client to a value specified
  91. * and can possibly reduce lags or latency when a game has to load assets
  92. * on demand.
  93. */
  94. void game_mode_apply_ioprio(const GameModeContext *self, const pid_t client, int expected)
  95. {
  96. if (expected == IOPRIO_DONT_SET)
  97. /* Silently bail if fed a don't set (invalid) */
  98. return;
  99. GameModeConfig *config = game_mode_config_from_context(self);
  100. /* read configuration "ioprio" (0..7) */
  101. int ioprio = (int)config_get_ioprio_value(config);
  102. /* Special value to simply not set the value */
  103. if (ioprio == IOPRIO_DONT_SET)
  104. return;
  105. LOG_MSG("Setting ioprio value...\n");
  106. /* If fed the default, we'll try and reset the value back */
  107. if (expected != IOPRIO_DEFAULT) {
  108. expected = (int)ioprio;
  109. ioprio = IOPRIO_DEFAULT;
  110. }
  111. int current = game_mode_get_ioprio(client);
  112. if (current == IOPRIO_DONT_SET) {
  113. /* Couldn't get the ioprio value, let's bail */
  114. return;
  115. } else if (current != expected) {
  116. /* Don't try and adjust the ioprio value if the value we got doesn't match default */
  117. LOG_ERROR("Refused to set ioprio on client [%d]: prio was (%d) but we expected (%d)\n",
  118. client,
  119. current,
  120. expected);
  121. } else {
  122. /*
  123. * For now we only support IOPRIO_CLASS_BE
  124. * IOPRIO_CLASS_RT requires CAP_SYS_ADMIN but should be possible with a polkit process
  125. */
  126. int p = ioprio;
  127. ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, ioprio);
  128. if (ioprio_set(IOPRIO_WHO_PROCESS, client, ioprio) != 0) {
  129. LOG_ERROR("Setting client [%d] IO priority to (%d) failed with error %d, ignoring\n",
  130. client,
  131. p,
  132. errno);
  133. }
  134. }
  135. }