static void test_fail(char *file, int line, char *call, int retval) { char buf[128]; memset(buf, '\0', sizeof(buf)); if (retval != 0) fprintf (stdout,"%-40s FAILED\nLine # %d\n", file, line); else { fprintf (stdout,"%-40s SKIPPED\n", file); fprintf (stdout,"Line # %d\n", line); } if (retval == PAPI_ESYS) { sprintf (buf, "System error in %s", call); perror (buf); } else if (retval > 0) fprintf (stdout,"Error: %s\n", call); else if (retval == 0) fprintf (stdout,"Error: %s\n", call); else { char errstring[PAPI_MAX_STR_LEN]; PAPI_perror (retval, errstring, PAPI_MAX_STR_LEN); fprintf (stdout,"Error in %s: %s\n", call, errstring); } fprintf (stdout,"\n"); if (PAPI_is_initialized ()) PAPI_shutdown (); exit (1); }
/* This routine mimics the previous implementation of test_fail() by exiting on completion. It caused problems for threaded apps. If you are not threaded and want to exit, replace calls to test_fail() with calls to test_fail_exit(). */ void test_fail_exit( char *file, int line, char *call, int retval ) { test_fail( file, line, call, retval ); if ( PAPI_is_initialized( ) ) PAPI_shutdown( ); exit( 1 ); }
/* ** GPTL_PAPIlibraryinit: Call PAPI_library_init if necessary ** ** Return value: 0 (success) or GPTLerror (failure) */ int GPTL_PAPIlibraryinit () { int ret; static const char *thisfunc = "GPTL_PAPIlibraryinit"; if ((ret = PAPI_is_initialized ()) == PAPI_NOT_INITED) { if ((ret = PAPI_library_init (PAPI_VER_CURRENT)) != PAPI_VER_CURRENT) { fprintf (stderr, "%s: ret=%d PAPI_VER_CURRENT=%d\n", thisfunc, ret, (int) PAPI_VER_CURRENT); return GPTLerror ("%s: PAPI_library_init failure:%s\n", thisfunc, PAPI_strerror (ret)); } } return 0; }
int GPTL_PAPIlibraryinit () { int ret; if ((ret = PAPI_is_initialized ()) == PAPI_NOT_INITED) { if ((ret = PAPI_library_init (PAPI_VER_CURRENT)) != PAPI_VER_CURRENT) { fprintf (stderr, "GPTL_PAPIlibraryinit: ret=%d PAPI_VER_CURRENT=%d\n", ret, (int) PAPI_VER_CURRENT); return GPTLerror ("GPTL_PAPIlibraryinit: PAPI_library_init failure:%s\n", PAPI_strerror (ret)); } } return 0; }
void HWCBE_PAPI_CleanUp (unsigned nthreads) { UNREFERENCED_PARAMETER(nthreads); if (PAPI_is_initialized()) { int state; int i; unsigned t; if (PAPI_state (HWCEVTSET(THREADID), &state) == PAPI_OK) { if (state & PAPI_RUNNING) { long long tmp[MAX_HWC]; PAPI_stop (HWCEVTSET(THREADID), tmp); } } for (i = 0; i < HWC_num_sets; i++) { for (t = 0; t < nthreads; t++) { /* Remove all events in the eventset and destroy the eventset */ PAPI_cleanup_eventset(HWC_sets[i].eventsets[t]); PAPI_destroy_eventset(&HWC_sets[i].eventsets[t]); } xfree (HWC_sets[i].eventsets); } #if defined(PAPI_SAMPLING_SUPPORT) for (i = 0; i < HWC_num_sets; i++) { /* Free extra allocated memory */ if (HWC_sets[i].NumOverflows > 0) { xfree (HWC_sets[i].OverflowValue); xfree (HWC_sets[i].OverflowCounter); } } #endif xfree (HWC_sets); PAPI_shutdown(); } }
void polybench_papi_close() { # ifdef _OPENMP #pragma omp parallel { if (omp_get_thread_num () == polybench_papi_counters_threadid) { # endif int retval; if ((retval = PAPI_destroy_eventset (&polybench_papi_eventset)) != PAPI_OK) test_fail (__FILE__, __LINE__, "PAPI_destroy_eventset", retval); if (PAPI_is_initialized ()) PAPI_shutdown (); # ifdef _OPENMP } } #pragma omp barrier # endif }
uint64_t vt_metric_clckrt(void) { const PAPI_hw_info_t* hwinfo = NULL; double hertz; if (!PAPI_is_initialized()) { /* initialize PAPI, since it hasn't already been initialized */ int retval = PAPI_library_init(PAPI_VER_CURRENT); if ( retval != PAPI_VER_CURRENT ) metric_error(retval, "PAPI_library_init"); } hwinfo = PAPI_get_hardware_info(); if ( hwinfo == NULL) vt_error_msg("Failed to access PAPI hardware info\n"); vt_cntl_msg(2, "Clock rate: %f MHz", hwinfo->mhz); hertz = hwinfo->mhz * 1000000.0; return (uint64_t)hertz; }
void elg_pform_init() { FILE *cpuinfofp; char line[1024]; /* Determine number of CPUs */ if ((cpuinfofp = fopen (ELG_PROCDIR "cpuinfo", "r")) == NULL) elg_error_msg("Cannot open file %s: %s\n", ELG_PROCDIR "cpuinfo", strerror(errno)); while (fgets(line, sizeof (line), cpuinfofp)) { if (!strncmp("processor", line, 9)) elg_cpu_count++; } fclose(cpuinfofp); /* Initialize PAPI */ if (PAPI_NOT_INITED == PAPI_is_initialized()) if (PAPI_VER_CURRENT != PAPI_library_init(PAPI_VER_CURRENT)) elg_error_msg("PAPI library initialization failed!"); }
void test_pass( char *file, long long **values, int num_tests ) { int line_pad; line_pad=(int)(50-strlen(file)); if (line_pad<0) line_pad=0; if ( TEST_WARN ) { if (TESTS_COLOR) { fprintf( stdout, "%-*s %sPASSED with WARNING%s\n", line_pad, file, YELLOW, NORMAL); } else { fprintf( stdout, "%-*s PASSED with WARNING\n", line_pad, file ); } } else { if (TESTS_COLOR) { fprintf( stdout, "%-*s %sPASSED%s\n", line_pad, file, GREEN, NORMAL ); } else { fprintf( stdout, "%-*s PASSED\n", line_pad, file ); } } if ( values ) free_test_space( values, num_tests ); if ( PAPI_is_initialized( ) ) PAPI_shutdown( ); exit( 0 ); }
double esd_metric_clckrt() { const char* env; const PAPI_hw_info_t* hwinfo = NULL; if (!PAPI_is_initialized()) { /* read configuration variable "EPK_METRICS". Return if unset. */ env = epk_get(EPK_METRICS); if ( env == NULL || strlen(env)==0) return 0.0; /* initialize PAPI, since it hasn't already been initialized */ int retval = PAPI_library_init(PAPI_VER_CURRENT); if ( retval != PAPI_VER_CURRENT ) esd_metric_error(retval, "PAPI_library_init"); } hwinfo = PAPI_get_hardware_info(); if ( hwinfo == NULL) elg_error_msg("Failed to access PAPI hardware info"); elg_cntl_msg("Clock rate: %f MHz", hwinfo->mhz); return hwinfo->mhz * 1000000.0; }
int main(int argc, char** argv) { // Parse and validate command-line arguments. if (argc != 2) usage("Invalid number of arguments"); std::string arg(argv[1]); const test_vector_t* test_vec = nullptr; if (arg == "objc") test_vec = &objc_tests; else if (arg == "calls") test_vec = &call_tests; else if (arg == "classes") test_vec = &classes_tests; else if (arg == "hierarchy") test_vec = &hierarchy_tests; else usage("Invalid argument"); // Initialize PAPI library. int rc = PAPI_library_init(PAPI_VER_CURRENT); if (rc != PAPI_VER_CURRENT && rc > 0) fail("PAPI library version mismatch"); else if (rc < 0) fail("failed to init PAPI libarary"); rc = PAPI_is_initialized(); if (rc != PAPI_LOW_LEVEL_INITED) fail("failed to init PAPI libarary"); // Create and fill event set with 2 events: total cycles and total insns. // TODO: implement RAII wrappers for PAPI. int event_set = PAPI_NULL; rc = PAPI_create_eventset(&event_set); if (rc != PAPI_OK) papi_fail(rc, "failed to create event set"); rc = PAPI_add_event(event_set, PAPI_TOT_CYC); if (rc != PAPI_OK) papi_fail(rc, "failed to add total cycles counter"); rc = PAPI_add_event(event_set, PAPI_TOT_INS); if (rc != PAPI_OK) papi_fail(rc, "failed to add total insns counter"); // Warm up. for (const test_t& test : *test_vec) test.second(); // Output CSV header. std::cout << "test,tot_cyc,tot_insn,iter\n"; enum counter_t { CNT_TOT_CYC = 0, CNT_TOT_INS = 1, CNT_NUM }; long long counters[CNT_NUM]; // Benchmark. for (const test_t& test : *test_vec) { // Start counters. rc = PAPI_start(event_set); if (rc != PAPI_OK) papi_fail(rc, "failed to start counters"); // Run tests. for (size_t i = 0; i < num_iter; i++) test.second(); // Stop counters and output one CSV row. rc = PAPI_stop(event_set, counters); if (rc != PAPI_OK) papi_fail(rc, "failed to stop counters"); rc = PAPI_reset(event_set); if (rc != PAPI_OK) papi_fail(rc, "failed to reset counters"); std::cout << test.first << ',' << counters[CNT_TOT_CYC] << ',' << counters[CNT_TOT_INS] << ',' << num_iter << '\n'; } // Destroy PAPI event set. rc = PAPI_cleanup_eventset(event_set); if (rc != PAPI_OK) papi_fail(rc, "failed to cleanup event set"); rc = PAPI_destroy_eventset(&event_set); if (rc != PAPI_OK) papi_fail(rc, "failed to destroy event set"); return 0; }
void init_energy(pwr_ctx_t *ctx) { char buf[1024] = { '\0' }; int ret, num_comp, cid, code = 0; const PAPI_component_info_t *comp_info = NULL; PAPI_event_info_t evinfo; assert(ctx != NULL); assert(pwr_is_initialized(ctx, PWR_MODULE_STRUCT)); assert(!pwr_is_initialized(ctx, PWR_MODULE_ENERGY)); // Initialize PAPI if ( !PAPI_is_initialized() ) { ret = PAPI_library_init(PAPI_VER_CURRENT); if (PAPI_VER_CURRENT != ret ) { PAPI_shutdown(); if (ctx->err_fd) { fprintf(ctx->err_fd, "Unexpected PAPI version\n"); } ctx->error = PWR_ARCH_UNSUPPORTED; return; } } // Search PAPI components for RAPL (Intel energy counting) events num_comp = PAPI_num_components(); for (cid = 0; cid < num_comp; ++cid) { comp_info = PAPI_get_component_info(cid); if (NULL == comp_info) { PAPI_shutdown(); if (ctx->err_fd) { fprintf(ctx->err_fd, "Invalid PAPI module found\n"); } ctx->error = PWR_INIT_ERR; return; } if (strstr(comp_info->name, "rapl")) { if (0 == comp_info->num_native_events) { PAPI_shutdown(); if (ctx->err_fd) { fprintf(ctx->err_fd, "RAPL counters not available\n"); } ctx->error = PWR_UNAVAILABLE; return; } break; } } if (cid == num_comp) { if (ctx->err_fd) { fprintf(ctx->err_fd, "Cannot find RAPL module in PAPI\n"); } ctx->error = PWR_UNAVAILABLE; return; } // Create PAPI event set ctx->event_set = PAPI_NULL; ret = PAPI_create_eventset(&ctx->event_set); if (PAPI_OK != ret) { PAPI_shutdown(); if (ctx->err_fd) { fprintf(ctx->err_fd, "Failed to create a PAPI event set\n"); } ctx->error = PWR_INIT_ERR; return; } // count how many counters can be added ctx->emeas = malloc(sizeof(*ctx->emeas)); ctx->emeas->nbValues = 0; for (int i = 0; i < PWR_MAX_PHYS_CPU; ++i) { snprintf(buf, 1024, "PACKAGE_ENERGY:PACKAGE%d", i); ret = PAPI_query_named_event(buf); if (PAPI_OK != ret) { break; } ++ctx->emeas->nbValues; } for (int i = 0; i < PWR_MAX_PHYS_CPU; ++i) { snprintf(buf, 1024, "DRAM_ENERGY:PACKAGE%d", i); ret = PAPI_query_named_event(buf); if (PAPI_OK != ret) { break; } ++ctx->emeas->nbValues; } ctx->emeas->values = malloc(ctx->emeas->nbValues * sizeof(*ctx->emeas->values)); ctx->emeas->units = malloc(ctx->emeas->nbValues * sizeof(*ctx->emeas->units)); ctx->emeas->names = malloc(ctx->emeas->nbValues * sizeof(*ctx->emeas->names)); // fetch the counter information unsigned int cpt_i = 0; for (int i = 0; i < PWR_MAX_PHYS_CPU; ++i) { snprintf(buf, 1024, "PACKAGE_ENERGY:PACKAGE%d", i); ret = PAPI_query_named_event(buf); if (PAPI_OK != ret) { break; } ret = PAPI_add_named_event(ctx->event_set, buf); PAPI_event_name_to_code(buf, &code); PAPI_get_event_info(code, &evinfo); ctx->emeas->units[cpt_i] = strdup(evinfo.units); ctx->emeas->names[cpt_i] = strdup(buf); ++cpt_i; } // same for DRAM for (int i = 0; i < PWR_MAX_PHYS_CPU; ++i) { snprintf(buf, 1024, "DRAM_ENERGY:PACKAGE%d", i); ret = PAPI_query_named_event(buf); if (PAPI_OK != ret) { break; } ret = PAPI_add_named_event(ctx->event_set, buf); PAPI_event_name_to_code(buf, &code); PAPI_get_event_info(code, &evinfo); ctx->emeas->units[cpt_i] = strdup(evinfo.units); ctx->emeas->names[cpt_i] = strdup(buf); ++cpt_i; } ctx->emeas_running = false; ctx->error = PWR_OK; ctx->module_init |= (1U << PWR_MODULE_ENERGY); return; }