PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
host_micpower_util.c
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 
11 #include "papi.h"
12 
13 #define MAX_EVENTS 128
14 
15 char events[MAX_EVENTS][BUFSIZ];
16 char filenames[MAX_EVENTS][BUFSIZ];
17 
18 FILE *fff[MAX_EVENTS];
19 
20 static int num_events=0;
21 
23 int retval;
24 int i;
26 long long values[MAX_EVENTS];
28 
29 void
31 {
32  /* Do one final read */
34  if (retval != PAPI_OK) {
35  fprintf(stderr, "PAPI_start() failed\n");
36  }
37 
40 
41  for(i=0;i<num_events;i++) {
42 
43  fprintf(fff[i],"%.4f %.1f (* Average Power for %s *)\n",
44  total_time,
45  ((double)values[i]/1.0e9)/elapsed_time,
46  events[i]);
47  fflush(fff[i]);
48  fclose(fff[i]);
49  }
50 
51  /* Reap your child */
52  wait(NULL);
54  PAPI_shutdown();
55 
56  /* Die */
57  exit(0);
58 }
59 
60 int main (int argc, char **argv)
61 {
62 
63  int cid,rapl_cid=-1,numcmp;
64  int code,enum_retval;
65  const PAPI_component_info_t *cmpinfo = NULL;
66  char event_name[BUFSIZ];
67 
68  if (argc < 2) {
69  fprintf(stderr, "Usage: %s program_to_run [options for program]\n", argv[0] );
70  exit(0);
71  }
72 
73  switch( fork() ) {
74  case -1:
75  fprintf(stderr,"Fork failed!, bailing");
76  exit(1);
77  case 0:
78  /* TODO, */
79  sleep(1); /* Give parent time to init papi and open files */
80 
81  execvp( argv[1], &argv[1]);
82  default:
83  signal(SIGCHLD, child_exit);
84  break;
85  }
86 
87  /* PAPI Initialization */
89  if ( retval != PAPI_VER_CURRENT ) {
90  fprintf(stderr,"PAPI_library_init failed\n");
91  exit(1);
92  }
93 
94  numcmp = PAPI_num_components();
95 
96  for(cid=0; cid<numcmp; cid++) {
97 
98  if ( (cmpinfo = PAPI_get_component_info(cid)) == NULL) {
99  fprintf(stderr,"PAPI_get_component_info failed\n");
100  exit(1);
101  }
102 
103  if (strstr(cmpinfo->name,"micpower")) {
104  rapl_cid=cid;
105  printf("Found micpower component at cid %d\n", rapl_cid);
106 
107  if (cmpinfo->disabled) {
108  fprintf(stderr,"No micpower events found: %s\n",
109  cmpinfo->disabled_reason);
110  exit(1);
111  }
112  break;
113  }
114  }
115 
116  /* Component not found */
117  if (cid==numcmp) {
118  fprintf(stderr,"No micpower component found\n");
119  exit(1);
120  }
121 
122 
123  /* Find Events */
124  code = PAPI_NATIVE_MASK;
125 
126  enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_FIRST, cid );
127 
128  while ( enum_retval == PAPI_OK ) {
129 
130  retval = PAPI_event_code_to_name( code, event_name );
131  if ( retval != PAPI_OK ) {
132  printf("Error translating %#x\n",code);
133  exit(1);
134  }
135 
136  printf("Found: %s\n",event_name);
137  strncpy(events[num_events],event_name,BUFSIZ);
138  sprintf(filenames[num_events],"results.%s",event_name);
139  num_events++;
140 
141  if (num_events==MAX_EVENTS) {
142  printf("Too many events! %d\n",num_events);
143  exit(1);
144  }
145 
146  enum_retval = PAPI_enum_cmp_event( &code, PAPI_ENUM_EVENTS, cid );
147 
148  }
149 
150  if (num_events==0) {
151  printf("Error! No RAPL events found!\n");
152  exit(1);
153  }
154 
155  /* Open output files */
156  for(i=0;i<num_events;i++) {
157  fff[i]=fopen(filenames[i],"w");
158  if (fff[i]==NULL) {
159  fprintf(stderr,"Could not open %s\n",filenames[i]);
160  exit(1);
161  }
162  }
163 
164 
165  /* Create EventSet */
167  if (retval != PAPI_OK) {
168  fprintf(stderr,"Error creating eventset!\n");
169  }
170 
171  for(i=0;i<num_events;i++) {
172 
174  if (retval != PAPI_OK) {
175  fprintf(stderr,"Error adding event %s\n",events[i]);
176  }
177  }
178 
179 
180 
182 
183  while(1) {
184 
185  /* Start Counting */
188  if (retval != PAPI_OK) {
189  fprintf(stderr,"PAPI_start() failed\n");
190  exit(1);
191  }
192 
193 
194  usleep(100000);
195 
196  /* Stop Counting */
199  if (retval != PAPI_OK) {
200  fprintf(stderr, "PAPI_start() failed\n");
201  }
202 
205 
206  for(i=0;i<num_events;i++) {
207  if( (strstr(events[i],"vccp") != NULL) ||
208  (strstr(events[i],"vddg") != NULL) ||
209  (strstr(events[i],"vddq") != NULL) ) {
210 
211  fprintf(fff[i],"%.4f %.1f (* Average Voltage (Volt) for %s *)\n",
212  total_time,
213  ((double)values[i]/1.0e6),
214  events[i]);
215  } else {
216  fprintf(fff[i],"%.4f %.1f (* Average Power (Watt) for %s *)\n",
217  total_time,
218  ((double)values[i]/1.0e6),
219  events[i]);
220  }
221 
222  fflush(fff[i]);
223  }
224  }
225 
226  return 0;
227 }
228 
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:23
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:625
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2232
#define PAPI_NATIVE_MASK
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:805
sleep(1)
char filenames[MAX_EVENTS][BUFSIZ]
#define PAPI_NULL
Definition: fpapi.h:13
int PAPI_add_named_event(int EventSet, char *EventName)
Definition: papi.c:1816
static int num_events
int PAPI_num_components(void)
Definition: papi.c:4285
return PAPI_OK
Definition: linux-nvml.c:458
fclose(thread_wqfd)
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 ** argv
Definition: iozone.c:1610
long long start_time
char disabled_reason[PAPI_MAX_STR_LEN]
Definition: papi.h:632
#define MAX_EVENTS
int PAPI_library_init(int version)
Definition: papi.c:495
int i
Definition: fileop.c:140
void PAPI_shutdown(void)
Definition: papi.c:4357
int wait()
void child_exit()
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
char events[MAX_EVENTS][BUFSIZ]
long long before_time
int PAPI_enum_cmp_event(int *EventCode, int modifier, int cidx)
Definition: papi.c:1311
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1406
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:924
int EventSet
long long after_time
long long PAPI_get_real_nsec(void)
Definition: papi.c:6111
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:1944
int PAPI_start(int EventSet)
Definition: papi.c:2026
double elapsed_time
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
signal(SIGINT, signal_handler)
double total_time
Definition: iozone.c:1268