@@ -7404,6 +7404,160 @@ void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
74047404 return ;
74057405}
74067406
7407+ static int fread_int (char * path , int * val )
7408+ {
7409+ FILE * filep ;
7410+ int ret ;
7411+
7412+ filep = fopen (path , "r" );
7413+ if (!filep )
7414+ return -1 ;
7415+
7416+ ret = fscanf (filep , "%d" , val );
7417+ fclose (filep );
7418+ return ret ;
7419+ }
7420+
7421+ static int fread_ull (char * path , unsigned long long * val )
7422+ {
7423+ FILE * filep ;
7424+ int ret ;
7425+
7426+ filep = fopen (path , "r" );
7427+ if (!filep )
7428+ return -1 ;
7429+
7430+ ret = fscanf (filep , "%llu" , val );
7431+ fclose (filep );
7432+ return ret ;
7433+ }
7434+
7435+ static int fread_str (char * path , char * buf , int size )
7436+ {
7437+ FILE * filep ;
7438+ int ret ;
7439+ char * cp ;
7440+
7441+ filep = fopen (path , "r" );
7442+ if (!filep )
7443+ return -1 ;
7444+
7445+ ret = fread (buf , 1 , size , filep );
7446+ fclose (filep );
7447+
7448+ /* replace '\n' with '\0' */
7449+ cp = strchr (buf , '\n' );
7450+ if (cp != NULL )
7451+ * cp = '\0' ;
7452+
7453+ return ret ;
7454+ }
7455+
7456+ #define PATH_RAPL_SYSFS "/sys/class/powercap"
7457+
7458+ static int dump_one_domain (char * domain_path )
7459+ {
7460+ char path [PATH_MAX ];
7461+ char str [PATH_MAX ];
7462+ unsigned long long val ;
7463+ int constraint ;
7464+ int enable ;
7465+ int ret ;
7466+
7467+ snprintf (path , PATH_MAX , "%s/name" , domain_path );
7468+ ret = fread_str (path , str , PATH_MAX );
7469+ if (ret <= 0 )
7470+ return -1 ;
7471+
7472+ fprintf (outf , "%s: %s" , domain_path + strlen (PATH_RAPL_SYSFS ) + 1 , str );
7473+
7474+ snprintf (path , PATH_MAX , "%s/enabled" , domain_path );
7475+ ret = fread_int (path , & enable );
7476+ if (ret <= 0 )
7477+ return -1 ;
7478+
7479+ if (!enable ) {
7480+ fputs (" disabled\n" , outf );
7481+ return 0 ;
7482+ }
7483+
7484+ for (constraint = 0 ;; constraint ++ )
7485+ {
7486+ snprintf (path , PATH_MAX , "%s/constraint_%d_time_window_us" , domain_path , constraint );
7487+ ret = fread_ull (path , & val );
7488+ if (ret <= 0 )
7489+ break ;
7490+
7491+ if (val > 1000000 )
7492+ fprintf (outf , " %0.1fs" , (double )val /1000000 );
7493+ else if (val > 1000 )
7494+ fprintf (outf , " %0.1fms" , (double )val /1000 );
7495+ else
7496+ fprintf (outf , " %0.1fus" , (double )val );
7497+
7498+ snprintf (path , PATH_MAX , "%s/constraint_%d_power_limit_uw" , domain_path , constraint );
7499+ ret = fread_ull (path , & val );
7500+ if (ret > 0 && val )
7501+ fprintf (outf , ":%lluW" , val / 1000000 );
7502+
7503+ snprintf (path , PATH_MAX , "%s/constraint_%d_max_power_uw" , domain_path , constraint );
7504+ ret = fread_ull (path , & val );
7505+ if (ret > 0 && val )
7506+ fprintf (outf , ",max:%lluW" , val / 1000000 );
7507+ }
7508+ fputc ('\n' , outf );
7509+
7510+ return 0 ;
7511+ }
7512+
7513+ static int print_rapl_sysfs (void )
7514+ {
7515+ DIR * dir , * cdir ;
7516+ struct dirent * entry , * centry ;
7517+ char path [PATH_MAX ];
7518+ char str [PATH_MAX ];
7519+
7520+ if ((dir = opendir (PATH_RAPL_SYSFS )) == NULL ) {
7521+ warn ("open %s failed" , PATH_RAPL_SYSFS );
7522+ return 1 ;
7523+ }
7524+
7525+ while ((entry = readdir (dir )) != NULL ) {
7526+ if (strlen (entry -> d_name ) > 100 )
7527+ continue ;
7528+
7529+ if (strncmp (entry -> d_name , "intel-rapl" , strlen ("intel-rapl" )))
7530+ continue ;
7531+
7532+ snprintf (path , PATH_MAX , "%s/%s/name" , PATH_RAPL_SYSFS , entry -> d_name );
7533+
7534+ /* Parse top level domains first, including package and psys */
7535+ fread_str (path , str , PATH_MAX );
7536+ if (strncmp (str , "package" , strlen ("package" )) &&
7537+ strncmp (str , "psys" , strlen ("psys" )))
7538+ continue ;
7539+
7540+ snprintf (path , PATH_MAX , "%s/%s" , PATH_RAPL_SYSFS , entry -> d_name );
7541+ if ((cdir = opendir (path )) == NULL ) {
7542+ perror ("opendir() error" );
7543+ return 1 ;
7544+ }
7545+
7546+ dump_one_domain (path );
7547+
7548+ while ((centry = readdir (cdir )) != NULL ) {
7549+ if (strncmp (centry -> d_name , "intel-rapl" , strlen ("intel-rapl" )))
7550+ continue ;
7551+ snprintf (path , PATH_MAX , "%s/%s/%s" , PATH_RAPL_SYSFS , entry -> d_name , centry -> d_name );
7552+ dump_one_domain (path );
7553+ }
7554+ closedir (cdir );
7555+ }
7556+
7557+ closedir (dir );
7558+ return 0 ;
7559+ }
7560+
74077561int print_rapl (struct thread_data * t , struct core_data * c , struct pkg_data * p )
74087562{
74097563 unsigned long long msr ;
@@ -7538,6 +7692,8 @@ void probe_rapl(void)
75387692 if (quiet )
75397693 return ;
75407694
7695+ print_rapl_sysfs ();
7696+
75417697 if (!platform -> rapl_msrs || no_msr )
75427698 return ;
75437699
0 commit comments