123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- #include "logging.h"
- #include <sys/types.h>
- #include <dirent.h>
- #include <ctype.h>
- static const int max_governors = 128;
- static const int max_governor_length = PATH_MAX+1;
- static int fetch_governors( char governors[max_governors][max_governor_length] )
- {
- const char* cpu_base_path = "/sys/devices/system/cpu/";
- DIR* dir = opendir( cpu_base_path );
- if( !dir )
- FATAL_ERRORNO( "cpu device path not found" );
- int num_governors = 0;
-
- struct dirent* ent;
- while( ( ent = readdir(dir) ) && num_governors < max_governors )
- {
-
- if( strncmp( ent->d_name, "cpu", 3 ) == 0 )
- {
-
- const int len = strlen( ent->d_name );
- if( len > 3 && len < 5
- && isdigit( ent->d_name[3] ) )
- {
-
- char path[PATH_MAX] = {};
- snprintf( path, sizeof(path), "%s%s/cpufreq/scaling_governor", cpu_base_path, ent->d_name );
-
-
-
- char fullpath[PATH_MAX] = {};
- const char* ptr = realpath( path, fullpath );
- if( fullpath != ptr )
- continue;
-
- for( int i = 0; i < num_governors; i++ )
- {
- if( strncmp( fullpath, governors[i], max_governor_length ) == 0 )
- continue;
- }
- strncpy( governors[num_governors], fullpath, max_governor_length );
- num_governors++;
- }
- }
- }
- closedir(dir);
- return num_governors;
- }
- const char* get_gov_state()
- {
-
- static char governor[64];
- memset( governor, 0, sizeof(governor) );
-
- char governors[max_governors][max_governor_length];
- memset( governors, 0, sizeof(governors) );
- int num = fetch_governors( governors );
-
- for( int i = 0; i < num; i++ )
- {
- const char* gov = governors[i];
- FILE* f = fopen( gov, "r" );
- if( !f )
- {
- LOG_ERROR( "Failed to open file for read %s\n", gov );
- continue;
- }
-
- fseek( f, 0, SEEK_END );
- int length = ftell(f);
- fseek( f, 0, SEEK_SET );
- char contents[length];
- if( fread(contents, 1, length, f) > 0 )
- {
-
- strtok(contents, "\n");
- if( strlen(governor) > 0 && strncmp( governor, contents, 64 ) != 0 )
- {
-
-
- LOG_ERROR( "Governors malformed: got \"%s\", expected \"%s\"", contents, governor );
- return "malformed";
- }
-
- strncpy( governor, contents, sizeof(governor) );
- }
- else
- {
- LOG_ERROR( "Failed to read contents of %s\n", gov );
- }
- fclose( f );
- }
- return governor;
- }
- void set_gov_state( const char* value )
- {
- char governors[max_governors][max_governor_length];
- memset( governors, 0, sizeof(governors) );
- int num = fetch_governors( governors );
- LOG_MSG( "Setting governors to %s\n", value );
- for( int i = 0; i < num; i++ )
- {
- const char* gov = governors[i];
- FILE* f = fopen( gov, "w" );
- if( !f )
- {
- LOG_ERROR( "Failed to open file for write %s\n", gov );
- continue;
- }
- fprintf( f, "%s\n", value );
- fclose( f );
- }
- }
- int main( int argc, char *argv[] )
- {
- if( argc < 2 )
- {
- fprintf( stderr, "usage: cpugovctl [get] [set VALUE]\n" );
- exit( EXIT_FAILURE );
- }
- if( strncmp( argv[1], "get", 3 ) == 0 )
- {
- printf( "%s", get_gov_state() );
- }
- else if( strncmp( argv[1], "set", 3 ) == 0 )
- {
- const char* value = argv[2];
- set_gov_state( value );
- }
- else
- {
- exit( EXIT_FAILURE );
- }
- }
|