PAPI  5.3.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  retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx );
241  continue;
242  }
243  xmlize_event( f, &info, -1 );
244  }
245  } while ( PAPI_enum_cmp_event( &k, PAPI_NTV_ENUM_UMASKS, cidx ) == PAPI_OK);
246  fprintf( f, " </event>\n" );
247  }
248  } else {
249  /* this event has no unit masks; test & write the event */
250  if ( test_event( i ) == PAPI_OK ) {
251  xmlize_event( f, &info, num );
252  fprintf( f, " </event>\n" );
253  }
254  }
255  retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx );
256  }
257  fprintf( f, " </eventset>\n" );
258 }
259 
260 /****************************************/
261 /* Print usage information */
262 /****************************************/
263 
264 static void
265 usage( char *argv[] )
266 {
267  fprintf( stderr, "Usage: %s [options] [[event1] event2 ...]\n", argv[0] );
268  fprintf( stderr, " options: -h print help message\n" );
269  fprintf( stderr, " -p print only preset events\n" );
270  fprintf( stderr, " -n print only native events\n" );
271  fprintf( stderr," -c n print only events for component index n\n" );
272  fprintf( stderr, "If event1, event2, etc., are specified, then only events\n");
273  fprintf( stderr, "that can be run in addition to these events will be printed\n\n");
274 }
275 
276 static void
277 parse_command_line (int argc, char **argv, int numc) {
278 
279  int i,retval;
280 
281  for( i = 1; i < argc; i++ ) {
282  if ( argv[i][0] == '-' ) {
283  switch ( argv[i][1] ) {
284  case 'c':
285  /* only events for specified component */
286 
287  /* UGH, what is this, the IOCCC? */
288  cidx = (i+1) < argc ? atoi( argv[(i++)+1] ) : -1;
289  if ( cidx < 0 || cidx >= numc ) {
290  fprintf( stderr,"Error: component index %d out of bounds (0..%d)\n",
291  cidx, numc - 1 );
292  usage( argv );
293  exit(1);
294  }
295  break;
296 
297  case 'p':
298  /* only preset events */
299  preset = 1;
300  native = 0;
301  break;
302 
303  case 'n':
304  /* only native events */
305  native = 1;
306  preset = 0;
307  break;
308 
309  case 'h':
310  /* print help */
311  usage( argv );
312  exit(0);
313  break;
314 
315  default:
316  fprintf( stderr,
317  "Error: unknown option: %s\n", argv[i] );
318  usage( argv );
319  exit(1);
320  }
321  } else {
322 
323  /* If event names are specified, add them to the */
324  /* EventSet and test if other events can be run with them */
325 
326  int code = -1;
327 
328  retval = PAPI_event_name_to_code( argv[i], &code );
329  retval = PAPI_query_event( code );
330  if ( retval != PAPI_OK ) {
331  fprintf( stderr, "Error: unknown event: %s\n", argv[i] );
332  usage( argv );
333  exit(1);
334  }
335 
336  retval = PAPI_add_event( EventSet, code );
337  if ( retval != PAPI_OK ) {
338  fprintf( stderr,
339  "Error: event %s cannot be counted with others\n",
340  argv[i] );
341  usage( argv );
342  exit(1);
343  }
344  }
345  }
346 
347 }
348 
349 
350 int
351 main( int argc, char **argv)
352 {
353  int retval;
354  const PAPI_component_info_t *comp;
355 
356  int numc = 0;
357 
358  /* Set TESTS_QUIET variable */
359  tests_quiet( argc, argv );
360 
362  if ( retval != PAPI_VER_CURRENT ) {
363  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
364  }
365 
366  /* report any return codes less than 0? */
367  /* Why? */
368 #if 0
369  retval = PAPI_set_debug( PAPI_VERB_ECONT );
370  if ( retval != PAPI_OK ) {
371  test_fail( __FILE__, __LINE__, "PAPI_set_debug", retval );
372  }
373 #endif
374 
375  /* Create EventSet to use */
377 
378  retval = PAPI_create_eventset( &EventSet );
379  if ( retval != PAPI_OK ) {
380  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
381  return 1;
382  }
383 
384  /* Get number of components */
385  numc = PAPI_num_components( );
386 
387  /* parse command line arguments */
388  parse_command_line(argc,argv,numc);
389 
390  /* print XML header */
391  fprintf( stdout, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
392  fprintf( stdout, "<eventinfo>\n" );
393 
394 
395  /* print hardware info */
396  papi_xml_hwinfo( stdout );
397 
398  /* If a specific component specified, only print events from there */
399  if ( cidx >= 0 ) {
400  comp = PAPI_get_component_info( cidx );
401 
402  fprintf( stdout, "<component index=\"%d\" type=\"%s\" id=\"%s\">\n",
403  cidx, cidx ? "Unknown" : "CPU", comp->name );
404 
405  if ( native )
406  enum_native_events( stdout, cidx);
407  if ( preset )
408  enum_preset_events( stdout, cidx);
409 
410  fprintf( stdout, "</component>\n" );
411  }
412  else {
413  /* Otherwise, print info for all components */
414  for ( cidx = 0; cidx < numc; cidx++ ) {
415  comp = PAPI_get_component_info( cidx );
416 
417  fprintf( stdout, "<component index=\"%d\" type=\"%s\" id=\"%s\">\n",
418  cidx, cidx ? "Unknown" : "CPU", comp->name );
419 
420  if ( native )
421  enum_native_events( stdout, cidx );
422  if ( preset )
423  enum_preset_events( stdout, cidx );
424 
425  fprintf( stdout, "</component>\n" );
426 
427  /* clean out eventset */
428  retval = PAPI_cleanup_eventset( EventSet );
429  if ( retval != PAPI_OK )
430  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
431  retval = PAPI_destroy_eventset( &EventSet );
432  if ( retval != PAPI_OK )
433  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
435 
436  retval = PAPI_create_eventset( &EventSet );
437  if ( retval != PAPI_OK ) {
438  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval ); }
439 
440  /* re-parse command line to set up any events specified */
441  parse_command_line (argc, argv, numc);
442 
443 
444  }
445  }
446  fprintf( stdout, "</eventinfo>\n" );
447 
448  return 0;
449 }
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:625
int atoi()
#define PAPI_NATIVE_MASK
int cores
Definition: papi.h:778
double f(double a)
Definition: cpi.c:23
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:803
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1597
Hardware info structure.
Definition: papi.h:775
#define PAPI_NULL
Definition: fpapi.h:13
int PAPI_remove_event(int EventSet, int EventCode)
Definition: papi.c:1692
static void xmlize_event(FILE *f, PAPI_event_info_t *info, int num)
Definition: event_info.c:147
int cpu_min_mhz
Definition: papi.h:792
char long_descr[PAPI_HUGE_STR_LEN]
Definition: papi.h:964
int PAPI_num_components(void)
Definition: papi.c:4272
#define PAPI_VERB_ECONT
Definition: fpapi.h:39
char symbol[PAPI_HUGE_STR_LEN]
Definition: papi.h:961
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:789
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:841
static int papi_xml_hwinfo(FILE *f)
Definition: event_info.c:82
char ** argv
Definition: iozone.c:1610
int threads
Definition: papi.h:777
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
int PAPI_library_init(int version)
Definition: papi.c:495
float revision
Definition: papi.h:786
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
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:788
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int nnodes
Definition: papi.h:780
int PAPI_enum_cmp_event(int *EventCode, int modifier, int cidx)
Definition: papi.c:1305
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2795
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1399
int cpuid_family
Definition: papi.h:787
#define PAPI_ESYS
Definition: fpapi.h:108
char vendor_string[PAPI_MAX_STR_LEN]
Definition: papi.h:783
int PAPI_event_name_to_code(char *in, int *out)
Definition: papi.c:1003
int PAPI_query_event(int EventCode)
Definition: papi.c:695
int EventSet
int vendor
Definition: papi.h:782
static void enum_native_events(FILE *f, int cidx)
Definition: event_info.c:205
int model
Definition: papi.h:784
int cpu_max_mhz
Definition: papi.h:791
int totalcpus
Definition: papi.h:781
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:1937
char model_string[PAPI_MAX_STR_LEN]
Definition: papi.h:785
static int test_event(int evt)
Definition: event_info.c:126
const PAPI_hw_info_t * PAPI_get_hardware_info(void)
Definition: papi.c:6046
ssize_t retval
Definition: libasync.c:338
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
int PAPI_set_debug(int level)
Definition: papi.c:3027
int sockets
Definition: papi.h:779
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:277
int int op
Definition: iozone.c:19389
int ncpu
Definition: papi.h:776