|
PAPI
5.1.0.2
|
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 } while (retval); 00137 00138 if ( i>1 && retval != PAPI_OK ) { 00139 test_fail(__FILE__, __LINE__,"PAPI_add_event",retval); 00140 } 00141 00142 PAPI_event=PAPI_TOT_CYC; 00143 00144 /* arbitrary */ 00145 mythreshold = 2000000; 00146 if (!TESTS_QUIET) { 00147 printf("Using %x for the overflow event, threshold %d\n", 00148 PAPI_event,mythreshold); 00149 } 00150 00151 /* Start the run calibration run */ 00152 retval = PAPI_start( EventSet ); 00153 if ( retval != PAPI_OK ) { 00154 test_fail(__FILE__, __LINE__,"PAPI_start",retval); 00155 } 00156 00157 do_ints(num_flops,TESTS_QUIET); 00158 do_flops( 3000000 ); 00159 00160 /* stop the calibration run */ 00161 retval = PAPI_stop( EventSet, values0 ); 00162 if ( retval != PAPI_OK ) { 00163 test_fail(__FILE__, __LINE__,"PAPI_stop",retval); 00164 } 00165 00166 00167 /* set up overflow handler */ 00168 retval = PAPI_overflow( EventSet,PAPI_event,mythreshold, 0, handler ); 00169 if ( retval != PAPI_OK ) { 00170 test_fail(__FILE__, __LINE__,"PAPI_overflow",retval); 00171 } 00172 00173 /* Start overflow run */ 00174 retval = PAPI_start( EventSet ); 00175 if ( retval != PAPI_OK ) { 00176 test_fail(__FILE__, __LINE__,"PAPI_start",retval); 00177 } 00178 retval = PAPI_start( EventSet2 ); 00179 if ( retval != PAPI_OK ) { 00180 test_fail(__FILE__, __LINE__,"PAPI_start",retval); 00181 } 00182 00183 do_ints(num_flops,TESTS_QUIET); 00184 do_flops( num_flops ); 00185 00186 /* stop overflow run */ 00187 retval = PAPI_stop( EventSet, values1 ); 00188 if ( retval != PAPI_OK ) { 00189 test_fail(__FILE__, __LINE__,"PAPI_stop",retval); 00190 } 00191 00192 retval = PAPI_stop( EventSet2, values2 ); 00193 if ( retval != PAPI_OK ) { 00194 test_fail(__FILE__, __LINE__,"PAPI_stop",retval); 00195 } 00196 00197 retval = PAPI_overflow( EventSet, PAPI_event, 0, 0, handler ); 00198 if ( retval != PAPI_OK ) { 00199 test_fail(__FILE__, __LINE__,"PAPI_overflow",retval); 00200 } 00201 00202 retval = PAPI_event_code_to_name( PAPI_event, event_name1 ); 00203 if (retval != PAPI_OK) { 00204 test_fail(__FILE__, __LINE__,"PAPI_event_code_to_name\n", retval); 00205 } 00206 00207 if (!TESTS_QUIET) { 00208 printf("%s: %lld %lld\n",event_name1,values0[0],values1[0]); 00209 } 00210 00211 retval = PAPI_event_code_to_name( PAPI_TOT_INS, event_name1 ); 00212 if (retval != PAPI_OK) { 00213 test_fail(__FILE__, __LINE__,"PAPI_event_code_to_name\n",retval); 00214 } 00215 00216 if (!TESTS_QUIET) { 00217 printf("%s: %lld %lld\n",event_name1,values0[1],values1[1]); 00218 } 00219 00220 retval = PAPI_cleanup_eventset( EventSet ); 00221 if ( retval != PAPI_OK ) { 00222 test_fail(__FILE__, __LINE__,"PAPI_cleanup_eventset",retval); 00223 } 00224 00225 retval = PAPI_destroy_eventset( &EventSet ); 00226 if ( retval != PAPI_OK ) { 00227 test_fail(__FILE__, __LINE__,"PAPI_destroy_eventset",retval); 00228 } 00229 00230 if (rapl_backward) { 00231 test_fail(__FILE__, __LINE__,"RAPL counts went backward!",0); 00232 } 00233 00234 test_pass( __FILE__, NULL, 0 ); 00235 00236 00237 return 0; 00238 }