PAPI  5.6.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 units[MAX_EVENTS][BUFSIZ];
17 char filenames[MAX_EVENTS][BUFSIZ];
18 
19 FILE *fff[MAX_EVENTS];
20 
21 static int num_events=0;
22 
23 int main (int argc, char **argv)
24 {
25 
26  int retval,cid,rapl_cid=-1,numcmp;
27  int EventSet = PAPI_NULL;
28  long long values[MAX_EVENTS];
29  int i,code,enum_retval;
30  PAPI_event_info_t evinfo;
31  const PAPI_component_info_t *cmpinfo = NULL;
32  long long start_time,before_time,after_time;
33  double elapsed_time,total_time;
34  char event_name[BUFSIZ];
35 
36  /* PAPI Initialization */
38  if ( retval != PAPI_VER_CURRENT ) {
39  fprintf(stderr,"PAPI_library_init failed\n");
40  exit(1);
41  }
42 
43  numcmp = PAPI_num_components();
44 
45  for(cid=0; cid<numcmp; cid++) {
46 
47  if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
48  fprintf(stderr,"PAPI_get_component_info failed\n");
49  exit(1);
50  }
51 
52  if (strstr(cmpinfo->name,"rapl")) {
53  rapl_cid=cid;
54  printf("Found rapl component at cid %d\n", rapl_cid);
55 
56  if (cmpinfo->disabled) {
57  fprintf(stderr,"No rapl events found: %s\n",
58  cmpinfo->disabled_reason);
59  exit(1);
60  }
61  break;
62  }
63  }
64 
65  /* Component not found */
66  if (cid==numcmp) {
67  fprintf(stderr,"No rapl component found\n");
68  exit(1);
69  }
70 
71  /* Find Events */
72  code = PAPI_NATIVE_MASK;
73 
74  enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, cid );
75 
76  while ( enum_retval == PAPI_OK ) {
77 
78  retval = PAPI_event_code_to_name( code, event_name );
79  if ( retval != PAPI_OK ) {
80  printf("Error translating %#x\n",code);
81  exit(1);
82  }
83 
84  printf("Found: %s\n",event_name);
85  strncpy(events[num_events],event_name,BUFSIZ);
86  sprintf(filenames[num_events],"results.%s",event_name);
87 
88 
89  /* Find additional event information: unit, data type */
90  retval = PAPI_get_event_info(code, &evinfo);
91  if (retval != PAPI_OK) {
92  printf("Error getting event info for %#x\n",code);
93  exit(1);
94  }
95 
96  strncpy(units[num_events],evinfo.units,sizeof(units[0])-1);
97  /* buffer must be null terminated to safely use strstr operation on it below */
98  units[num_events][sizeof(units[0])-1] = '\0';
99 
100  data_type[num_events] = evinfo.data_type;
101 
102  num_events++;
103 
104  if (num_events==MAX_EVENTS) {
105  printf("Too many events! %d\n",num_events);
106  exit(1);
107  }
108 
109  enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, cid );
110 
111  }
112 
113 
114 
115  if (num_events==0) {
116  printf("Error! No RAPL events found!\n");
117  exit(1);
118  }
119 
120  /* Open output files */
121  for(i=0;i<num_events;i++) {
122  fff[i]=fopen(filenames[i],"w");
123  if (fff[i]==NULL) {
124  fprintf(stderr,"Could not open %s\n",filenames[i]);
125  exit(1);
126  }
127  }
128 
129 
130  /* Create EventSet */
131  retval = PAPI_create_eventset( &EventSet );
132  if (retval != PAPI_OK) {
133  fprintf(stderr,"Error creating eventset!\n");
134  }
135 
136  for(i=0;i<num_events;i++) {
137 
138  retval = PAPI_add_named_event( EventSet, events[i] );
139  if (retval != PAPI_OK) {
140  fprintf(stderr,"Error adding event %s\n",events[i]);
141  }
142  }
143 
144 
145 
146  start_time=PAPI_get_real_nsec();
147 
148  while(1) {
149 
150  /* Start Counting */
151  before_time=PAPI_get_real_nsec();
152  retval = PAPI_start( EventSet);
153  if (retval != PAPI_OK) {
154  fprintf(stderr,"PAPI_start() failed\n");
155  exit(1);
156  }
157 
158 
159  usleep(100000);
160 
161  /* Stop Counting */
162  after_time=PAPI_get_real_nsec();
163  retval = PAPI_stop( EventSet, values);
164  if (retval != PAPI_OK) {
165  fprintf(stderr, "PAPI_start() failed\n");
166  }
167 
168  total_time=((double)(after_time-start_time))/1.0e9;
169  elapsed_time=((double)(after_time-before_time))/1.0e9;
170 
171  for(i=0;i<num_events;i++) {
172 
173 
174  if (!strstr(events[i],"ENERGY")) {
175 
176  /* Scaled fixed value */
177  if (data_type[i] == PAPI_DATATYPE_FP64) {
178 
179  union {
180  long long ll;
181  double fp;
182  } result;
183 
184  result.ll=values[i];
185 
186 
187  fprintf(fff[i],"%.4f %.3f (* %s in (%s) *)\n",
188  total_time,
189  result.fp,
190  events[i], units[i] );
191 
192  }
193  /* Fixed value counts */
194  else if (data_type[i] == PAPI_DATATYPE_UINT64) {
195 
196  fprintf(fff[i],"%.4f %lld (* %s *)\n",
197  total_time,
198  values[i],
199  events[i] );
200 
201  }
202  }
203 
204  /* Energy measurement counts */
205  else if (strstr(events[i],"ENERGY_CNT")) {
206 
207  fprintf(fff[i],"%.4f %lld (* %s *)\n",
208  total_time,
209  values[i],
210  events[i] );
211 
212  }
213 
214  /* Scaled energy measurements */
215  else {
216  fprintf(fff[i],"%.4f %.3f %s %.3f %s (* Average Power for %s *)\n",
217  total_time,
218  ((double)values[i]/1.0e9), "J",
219  ((double)values[i]/1.0e9)/elapsed_time, "W",
220  events[i] );
221  };
222 
223  fflush(fff[i]);
224  }
225  }
226 
227  return 0;
228 }
229 
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:629
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
#define PAPI_NATIVE_MASK
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:796
#define PAPI_NULL
Definition: papi.h:292
int PAPI_num_components(void)
Definition: papi.c:4387
char filenames[MAX_EVENTS][BUFSIZ]
int EventSet
return PAPI_OK
Definition: linux-nvml.c:497
char units[MAX_EVENTS][BUFSIZ]
Definition: powercap_plot.c:15
fflush(stdout)
static int num_events
Definition: rapl_plot.c:21
static FILE * fp
static double
Definition: fileop.c:1281
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
FILE * fff[MAX_EVENTS]
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:835
int PAPI_add_named_event(int EventSet, const char *EventName)
Definition: papi.c:1876
char events[MAX_EVENTS][BUFSIZ]
char ** argv
Definition: iozone.c:1610
char disabled_reason[PAPI_MAX_STR_LEN]
Definition: papi.h:636
int PAPI_library_init(int version)
Definition: papi.c:500
int i
Definition: fileop.c:140
int PAPI_enum_cmp_event(int *EventCode, int modifier, int cidx)
Definition: papi.c:1357
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:915
#define MAX_EVENTS
Definition: rapl_plot.c:12
printf("\tTry: -i 0 -i 1 \n\n")
long long PAPI_get_real_nsec(void)
Definition: papi.c:6237
int data_type[MAX_EVENTS]
Definition: powercap_plot.c:16
int PAPI_start(int EventSet)
Definition: papi.c:2096
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:225
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
char units[PAPI_MIN_STR_LEN]
Definition: papi.h:975
double total_time
Definition: iozone.c:1268