gamemode.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Copyright (c) 2017, 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 "governors.h"
  29. #include "logging.h"
  30. #include <signal.h>
  31. #include <string.h>
  32. // Storage for game PIDs
  33. #define MAX_GAMES 16
  34. static int game_pids[MAX_GAMES];
  35. static int num_games = 0;
  36. // Constant to control how often we'll wake up and check process ids
  37. static const int wakeup_timer = 5;
  38. static void start_alarm_timer();
  39. // Called once to enter game mode
  40. // Must be followed by a call to leave_game_mode
  41. // Must be called after init_game_mode
  42. static void enter_game_mode()
  43. {
  44. LOG_MSG("Entering Game Mode...\n");
  45. set_governors("performance");
  46. // Set up the alarm callback
  47. start_alarm_timer();
  48. }
  49. // Called once to leave game mode
  50. // Must be called after an equivelant call to enter_game_mode
  51. static void leave_game_mode()
  52. {
  53. LOG_MSG("Leaving Game Mode...\n");
  54. set_governors(NULL);
  55. }
  56. // Set up an alarm callback to check for current process ids
  57. static void alarm_handler(int sig)
  58. {
  59. // Quick return if no games, and don't register another callback
  60. if (num_games == 0) {
  61. return;
  62. }
  63. // Check if games are alive at all
  64. for (int i = 0; i < num_games;) {
  65. int game = game_pids[i];
  66. if (kill(game, 0) != 0) {
  67. LOG_MSG("Removing expired game [%i]...\n", game);
  68. memmove(&game_pids[i], &game_pids[i + 1], MAX_GAMES - (i + 1));
  69. num_games--;
  70. } else {
  71. i++;
  72. }
  73. }
  74. // Either trigger another alarm, or reset the governors
  75. if (num_games) {
  76. start_alarm_timer();
  77. } else {
  78. leave_game_mode();
  79. }
  80. }
  81. // Call to trigger starting the alarm timer for pid checks
  82. static void start_alarm_timer()
  83. {
  84. // Set up process check alarms
  85. signal(SIGALRM, alarm_handler);
  86. alarm(wakeup_timer);
  87. }
  88. // Intialise any game mode needs
  89. void init_game_mode()
  90. {
  91. // Read current governer state before setting up any message handling
  92. update_initial_gov_state();
  93. LOG_MSG("governor is set to [%s]\n", get_initial_governor());
  94. }
  95. // Called on exit to clean up the governors if appropriate
  96. void term_game_mode()
  97. {
  98. if (num_games) {
  99. leave_game_mode();
  100. }
  101. }
  102. // Register a game pid with the game mode
  103. // Will trigger enter game mode if appropriate
  104. void register_game(int pid)
  105. {
  106. // Check for duplicates
  107. for (int i = 0; i < num_games; i++) {
  108. if (game_pids[i] == pid) {
  109. LOG_ERROR("Addition requested for already known process [%i]\n", pid);
  110. return;
  111. }
  112. }
  113. // Check we've not already hit max
  114. if (num_games == MAX_GAMES) {
  115. LOG_ERROR("Max games (%i) reached, could not add [%i]\n", MAX_GAMES, pid);
  116. return;
  117. }
  118. // Add the game to the database
  119. LOG_MSG("Adding game: %i\n", pid);
  120. game_pids[num_games] = pid;
  121. num_games++;
  122. if (num_games == 1) {
  123. enter_game_mode();
  124. }
  125. }
  126. // Remove a game from game mode
  127. // Will exit game mode if appropriate
  128. void unregister_game(int pid)
  129. {
  130. bool found = false;
  131. // Check list even contains this entry
  132. for (int i = 0; i < num_games; i++) {
  133. if (game_pids[i] == pid) {
  134. LOG_MSG("Removing game: %i\n", pid);
  135. memmove(&game_pids[i], &game_pids[i + 1], MAX_GAMES - (i + 1));
  136. num_games--;
  137. found = true;
  138. }
  139. }
  140. if (!found) {
  141. LOG_ERROR("Removal requested for unknown process [%i]\n", pid);
  142. return;
  143. }
  144. // Leave game mode if needed
  145. if (num_games == 0) {
  146. leave_game_mode();
  147. }
  148. }