cpugovctl.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "logging.h"
  27. #include <sys/types.h>
  28. #include <dirent.h>
  29. #include <ctype.h>
  30. #define MAX_GOVERNORS 128
  31. #define MAX_GOVERNOR_LENGTH PATH_MAX+1
  32. // Governers are located at /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
  33. static int fetch_governors( char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] )
  34. {
  35. const char* cpu_base_path = "/sys/devices/system/cpu/";
  36. DIR* dir = opendir( cpu_base_path );
  37. if( !dir )
  38. FATAL_ERRORNO( "cpu device path not found" );
  39. int num_governors = 0;
  40. // Explore the directory
  41. struct dirent* ent;
  42. while( ( ent = readdir(dir) ) && num_governors < MAX_GOVERNORS )
  43. {
  44. // CPU directories all start with "cpu"
  45. if( strncmp( ent->d_name, "cpu", 3 ) == 0 )
  46. {
  47. // Check if this matches "cpu\d+"
  48. const int len = strlen( ent->d_name );
  49. if( len > 3 && len < 5
  50. && isdigit( ent->d_name[3] ) )
  51. {
  52. // Construct the full path
  53. char path[PATH_MAX] = {};
  54. snprintf( path, sizeof(path), "%s%s/cpufreq/scaling_governor", cpu_base_path, ent->d_name );
  55. // Get the real path to the file
  56. // Traditionally cpufreq symlinks to a policy directory that can be shared
  57. // So let's prevent duplicates
  58. char fullpath[PATH_MAX] = {};
  59. const char* ptr = realpath( path, fullpath );
  60. if( fullpath != ptr )
  61. continue;
  62. // Only add if unique
  63. for( int i = 0; i < num_governors; i++ )
  64. {
  65. if( strncmp( fullpath, governors[i], MAX_GOVERNOR_LENGTH ) == 0 )
  66. continue;
  67. }
  68. strncpy( governors[num_governors], fullpath, MAX_GOVERNOR_LENGTH );
  69. num_governors++;
  70. }
  71. }
  72. }
  73. closedir(dir);
  74. return num_governors;
  75. }
  76. // Get the current governor state
  77. const char* get_gov_state()
  78. {
  79. // To be returned
  80. static char governor[64] = {0};
  81. // State of all the overnors
  82. char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = {{0}};
  83. int num = fetch_governors( governors );
  84. // Check the list
  85. for( int i = 0; i < num; i++ )
  86. {
  87. const char* gov = governors[i];
  88. FILE* f = fopen( gov, "r" );
  89. if( !f )
  90. {
  91. LOG_ERROR( "Failed to open file for read %s\n", gov );
  92. continue;
  93. }
  94. // Pull out the file contents
  95. fseek( f, 0, SEEK_END );
  96. int length = ftell(f);
  97. fseek( f, 0, SEEK_SET );
  98. char contents[length];
  99. if( fread(contents, 1, length, f) > 0 )
  100. {
  101. // Files have a newline
  102. strtok(contents, "\n");
  103. if( strlen(governor) > 0 && strncmp( governor, contents, 64 ) != 0 )
  104. {
  105. // Don't handle the mixed case, this shouldn't ever happen
  106. // But it is a clear sign we shouldn't carry on
  107. LOG_ERROR( "Governors malformed: got \"%s\", expected \"%s\"", contents, governor );
  108. return "malformed";
  109. }
  110. strncpy( governor, contents, sizeof(governor) );
  111. }
  112. else
  113. {
  114. LOG_ERROR( "Failed to read contents of %s\n", gov );
  115. }
  116. fclose( f );
  117. }
  118. return governor;
  119. }
  120. // Sets all governors to a value
  121. void set_gov_state( const char* value )
  122. {
  123. char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH] = {{0}};
  124. int num = fetch_governors( governors );
  125. LOG_MSG( "Setting governors to %s\n", value );
  126. for( int i = 0; i < num; i++ )
  127. {
  128. const char* gov = governors[i];
  129. FILE* f = fopen( gov, "w" );
  130. if( !f )
  131. {
  132. LOG_ERROR( "Failed to open file for write %s\n", gov );
  133. continue;
  134. }
  135. fprintf( f, "%s\n", value );
  136. fclose( f );
  137. }
  138. }
  139. // Main entry point
  140. int main( int argc, char *argv[] )
  141. {
  142. if( argc < 2 )
  143. {
  144. fprintf( stderr, "usage: cpugovctl [get] [set VALUE]\n" );
  145. exit( EXIT_FAILURE );
  146. }
  147. if( strncmp( argv[1], "get", 3 ) == 0 )
  148. {
  149. printf( "%s", get_gov_state() );
  150. }
  151. else if( strncmp( argv[1], "set", 3 ) == 0 )
  152. {
  153. const char* value = argv[2];
  154. set_gov_state( value );
  155. }
  156. else
  157. {
  158. exit( EXIT_FAILURE );
  159. }
  160. }