gamemode.c 4.6 KB

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