cpugovctl.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. static const int max_governors = 128;
  31. static const int 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];
  81. memset( governor, 0, sizeof(governor) );
  82. // State of all the overnors
  83. char governors[max_governors][max_governor_length];
  84. memset( governors, 0, sizeof(governors) );
  85. int num = fetch_governors( governors );
  86. // Check the list
  87. for( int i = 0; i < num; i++ )
  88. {
  89. const char* gov = governors[i];
  90. FILE* f = fopen( gov, "r" );
  91. if( !f )
  92. {
  93. LOG_ERROR( "Failed to open file for read %s\n", gov );
  94. continue;
  95. }
  96. // Pull out the file contents
  97. fseek( f, 0, SEEK_END );
  98. int length = ftell(f);
  99. fseek( f, 0, SEEK_SET );
  100. char contents[length];
  101. if( fread(contents, 1, length, f) > 0 )
  102. {
  103. // Files have a newline
  104. strtok(contents, "\n");
  105. if( strlen(governor) > 0 && strncmp( governor, contents, 64 ) != 0 )
  106. {
  107. // Don't handle the mixed case, this shouldn't ever happen
  108. // But it is a clear sign we shouldn't carry on
  109. LOG_ERROR( "Governors malformed: got \"%s\", expected \"%s\"", contents, governor );
  110. return "malformed";
  111. }
  112. strncpy( governor, contents, sizeof(governor) );
  113. }
  114. else
  115. {
  116. LOG_ERROR( "Failed to read contents of %s\n", gov );
  117. }
  118. fclose( f );
  119. }
  120. return governor;
  121. }
  122. // Sets all governors to a value
  123. void set_gov_state( const char* value )
  124. {
  125. char governors[max_governors][max_governor_length];
  126. memset( governors, 0, sizeof(governors) );
  127. int num = fetch_governors( governors );
  128. LOG_MSG( "Setting governors to %s\n", value );
  129. for( int i = 0; i < num; i++ )
  130. {
  131. const char* gov = governors[i];
  132. FILE* f = fopen( gov, "w" );
  133. if( !f )
  134. {
  135. LOG_ERROR( "Failed to open file for write %s\n", gov );
  136. continue;
  137. }
  138. fprintf( f, "%s\n", value );
  139. fclose( f );
  140. }
  141. }
  142. // Main entry point
  143. int main( int argc, char *argv[] )
  144. {
  145. if( argc < 2 )
  146. {
  147. fprintf( stderr, "usage: cpugovctl [get] [set VALUE]\n" );
  148. exit( EXIT_FAILURE );
  149. }
  150. if( strncmp( argv[1], "get", 3 ) == 0 )
  151. {
  152. printf( "%s", get_gov_state() );
  153. }
  154. else if( strncmp( argv[1], "set", 3 ) == 0 )
  155. {
  156. const char* value = argv[2];
  157. set_gov_state( value );
  158. }
  159. else
  160. {
  161. exit( EXIT_FAILURE );
  162. }
  163. }