rapl_plot.c File Reference

Include dependency graph for rapl_plot.c:

Go to the source code of this file.

Defines

#define MAX_EVENTS   128

Functions

int main (int argc, char **argv)

Variables

char events [MAX_EVENTS][BUFSIZ]
char units [MAX_EVENTS][BUFSIZ]
int data_type [MAX_EVENTS]
char filenames [MAX_EVENTS][BUFSIZ]
FILE * fff [MAX_EVENTS]
static int num_events = 0

Define Documentation

#define MAX_EVENTS   128
Author:
Vince Weaver

Definition at line 12 of file rapl_plot.c.


Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 23 of file rapl_plot.c.

00024 {
00025 
00026     int retval,cid,rapl_cid=-1,numcmp;
00027     int EventSet = PAPI_NULL;
00028     long long values[MAX_EVENTS];
00029     int i,code,enum_retval;
00030     PAPI_event_info_t evinfo;
00031     const PAPI_component_info_t *cmpinfo = NULL;
00032     long long start_time,before_time,after_time;
00033     double elapsed_time,total_time;
00034     char event_name[BUFSIZ];
00035 
00036     /* PAPI Initialization */
00037     retval = PAPI_library_init( PAPI_VER_CURRENT );
00038     if ( retval != PAPI_VER_CURRENT ) {
00039         fprintf(stderr,"PAPI_library_init failed\n");
00040         exit(1);
00041     }
00042 
00043     numcmp = PAPI_num_components();
00044 
00045     for(cid=0; cid<numcmp; cid++) {
00046 
00047         if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
00048             fprintf(stderr,"PAPI_get_component_info failed\n");
00049             exit(1);
00050         }
00051 
00052         if (strstr(cmpinfo->name,"rapl")) {
00053             rapl_cid=cid;
00054             printf("Found rapl component at cid %d\n", rapl_cid);
00055 
00056             if (cmpinfo->disabled) {
00057                 fprintf(stderr,"No rapl events found: %s\n",
00058                         cmpinfo->disabled_reason);
00059                 exit(1);
00060             }
00061             break;
00062         }
00063     }
00064 
00065     /* Component not found */
00066     if (cid==numcmp) {
00067         fprintf(stderr,"No rapl component found\n");
00068         exit(1);
00069     }
00070 
00071     /* Find Events */
00072     code = PAPI_NATIVE_MASK;
00073 
00074     enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, cid );
00075 
00076     while ( enum_retval == PAPI_OK ) {
00077 
00078         retval = PAPI_event_code_to_name( code, event_name );
00079         if ( retval != PAPI_OK ) {
00080             printf("Error translating %#x\n",code);
00081             exit(1);
00082         }
00083 
00084         printf("Found: %s\n",event_name);
00085         strncpy(events[num_events],event_name,BUFSIZ);
00086         sprintf(filenames[num_events],"results.%s",event_name);
00087 
00088 
00089         /* Find additional event information: unit, data type */
00090         retval = PAPI_get_event_info(code, &evinfo);
00091         if (retval != PAPI_OK) {
00092             printf("Error getting event info for %#x\n",code);
00093             exit(1);
00094         }
00095 
00096         strncpy(units[num_events],evinfo.units,sizeof(units[0])-1);
00097         /* buffer must be null terminated to safely use strstr operation on it below */
00098         units[num_events][sizeof(units[0])-1] = '\0';
00099 
00100         data_type[num_events] = evinfo.data_type;
00101 
00102         num_events++;
00103 
00104         if (num_events==MAX_EVENTS) {
00105             printf("Too many events! %d\n",num_events);
00106             exit(1);
00107         }
00108 
00109         enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, cid );
00110 
00111     }
00112 
00113 
00114 
00115     if (num_events==0) {
00116         printf("Error!  No RAPL events found!\n");
00117         exit(1);
00118     }
00119 
00120     /* Open output files */
00121     for(i=0;i<num_events;i++) {
00122         fff[i]=fopen(filenames[i],"w");
00123         if (fff[i]==NULL) {
00124             fprintf(stderr,"Could not open %s\n",filenames[i]);
00125             exit(1);
00126         }
00127     }
00128 
00129 
00130     /* Create EventSet */
00131     retval = PAPI_create_eventset( &EventSet );
00132     if (retval != PAPI_OK) {
00133         fprintf(stderr,"Error creating eventset!\n");
00134     }
00135 
00136     for(i=0;i<num_events;i++) {
00137 
00138         retval = PAPI_add_named_event( EventSet, events[i] );
00139         if (retval != PAPI_OK) {
00140             fprintf(stderr,"Error adding event %s\n",events[i]);
00141         }
00142     }
00143 
00144 
00145 
00146     start_time=PAPI_get_real_nsec();
00147 
00148     while(1) {
00149 
00150         /* Start Counting */
00151         before_time=PAPI_get_real_nsec();
00152         retval = PAPI_start( EventSet);
00153         if (retval != PAPI_OK) {
00154             fprintf(stderr,"PAPI_start() failed\n");
00155             exit(1);
00156         }
00157 
00158 
00159         usleep(100000);
00160 
00161         /* Stop Counting */
00162         after_time=PAPI_get_real_nsec();
00163         retval = PAPI_stop( EventSet, values);
00164         if (retval != PAPI_OK) {
00165             fprintf(stderr, "PAPI_start() failed\n");
00166         }
00167 
00168         total_time=((double)(after_time-start_time))/1.0e9;
00169         elapsed_time=((double)(after_time-before_time))/1.0e9;
00170 
00171         for(i=0;i<num_events;i++) {
00172 
00173 
00174             if (!strstr(events[i],"ENERGY")) {
00175 
00176                 /* Scaled fixed value */
00177                 if (data_type[i] == PAPI_DATATYPE_FP64) {
00178 
00179                     union {
00180                         long long ll;
00181                         double fp;
00182                     } result;
00183 
00184                     result.ll=values[i];
00185 
00186 
00187                     fprintf(fff[i],"%.4f  %.3f  (* %s in (%s) *)\n",
00188                             total_time,
00189                             result.fp, 
00190                             events[i], units[i] );
00191 
00192                 } 
00193                 /* Fixed value counts */
00194                 else if (data_type[i] == PAPI_DATATYPE_UINT64) {
00195 
00196                     fprintf(fff[i],"%.4f  %lld  (* %s *)\n",
00197                             total_time,
00198                             values[i], 
00199                             events[i] );
00200 
00201                 }
00202             }
00203 
00204             /* Energy measurement counts */
00205             else if (strstr(events[i],"ENERGY_CNT")) {  
00206 
00207                 fprintf(fff[i],"%.4f  %lld  (* %s *)\n",
00208                         total_time,
00209                         values[i], 
00210                         events[i] );
00211 
00212             }
00213 
00214             /* Scaled energy measurements */
00215             else {
00216                 fprintf(fff[i],"%.4f  %.3f %s  %.3f %s  (* Average Power for %s *)\n",
00217                         total_time,
00218                         ((double)values[i]/1.0e9), "J",
00219                         ((double)values[i]/1.0e9)/elapsed_time, "W", 
00220                         events[i] );
00221             };
00222 
00223             fflush(fff[i]);
00224         }
00225     }
00226 
00227     return 0;
00228 }

Here is the call graph for this function:


Variable Documentation

int data_type[MAX_EVENTS]

Definition at line 16 of file rapl_plot.c.

char events[MAX_EVENTS][BUFSIZ]

Definition at line 14 of file rapl_plot.c.

FILE* fff[MAX_EVENTS]

Definition at line 19 of file rapl_plot.c.

char filenames[MAX_EVENTS][BUFSIZ]

Definition at line 17 of file rapl_plot.c.

int num_events = 0 [static]

Definition at line 21 of file rapl_plot.c.

char units[MAX_EVENTS][BUFSIZ]

Definition at line 15 of file rapl_plot.c.


Generated on 8 Sep 2016 for PAPI by  doxygen 1.6.1