void HWCBE_PAPI_Initialize (int TRCOptions) { UNREFERENCED_PARAMETER(TRCOptions); int rc; void *thread_identifier_function; /* PAPI initialization */ rc = PAPI_library_init (PAPI_VER_CURRENT); if (rc != PAPI_VER_CURRENT) { if (rc > 0) { fprintf (stderr, PACKAGE_NAME": PAPI library version mismatch!\n" " "PACKAGE_NAME" is compiled against PAPI v%d.%d , and \n" " PAPI_library_init reported v%d.%d ,\n" " Check that LD_LIBRARY_PATH points to the correct PAPI library.\n", PAPI_VERSION_MAJOR(PAPI_VER_CURRENT), PAPI_VERSION_MINOR(PAPI_VER_CURRENT), PAPI_VERSION_MAJOR(rc), PAPI_VERSION_MINOR(rc)); } fprintf (stderr, PACKAGE_NAME": Can't use hardware counters!\n"); fprintf (stderr, PACKAGE_NAME": PAPI library error: %s\n", PAPI_strerror (rc)); if (rc == PAPI_ESYS) perror (PACKAGE_NAME": PAPI system error is "); return; } #if defined(PAPI_SAMPLING_SUPPORT) /* Use any kind of sampling -- software or hardware */ SamplingSupport = TRUE; #endif thread_identifier_function = Extrae_get_thread_number_function(); if (thread_identifier_function != NULL) { if ((rc = PAPI_thread_init ((unsigned long (*)(void)) thread_identifier_function)) != PAPI_OK) { fprintf (stderr, PACKAGE_NAME": PAPI_thread_init failed! Reason: %s\n", PAPI_strerror(rc)); return; } } }
int main( int argc, char **argv ) { int i; tests_quiet( argc, argv ); /* Set TESTS_QUIET variable */ // if ( ( retval = // PAPI_library_init( PAPI_VER_CURRENT ) ) != PAPI_VER_CURRENT ) // test_fail( __FILE__, __LINE__, "PAPI_library_init", retval ); printf( "\n----------------------------------\n" ); printf( "For PAPI Version: %d.%d.%d.%d\n", PAPI_VERSION_MAJOR( PAPI_VERSION ), PAPI_VERSION_MINOR( PAPI_VERSION ), PAPI_VERSION_REVISION( PAPI_VERSION ), PAPI_VERSION_INCREMENT( PAPI_VERSION ) ); printf( "There are %d error codes defined\n", PAPI_NUM_ERRORS ); printf( "----------------------------------\n" ); for (i = 0; i < PAPI_NUM_ERRORS; i++) { char *errstr, *errdescr; errstr = PAPI_strerror( -i ); errdescr = PAPI_descr_error( -i ); printf( "Error code %4d: %-15s | %s\n", -i, errstr, errdescr ); } printf( "----------------------------------\n\n" ); exit( 1 ); }
/* Support routine to display header information to the screen from the hardware info data structure. The same code was duplicated in a number of tests and utilities. Seems to make sense to refactor. This may not be the best place for it to live, but it works for now. */ int papi_print_header( char *prompt, const PAPI_hw_info_t ** hwinfo ) { if ( ( *hwinfo = PAPI_get_hardware_info( ) ) == NULL ) return ( PAPI_ESBSTR ); printf( "%s", prompt ); printf ( "--------------------------------------------------------------------------------\n" ); printf( "PAPI Version : %d.%d.%d.%d\n", PAPI_VERSION_MAJOR( PAPI_VERSION ), PAPI_VERSION_MINOR( PAPI_VERSION ), PAPI_VERSION_REVISION( PAPI_VERSION ), PAPI_VERSION_INCREMENT( PAPI_VERSION ) ); printf( "Vendor string and code : %s (%d)\n", ( *hwinfo )->vendor_string, ( *hwinfo )->vendor ); printf( "Model string and code : %s (%d)\n", ( *hwinfo )->model_string, ( *hwinfo )->model ); printf( "CPU Revision : %f\n", ( *hwinfo )->revision ); if ( ( *hwinfo )->cpuid_family > 0 ) printf ( "CPUID Info : Family: %d Model: %d Stepping: %d\n", ( *hwinfo )->cpuid_family, ( *hwinfo )->cpuid_model, ( *hwinfo )->cpuid_stepping ); printf( "CPU Megahertz : %f\n", ( *hwinfo )->mhz ); printf( "CPU Clock Megahertz : %d\n", ( *hwinfo )->clock_mhz ); if ( ( *hwinfo )->threads > 0 ) printf( "Hdw Threads per core : %d\n", ( *hwinfo )->threads ); if ( ( *hwinfo )->cores > 0 ) printf( "Cores per Socket : %d\n", ( *hwinfo )->cores ); if ( ( *hwinfo )->sockets > 0 ) printf( "Sockets : %d\n", ( *hwinfo )->sockets ); if ( ( *hwinfo )->nnodes > 0 ) printf( "NUMA Nodes : %d\n", ( *hwinfo )->nnodes ); printf( "CPUs per Node : %d\n", ( *hwinfo )->ncpu ); printf( "Total CPUs : %d\n", ( *hwinfo )->totalcpus ); printf( "Running in a VM : %s\n", ( *hwinfo )->virtualized? "yes":"no"); if ( (*hwinfo)->virtualized) { printf( "VM Vendor: : %s\n", (*hwinfo)->virtual_vendor_string); } printf( "Number Hardware Counters : %d\n", PAPI_get_opt( PAPI_MAX_HWCTRS, NULL ) ); printf( "Max Multiplex Counters : %d\n", PAPI_get_opt( PAPI_MAX_MPX_CTRS, NULL ) ); printf ( "--------------------------------------------------------------------------------\n" ); printf( "\n" ); return PAPI_OK; }
/* Support routine to display header information to the screen from the hardware info data structure. The same code was duplicated in a number of tests and utilities. Seems to make sense to refactor. This may not be the best place for it to live, but it works for now. */ int papi_print_header( char *prompt, int event_flag, const PAPI_hw_info_t ** hwinfo ) { if ( ( *hwinfo = PAPI_get_hardware_info( ) ) == NULL ) return ( PAPI_ESBSTR ); printf( "%s", prompt ); printf ( "--------------------------------------------------------------------------------\n" ); printf( "PAPI Version : %d.%d.%d.%d\n", PAPI_VERSION_MAJOR( PAPI_VERSION ), PAPI_VERSION_MINOR( PAPI_VERSION ), PAPI_VERSION_REVISION( PAPI_VERSION ), PAPI_VERSION_INCREMENT( PAPI_VERSION ) ); printf( "Vendor string and code : %s (%d)\n", ( *hwinfo )->vendor_string, ( *hwinfo )->vendor ); printf( "Model string and code : %s (%d)\n", ( *hwinfo )->model_string, ( *hwinfo )->model ); printf( "CPU Revision : %f\n", ( *hwinfo )->revision ); if ( ( *hwinfo )->cpuid_family > 0 ) printf ( "CPUID Info : Family: %d Model: %d Stepping: %d\n", ( *hwinfo )->cpuid_family, ( *hwinfo )->cpuid_model, ( *hwinfo )->cpuid_stepping ); printf( "CPU Megahertz : %f\n", ( *hwinfo )->mhz ); printf( "CPU Clock Megahertz : %d\n", ( *hwinfo )->clock_mhz ); if ( ( *hwinfo )->threads > 0 ) printf( "Hdw Threads per core : %d\n", ( *hwinfo )->threads ); if ( ( *hwinfo )->cores > 0 ) printf( "Cores per Socket : %d\n", ( *hwinfo )->cores ); if ( ( *hwinfo )->sockets > 0 ) printf( "Sockets : %d\n", ( *hwinfo )->sockets ); if ( ( *hwinfo )->nnodes > 0 ) printf( "NUMA Nodes : %d\n", ( *hwinfo )->nnodes ); printf( "CPU's per Node : %d\n", ( *hwinfo )->ncpu ); printf( "Total CPU's : %d\n", ( *hwinfo )->totalcpus ); printf( "Number Hardware Counters : %d\n", PAPI_get_opt( PAPI_MAX_HWCTRS, NULL ) ); printf( "Max Multiplex Counters : %d\n", PAPI_get_opt( PAPI_MAX_MPX_CTRS, NULL ) ); printf ( "--------------------------------------------------------------------------------\n" ); if ( event_flag ) printf ( "The following correspond to fields in the PAPI_event_info_t structure.\n" ); printf( "\n" ); return ( PAPI_OK ); }
void initialize() { int ret; ret = PAPI_library_init(PAPI_VER_CURRENT); if (ret != PAPI_VER_CURRENT) { fprintf(stderr, "PAPI API 'PAPU_library_init' error !\n"); exit(1); } fprintf(stdout, "PAPI_VERSION: %d.%d.%d\n", PAPI_VERSION_MAJOR( PAPI_VERSION ), PAPI_VERSION_MINOR( PAPI_VERSION ), PAPI_VERSION_REVISION( PAPI_VERSION ) ); ret = PAPI_create_eventset(&eventSet); CHECK_PAPI_ERROR(ret, "PAPI_create_eventset"); }
int cudaSetDevice(int devnum, int n1, int n2, int n3, void *ptr1) { static int onetime = 0; int retval, retval_cudaSetDevice; //printf("cudaSetDevice wrapper %d\n", devnum); if ( onetime==0 ) { onetime=1; // Load the papi library dynamically and read the relevant functions dl1 = dlopen( "libpapi.so", RTLD_NOW | RTLD_GLOBAL ); if ( dl1==NULL ) printf("Intercept cudaSetDevice: Cannot load libpapi.so\n"); PAPI_library_init_ptr = dlsym( dl1, "PAPI_library_init" ); PAPI_create_eventset_ptr = dlsym( dl1, "PAPI_create_eventset" ); PAPI_add_named_event_ptr = dlsym( dl1, "PAPI_add_named_event" ); PAPI_start_ptr = dlsym( dl1, "PAPI_start" ); PAPI_stop_ptr = dlsym( dl1, "PAPI_stop" ); // Start using PAPI printf("Intercept cudaSetDevice: Initializing PAPI on device %d\n", devnum); retval = (PAPI_library_init_ptr)( PAPI_VER_CURRENT ); if( retval != PAPI_VER_CURRENT ) fprintf( stdout, "PAPI_library_init failed\n" ); printf( "PAPI version: %d.%d.%d\n", PAPI_VERSION_MAJOR( PAPI_VERSION ), PAPI_VERSION_MINOR( PAPI_VERSION ), PAPI_VERSION_REVISION( PAPI_VERSION ) ); retval = (PAPI_create_eventset_ptr)( &EventSet ); if( retval != PAPI_OK ) fprintf( stdout, "PAPI_create_eventset failed\n" ); } int (*original_function)(int devnum, int n1, int n2, int n3, void *ptr1); original_function = dlsym(RTLD_NEXT, "cudaSetDevice"); retval_cudaSetDevice = (*original_function)( devnum, n1, n2, n3, ptr1 ); if ( devseen[devnum]==0 ) { devseen[devnum]=1; char tmpEventName[120]; printf("Intercept cudaSetDevice: Attaching events for device on device %d\n", devnum); snprintf( tmpEventName, 110, "cuda:::device:%d:%s", devnum, "inst_executed" ); retval = (PAPI_add_named_event_ptr)( EventSet, tmpEventName ); if (retval!=PAPI_OK) printf( "Could not add event %s\n", tmpEventName ); } return retval_cudaSetDevice; }
int main (int argc, char *argv[]) { UNREFERENCED_PARAMETER(argc); UNREFERENCED_PARAMETER(argv); char *cpuinfo; printf (PACKAGE_STRING" SVN revision %d based on " EXTRAE_SVN_BRANCH"\n", EXTRAE_SVN_REVISION); cpuinfo = search_in_cpu_info ("model name"); if (cpuinfo == NULL) cpuinfo = search_in_cpu_info ("Processor"); printf ("CPU info%s", cpuinfo); #if defined(HAVE_LIBUNWIND_H) printf ("Using libunwind v%d.%d from %s\n", UNW_VERSION_MAJOR, UNW_VERSION_MINOR, UNWIND_HOME); #else printf ("Using backtrace or don't using calltrace?\n"); #endif #if defined(HAVE_PAPI_H) printf ("Using PAPI v%d.%d.%d from %s\n", PAPI_VERSION_MAJOR(PAPI_VERSION), PAPI_VERSION_MINOR(PAPI_VERSION), PAPI_VERSION_REVISION(PAPI_VERSION), PAPI_HOME); #elif defined(PMAPI_COUNTERS) printf ("Using PMAPI\n"); #else printf ("Don't using hwc\n"); #endif printf ("Running on: "); #if defined(linux) printf ("linux"); #elif defined(__FreeBSD__) printf ("FreeBSD"); #elif defined(__APPLE__) printf ("MacOS"); #elif defined(_AIX) printf ("AIX"); #else printf ("unknown OS"); #endif printf (" running on "); #if defined(__x86_64__) || defined(x86_64) || defined(__amd64__) || defined(amd64) printf ("x86 with 64 bit extensions"); #elif defined(__i386__) printf ("x86"); #elif defined(__ia64__) printf ("intel itanium"); #elif defined(__powerpc__) printf ("powerpc"); #elif defined(__powerpc64__) printf ("powerpc with 64 bit extensions"); #elif defined(__arm__) printf ("ARM"); #else printf ("unknown"); #endif printf (" processor\n"); return 0; }
/** * Called by the CBTF collector service in order to start data collection. */ void cbtf_collector_start(const CBTF_DataHeader* header) { /** * Start sampling. * * Starts hardware counter (HWC) sampling for the thread executing this * function. Initializes the appropriate thread-local data structures and * then enables the sampling counter. * * @param arguments Encoded function arguments. */ /* Create and access our thread-local storage */ #ifdef USE_EXPLICIT_TLS TLS* tls = malloc(sizeof(TLS)); Assert(tls != NULL); CBTF_SetTLS(TLSKey, tls); #else TLS* tls = &the_tls; #endif Assert(tls != NULL); tls->defer_sampling=false; #ifndef NDEBUG IsCollectorDebugEnabled = (getenv("CBTF_DEBUG_COLLECTOR") != NULL); IsCollectorDetailsDebugEnabled = (getenv("CBTF_DEBUG_COLLECTOR_DETAILS") != NULL); #if defined (HAVE_OMPT) IsOMPTDebugEnabled = (getenv("CBTF_DEBUG_COLLECTOR_OMPT") != NULL); #endif #endif /* Decode the passed function arguments */ // Need to handle the arguments... CBTF_hwcsamp_start_sampling_args args; memset(&args, 0, sizeof(args)); args.sampling_rate = 100; /* First set defaults */ int hwcsamp_rate = 100; char* hwcsamp_papi_event = "PAPI_TOT_CYC,PAPI_TOT_INS"; #if defined (CBTF_SERVICE_USE_OFFLINE) char* hwcsamp_event_param = getenv("CBTF_HWCSAMP_EVENTS"); if (hwcsamp_event_param != NULL) { hwcsamp_papi_event=hwcsamp_event_param; } const char* sampling_rate = getenv("CBTF_HWCSAMP_RATE"); if (sampling_rate != NULL) { hwcsamp_rate=atoi(sampling_rate); } args.collector = 1; args.experiment = 0; tls->data.interval = (uint64_t)(1000000000) / (uint64_t)(hwcsamp_rate);; #endif /* Initialize the actual data blob */ memcpy(&tls->header, header, sizeof(CBTF_DataHeader)); initialize_data(tls); /* We can not assign mpi rank in the header at this point as it may not * be set yet. assign an integer tid value. omp_tid is used regardless of * whether the application is using openmp threads. * libmonitor uses the same numbering scheme as openmp. */ tls->header.omp_tid = monitor_get_thread_num(); tls->header.id = strdup(cbtf_collector_unique_id); tls->header.time_begin = CBTF_GetTime(); #ifndef NDEBUG if (IsCollectorDebugEnabled) { fprintf(stderr,"[%ld,%d] ENTER cbtf_collector_start\n",tls->header.pid,tls->header.omp_tid); } #endif if(hwcsamp_papi_init_done == 0) { #ifndef NDEBUG if (IsCollectorDebugEnabled) { fprintf(stderr,"[%ld,%d] cbtf_collector_start: initialize papi\n",tls->header.pid,tls->header.omp_tid); } #endif CBTF_init_papi(); tls->EventSet = PAPI_NULL; tls->data.clock_mhz = (float) hw_info->mhz; hwcsamp_papi_init_done = 1; } else { tls->data.clock_mhz = (float) hw_info->mhz; } /* PAPI SETUP */ CBTF_Create_Eventset(&tls->EventSet); int rval = PAPI_OK; #ifndef NDEBUG if (IsCollectorDebugEnabled) { fprintf(stderr, "PAPI Version: %d.%d.%d.%d\n", PAPI_VERSION_MAJOR( PAPI_VERSION ), PAPI_VERSION_MINOR( PAPI_VERSION ), PAPI_VERSION_REVISION( PAPI_VERSION ), PAPI_VERSION_INCREMENT( PAPI_VERSION ) ); fprintf(stderr,"System has %d hardware counters.\n", PAPI_num_counters()); } #endif /* In Component PAPI, EventSets must be assigned a component index * before you can fiddle with their internals. 0 is always the cpu component */ #if (PAPI_VERSION_MAJOR(PAPI_VERSION)>=4) rval = PAPI_assign_eventset_component( tls->EventSet, 0 ); if (rval != PAPI_OK) { CBTF_PAPIerror(rval,"CBTF_Create_Eventset assign_eventset_component"); return; } #endif /* NOTE: if multiplex is turned on, papi internaly uses a SIGPROF handler. * Since we are sampling potentially with SIGPROF or now SIGRTMIN and we * prefer to limit our events to 6, we do not need multiplexing. */ if (getenv("CBTF_HWCSAMP_MULTIPLEX") != NULL) { #if !defined(RUNTIME_PLATFORM_BGP) rval = PAPI_set_multiplex( tls->EventSet ); if ( rval == PAPI_ENOSUPP) { fprintf(stderr,"CBTF_Create_Eventset: Multiplex not supported\n"); } else if (rval != PAPI_OK) { CBTF_PAPIerror(rval,"CBTF_Create_Eventset set_multiplex"); } #endif } /* TODO: check return values of direct PAPI calls * and handle them as needed. */ /* Rework the code here to call PAPI directly rather than * call any OPENSS helper functions due to inconsitent * behaviour seen on various lab systems */ int eventcode = 0; rval = PAPI_OK; if (hwcsamp_papi_event != NULL) { char *tfptr, *saveptr=NULL, *tf_token; tfptr = strdup(hwcsamp_papi_event); for (tf_token = strtok_r(tfptr, ",", &saveptr); tf_token != NULL; tf_token = strtok_r(NULL, ",", &saveptr) ) { PAPI_event_name_to_code(tf_token,&eventcode); rval = PAPI_add_event(tls->EventSet,eventcode); if (rval != PAPI_OK) { CBTF_PAPIerror(rval,"CBTF_Create_Eventset PAPI_event_name_to_code"); } } if (tfptr) free(tfptr); } else { PAPI_event_name_to_code("PAPI_TOT_CYC",&eventcode); rval = PAPI_add_event(tls->EventSet,eventcode); PAPI_event_name_to_code("PAPI_TOT_INS",&eventcode); rval = PAPI_add_event(tls->EventSet,eventcode); } #if defined (HAVE_OMPT) /* these are ompt specific.*/ /* initialize the flags and counts for idle,wait_barrier. */ tls->thread_idle = tls->thread_wait_barrier = tls->thread_barrier = false; #endif /* Begin sampling */ tls->header.time_begin = CBTF_GetTime(); CBTF_Start(tls->EventSet); CBTF_Timer(tls->data.interval, hwcsampTimerHandler); }
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; }
static int papi_internal_init(pmdaInterface *dp) { int ec; int sts; PAPI_event_info_t info; char entry[PAPI_HUGE_STR_LEN+12]; // the length papi uses for the symbol name unsigned int i = 0; pmID pmid; sts = sprintf(papi_version, "%d.%d.%d", PAPI_VERSION_MAJOR(PAPI_VERSION), PAPI_VERSION_MINOR(PAPI_VERSION), PAPI_VERSION_REVISION(PAPI_VERSION)); if (sts < 0) { __pmNotifyErr(LOG_ERR, "%s failed to create papi version metric.\n",pmProgname); return PM_ERR_GENERIC; } if ((sts = __pmNewPMNS(&papi_tree)) < 0) { __pmNotifyErr(LOG_ERR, "%s failed to create dynamic papi pmns: %s\n", pmProgname, pmErrStr(sts)); papi_tree = NULL; return PM_ERR_GENERIC; } number_of_counters = PAPI_num_counters(); if (number_of_counters < 0) { __pmNotifyErr(LOG_ERR, "hardware does not support performance counters\n"); return PM_ERR_APPVERSION; } else if (number_of_counters == 0) { __pmNotifyErr(LOG_WARNING, "no performance counters\n"); } sts = PAPI_library_init(PAPI_VER_CURRENT); if (sts != PAPI_VER_CURRENT) { __pmNotifyErr(LOG_ERR, "PAPI_library_init error (%d)\n", sts); return PM_ERR_GENERIC; } ec = PAPI_PRESET_MASK; PAPI_enum_event(&ec, PAPI_ENUM_FIRST); do { if (PAPI_get_event_info(ec, &info) == PAPI_OK) { if (info.count && PAPI_PRESET_ENUM_AVAIL) { expand_papi_info(i); memcpy(&papi_info[i].info, &info, sizeof(PAPI_event_info_t)); memcpy(&papi_info[i].papi_string_code, info.symbol + 5, strlen(info.symbol)-5); snprintf(entry, sizeof(entry),"papi.system.%s", papi_info[i].papi_string_code); pmid = pmid_build(dp->domain, CLUSTER_PAPI, i); papi_info[i].pmid = pmid; __pmAddPMNSNode(papi_tree, pmid, entry); memset(&entry[0], 0, sizeof(entry)); papi_info[i].position = -1; papi_info[i].metric_enabled = 0; expand_values(i); i++; } } } while(PAPI_enum_event(&ec, 0) == PAPI_OK); #if defined(HAVE_PAPI_DISABLED_COMP) char *tokenized_string; int number_of_components; int component_id; int native; number_of_components = PAPI_num_components(); native = 0 | PAPI_NATIVE_MASK; for (component_id = 0; component_id < number_of_components; component_id++) { const PAPI_component_info_t *component; component = PAPI_get_component_info(component_id); if (component->disabled || (strcmp("perf_event", component->name) && strcmp("perf_event_uncore", component->name))) continue; sts = PAPI_enum_cmp_event (&native, PAPI_ENUM_FIRST, component_id); if (sts == PAPI_OK) do { if (PAPI_get_event_info(native, &info) == PAPI_OK) { char local_native_metric_name[PAPI_HUGE_STR_LEN] = ""; int was_tokenized = 0; expand_papi_info(i); memcpy(&papi_info[i].info, &info, sizeof(PAPI_event_info_t)); tokenized_string = strtok(info.symbol, "::: -"); while (tokenized_string != NULL) { size_t remaining = sizeof(local_native_metric_name) - strlen(local_native_metric_name) - 1; if (remaining < 1) break; strncat(local_native_metric_name, tokenized_string, remaining); was_tokenized = 1; tokenized_string=strtok(NULL, "::: -"); if (tokenized_string) { remaining = sizeof(local_native_metric_name) - strlen(local_native_metric_name) - 1; if (remaining < 1) break; strncat(local_native_metric_name, ".", remaining); } } if (!was_tokenized) { strncpy(papi_info[i].papi_string_code, info.symbol, sizeof(papi_info[i].papi_string_code) - 1); } else { strncpy(papi_info[i].papi_string_code, local_native_metric_name, sizeof(papi_info[i].papi_string_code) - 1); } snprintf(entry, sizeof(entry),"papi.system.%s", papi_info[i].papi_string_code); pmid = pmid_build(dp->domain, CLUSTER_PAPI, i); papi_info[i].pmid = pmid; __pmAddPMNSNode(papi_tree, pmid, entry); memset(&entry[0], 0, sizeof(entry)); papi_info[i].position = -1; papi_info[i].metric_enabled = 0; expand_values(i); i++; } } while (PAPI_enum_cmp_event(&native, PAPI_ENUM_EVENTS, component_id) == PAPI_OK); } #endif pmdaTreeRebuildHash(papi_tree, number_of_events); /* Set one-time settings for all future EventSets. */ if ((sts = PAPI_set_domain(PAPI_DOM_ALL)) != PAPI_OK) { handle_papi_error(sts, 0); return PM_ERR_GENERIC; } if ((sts = PAPI_multiplex_init()) != PAPI_OK) { handle_papi_error(sts, 0); return PM_ERR_GENERIC; } sts = refresh_metrics(0); if (sts != PAPI_OK) return PM_ERR_GENERIC; return 0; }