|
PAPI
5.0.1.0
|
00001 /****************************/ 00002 /* THIS IS OPEN SOURCE CODE */ 00003 /****************************/ 00004 00021 #include <stdio.h> 00022 #include <stdlib.h> 00023 #include "papi_test.h" 00024 00025 #define NUM_EVENTS 3 00026 00027 int main (int argc, char **argv) 00028 { 00029 00030 int retval,i; 00031 int EventSet = PAPI_NULL; 00032 long long values[NUM_EVENTS]; 00033 const PAPI_component_info_t *cmpinfo = NULL; 00034 int numcmp,cid,example_cid=-1; 00035 int code,maximum_code=0; 00036 char event_name[PAPI_MAX_STR_LEN]; 00037 PAPI_event_info_t event_info; 00038 00039 /* Set TESTS_QUIET variable */ 00040 tests_quiet( argc, argv ); 00041 00042 /* PAPI Initialization */ 00043 retval = PAPI_library_init( PAPI_VER_CURRENT ); 00044 if ( retval != PAPI_VER_CURRENT ) { 00045 test_fail(__FILE__, __LINE__,"PAPI_library_init failed\n",retval); 00046 } 00047 00048 if (!TESTS_QUIET) { 00049 printf( "Testing example component with PAPI %d.%d.%d\n", 00050 PAPI_VERSION_MAJOR( PAPI_VERSION ), 00051 PAPI_VERSION_MINOR( PAPI_VERSION ), 00052 PAPI_VERSION_REVISION( PAPI_VERSION ) ); 00053 } 00054 00055 /* Find our component */ 00056 00057 numcmp = PAPI_num_components(); 00058 for( cid=0; cid<numcmp; cid++) { 00059 if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) { 00060 test_fail(__FILE__, __LINE__, 00061 "PAPI_get_component_info failed\n", 0); 00062 } 00063 if (!TESTS_QUIET) { 00064 printf("\tComponent %d - %d events - %s\n", cid, 00065 cmpinfo->num_native_events, 00066 cmpinfo->name); 00067 } 00068 if (strstr(cmpinfo->name,"example.c")) { 00069 /* FOUND! */ 00070 example_cid=cid; 00071 } 00072 } 00073 00074 00075 if (example_cid<0) { 00076 test_skip(__FILE__, __LINE__, 00077 "Example component not found\n", 0); 00078 } 00079 00080 if (!TESTS_QUIET) { 00081 printf("\nFound Example Component at id %d\n",example_cid); 00082 printf("\nListing all events in this component:\n"); 00083 } 00084 00085 /**************************************************/ 00086 /* Listing all available events in this component */ 00087 /* Along with descriptions */ 00088 /**************************************************/ 00089 code = PAPI_NATIVE_MASK; 00090 00091 retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, example_cid ); 00092 00093 while ( retval == PAPI_OK ) { 00094 if (PAPI_event_code_to_name( code, event_name )!=PAPI_OK) { 00095 printf("Error translating %x\n",code); 00096 test_fail( __FILE__, __LINE__, 00097 "PAPI_event_code_to_name", retval ); 00098 } 00099 00100 if (PAPI_get_event_info( code, &event_info)!=PAPI_OK) { 00101 printf("Error getting info for event %x\n",code); 00102 test_fail( __FILE__, __LINE__, 00103 "PAPI_get_event_info()", retval ); 00104 } 00105 00106 if (!TESTS_QUIET) { 00107 printf("\tEvent 0x%x: %s -- %s\n", 00108 code,event_name,event_info.long_descr); 00109 } 00110 00111 maximum_code=code; 00112 00113 retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, example_cid ); 00114 00115 } 00116 if (!TESTS_QUIET) printf("\n"); 00117 00118 /**********************************/ 00119 /* Try accessing an invalid event */ 00120 /**********************************/ 00121 00122 retval=PAPI_event_code_to_name( maximum_code+10, event_name ); 00123 if (retval!=PAPI_ENOEVNT) { 00124 test_fail( __FILE__, __LINE__, 00125 "Failed to return PAPI_ENOEVNT on invalid event", retval ); 00126 } 00127 00128 /***********************************/ 00129 /* Test the EXAMPLE_ZERO event */ 00130 /***********************************/ 00131 00132 retval = PAPI_create_eventset( &EventSet ); 00133 if ( retval != PAPI_OK ) { 00134 test_fail( __FILE__, __LINE__, 00135 "PAPI_create_eventset() failed\n", retval ); 00136 } 00137 00138 retval = PAPI_event_name_to_code("EXAMPLE_ZERO", &code); 00139 if ( retval != PAPI_OK ) { 00140 test_fail( __FILE__, __LINE__, 00141 "EXAMPLE_ZERO not found\n",retval ); 00142 } 00143 00144 retval = PAPI_add_event( EventSet, code); 00145 if ( retval != PAPI_OK ) { 00146 test_fail( __FILE__, __LINE__, 00147 "PAPI_add_events failed\n", retval ); 00148 } 00149 00150 retval = PAPI_start( EventSet ); 00151 if ( retval != PAPI_OK ) { 00152 test_fail( __FILE__, __LINE__, 00153 "PAPI_start failed\n",retval ); 00154 } 00155 00156 retval = PAPI_stop( EventSet, values ); 00157 if ( retval != PAPI_OK ) { 00158 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); 00159 } 00160 00161 if (!TESTS_QUIET) printf("Testing EXAMPLE_ZERO: %lld\n",values[0]); 00162 00163 if (values[0]!=0) { 00164 test_fail( __FILE__, __LINE__, "Result should be 0!\n", 0); 00165 } 00166 00167 retval = PAPI_cleanup_eventset(EventSet); 00168 if (retval != PAPI_OK) { 00169 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval); 00170 } 00171 00172 retval = PAPI_destroy_eventset(&EventSet); 00173 if (retval != PAPI_OK) { 00174 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval); 00175 } 00176 00177 EventSet=PAPI_NULL; 00178 00179 00180 /***********************************/ 00181 /* Test the EXAMPLE_CONSTANT event */ 00182 /***********************************/ 00183 00184 retval = PAPI_create_eventset( &EventSet ); 00185 if ( retval != PAPI_OK ) { 00186 test_fail( __FILE__, __LINE__, 00187 "PAPI_create_eventset() failed\n", retval ); 00188 } 00189 00190 retval = PAPI_event_name_to_code("EXAMPLE_CONSTANT", &code); 00191 if ( retval != PAPI_OK ) { 00192 test_fail( __FILE__, __LINE__, 00193 "EXAMPLE_CONSTANT not found\n",retval ); 00194 } 00195 00196 retval = PAPI_add_event( EventSet, code); 00197 if ( retval != PAPI_OK ) { 00198 test_fail( __FILE__, __LINE__, 00199 "PAPI_add_events failed\n", retval ); 00200 } 00201 00202 retval = PAPI_start( EventSet ); 00203 if ( retval != PAPI_OK ) { 00204 test_fail( __FILE__, __LINE__, 00205 "PAPI_start failed\n",retval ); 00206 } 00207 00208 retval = PAPI_stop( EventSet, values ); 00209 if ( retval != PAPI_OK ) { 00210 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); 00211 } 00212 00213 if (!TESTS_QUIET) printf("Testing EXAMPLE_CONSTANT: %lld\n",values[0]); 00214 00215 if (values[0]!=42) { 00216 test_fail( __FILE__, __LINE__, "Result should be 42!\n", 0); 00217 } 00218 00219 retval = PAPI_cleanup_eventset(EventSet); 00220 if (retval != PAPI_OK) { 00221 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval); 00222 } 00223 00224 retval = PAPI_destroy_eventset(&EventSet); 00225 if (retval != PAPI_OK) { 00226 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval); 00227 } 00228 00229 EventSet=PAPI_NULL; 00230 00231 00232 00233 /***********************************/ 00234 /* Test the EXAMPLE_AUTOINC event */ 00235 /***********************************/ 00236 00237 retval = PAPI_create_eventset( &EventSet ); 00238 if ( retval != PAPI_OK ) { 00239 test_fail( __FILE__, __LINE__, 00240 "PAPI_create_eventset() failed\n", retval ); 00241 } 00242 00243 retval = PAPI_event_name_to_code("EXAMPLE_AUTOINC", &code); 00244 if ( retval != PAPI_OK ) { 00245 test_fail( __FILE__, __LINE__, 00246 "EXAMPLE_AUTOINC not found\n",retval ); 00247 } 00248 00249 retval = PAPI_add_event( EventSet, code); 00250 if ( retval != PAPI_OK ) { 00251 test_fail( __FILE__, __LINE__, 00252 "PAPI_add_events failed\n", retval ); 00253 } 00254 00255 if (!TESTS_QUIET) printf("Testing EXAMPLE_AUTOINC: "); 00256 00257 for(i=0;i<10;i++) { 00258 00259 retval = PAPI_start( EventSet ); 00260 if ( retval != PAPI_OK ) { 00261 test_fail( __FILE__, __LINE__, 00262 "PAPI_start failed\n",retval ); 00263 } 00264 00265 retval = PAPI_stop( EventSet, values ); 00266 if ( retval != PAPI_OK ) { 00267 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); 00268 } 00269 00270 if (!TESTS_QUIET) printf("%lld ",values[0]); 00271 00272 if (values[0]!=i) { 00273 test_fail( __FILE__, __LINE__, "Result wrong!\n", 0); 00274 } 00275 } 00276 00277 if (!TESTS_QUIET) printf("\n"); 00278 00279 00280 /***********************************/ 00281 /* Test multiple reads */ 00282 /***********************************/ 00283 00284 retval = PAPI_start( EventSet ); 00285 if ( retval != PAPI_OK ) { 00286 test_fail( __FILE__, __LINE__, 00287 "PAPI_start failed\n",retval ); 00288 } 00289 00290 for(i=0;i<10;i++) { 00291 00292 retval=PAPI_read( EventSet, values); 00293 if ( retval != PAPI_OK ) { 00294 test_fail( __FILE__, __LINE__, "PAPI_read failed\n", retval); 00295 } 00296 if (!TESTS_QUIET) printf("%lld ",values[0]); 00297 } 00298 00299 retval = PAPI_stop( EventSet, values ); 00300 if ( retval != PAPI_OK ) { 00301 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); 00302 } 00303 if (!TESTS_QUIET) printf("%lld\n",values[0]); 00304 00305 // if (values[0]!=i) { 00306 // test_fail( __FILE__, __LINE__, "Result wrong!\n", 0); 00307 //} 00308 00309 /***********************************/ 00310 /* Test PAPI_reset() */ 00311 /***********************************/ 00312 00313 retval = PAPI_reset( EventSet); 00314 if ( retval != PAPI_OK ) { 00315 test_fail( __FILE__, __LINE__, 00316 "PAPI_reset() failed\n",retval ); 00317 } 00318 00319 retval = PAPI_start( EventSet ); 00320 if ( retval != PAPI_OK ) { 00321 test_fail( __FILE__, __LINE__, 00322 "PAPI_start failed\n",retval ); 00323 } 00324 00325 retval = PAPI_reset( EventSet); 00326 if ( retval != PAPI_OK ) { 00327 test_fail( __FILE__, __LINE__, 00328 "PAPI_reset() failed\n",retval ); 00329 } 00330 00331 retval = PAPI_stop( EventSet, values ); 00332 if ( retval != PAPI_OK ) { 00333 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); 00334 } 00335 00336 00337 if (!TESTS_QUIET) printf("Testing EXAMPLE_AUTOINC after PAPI_reset(): %lld\n", 00338 values[0]); 00339 00340 if (values[0]!=0) { 00341 test_fail( __FILE__, __LINE__, "Result not zero!\n", 0); 00342 } 00343 00344 retval = PAPI_cleanup_eventset(EventSet); 00345 if (retval != PAPI_OK) { 00346 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval); 00347 } 00348 00349 retval = PAPI_destroy_eventset(&EventSet); 00350 if (retval != PAPI_OK) { 00351 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval); 00352 } 00353 00354 EventSet=PAPI_NULL; 00355 00356 00357 /***********************************/ 00358 /* Test multiple events */ 00359 /***********************************/ 00360 00361 if (!TESTS_QUIET) printf("Testing Multiple Events: "); 00362 00363 retval = PAPI_create_eventset( &EventSet ); 00364 if ( retval != PAPI_OK ) { 00365 test_fail( __FILE__, __LINE__, 00366 "PAPI_create_eventset() failed\n", retval ); 00367 } 00368 00369 retval = PAPI_event_name_to_code("EXAMPLE_CONSTANT", &code); 00370 if ( retval != PAPI_OK ) { 00371 test_fail( __FILE__, __LINE__, 00372 "EXAMPLE_CONSTANT not found\n",retval ); 00373 } 00374 00375 retval = PAPI_add_event( EventSet, code); 00376 if ( retval != PAPI_OK ) { 00377 test_fail( __FILE__, __LINE__, 00378 "PAPI_add_events failed\n", retval ); 00379 } 00380 00381 retval = PAPI_event_name_to_code("EXAMPLE_GLOBAL_AUTOINC", &code); 00382 if ( retval != PAPI_OK ) { 00383 test_fail( __FILE__, __LINE__, 00384 "EXAMPLE_GLOBAL_AUTOINC not found\n",retval ); 00385 } 00386 00387 retval = PAPI_add_event( EventSet, code); 00388 if ( retval != PAPI_OK ) { 00389 test_fail( __FILE__, __LINE__, 00390 "PAPI_add_events failed\n", retval ); 00391 } 00392 00393 retval = PAPI_event_name_to_code("EXAMPLE_ZERO", &code); 00394 if ( retval != PAPI_OK ) { 00395 test_fail( __FILE__, __LINE__, 00396 "EXAMPLE_ZERO not found\n",retval ); 00397 } 00398 00399 retval = PAPI_add_event( EventSet, code); 00400 if ( retval != PAPI_OK ) { 00401 test_fail( __FILE__, __LINE__, 00402 "PAPI_add_events failed\n", retval ); 00403 } 00404 00405 00406 retval = PAPI_start( EventSet ); 00407 if ( retval != PAPI_OK ) { 00408 test_fail( __FILE__, __LINE__, 00409 "PAPI_start failed\n",retval ); 00410 } 00411 00412 retval = PAPI_stop( EventSet, values ); 00413 if ( retval != PAPI_OK ) { 00414 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); 00415 } 00416 00417 if (!TESTS_QUIET) { 00418 for(i=0;i<3;i++) { 00419 printf("%lld ",values[i]); 00420 } 00421 printf("\n"); 00422 } 00423 00424 if (values[0]!=42) { 00425 test_fail( __FILE__, __LINE__, "Result should be 42!\n", 0); 00426 } 00427 00428 if (values[2]!=0) { 00429 test_fail( __FILE__, __LINE__, "Result should be 0!\n", 0); 00430 } 00431 00432 retval = PAPI_cleanup_eventset(EventSet); 00433 if (retval != PAPI_OK) { 00434 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval); 00435 } 00436 00437 retval = PAPI_destroy_eventset(&EventSet); 00438 if (retval != PAPI_OK) { 00439 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval); 00440 } 00441 00442 EventSet=PAPI_NULL; 00443 00444 /***********************************/ 00445 /* Test writing to an event */ 00446 /***********************************/ 00447 00448 if (!TESTS_QUIET) printf("Testing Write\n"); 00449 00450 retval = PAPI_create_eventset( &EventSet ); 00451 if ( retval != PAPI_OK ) { 00452 test_fail( __FILE__, __LINE__, 00453 "PAPI_create_eventset() failed\n", retval ); 00454 } 00455 00456 retval = PAPI_event_name_to_code("EXAMPLE_CONSTANT", &code); 00457 if ( retval != PAPI_OK ) { 00458 test_fail( __FILE__, __LINE__, 00459 "EXAMPLE_CONSTANT not found\n",retval ); 00460 } 00461 00462 retval = PAPI_add_event( EventSet, code); 00463 if ( retval != PAPI_OK ) { 00464 test_fail( __FILE__, __LINE__, 00465 "PAPI_add_events failed\n", retval ); 00466 } 00467 00468 retval = PAPI_event_name_to_code("EXAMPLE_GLOBAL_AUTOINC", &code); 00469 if ( retval != PAPI_OK ) { 00470 test_fail( __FILE__, __LINE__, 00471 "EXAMPLE_GLOBAL_AUTOINC not found\n",retval ); 00472 } 00473 00474 retval = PAPI_add_event( EventSet, code); 00475 if ( retval != PAPI_OK ) { 00476 test_fail( __FILE__, __LINE__, 00477 "PAPI_add_events failed\n", retval ); 00478 } 00479 00480 retval = PAPI_event_name_to_code("EXAMPLE_ZERO", &code); 00481 if ( retval != PAPI_OK ) { 00482 test_fail( __FILE__, __LINE__, 00483 "EXAMPLE_ZERO not found\n",retval ); 00484 } 00485 00486 retval = PAPI_add_event( EventSet, code); 00487 if ( retval != PAPI_OK ) { 00488 test_fail( __FILE__, __LINE__, 00489 "PAPI_add_events failed\n", retval ); 00490 } 00491 00492 00493 retval = PAPI_start( EventSet ); 00494 if ( retval != PAPI_OK ) { 00495 test_fail( __FILE__, __LINE__, 00496 "PAPI_start failed\n",retval ); 00497 } 00498 00499 retval = PAPI_read ( EventSet, values ); 00500 if ( retval != PAPI_OK ) { 00501 test_fail( __FILE__, __LINE__, 00502 "PAPI_read failed\n",retval ); 00503 } 00504 00505 if (!TESTS_QUIET) { 00506 printf("Before values: "); 00507 for(i=0;i<3;i++) { 00508 printf("%lld ",values[i]); 00509 } 00510 printf("\n"); 00511 } 00512 00513 values[0]=100; 00514 values[1]=200; 00515 values[2]=300; 00516 00517 retval = PAPI_write ( EventSet, values ); 00518 if ( retval != PAPI_OK ) { 00519 test_fail( __FILE__, __LINE__, 00520 "PAPI_write failed\n",retval ); 00521 } 00522 00523 retval = PAPI_stop( EventSet, values ); 00524 if ( retval != PAPI_OK ) { 00525 test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); 00526 } 00527 00528 if (!TESTS_QUIET) { 00529 printf("After values: "); 00530 for(i=0;i<3;i++) { 00531 printf("%lld ",values[i]); 00532 } 00533 printf("\n"); 00534 } 00535 00536 00537 if (values[0]!=42) { 00538 test_fail( __FILE__, __LINE__, "Result should be 42!\n", 0); 00539 } 00540 00541 if (values[1]!=200) { 00542 test_fail( __FILE__, __LINE__, "Result should be 200!\n", 0); 00543 } 00544 00545 if (values[2]!=0) { 00546 test_fail( __FILE__, __LINE__, "Result should be 0!\n", 0); 00547 } 00548 00549 retval = PAPI_cleanup_eventset(EventSet); 00550 if (retval != PAPI_OK) { 00551 test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval); 00552 } 00553 00554 retval = PAPI_destroy_eventset(&EventSet); 00555 if (retval != PAPI_OK) { 00556 test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval); 00557 } 00558 00559 EventSet=PAPI_NULL; 00560 00561 00562 /************/ 00563 /* All Done */ 00564 /************/ 00565 00566 if (!TESTS_QUIET) printf("\n"); 00567 00568 test_pass( __FILE__, NULL, 0 ); 00569 00570 return 0; 00571 } 00572