PAPI  5.0.1.0
rapl_plot.c
Go to the documentation of this file.
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 NUM_EVENTS 6
00013 
00014 char events[NUM_EVENTS][BUFSIZ]={
00015   "rapl:::PACKAGE_ENERGY:PACKAGE0",
00016   "rapl:::PACKAGE_ENERGY:PACKAGE1",
00017   "rapl:::DRAM_ENERGY:PACKAGE0",
00018   "rapl:::DRAM_ENERGY:PACKAGE1",
00019   "rapl:::PP0_ENERGY:PACKAGE0",
00020   "rapl:::PP0_ENERGY:PACKAGE1",
00021 };
00022 
00023 char filenames[NUM_EVENTS][BUFSIZ]={
00024   "results.PACKAGE_ENERGY_PACKAGE0",
00025   "results.PACKAGE_ENERGY_PACKAGE1",
00026   "results.DRAM_ENERGY_PACKAGE0",
00027   "results.DRAM_ENERGY_PACKAGE1",
00028   "results.PP0_ENERGY_PACKAGE0",
00029   "results.PP0_ENERGY_PACKAGE1",
00030 };
00031 
00032 FILE *fff[NUM_EVENTS];
00033 
00034 
00035 int main (int argc, char **argv)
00036 {
00037 
00038     int retval,cid,rapl_cid=-1,numcmp;
00039     int EventSet = PAPI_NULL;
00040     long long values[NUM_EVENTS];
00041     int i;
00042     const PAPI_component_info_t *cmpinfo = NULL;
00043     long long start_time,before_time,after_time;
00044     double elapsed_time,total_time;
00045 
00046     
00047 
00048     /* PAPI Initialization */
00049      retval = PAPI_library_init( PAPI_VER_CURRENT );
00050      if ( retval != PAPI_VER_CURRENT ) {
00051         fprintf(stderr,"PAPI_library_init failed\n");
00052     exit(1);
00053      }
00054 
00055      numcmp = PAPI_num_components();
00056 
00057      for(cid=0; cid<numcmp; cid++) {
00058 
00059     if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
00060        fprintf(stderr,"PAPI_get_component_info failed\n");
00061        exit(1);
00062     }
00063 
00064     if (strstr(cmpinfo->name,"rapl")) {
00065        rapl_cid=cid;
00066        printf("Found rapl component at cid %d\n", rapl_cid);
00067 
00068            if (cmpinfo->num_native_events==0) {
00069          fprintf(stderr,"No rapl events found\n");
00070          exit(1);
00071            }
00072        break;
00073     }
00074      }
00075 
00076      /* Component not found */
00077      if (cid==numcmp) {
00078         fprintf(stderr,"No rapl component found\n");
00079         exit(1);
00080      }
00081 
00082 
00083      /* Open output files */
00084      for(i=0;i<NUM_EVENTS;i++) {
00085         fff[i]=fopen(filenames[i],"w");
00086     if (fff[i]==NULL) {
00087        fprintf(stderr,"Could not open %s\n",filenames[i]);
00088        exit(1);
00089     }
00090      }
00091                    
00092 
00093      /* Create EventSet */
00094      retval = PAPI_create_eventset( &EventSet );
00095      if (retval != PAPI_OK) {
00096         fprintf(stderr,"Error creating eventset!\n");
00097      }
00098 
00099      for(i=0;i<NUM_EVENTS;i++) {
00100     
00101         retval = PAPI_add_named_event( EventSet, events[i] );
00102         if (retval != PAPI_OK) {
00103        fprintf(stderr,"Error adding event %s\n",events[i]);
00104     }
00105      }
00106 
00107   
00108 
00109      start_time=PAPI_get_real_nsec();
00110 
00111      while(1) {
00112 
00113         /* Start Counting */
00114         before_time=PAPI_get_real_nsec();
00115         retval = PAPI_start( EventSet);
00116         if (retval != PAPI_OK) {
00117            fprintf(stderr,"PAPI_start() failed\n");
00118        exit(1);
00119         }
00120 
00121 
00122         usleep(100000);
00123 
00124         /* Stop Counting */
00125         after_time=PAPI_get_real_nsec();
00126         retval = PAPI_stop( EventSet, values);
00127         if (retval != PAPI_OK) {
00128            fprintf(stderr, "PAPI_start() failed\n");
00129         }
00130 
00131         total_time=((double)(after_time-start_time))/1.0e9;
00132         elapsed_time=((double)(after_time-before_time))/1.0e9;
00133 
00134         for(i=0;i<NUM_EVENTS;i++) {
00135 
00136        fprintf(fff[i],"%.4f %.1f (* Average Power for %s *)\n",
00137            total_time,
00138            ((double)values[i]/1.0e9)/elapsed_time,
00139            events[i]);
00140        fflush(fff[i]);
00141         }
00142      }
00143         
00144      return 0;
00145 }
00146 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines