int main (int argc, char **argv) { int retval,cid,rapl_cid=-1,numcmp; int EventSet = PAPI_NULL; long long values[MAX_EVENTS]; int i,code,enum_retval; const PAPI_component_info_t *cmpinfo = NULL; long long start_time,write_start_time,write_end_time,read_start_time,read_end_time; char event_name[BUFSIZ]; union { long long ll; double dbl; } event_value_union; static int num_events=0; FILE *fileout; /* PAPI Initialization */ retval = PAPI_library_init( PAPI_VER_CURRENT ); if ( retval != PAPI_VER_CURRENT ) { fprintf(stderr,"PAPI_library_init failed\n"); exit(1); } /* Find the libmsr component */ numcmp = PAPI_num_components(); for(cid=0; cid<numcmp; cid++) { if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) { fprintf(stderr,"PAPI_get_component_info failed\n"); exit(1); } if (strstr(cmpinfo->name,"libmsr")) { rapl_cid=cid; printf("Found libmsr component at cid %d\n", rapl_cid); if (cmpinfo->disabled) { fprintf(stderr,"No libmsr events found: %s\n", cmpinfo->disabled_reason); exit(1); } break; } } /* Component not found */ if (cid==numcmp) { fprintf(stderr,"No libmsr component found\n"); exit(1); } /* Find events in the component */ code = PAPI_NATIVE_MASK; enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, cid ); while ( enum_retval == PAPI_OK ) { retval = PAPI_event_code_to_name( code, event_name ); if ( retval != PAPI_OK ) { printf("Error translating %#x\n",code); exit(1); } printf("Found: %s\n",event_name); strncpy(events[num_events],event_name,BUFSIZ); sprintf(filenames[num_events],"results.%s",event_name); num_events++; if (num_events==MAX_EVENTS) { printf("Too many events! %d\n",num_events); exit(1); } enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, cid ); } if (num_events==0) { printf("Error! No libmsr events found!\n"); exit(1); } /* Open output file */ char fileoutname[]="libmsr_write_test_output.txt"; fileout=fopen( fileoutname ,"w" ); if ( fileout==NULL) { fprintf( stderr,"Could not open %s\n",fileoutname ); exit(1); } /* Create EventSet */ retval = PAPI_create_eventset( &EventSet ); if (retval != PAPI_OK) { fprintf(stderr,"Error creating eventset!\n"); } for(i=0;i<num_events;i++) { retval = PAPI_add_named_event( EventSet, events[i] ); if (retval != PAPI_OK) fprintf(stderr,"Error adding event %s\n",events[i]); } start_time=PAPI_get_real_nsec(); /* Grab the initial values for the events */ retval = PAPI_start( EventSet); if (retval != PAPI_OK) { fprintf(stderr,"PAPI_start() failed\n"); exit(1); } /* Initial checking read */ retval = PAPI_read( EventSet, values); if (retval != PAPI_OK) { fprintf(stderr,"PAPI_read() failed\n"); exit(1); } /* Write a header line */ fprintf( fileout, "ACTION TIME-STAMP TIME-FOR-UNIT-WORK TIME-OVERHEAD-RW\t" ); for(i=0; i<num_events; i++) fprintf( fileout, "%s ", events[i]+9 ); fprintf( fileout, "\n" ); /* Read the initial values */ retval = PAPI_read( EventSet, values); if (retval != PAPI_OK) { fprintf(stderr,"PAPI_read() failed\n"); exit(1); } fprintf( fileout, "INIT %8.3f %8.3f ", ((double)(PAPI_get_real_nsec()-start_time))/1.0e9, 0.0 ); fprintf( fileout, "%8.3e ", 0.0); for(i=0; i<num_events; i++) { event_value_union.ll = values[i]; fprintf( fileout, "%8.3f ", event_value_union.dbl ); } fprintf( fileout, "\n" ); int rpt=0; int limit1base=10; int limit2base=10; while(rpt++<200) { //printf("rpt %d\n", rpt); if ( rpt % 10 == 0 ) { for (i=0; i<num_events; i++) { event_value_union.ll = values[i]; if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_1:PACKAGE0" )) event_value_union.dbl=limit1base+(rpt/2); else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_1:PACKAGE0" )) event_value_union.dbl=1.0; else if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_2:PACKAGE0" )) event_value_union.dbl=limit2base+(rpt/2); else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_2:PACKAGE0" )) event_value_union.dbl=1.0; else if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_1:PACKAGE1" )) event_value_union.dbl=limit1base+(rpt/2); else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_1:PACKAGE1" )) event_value_union.dbl=1.0; else if ( !strcmp( events[i], "libmsr:::PKG_POWER_LIMIT_2:PACKAGE1" )) event_value_union.dbl=limit2base+(rpt/2); else if ( !strcmp( events[i], "libmsr:::PKG_TIME_WINDOW_POWER_LIMIT_2:PACKAGE1" )) event_value_union.dbl=1.0; else event_value_union.dbl=PAPI_NULL; values[i]=event_value_union.ll; } write_start_time=PAPI_get_real_nsec(); retval = PAPI_write( EventSet, values ); write_end_time=PAPI_get_real_nsec(); if (retval != PAPI_OK) { fprintf(stderr,"PAPI_write() failed\n"); exit(1); } fprintf( fileout, "SET %8.3f %8.3f ", ((double)(PAPI_get_real_nsec()-start_time))/1.0e9, 0.0 ); fprintf( fileout, "%8.3e ", ((double)(write_end_time-write_start_time))/1.0e9 ); for(i=0; i<num_events; i++) { event_value_union.ll = values[i]; fprintf( fileout, "%8.3f ", event_value_union.dbl ); } fprintf( fileout, "\n" ); } /* DO SOME WORK TO USE ENERGY */ //usleep(100000); double work_start_time=PAPI_get_real_nsec(); ompcpuloadprimes( 100000 ); double work_time=PAPI_get_real_nsec()-work_start_time; //printf("primescount %d\n", primescount); /* Read and output the values */ read_start_time=PAPI_get_real_nsec(); retval = PAPI_read( EventSet, values ); read_end_time=PAPI_get_real_nsec(); if (retval != PAPI_OK) { fprintf(stderr,"PAPI_read() failed\n"); exit(1); } fprintf( fileout, "READ %8.3f %8.3f ", ((double)(PAPI_get_real_nsec()-start_time))/1.0e9, work_time/1.0e9 ); fprintf( fileout, "%8.3e ", ((double)(read_end_time-read_start_time))/1.0e9 ); for(i=0; i<num_events; i++) { event_value_union.ll = values[i]; fprintf( fileout, "%8.3f ", event_value_union.dbl ); } fprintf( fileout, "\n" ); } retval = PAPI_stop( EventSet, values); return 0; }
int main (int argc, char **argv) { int retval,i; int EventSet = PAPI_NULL; long long values[NUM_EVENTS]; const PAPI_component_info_t *cmpinfo = NULL; int numcmp,cid,example_cid=-1; int code,maximum_code=0; char event_name[PAPI_MAX_STR_LEN]; PAPI_event_info_t event_info; /* Set TESTS_QUIET variable */ tests_quiet( argc, argv ); /* PAPI Initialization */ retval = PAPI_library_init( PAPI_VER_CURRENT ); if ( retval != PAPI_VER_CURRENT ) { test_fail(__FILE__, __LINE__,"PAPI_library_init failed\n",retval); } if (!TESTS_QUIET) { printf( "Testing example component with PAPI %d.%d.%d\n", PAPI_VERSION_MAJOR( PAPI_VERSION ), PAPI_VERSION_MINOR( PAPI_VERSION ), PAPI_VERSION_REVISION( PAPI_VERSION ) ); } /* Find our component */ numcmp = PAPI_num_components(); for( cid=0; cid<numcmp; cid++) { if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) { test_fail(__FILE__, __LINE__, "PAPI_get_component_info failed\n", 0); } if (!TESTS_QUIET) { printf("\tComponent %d - %d events - %s\n", cid, cmpinfo->num_native_events, cmpinfo->name); } if (strstr(cmpinfo->name,"example.c")) { /* FOUND! */ example_cid=cid; } } if (example_cid<0) { test_skip(__FILE__, __LINE__, "Example component not found\n", 0); } if (!TESTS_QUIET) { printf("\nFound Example Component at id %d\n",example_cid); printf("\nListing all events in this component:\n"); } /**************************************************/ /* Listing all available events in this component */ /* Along with descriptions */ /**************************************************/ code = PAPI_NATIVE_MASK | PAPI_COMPONENT_MASK(example_cid); retval = PAPI_enum_event( &code, PAPI_ENUM_FIRST ); while ( retval == PAPI_OK ) { if (PAPI_event_code_to_name( code, event_name )!=PAPI_OK) { printf("Error translating %x\n",code); test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval ); } if (PAPI_get_event_info( code, &event_info)!=PAPI_OK) { printf("Error getting info for event %x\n",code); test_fail( __FILE__, __LINE__, "PAPI_get_event_info()", retval ); } if (!TESTS_QUIET) { printf("\tEvent 0x%x: %s -- %s\n", code,event_name,event_info.long_descr); } maximum_code=code; retval = PAPI_enum_event( &code, PAPI_ENUM_EVENTS ); } if (!TESTS_QUIET) printf("\n"); /**********************************/ /* Try accessing an invalid event */ /**********************************/ retval=PAPI_event_code_to_name( maximum_code+10, event_name ); if (retval!=PAPI_ENOEVNT) { test_fail( __FILE__, __LINE__, "Failed to return PAPI_ENOEVNT on invalid event", retval ); } /***********************************/ /* Test the EXAMPLE_ZERO event */ /***********************************/ retval = PAPI_create_eventset( &EventSet ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_create_eventset() failed\n", retval ); } retval = PAPI_event_name_to_code("EXAMPLE_ZERO", &code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "EXAMPLE_ZERO not found\n",retval ); } retval = PAPI_add_event( EventSet, code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_add_events failed\n", retval ); } retval = PAPI_start( EventSet ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_start failed\n",retval ); } retval = PAPI_stop( EventSet, values ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); } if (!TESTS_QUIET) printf("Testing EXAMPLE_ZERO: %lld\n",values[0]); if (values[0]!=0) { test_fail( __FILE__, __LINE__, "Result should be 0!\n", 0); } retval = PAPI_cleanup_eventset(EventSet); if (retval != PAPI_OK) { test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval); } retval = PAPI_destroy_eventset(&EventSet); if (retval != PAPI_OK) { test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval); } EventSet=PAPI_NULL; /***********************************/ /* Test the EXAMPLE_CONSTANT event */ /***********************************/ retval = PAPI_create_eventset( &EventSet ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_create_eventset() failed\n", retval ); } retval = PAPI_event_name_to_code("EXAMPLE_CONSTANT", &code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "EXAMPLE_CONSTANT not found\n",retval ); } retval = PAPI_add_event( EventSet, code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_add_events failed\n", retval ); } retval = PAPI_start( EventSet ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_start failed\n",retval ); } retval = PAPI_stop( EventSet, values ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); } if (!TESTS_QUIET) printf("Testing EXAMPLE_CONSTANT: %lld\n",values[0]); if (values[0]!=42) { test_fail( __FILE__, __LINE__, "Result should be 42!\n", 0); } retval = PAPI_cleanup_eventset(EventSet); if (retval != PAPI_OK) { test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval); } retval = PAPI_destroy_eventset(&EventSet); if (retval != PAPI_OK) { test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval); } EventSet=PAPI_NULL; /***********************************/ /* Test the EXAMPLE_AUTOINC event */ /***********************************/ retval = PAPI_create_eventset( &EventSet ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_create_eventset() failed\n", retval ); } retval = PAPI_event_name_to_code("EXAMPLE_AUTOINC", &code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "EXAMPLE_AUTOINC not found\n",retval ); } retval = PAPI_add_event( EventSet, code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_add_events failed\n", retval ); } if (!TESTS_QUIET) printf("Testing EXAMPLE_AUTOINC: "); for(i=0;i<10;i++) { retval = PAPI_start( EventSet ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_start failed\n",retval ); } retval = PAPI_stop( EventSet, values ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); } if (!TESTS_QUIET) printf("%lld ",values[0]); if (values[0]!=i) { test_fail( __FILE__, __LINE__, "Result wrong!\n", 0); } } if (!TESTS_QUIET) printf("\n"); /***********************************/ /* Test multiple reads */ /***********************************/ retval = PAPI_start( EventSet ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_start failed\n",retval ); } for(i=0;i<10;i++) { retval=PAPI_read( EventSet, values); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_read failed\n", retval); } if (!TESTS_QUIET) printf("%lld ",values[0]); } retval = PAPI_stop( EventSet, values ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); } if (!TESTS_QUIET) printf("%lld\n",values[0]); // if (values[0]!=i) { // test_fail( __FILE__, __LINE__, "Result wrong!\n", 0); //} /***********************************/ /* Test PAPI_reset() */ /***********************************/ retval = PAPI_reset( EventSet); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_reset() failed\n",retval ); } retval = PAPI_start( EventSet ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_start failed\n",retval ); } retval = PAPI_reset( EventSet); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_reset() failed\n",retval ); } retval = PAPI_stop( EventSet, values ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); } if (!TESTS_QUIET) printf("Testing EXAMPLE_AUTOINC after PAPI_reset(): %lld\n", values[0]); if (values[0]!=0) { test_fail( __FILE__, __LINE__, "Result not zero!\n", 0); } retval = PAPI_cleanup_eventset(EventSet); if (retval != PAPI_OK) { test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval); } retval = PAPI_destroy_eventset(&EventSet); if (retval != PAPI_OK) { test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval); } EventSet=PAPI_NULL; /***********************************/ /* Test multiple events */ /***********************************/ if (!TESTS_QUIET) printf("Testing Multiple Events: "); retval = PAPI_create_eventset( &EventSet ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_create_eventset() failed\n", retval ); } retval = PAPI_event_name_to_code("EXAMPLE_CONSTANT", &code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "EXAMPLE_CONSTANT not found\n",retval ); } retval = PAPI_add_event( EventSet, code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_add_events failed\n", retval ); } retval = PAPI_event_name_to_code("EXAMPLE_GLOBAL_AUTOINC", &code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "EXAMPLE_GLOBAL_AUTOINC not found\n",retval ); } retval = PAPI_add_event( EventSet, code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_add_events failed\n", retval ); } retval = PAPI_event_name_to_code("EXAMPLE_ZERO", &code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "EXAMPLE_ZERO not found\n",retval ); } retval = PAPI_add_event( EventSet, code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_add_events failed\n", retval ); } retval = PAPI_start( EventSet ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_start failed\n",retval ); } retval = PAPI_stop( EventSet, values ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); } if (!TESTS_QUIET) { for(i=0;i<3;i++) { printf("%lld ",values[i]); } printf("\n"); } if (values[0]!=42) { test_fail( __FILE__, __LINE__, "Result should be 42!\n", 0); } if (values[2]!=0) { test_fail( __FILE__, __LINE__, "Result should be 0!\n", 0); } retval = PAPI_cleanup_eventset(EventSet); if (retval != PAPI_OK) { test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval); } retval = PAPI_destroy_eventset(&EventSet); if (retval != PAPI_OK) { test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval); } EventSet=PAPI_NULL; /***********************************/ /* Test writing to an event */ /***********************************/ if (!TESTS_QUIET) printf("Testing Write\n"); retval = PAPI_create_eventset( &EventSet ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_create_eventset() failed\n", retval ); } retval = PAPI_event_name_to_code("EXAMPLE_CONSTANT", &code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "EXAMPLE_CONSTANT not found\n",retval ); } retval = PAPI_add_event( EventSet, code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_add_events failed\n", retval ); } retval = PAPI_event_name_to_code("EXAMPLE_GLOBAL_AUTOINC", &code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "EXAMPLE_GLOBAL_AUTOINC not found\n",retval ); } retval = PAPI_add_event( EventSet, code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_add_events failed\n", retval ); } retval = PAPI_event_name_to_code("EXAMPLE_ZERO", &code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "EXAMPLE_ZERO not found\n",retval ); } retval = PAPI_add_event( EventSet, code); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_add_events failed\n", retval ); } retval = PAPI_start( EventSet ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_start failed\n",retval ); } retval = PAPI_read ( EventSet, values ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_read failed\n",retval ); } if (!TESTS_QUIET) { printf("Before values: "); for(i=0;i<3;i++) { printf("%lld ",values[i]); } printf("\n"); } values[0]=100; values[1]=200; values[2]=300; retval = PAPI_write ( EventSet, values ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_write failed\n",retval ); } retval = PAPI_stop( EventSet, values ); if ( retval != PAPI_OK ) { test_fail( __FILE__, __LINE__, "PAPI_stop failed\n", retval); } if (!TESTS_QUIET) { printf("After values: "); for(i=0;i<3;i++) { printf("%lld ",values[i]); } printf("\n"); } if (values[0]!=42) { test_fail( __FILE__, __LINE__, "Result should be 42!\n", 0); } if (values[1]!=200) { test_fail( __FILE__, __LINE__, "Result should be 200!\n", 0); } if (values[2]!=0) { test_fail( __FILE__, __LINE__, "Result should be 0!\n", 0); } retval = PAPI_cleanup_eventset(EventSet); if (retval != PAPI_OK) { test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset!\n", retval); } retval = PAPI_destroy_eventset(&EventSet); if (retval != PAPI_OK) { test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset!\n", retval); } EventSet=PAPI_NULL; /************/ /* All Done */ /************/ if (!TESTS_QUIET) printf("\n"); test_pass( __FILE__, NULL, 0 ); return 0; }