PAPI  5.3.0.0
rapl_overflow.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "papi.h"
00003 #include "papi_test.h"
00004 
00005 static int total = 0;                  /* total overflows */
00006 
00007 static long long values[2];
00008 static long long rapl_values[2];
00009 static long long old_rapl_values[2] = {0,0};
00010 static int rapl_backward=0;
00011 
00012 int EventSet2=PAPI_NULL;
00013 
00014 int quiet=0;
00015 
00016 void handler( int EventSet, void *address, 
00017           long long overflow_vector, void *context ) {
00018 
00019     ( void ) context;
00020     ( void ) address;
00021     ( void ) overflow_vector;
00022 
00023 #if 0
00024     fprintf( stderr, "handler(%d ) Overflow at %p! bit=0x%llx \n",
00025                          EventSet, address, overflow_vector );
00026 #endif
00027     
00028     PAPI_read(EventSet,values);
00029     if (!quiet) printf("%lld %lld\t",values[0],values[1]);
00030     PAPI_read(EventSet2,rapl_values);
00031     if (!quiet) printf("RAPL: %lld %lld\n",rapl_values[0],rapl_values[1]);
00032 
00033     if ((rapl_values[0]<old_rapl_values[0]) ||
00034         (rapl_values[1]<old_rapl_values[1])) {
00035        if (!quiet) printf("RAPL decreased!\n");
00036        rapl_backward=1;
00037     }
00038     old_rapl_values[0]=rapl_values[0];
00039     old_rapl_values[1]=rapl_values[1];
00040 
00041 
00042     total++;
00043 }
00044 
00045 
00046 void do_ints(int n,int quiet)
00047 {
00048   int i,c=n;
00049 
00050   for(i=0;i<n;i++) {
00051      c+=c*i*n;
00052   }
00053   if (!quiet) printf("%d\n",c);
00054 }
00055 
00056 
00057 
00058 int
00059 main( int argc, char **argv )
00060 {
00061     int EventSet = PAPI_NULL;
00062     long long values0[2],values1[2],values2[2];
00063     int num_flops = 3000000, retval;
00064     int mythreshold = 1000000;
00065     char event_name1[PAPI_MAX_STR_LEN];
00066         int PAPI_event;
00067     int cid,numcmp,rapl_cid;
00068     const PAPI_component_info_t *cmpinfo = NULL;
00069     int i;
00070 
00071     /* Set TESTS_QUIET variable */
00072     tests_quiet( argc, argv );      
00073 
00074     quiet=TESTS_QUIET;
00075 
00076     /* Init PAPI */
00077     retval = PAPI_library_init( PAPI_VER_CURRENT );
00078     if ( retval != PAPI_VER_CURRENT ) {
00079       test_fail(__FILE__, __LINE__,"PAPI_library_init",retval);
00080     }
00081 
00082     numcmp = PAPI_num_components();
00083 
00084     for(cid=0; cid<numcmp; cid++) {
00085 
00086       if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
00087         test_fail(__FILE__, __LINE__,"PAPI_get_component_info failed\n", 0);
00088       }
00089 
00090       if (strstr(cmpinfo->name,"rapl")) {
00091         rapl_cid=cid;
00092         if (!TESTS_QUIET) printf("Found rapl component at cid %d\n",
00093                      rapl_cid);
00094         if (cmpinfo->num_native_events==0) {
00095               test_skip(__FILE__,__LINE__,"No rapl events found",0);
00096         }
00097         break;
00098       }
00099     }
00100 
00101     /* Component not found */
00102     if (cid==numcmp) {
00103       test_skip(__FILE__,__LINE__,"No rapl component found\n",0);
00104     }
00105 
00106 
00107     /* add PAPI_TOT_CYC and PAPI_TOT_INS */
00108     retval=PAPI_create_eventset(&EventSet);
00109     if ( retval != PAPI_OK ) {
00110        test_fail(__FILE__, __LINE__,"PAPI_create_eventset",retval);
00111     }
00112 
00113     retval=PAPI_add_event(EventSet,PAPI_TOT_CYC);
00114     if ( retval != PAPI_OK ) {
00115        test_fail(__FILE__, __LINE__,"PAPI_add_event",retval);
00116     }
00117 
00118     retval=PAPI_add_event(EventSet,PAPI_TOT_INS);
00119     if ( retval != PAPI_OK ) {
00120        test_fail(__FILE__, __LINE__,"PAPI_add_event",retval);
00121     }
00122 
00123     /* Add some RAPL events */
00124     retval=PAPI_create_eventset(&EventSet2);
00125     if ( retval != PAPI_OK ) {
00126        test_fail(__FILE__, __LINE__,"PAPI_create_eventset",retval);
00127     }
00128 
00129     /* Add an event for each packages 0-n  */
00130     i = 0;
00131     do {
00132         char buffer[80];
00133         sprintf(&(buffer[0]), "rapl:::PACKAGE_ENERGY:PACKAGE%d", i);
00134         retval=PAPI_add_named_event(EventSet2,buffer);
00135         ++i;
00136     /* protect against insane PAPI library, the value 64 is the same value as 
00137      * RAPL_MAX_COUNTERS in linux-rapl.c, and feels reasonable. */
00138     } while ( 0 < retval && i < 64 );
00139 
00140     PAPI_event=PAPI_TOT_CYC;
00141 
00142     /* arbitrary */
00143     mythreshold = 2000000;
00144     if (!TESTS_QUIET) {
00145        printf("Using %#x for the overflow event, threshold %d\n",
00146           PAPI_event,mythreshold);
00147     }
00148 
00149     /* Start the run calibration run */
00150     retval = PAPI_start( EventSet );
00151     if ( retval != PAPI_OK ) {
00152        test_fail(__FILE__, __LINE__,"PAPI_start",retval);
00153     }
00154 
00155     do_ints(num_flops,TESTS_QUIET);
00156     do_flops( 3000000 );
00157 
00158     /* stop the calibration run */
00159     retval = PAPI_stop( EventSet, values0 );
00160     if ( retval != PAPI_OK ) {
00161        test_fail(__FILE__, __LINE__,"PAPI_stop",retval);
00162     }
00163 
00164 
00165     /* set up overflow handler */
00166     retval = PAPI_overflow( EventSet,PAPI_event,mythreshold, 0, handler );
00167     if ( retval != PAPI_OK ) {
00168        test_fail(__FILE__, __LINE__,"PAPI_overflow",retval);
00169     }
00170 
00171     /* Start overflow run */
00172     retval = PAPI_start( EventSet );
00173     if ( retval != PAPI_OK ) {
00174        test_fail(__FILE__, __LINE__,"PAPI_start",retval);
00175     }
00176     retval = PAPI_start( EventSet2 );
00177     if ( retval != PAPI_OK ) {
00178        test_fail(__FILE__, __LINE__,"PAPI_start",retval);
00179     }
00180 
00181     do_ints(num_flops,TESTS_QUIET);
00182     do_flops( num_flops );
00183 
00184     /* stop overflow run */
00185     retval = PAPI_stop( EventSet, values1 );
00186     if ( retval != PAPI_OK ) {
00187        test_fail(__FILE__, __LINE__,"PAPI_stop",retval);
00188     }
00189 
00190     retval = PAPI_stop( EventSet2, values2 );
00191     if ( retval != PAPI_OK ) {
00192        test_fail(__FILE__, __LINE__,"PAPI_stop",retval);
00193     }
00194 
00195     retval = PAPI_overflow( EventSet, PAPI_event, 0, 0, handler );
00196     if ( retval != PAPI_OK ) {
00197        test_fail(__FILE__, __LINE__,"PAPI_overflow",retval);
00198     }
00199 
00200     retval = PAPI_event_code_to_name( PAPI_event, event_name1 );
00201     if (retval != PAPI_OK) {
00202        test_fail(__FILE__, __LINE__,"PAPI_event_code_to_name\n", retval);
00203     }
00204 
00205     if (!TESTS_QUIET) {
00206        printf("%s: %lld %lld\n",event_name1,values0[0],values1[0]);
00207     }
00208 
00209     retval = PAPI_event_code_to_name( PAPI_TOT_INS, event_name1 );
00210     if (retval != PAPI_OK) {
00211       test_fail(__FILE__, __LINE__,"PAPI_event_code_to_name\n",retval);
00212     }
00213 
00214     if (!TESTS_QUIET) {
00215        printf("%s: %lld %lld\n",event_name1,values0[1],values1[1]);
00216     }
00217 
00218     retval = PAPI_cleanup_eventset( EventSet );
00219     if ( retval != PAPI_OK ) {
00220       test_fail(__FILE__, __LINE__,"PAPI_cleanup_eventset",retval);
00221     }
00222 
00223     retval = PAPI_destroy_eventset( &EventSet );
00224     if ( retval != PAPI_OK ) {
00225        test_fail(__FILE__, __LINE__,"PAPI_destroy_eventset",retval);
00226     }
00227 
00228     if (rapl_backward) {
00229        test_fail(__FILE__, __LINE__,"RAPL counts went backward!",0);
00230     }
00231 
00232     test_pass( __FILE__, NULL, 0 );
00233 
00234 
00235     return 0;
00236 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines