gamemode-ioprio.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /**
  71. * Apply io priorities
  72. *
  73. * This tries to change the io priority of the client to a value specified
  74. * and can possibly reduce lags or latency when a game has to load assets
  75. * on demand.
  76. */
  77. void game_mode_apply_ioprio(const GameModeContext *self, const pid_t client)
  78. {
  79. GameModeConfig *config = game_mode_config_from_context(self);
  80. LOG_MSG("Setting scheduling policies...\n");
  81. /*
  82. * read configuration "ioprio" (0..7)
  83. */
  84. int ioprio = (int)config_get_ioprio_value(config);
  85. if (IOPRIO_RESET_DEFAULT == ioprio) {
  86. LOG_MSG("IO priority will be reset to default behavior (based on CPU priority).\n");
  87. ioprio = 0;
  88. } else if (IOPRIO_DONT_SET == ioprio) {
  89. return;
  90. } else {
  91. /* maybe clamp the value */
  92. int invalid_ioprio = ioprio;
  93. ioprio = CLAMP(0, 7, ioprio);
  94. if (ioprio != invalid_ioprio)
  95. LOG_ONCE(ERROR,
  96. "IO priority value %d invalid, clamping to %d\n",
  97. invalid_ioprio,
  98. ioprio);
  99. /* We support only IOPRIO_CLASS_BE as IOPRIO_CLASS_RT required CAP_SYS_ADMIN */
  100. ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, ioprio);
  101. }
  102. /*
  103. * Actually apply the io priority
  104. */
  105. int c = IOPRIO_PRIO_CLASS(ioprio), p = IOPRIO_PRIO_DATA(ioprio);
  106. if (ioprio_set(IOPRIO_WHO_PROCESS, client, ioprio) == 0) {
  107. if (0 == ioprio)
  108. LOG_MSG("Resetting client [%d] IO priority.\n", client);
  109. else
  110. LOG_MSG("Setting client [%d] IO priority to (%d,%d).\n", client, c, p);
  111. } else {
  112. LOG_ERROR("Setting client [%d] IO priority to (%d,%d) failed with error %d, ignoring\n",
  113. client,
  114. c,
  115. p,
  116. errno);
  117. }
  118. }