|
PAPI
5.3.0.0
|
00001 00005 #include <stdio.h> 00006 #include <stdlib.h> 00007 #include <string.h> 00008 #include <unistd.h> 00009 00010 #include "papi.h" 00011 00012 #define MAX_EVENTS 128 00013 00014 char events[MAX_EVENTS][BUFSIZ]; 00015 char filenames[MAX_EVENTS][BUFSIZ]; 00016 00017 FILE *fff[MAX_EVENTS]; 00018 00019 static int num_events=0; 00020 00021 int main (int argc, char **argv) 00022 { 00023 00024 int retval,cid,rapl_cid=-1,numcmp; 00025 int EventSet = PAPI_NULL; 00026 long long values[MAX_EVENTS]; 00027 int i,code,enum_retval; 00028 const PAPI_component_info_t *cmpinfo = NULL; 00029 long long start_time,before_time,after_time; 00030 double elapsed_time,total_time; 00031 char event_name[BUFSIZ]; 00032 00033 /* PAPI Initialization */ 00034 retval = PAPI_library_init( PAPI_VER_CURRENT ); 00035 if ( retval != PAPI_VER_CURRENT ) { 00036 fprintf(stderr,"PAPI_library_init failed\n"); 00037 exit(1); 00038 } 00039 00040 numcmp = PAPI_num_components(); 00041 00042 for(cid=0; cid<numcmp; cid++) { 00043 00044 if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) { 00045 fprintf(stderr,"PAPI_get_component_info failed\n"); 00046 exit(1); 00047 } 00048 00049 if (strstr(cmpinfo->name,"rapl")) { 00050 rapl_cid=cid; 00051 printf("Found rapl component at cid %d\n", rapl_cid); 00052 00053 if (cmpinfo->disabled) { 00054 fprintf(stderr,"No rapl events found: %s\n", 00055 cmpinfo->disabled_reason); 00056 exit(1); 00057 } 00058 break; 00059 } 00060 } 00061 00062 /* Component not found */ 00063 if (cid==numcmp) { 00064 fprintf(stderr,"No rapl component found\n"); 00065 exit(1); 00066 } 00067 00068 /* Find Events */ 00069 code = PAPI_NATIVE_MASK; 00070 00071 enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, cid ); 00072 00073 while ( enum_retval == PAPI_OK ) { 00074 00075 retval = PAPI_event_code_to_name( code, event_name ); 00076 if ( retval != PAPI_OK ) { 00077 printf("Error translating %#x\n",code); 00078 exit(1); 00079 } 00080 00081 printf("Found: %s\n",event_name); 00082 strncpy(events[num_events],event_name,BUFSIZ); 00083 sprintf(filenames[num_events],"results.%s",event_name); 00084 num_events++; 00085 00086 if (num_events==MAX_EVENTS) { 00087 printf("Too many events! %d\n",num_events); 00088 exit(1); 00089 } 00090 00091 enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, cid ); 00092 00093 } 00094 00095 if (num_events==0) { 00096 printf("Error! No RAPL events found!\n"); 00097 exit(1); 00098 } 00099 00100 /* Open output files */ 00101 for(i=0;i<num_events;i++) { 00102 fff[i]=fopen(filenames[i],"w"); 00103 if (fff[i]==NULL) { 00104 fprintf(stderr,"Could not open %s\n",filenames[i]); 00105 exit(1); 00106 } 00107 } 00108 00109 00110 /* Create EventSet */ 00111 retval = PAPI_create_eventset( &EventSet ); 00112 if (retval != PAPI_OK) { 00113 fprintf(stderr,"Error creating eventset!\n"); 00114 } 00115 00116 for(i=0;i<num_events;i++) { 00117 00118 retval = PAPI_add_named_event( EventSet, events[i] ); 00119 if (retval != PAPI_OK) { 00120 fprintf(stderr,"Error adding event %s\n",events[i]); 00121 } 00122 } 00123 00124 00125 00126 start_time=PAPI_get_real_nsec(); 00127 00128 while(1) { 00129 00130 /* Start Counting */ 00131 before_time=PAPI_get_real_nsec(); 00132 retval = PAPI_start( EventSet); 00133 if (retval != PAPI_OK) { 00134 fprintf(stderr,"PAPI_start() failed\n"); 00135 exit(1); 00136 } 00137 00138 00139 usleep(100000); 00140 00141 /* Stop Counting */ 00142 after_time=PAPI_get_real_nsec(); 00143 retval = PAPI_stop( EventSet, values); 00144 if (retval != PAPI_OK) { 00145 fprintf(stderr, "PAPI_start() failed\n"); 00146 } 00147 00148 total_time=((double)(after_time-start_time))/1.0e9; 00149 elapsed_time=((double)(after_time-before_time))/1.0e9; 00150 00151 for(i=0;i<num_events;i++) { 00152 00153 fprintf(fff[i],"%.4f %.1f (* Average Power for %s *)\n", 00154 total_time, 00155 ((double)values[i]/1.0e9)/elapsed_time, 00156 events[i]); 00157 fflush(fff[i]); 00158 } 00159 } 00160 00161 return 0; 00162 } 00163