int main(int argc, char **argv) { double error; int i,fd_insts,fd_cycles,read_result; long long cycles_count, insts_count; float IPC; struct timespec before,after; long long nsecs; struct perf_event_attr pe; int ppr; if (argc < 2) ppr = 4; else ppr = atoi(argv[1]); memset(&pe,0,sizeof(struct perf_event_attr)); pe.type=PERF_TYPE_HARDWARE; pe.size=sizeof(struct perf_event_attr); pe.config=PERF_COUNT_HW_CPU_CYCLES; pe.disabled=1; pe.exclude_kernel=1; pe.exclude_hv=1; fd_cycles=perf_event_open(&pe,0,-1,-1,0); if (fd_cycles<0) { fprintf(stderr,"Error opening leader %llx\n",pe.config); } pe.type=PERF_TYPE_HW_CACHE; pe.config=PERF_COUNT_HW_CACHE_L1D; //pe.config=PERF_COUNT_HW_INSTRUCTIONS; fd_insts=perf_event_open(&pe,0,-1,fd_cycles,0); if (fd_cycles<0) { fprintf(stderr,"Error opening event %llx\n",pe.config); } ioctl(fd_cycles, PERF_EVENT_IOC_ENABLE,0); ioctl(fd_insts, PERF_EVENT_IOC_ENABLE,0); for(i=0;i<SLEEP_RUNS;i++) { ioctl(fd_cycles, PERF_EVENT_IOC_RESET, 0); ioctl(fd_insts, PERF_EVENT_IOC_RESET, 0); set_ppr(ppr); //sleep(1); clock_gettime(CLOCK_REALTIME,&before); naive_matrix_multiply(1); clock_gettime(CLOCK_REALTIME,&after); //ioctl(fd_cycles, PERF_EVENT_IOC_DISABLE,0); //ioctl(fd_insts, PERF_EVENT_IOC_DISABLE,0); read_result=read(fd_cycles,&cycles_count,sizeof(long long)); read_result=read(fd_insts,&insts_count,sizeof(long long)); IPC = (float) insts_count / cycles_count; nsecs=convert_to_ns(&before,&after); printf("[Run %d] CYL: %lld INS: %lld IPC: %f PPR: %d Time: %lld\n", i, cycles_count, insts_count, IPC, get_ppr(), nsecs); } return 0; }
int main(int argc, char **argv) { int retval; int i; int EventSet=PAPI_NULL; int event; long long *counts; struct timespec before,after; struct timespec start_before; struct timespec stop_after; long long init_ns=0,eventset_ns=0; long long ns=0; int count; char *machine_name; if (argc>2) { count=atoi(argv[2]); machine_name=strdup(argv[1]); } else { count=1; machine_name=strdup("core2"); } counts=calloc(count,sizeof(long long)); if (!strncmp(machine_name,"core2",5)) { memcpy(event_names,core2_events,16*64); } else if (!strncmp(machine_name,"nehalem",7)) { memcpy(event_names,nehalem_events,16*64); } else if (!strncmp(machine_name,"atom",4)) { memcpy(event_names,atom_events,16*64); } else if (!strncmp(machine_name,"amd0fh",6)) { memcpy(event_names,amd0fh_events,16*64); } else if (!strncmp(machine_name,"amd10h",6)) { memcpy(event_names,amd10h_events,16*64); } else if (!strncmp(machine_name,"power6",6)) { memcpy(event_names,power6_events,16*64); } else if (!strncmp(machine_name,"cortexA9",8)) { memcpy(event_names,cortexA9_events,16*64); } else { fprintf(stderr,"Unknown machine name %s\n",machine_name); exit(0); } /* measure init latency */ clock_gettime(CLOCK_REALTIME,&before); retval = PAPI_library_init(PAPI_VER_CURRENT); if (retval != PAPI_VER_CURRENT) { fprintf(stderr,"ERROR: PAPI_library_init %d\n", retval); } clock_gettime(CLOCK_REALTIME,&after); init_ns=convert_to_ns(&before,&after); printf("PAPI_library_init_latency: %lld ns\n",init_ns); /* measure eventset creation */ clock_gettime(CLOCK_REALTIME,&before); retval = PAPI_create_eventset(&EventSet); if (retval!=PAPI_OK) { fprintf(stderr,"PAPI_create_eventset failed\n"); } for(i=0;i<count;i++) { retval=PAPI_event_name_to_code(event_names[i],&event); if (retval!=PAPI_OK) { fprintf(stderr,"PAPI_event_name_to_code failed %s\n",event_names[i]); } retval=PAPI_add_event(EventSet,event); if (retval!=PAPI_OK) { //fprintf(stderr,"PAPI_add_event failed %x\n",event); count=i; printf("Eventset_creation_%d: %lld ns\n",i,0LL); printf("PAPI_start/stop/read latency: %lld ns\n",0LL); exit(1); } } clock_gettime(CLOCK_REALTIME,&after); eventset_ns=convert_to_ns(&before,&after); printf("Eventset_creation_%d: %lld ns\n",i,eventset_ns); clock_gettime(CLOCK_REALTIME,&start_before); PAPI_start(EventSet); PAPI_stop(EventSet,counts); clock_gettime(CLOCK_REALTIME,&stop_after); ns=convert_to_ns(&start_before,&stop_after); printf("PAPI_start/stop/read latency: %lld ns\n",ns); for(i=0;i<count;i++) { printf("%s %lld\n",event_names[i],counts[i]); } PAPI_shutdown(); return 0; }