#include #include #include #include #include "papi.h" #define error(func, ret) \ { fprintf(stderr, "%s failed.\n", func); if (ret) exit(ret);} // static PAPI_hw_info_t *info; static int events; static long long counters[128]; static char *events_desc[128]; static void cleanup(void) { if (PAPI_is_initialized()) PAPI_shutdown(); } static void add_event(int code, const char *name, const char *msg) { static int nevents = 0; if (PAPI_query_event(code) == PAPI_OK) { if (PAPI_add_event(events, code) != PAPI_OK) { fprintf(stderr, "PAPI_add_event failed for %s (%s).\n", name, msg); } else { fprintf(stderr, "Successfully added PAPI_FP_OPS.\n"); events_desc[nevents] = malloc(strlen(msg) + strlen(name) + 5); sprintf(events_desc[nevents], "%s (%s):", msg, name); nevents++; } } else { fprintf(stderr, "No event counter for %s.\n", msg); } } int main(int argc, char **argv) { int i, n; PAPI_option_t options; if (PAPI_library_init(PAPI_VER_CURRENT) != PAPI_VER_CURRENT) error("PAPI_library_init", 1); atexit(cleanup); // info = PAPI_get_hardware_info(); // if (info == NULL) // error("PAPI_get_hardware_info", 1); if (PAPI_create_eventset(&events) != PAPI_OK) error("PAPI_create_eventset", 1); #if 0 /* seems multiplexing does not work on PIII */ if (PAPI_multiplex_init() != PAPI_OK) error("PAPI_multiplex_init", 1); if (PAPI_set_multiplex(events) != PAPI_OK) error("PAPI_set_multiplex", 1); #endif options.domain.eventset = events; options.domain.domain = PAPI_DOM_ALL; PAPI_set_opt(PAPI_DOMAIN, &options); //add_event(PAPI_FP_OPS, "PAPI_FP_OPS", "floating point operations"); //add_event(PAPI_FP_INS, "PAPI_FP_INS", "floating point instructions"); add_event(PAPI_L1_TCM, "PAPI_L1_TCM", "L1 total cache misses"); add_event(PAPI_L2_TCM, "PAPI_L2_TCM", "L2 total cache misses"); //add_event(PAPI_L1_TCM, "PAPI_L1_TCH", "L1 total cache hits"); //add_event(PAPI_L2_TCM, "PAPI_L2_TCH", "L2 total cache hits"); /* Translation Lookaside Buffer */ //add_event(PAPI_TLB_TL, "PAPI_TLB_TL", "TLB buffer misses"); n = PAPI_num_events(events); fprintf(stderr, "%d counters initialized.\n", n); { double a[10000], b[sizeof(a)/sizeof(*a)]; PAPI_start(events); PAPI_reset(events); for (i = 0; i < sizeof(a)/sizeof(*a); i++) a[i] = b[i] * 3.0; PAPI_stop(events, counters); for (i = 0; i < n; i++) { fprintf(stderr, "%-40s %lld\n", events_desc[i], counters[i]); } } return 0; }