PAPI  5.4.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
event_info.c
Go to the documentation of this file.
1 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include "papi.h"
35 #include "papi_test.h"
36 
37 static int EventSet;
38 static int preset = 1;
39 static int native = 1;
40 static int cidx = -1;
41 
42 /**********************************************************************/
43 /* Take a string and print a version with properly escaped XML */
44 /**********************************************************************/
45 static int
46 xmlize( const char *msg, FILE *f )
47 {
48  const char *op;
49 
50  if ( !msg )
51  return PAPI_OK;
52 
53  for ( op = msg; *op != '\0'; op++ ) {
54  switch ( *op ) {
55  case '"':
56  fprintf( f, "&quot;" );
57  break;
58  case '&':
59  fprintf( f, "&amp;" );
60  break;
61  case '\'':
62  fprintf( f, "&apos;" );
63  break;
64  case '<':
65  fprintf( f, "&lt;" );
66  break;
67  case '>':
68  fprintf( f, "&gt;" );
69  break;
70  default:
71  fprintf( f, "%c", *op);
72  }
73  }
74 
75  return PAPI_OK;
76 }
77 
78 /*************************************/
79 /* print hardware info in XML format */
80 /*************************************/
81 static int
82 papi_xml_hwinfo( FILE * f )
83 {
84  const PAPI_hw_info_t *hwinfo;
85 
86  if ( ( hwinfo = PAPI_get_hardware_info( ) ) == NULL )
87  return PAPI_ESYS;
88 
89  fprintf( f, "<hardware>\n" );
90 
91  fprintf( f, " <vendor string=\"");
92  xmlize( hwinfo->vendor_string, f );
93  fprintf( f,"\"/>\n");
94  fprintf( f, " <vendorCode value=\"%d\"/>\n", hwinfo->vendor );
95  fprintf( f, " <model string=\"");
96  xmlize( hwinfo->model_string, f );
97  fprintf( f, "\"/>\n");
98  fprintf( f, " <modelCode value=\"%d\"/>\n", hwinfo->model );
99  fprintf( f, " <cpuRevision value=\"%f\"/>\n", hwinfo->revision );
100  fprintf( f, " <cpuID>\n" );
101  fprintf( f, " <family value=\"%d\"/>\n", hwinfo->cpuid_family );
102  fprintf( f, " <model value=\"%d\"/>\n", hwinfo->cpuid_model );
103  fprintf( f, " <stepping value=\"%d\"/>\n", hwinfo->cpuid_stepping );
104  fprintf( f, " </cpuID>\n" );
105  fprintf( f, " <cpuMaxMegahertz value=\"%d\"/>\n", hwinfo->cpu_max_mhz );
106  fprintf( f, " <cpuMinMegahertz value=\"%d\"/>\n", hwinfo->cpu_min_mhz );
107  fprintf( f, " <threads value=\"%d\"/>\n", hwinfo->threads );
108  fprintf( f, " <cores value=\"%d\"/>\n", hwinfo->cores );
109  fprintf( f, " <sockets value=\"%d\"/>\n", hwinfo->sockets );
110  fprintf( f, " <nodes value=\"%d\"/>\n", hwinfo->nnodes );
111  fprintf( f, " <cpuPerNode value=\"%d\"/>\n", hwinfo->ncpu );
112  fprintf( f, " <totalCPUs value=\"%d\"/>\n", hwinfo->totalcpus );
113  fprintf( f, "</hardware>\n" );
114 
115  return PAPI_OK;
116 }
117 
118 
119 
120 /****************************************************************/
121 /* Test if event can be added to an eventset */
122 /* (there might be existing events if specified on command line */
123 /****************************************************************/
124 
125 static int
126 test_event( int evt )
127 {
128  int retval;
129 
130  retval = PAPI_add_event( EventSet, evt );
131  if ( retval != PAPI_OK ) {
132  return retval;
133  }
134 
135  if ( ( retval = PAPI_remove_event( EventSet, evt ) ) != PAPI_OK ) {
136  fprintf( stderr, "Error removing event from eventset\n" );
137  exit( 1 );
138  }
139  return PAPI_OK;
140 }
141 
142 /***************************************/
143 /* Convert an event to XML */
144 /***************************************/
145 
146 static void
147 xmlize_event( FILE * f, PAPI_event_info_t * info, int num )
148 {
149 
150  if ( num >= 0 ) {
151  fprintf( f, " <event index=\"%d\" name=\"",num);
152  xmlize( info->symbol, f );
153  fprintf( f, "\" desc=\"");
154  xmlize( info->long_descr, f );
155  fprintf( f, "\">\n");
156  }
157  else {
158  fprintf( f," <modifier name=\"");
159  xmlize( info->symbol, f );
160  fprintf( f,"\" desc=\"");
161  xmlize( info->long_descr, f );
162  fprintf( f,"\"> </modifier>\n");
163  }
164 
165 }
166 
167 
168 /****************************************/
169 /* Print all preset events */
170 /****************************************/
171 
172 static void
173 enum_preset_events( FILE * f, int cidx)
174 {
175  int i, num;
176  int retval;
177  PAPI_event_info_t info;
178 
179  i = PAPI_PRESET_MASK;
180  fprintf( f, " <eventset type=\"PRESET\">\n" );
181  num = -1;
182  retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_FIRST, cidx );
183 
184  while ( retval == PAPI_OK ) {
185  num++;
186  retval = PAPI_get_event_info( i, &info );
187  if ( retval != PAPI_OK ) {
188  retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx );
189  continue;
190  }
191  if ( test_event( i ) == PAPI_OK ) {
192  xmlize_event( f, &info, num );
193  fprintf( f, " </event>\n" );
194  }
195  retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx );
196  }
197  fprintf( f, " </eventset>\n" );
198 }
199 
200 /****************************************/
201 /* Print all native events */
202 /****************************************/
203 
204 static void
205 enum_native_events( FILE * f, int cidx)
206 {
207  int i, k, num;
208  int retval;
209  PAPI_event_info_t info;
210 
211  i = PAPI_NATIVE_MASK;
212  fprintf( f, " <eventset type=\"NATIVE\">\n" );
213  num = -1;
214  retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_FIRST, cidx );
215 
216  while ( retval == PAPI_OK ) {
217 
218  num++;
219  retval = PAPI_get_event_info( i, &info );
220  if ( retval != PAPI_OK ) {
221  retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx );
222  continue;
223  }
224 
225  /* enumerate any umasks */
226  k = i;
227  if ( PAPI_enum_cmp_event( &k, PAPI_NTV_ENUM_UMASKS, cidx ) == PAPI_OK ) {
228 
229  /* Test if event can be added */
230  if ( test_event( k ) == PAPI_OK ) {
231 
232  /* add the event */
233  xmlize_event( f, &info, num );
234 
235  /* add the event's unit masks */
236  do {
237  retval = PAPI_get_event_info( k, &info );
238  if ( retval == PAPI_OK ) {
239  if ( test_event( k )!=PAPI_OK ) {
240  break;
241  }
242  xmlize_event( f, &info, -1 );
243  }
244  } while ( PAPI_enum_cmp_event( &k, PAPI_NTV_ENUM_UMASKS, cidx ) == PAPI_OK);
245  fprintf( f, " </event>\n" );
246  }
247  } else {
248  /* this event has no unit masks; test & write the event */
249  if ( test_event( i ) == PAPI_OK ) {
250  xmlize_event( f, &info, num );
251  fprintf( f, " </event>\n" );
252  }
253  }
254  retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx );
255  }
256  fprintf( f, " </eventset>\n" );
257 }
258 
259 /****************************************/
260 /* Print usage information */
261 /****************************************/
262 
263 static void
264 usage( char *argv[] )
265 {
266  fprintf( stderr, "Usage: %s [options] [[event1] event2 ...]\n", argv[0] );
267  fprintf( stderr, " options: -h print help message\n" );
268  fprintf( stderr, " -p print only preset events\n" );
269  fprintf( stderr, " -n print only native events\n" );
270  fprintf( stderr," -c n print only events for component index n\n" );
271  fprintf( stderr, "If event1, event2, etc., are specified, then only events\n");
272  fprintf( stderr, "that can be run in addition to these events will be printed\n\n");
273 }
274 
275 static void
276 parse_command_line (int argc, char **argv, int numc) {
277 
278  int i,retval;
279 
280  for( i = 1; i < argc; i++ ) {
281  if ( argv[i][0] == '-' ) {
282  switch ( argv[i][1] ) {
283  case 'c':
284  /* only events for specified component */
285 
286  /* UGH, what is this, the IOCCC? */
287  cidx = (i+1) < argc ? atoi( argv[(i++)+1] ) : -1;
288  if ( cidx < 0 || cidx >= numc ) {
289  fprintf( stderr,"Error: component index %d out of bounds (0..%d)\n",
290  cidx, numc - 1 );
291  usage( argv );
292  exit(1);
293  }
294  break;
295 
296  case 'p':
297  /* only preset events */
298  preset = 1;
299  native = 0;
300  break;
301 
302  case 'n':
303  /* only native events */
304  native = 1;
305  preset = 0;
306  break;
307 
308  case 'h':
309  /* print help */
310  usage( argv );
311  exit(0);
312  break;
313 
314  default:
315  fprintf( stderr,
316  "Error: unknown option: %s\n", argv[i] );
317  usage( argv );
318  exit(1);
319  }
320  } else {
321 
322  /* If event names are specified, add them to the */
323  /* EventSet and test if other events can be run with them */
324 
325  int code = -1;
326 
327  retval = PAPI_event_name_to_code( argv[i], &code );
328  retval = PAPI_query_event( code );
329  if ( retval != PAPI_OK ) {
330  fprintf( stderr, "Error: unknown event: %s\n", argv[i] );
331  usage( argv );
332  exit(1);
333  }
334 
335  retval = PAPI_add_event( EventSet, code );
336  if ( retval != PAPI_OK ) {
337  fprintf( stderr,
338  "Error: event %s cannot be counted with others\n",
339  argv[i] );
340  usage( argv );
341  exit(1);
342  }
343  }
344  }
345 
346 }
347 
348 
349 int
350 main( int argc, char **argv)
351 {
352  int retval;
353  const PAPI_component_info_t *comp;
354 
355  int numc = 0;
356 
357  /* Set TESTS_QUIET variable */
358  tests_quiet( argc, argv );
359 
361  if ( retval != PAPI_VER_CURRENT ) {
362  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
363  }
364 
365  /* report any return codes less than 0? */
366  /* Why? */
367 #if 0
368  retval = PAPI_set_debug( PAPI_VERB_ECONT );
369  if ( retval != PAPI_OK ) {
370  test_fail( __FILE__, __LINE__, "PAPI_set_debug", retval );
371  }
372 #endif
373 
374  /* Create EventSet to use */
376 
377  retval = PAPI_create_eventset( &EventSet );
378  if ( retval != PAPI_OK ) {
379  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
380  return 1;
381  }
382 
383  /* Get number of components */
384  numc = PAPI_num_components( );
385 
386  /* parse command line arguments */
387  parse_command_line(argc,argv,numc);
388 
389  /* print XML header */
390  fprintf( stdout, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
391  fprintf( stdout, "<eventinfo>\n" );
392 
393 
394  /* print hardware info */
395  papi_xml_hwinfo( stdout );
396 
397  /* If a specific component specified, only print events from there */
398  if ( cidx >= 0 ) {
399  comp = PAPI_get_component_info( cidx );
400 
401  fprintf( stdout, "<component index=\"%d\" type=\"%s\" id=\"%s\">\n",
402  cidx, cidx ? "Unknown" : "CPU", comp->name );
403 
404  if ( native )
405  enum_native_events( stdout, cidx);
406  if ( preset )
407  enum_preset_events( stdout, cidx);
408 
409  fprintf( stdout, "</component>\n" );
410  }
411  else {
412  /* Otherwise, print info for all components */
413  for ( cidx = 0; cidx < numc; cidx++ ) {
414  comp = PAPI_get_component_info( cidx );
415 
416  fprintf( stdout, "<component index=\"%d\" type=\"%s\" id=\"%s\">\n",
417  cidx, cidx ? "Unknown" : "CPU", comp->name );
418 
419  if ( native )
420  enum_native_events( stdout, cidx );
421  if ( preset )
422  enum_preset_events( stdout, cidx );
423 
424  fprintf( stdout, "</component>\n" );
425 
426  /* clean out eventset */
427  retval = PAPI_cleanup_eventset( EventSet );
428  if ( retval != PAPI_OK )
429  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
430  retval = PAPI_destroy_eventset( &EventSet );
431  if ( retval != PAPI_OK )
432  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
434 
435  retval = PAPI_create_eventset( &EventSet );
436  if ( retval != PAPI_OK ) {
437  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval ); }
438 
439  /* re-parse command line to set up any events specified */
440  parse_command_line (argc, argv, numc);
441 
442 
443  }
444  }
445  fprintf( stdout, "</eventinfo>\n" );
446 
447  return 0;
448 }
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:626
int atoi()
#define PAPI_NATIVE_MASK
int cores
Definition: papi.h:780
double f(double a)
Definition: cpi.c:23
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:807
#define PAPI_VERB_ECONT
Definition: papi.h:387
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1622
Hardware info structure.
Definition: papi.h:777
int PAPI_remove_event(int EventSet, int EventCode)
Definition: papi.c:1717
static void xmlize_event(FILE *f, PAPI_event_info_t *info, int num)
Definition: event_info.c:147
#define PAPI_NULL
Definition: papi.h:290
int cpu_min_mhz
Definition: papi.h:794
char long_descr[PAPI_HUGE_STR_LEN]
Definition: papi.h:966
int EventSet
Definition: data_range.c:25
int PAPI_num_components(void)
Definition: papi.c:4326
char symbol[PAPI_HUGE_STR_LEN]
Definition: papi.h:963
return PAPI_OK
Definition: linux-nvml.c:458
tests_quiet(argc, argv)
#define PAPI_PRESET_MASK
static int preset
Definition: event_info.c:38
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
int cpuid_stepping
Definition: papi.h:791
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:846
static int papi_xml_hwinfo(FILE *f)
Definition: event_info.c:82
char ** argv
Definition: iozone.c:1610
int threads
Definition: papi.h:779
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
int PAPI_library_init(int version)
Definition: papi.c:497
float revision
Definition: papi.h:788
int i
Definition: fileop.c:140
int k
Definition: iozone.c:19136
static int cidx
Definition: event_info.c:40
void usage(void)
Definition: fileop.c:1330
void int num
Definition: iozone.c:22151
#define PAPI_ESYS
Definition: papi.h:253
static int native
Definition: event_info.c:39
static int xmlize(const char *msg, FILE *f)
Definition: event_info.c:46
int cpuid_model
Definition: papi.h:790
int nnodes
Definition: papi.h:782
int PAPI_enum_cmp_event(int *EventCode, int modifier, int cidx)
Definition: papi.c:1324
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2834
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1424
int cpuid_family
Definition: papi.h:789
char vendor_string[PAPI_MAX_STR_LEN]
Definition: papi.h:785
int PAPI_event_name_to_code(char *in, int *out)
Definition: papi.c:1010
int PAPI_query_event(int EventCode)
Definition: papi.c:698
int vendor
Definition: papi.h:784
static void enum_native_events(FILE *f, int cidx)
Definition: event_info.c:205
int model
Definition: papi.h:786
int cpu_max_mhz
Definition: papi.h:793
int totalcpus
Definition: papi.h:783
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:1971
char model_string[PAPI_MAX_STR_LEN]
Definition: papi.h:787
static int test_event(int evt)
Definition: event_info.c:126
const PAPI_hw_info_t * PAPI_get_hardware_info(void)
Definition: papi.c:6111
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()
int PAPI_set_debug(int level)
Definition: papi.c:3070
int sockets
Definition: papi.h:781
static void enum_preset_events(FILE *f, int cidx)
Definition: event_info.c:173
static void parse_command_line(int argc, char **argv, int numc)
Definition: event_info.c:276
int int op
Definition: iozone.c:19389
int ncpu
Definition: papi.h:778