PAPI  5.4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rapl_plot.c
Go to the documentation of this file.
1 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 
10 #include "papi.h"
11 
12 #define MAX_EVENTS 128
13 
14 char events[MAX_EVENTS][BUFSIZ];
15 char filenames[MAX_EVENTS][BUFSIZ];
16 
17 FILE *fff[MAX_EVENTS];
18 
19 static int num_events=0;
20 
21 int main (int argc, char **argv)
22 {
23 
24  int retval,cid,rapl_cid=-1,numcmp;
25  int EventSet = PAPI_NULL;
26  long long values[MAX_EVENTS];
27  int i,code,enum_retval;
28  const PAPI_component_info_t *cmpinfo = NULL;
29  long long start_time,before_time,after_time;
30  double elapsed_time,total_time;
31  char event_name[BUFSIZ];
32 
33  /* PAPI Initialization */
35  if ( retval != PAPI_VER_CURRENT ) {
36  fprintf(stderr,"PAPI_library_init failed\n");
37  exit(1);
38  }
39 
40  numcmp = PAPI_num_components();
41 
42  for(cid=0; cid<numcmp; cid++) {
43 
44  if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
45  fprintf(stderr,"PAPI_get_component_info failed\n");
46  exit(1);
47  }
48 
49  if (strstr(cmpinfo->name,"rapl")) {
50  rapl_cid=cid;
51  printf("Found rapl component at cid %d\n", rapl_cid);
52 
53  if (cmpinfo->disabled) {
54  fprintf(stderr,"No rapl events found: %s\n",
55  cmpinfo->disabled_reason);
56  exit(1);
57  }
58  break;
59  }
60  }
61 
62  /* Component not found */
63  if (cid==numcmp) {
64  fprintf(stderr,"No rapl component found\n");
65  exit(1);
66  }
67 
68  /* Find Events */
69  code = PAPI_NATIVE_MASK;
70 
71  enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, cid );
72 
73  while ( enum_retval == PAPI_OK ) {
74 
75  retval = PAPI_event_code_to_name( code, event_name );
76  if ( retval != PAPI_OK ) {
77  printf("Error translating %#x\n",code);
78  exit(1);
79  }
80 
81  printf("Found: %s\n",event_name);
82  strncpy(events[num_events],event_name,BUFSIZ);
83  sprintf(filenames[num_events],"results.%s",event_name);
84  num_events++;
85 
86  if (num_events==MAX_EVENTS) {
87  printf("Too many events! %d\n",num_events);
88  exit(1);
89  }
90 
91  enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, cid );
92 
93  }
94 
95  if (num_events==0) {
96  printf("Error! No RAPL events found!\n");
97  exit(1);
98  }
99 
100  /* Open output files */
101  for(i=0;i<num_events;i++) {
102  fff[i]=fopen(filenames[i],"w");
103  if (fff[i]==NULL) {
104  fprintf(stderr,"Could not open %s\n",filenames[i]);
105  exit(1);
106  }
107  }
108 
109 
110  /* Create EventSet */
111  retval = PAPI_create_eventset( &EventSet );
112  if (retval != PAPI_OK) {
113  fprintf(stderr,"Error creating eventset!\n");
114  }
115 
116  for(i=0;i<num_events;i++) {
117 
118  retval = PAPI_add_named_event( EventSet, events[i] );
119  if (retval != PAPI_OK) {
120  fprintf(stderr,"Error adding event %s\n",events[i]);
121  }
122  }
123 
124 
125 
126  start_time=PAPI_get_real_nsec();
127 
128  while(1) {
129 
130  /* Start Counting */
131  before_time=PAPI_get_real_nsec();
132  retval = PAPI_start( EventSet);
133  if (retval != PAPI_OK) {
134  fprintf(stderr,"PAPI_start() failed\n");
135  exit(1);
136  }
137 
138 
139  usleep(100000);
140 
141  /* Stop Counting */
142  after_time=PAPI_get_real_nsec();
143  retval = PAPI_stop( EventSet, values);
144  if (retval != PAPI_OK) {
145  fprintf(stderr, "PAPI_start() failed\n");
146  }
147 
148  total_time=((double)(after_time-start_time))/1.0e9;
149  elapsed_time=((double)(after_time-before_time))/1.0e9;
150 
151  for(i=0;i<num_events;i++) {
152 
153  fprintf(fff[i],"%.4f %.1f (* Average Power for %s *)\n",
154  total_time,
155  ((double)values[i]/1.0e9)/elapsed_time,
156  events[i]);
157  fflush(fff[i]);
158  }
159  }
160 
161  return 0;
162 }
163 
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:23
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:626
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2258
#define PAPI_NATIVE_MASK
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:807
int PAPI_add_named_event(int EventSet, char *EventName)
Definition: papi.c:1835
#define PAPI_NULL
Definition: papi.h:290
static int num_events
int EventSet
Definition: data_range.c:25
int PAPI_num_components(void)
Definition: papi.c:4326
char filenames[MAX_EVENTS][BUFSIZ]
return PAPI_OK
Definition: linux-nvml.c:458
fflush(stdout)
#define printf
Definition: papi_test.h:125
static double
Definition: fileop.c:1281
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
FILE * fff[MAX_EVENTS]
char events[MAX_EVENTS][BUFSIZ]
char ** argv
Definition: iozone.c:1610
char disabled_reason[PAPI_MAX_STR_LEN]
Definition: papi.h:633
int PAPI_library_init(int version)
Definition: papi.c:497
int i
Definition: fileop.c:140
int PAPI_enum_cmp_event(int *EventCode, int modifier, int cidx)
Definition: papi.c:1324
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1424
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:926
#define MAX_EVENTS
Definition: rapl_plot.c:12
long long PAPI_get_real_nsec(void)
Definition: papi.c:6163
int PAPI_start(int EventSet)
Definition: papi.c:2053
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
#define PAPI_VER_CURRENT
Definition: papi.h:223
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
double total_time
Definition: iozone.c:1268