File: papi_xml_event_info.c

package info (click to toggle)
papi 5.7.0+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 9,856 kB
  • sloc: ansic: 93,265; fortran: 3,338; xml: 2,460; makefile: 815; sh: 290
file content (450 lines) | stat: -rw-r--r-- 12,040 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/** file papi_xml_event_info.c
 *     @page papi_xml_event_info
 * @brief papi_xml_event_info utility.
 *     @section  NAME
 *             papi_xml_event_info - provides detailed information for PAPI events in XML format
 *
 *     @section Synopsis
 *
 *     @section Description
 *             papi_native_avail is a PAPI utility program that reports information 
 *             about the events available on the current platform in an XML format.
 *
 *             It will attempt to create an EventSet with each event in it, which
 *             can be slow.
 *
 *     @section Options
 *      <ul>
 *          <li>-h   print help message
 *          <li>-p   print only preset events
 *          <li>-n   print only native events
 *          <li>-c COMPONENT  print only events from component number COMPONENT
 * event1, event2, ...  Print only events that can be created in the same
 *      event set with the events event1, event2, etc.
 *       </ul>
 *
 *     @section Bugs
 *             There are no known bugs in this utility.
 *             If you find a bug, it should be reported to the
 *             PAPI Mailing List at <ptools-perfapi@icl.utk.edu>.
 */

#include <stdio.h>
#include <stdlib.h>

#include "papi.h"

static int EventSet;
static int preset = 1;
static int native = 1;
static int cidx = -1;

/**********************************************************************/
/* Take a string and print a version with properly escaped XML        */
/**********************************************************************/
static int
xmlize( const char *msg, FILE *f )
{
	const char *op;

	if ( !msg )
		return PAPI_OK;

	for ( op = msg; *op != '\0'; op++ ) {
		switch ( *op ) {
		case '"':
			fprintf( f, "&quot;" );
			break;
		case '&':
			fprintf( f, "&amp;" );
			break;
		case '\'':
			fprintf( f, "&apos;" );
			break;
		case '<':
			fprintf( f, "&lt;" );
			break;
		case '>':
			fprintf( f, "&gt;" );
			break;
		default:
		        fprintf( f, "%c", *op);
		}
	}

	return PAPI_OK;
}

/*************************************/
/* print hardware info in XML format */
/*************************************/
static int
papi_xml_hwinfo( FILE * f )
{
	const PAPI_hw_info_t *hwinfo;

	if ( ( hwinfo = PAPI_get_hardware_info(  ) ) == NULL )
		return PAPI_ESYS;

	fprintf( f, "<hardware>\n" );

	fprintf( f, "  <vendor string=\"");
	   xmlize( hwinfo->vendor_string, f );
	   fprintf( f,"\"/>\n");
	fprintf( f, "  <vendorCode value=\"%d\"/>\n", hwinfo->vendor );
	fprintf( f, "  <model string=\"");
	xmlize( hwinfo->model_string, f );
	   fprintf( f, "\"/>\n");
	fprintf( f, "  <modelCode value=\"%d\"/>\n", hwinfo->model );
	fprintf( f, "  <cpuRevision value=\"%f\"/>\n", hwinfo->revision );
	fprintf( f, "  <cpuID>\n" );
	fprintf( f, "    <family value=\"%d\"/>\n", hwinfo->cpuid_family );
	fprintf( f, "    <model value=\"%d\"/>\n", hwinfo->cpuid_model );
	fprintf( f, "    <stepping value=\"%d\"/>\n", hwinfo->cpuid_stepping );
	fprintf( f, "  </cpuID>\n" );
	fprintf( f, "  <cpuMaxMegahertz value=\"%d\"/>\n", hwinfo->cpu_max_mhz );
	fprintf( f, "  <cpuMinMegahertz value=\"%d\"/>\n", hwinfo->cpu_min_mhz );
	fprintf( f, "  <threads value=\"%d\"/>\n", hwinfo->threads );
	fprintf( f, "  <cores value=\"%d\"/>\n", hwinfo->cores );
	fprintf( f, "  <sockets value=\"%d\"/>\n", hwinfo->sockets );
	fprintf( f, "  <nodes value=\"%d\"/>\n", hwinfo->nnodes );
	fprintf( f, "  <cpuPerNode value=\"%d\"/>\n", hwinfo->ncpu );
	fprintf( f, "  <totalCPUs value=\"%d\"/>\n", hwinfo->totalcpus );
	fprintf( f, "</hardware>\n" );

	return PAPI_OK;
}



/****************************************************************/
/* Test if event can be added to an eventset                    */
/* (there might be existing events if specified on command line */
/****************************************************************/

static int
test_event( int evt )
{
	int retval;

	retval = PAPI_add_event( EventSet, evt );
	if ( retval != PAPI_OK ) {
		return retval;
	}

	if ( ( retval = PAPI_remove_event( EventSet, evt ) ) != PAPI_OK ) {
	   fprintf( stderr, "Error removing event from eventset\n" );
	   exit( 1 );
	}
	return PAPI_OK;
}

/***************************************/
/* Convert an event to XML             */
/***************************************/

static void
xmlize_event( FILE * f, PAPI_event_info_t * info, int num )
{

	if ( num >= 0 ) {
	   fprintf( f, "    <event index=\"%d\" name=\"",num);
	   xmlize( info->symbol, f );
	   fprintf( f, "\" desc=\"");
	   xmlize( info->long_descr, f );
	   fprintf( f, "\">\n");
	}
	else {
	   fprintf( f,"        <modifier name=\"");
	   xmlize( info->symbol, f );
	   fprintf( f,"\" desc=\"");
	   xmlize( info->long_descr, f );
	   fprintf( f,"\"> </modifier>\n");
	}

}


/****************************************/
/* Print all preset events              */
/****************************************/

static void
enum_preset_events( FILE * f, int cidx)
{
	int i, num;
	int retval;
	PAPI_event_info_t info;

	i = PAPI_PRESET_MASK;
	fprintf( f, "  <eventset type=\"PRESET\">\n" );
	num = -1;
	retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_FIRST, cidx );

	while ( retval == PAPI_OK ) {
	   num++;
	   retval = PAPI_get_event_info( i, &info );
	   if ( retval != PAPI_OK ) {
	     retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx );
	      continue;
	   }
	   if ( test_event( i ) == PAPI_OK ) {
	      xmlize_event( f, &info, num );
	      fprintf( f, "    </event>\n" );
	   }
	   retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx );
	}
	fprintf( f, "  </eventset>\n" );
}

/****************************************/
/* Print all native events              */
/****************************************/

static void
enum_native_events( FILE * f, int cidx)
{
	int i, k, num;
	int retval;
	PAPI_event_info_t info;

	i = PAPI_NATIVE_MASK;
	fprintf( f, "  <eventset type=\"NATIVE\">\n" );
	num = -1;
	retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_FIRST, cidx );

	while ( retval == PAPI_OK ) {

	   num++;
	   retval = PAPI_get_event_info( i, &info );
	   if ( retval != PAPI_OK ) {
	      retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx );
	      continue;
	   }

	   /* enumerate any umasks */
	   k = i;
	   if ( PAPI_enum_cmp_event( &k, PAPI_NTV_ENUM_UMASKS, cidx ) == PAPI_OK ) {

	      /* Test if event can be added */
	      if ( test_event( k ) == PAPI_OK ) {

		 /* add the event */
		 xmlize_event( f, &info, num );

		 /* add the event's unit masks */
		 do {
		    retval = PAPI_get_event_info( k, &info );
		    if ( retval == PAPI_OK ) {
		       if ( test_event( k )!=PAPI_OK ) {
			   break;
		       }
		       xmlize_event( f, &info, -1 );
		    }
		 } while ( PAPI_enum_cmp_event( &k, PAPI_NTV_ENUM_UMASKS, cidx ) == PAPI_OK);
		 fprintf( f, "    </event>\n" );
	      }
	   } else {
              /* this event has no unit masks; test & write the event */
	      if ( test_event( i ) == PAPI_OK ) {
		 xmlize_event( f, &info, num );
		 fprintf( f, "    </event>\n" );
	      }
	   }
	   retval = PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx );
	}
	fprintf( f, "  </eventset>\n" );
}

/****************************************/
/* Print usage information              */
/****************************************/

static void
usage( char *argv[] )
{
	fprintf( stderr, "Usage: %s [options] [[event1] event2 ...]\n", argv[0] );
	fprintf( stderr, "     options: -h     print help message\n" );
	fprintf( stderr, "              -p     print only preset events\n" );
	fprintf( stderr, "              -n     print only native events\n" );
	fprintf( stderr,"              -c n   print only events for component index n\n" );
	fprintf( stderr, "If event1, event2, etc., are specified, then only events\n");
	fprintf( stderr, "that can be run in addition to these events will be printed\n\n");
}

static void
parse_command_line (int argc, char **argv, int numc) {

  int i,retval;

     for( i = 1; i < argc; i++ ) {
	if ( argv[i][0] == '-' ) {
	   switch ( argv[i][1] ) {
	      case 'c':
      	                 /* only events for specified component */

                         /* UGH, what is this, the IOCCC? */
                         cidx = (i+1) < argc ? atoi( argv[(i++)+1] ) : -1;
			 if ( cidx < 0 || cidx >= numc ) {
			    fprintf( stderr,"Error: component index %d out of bounds (0..%d)\n",
				     cidx, numc - 1 );
			    usage( argv );
			    exit(1);
			 }
			 break;

	      case 'p':
		         /* only preset events */
			 preset = 1;
			 native = 0;
			 break;

	      case 'n':
		         /* only native events */
			 native = 1;
			 preset = 0;
			 break;

	      case 'h':
		         /* print help */
			 usage( argv );
			 exit(0);
			 break;

	      default:
			 fprintf( stderr, 
				     "Error: unknown option: %s\n", argv[i] );
			 usage( argv );
			 exit(1);
	   }
	} else {

	   /* If event names are specified, add them to the */
	   /* EventSet and test if other events can be run with them */

	   int code = -1;

	   retval = PAPI_event_name_to_code( argv[i], &code );
	   retval = PAPI_query_event( code );
	   if ( retval != PAPI_OK ) {
	      fprintf( stderr, "Error: unknown event: %s\n", argv[i] );
	      usage( argv );
	      exit(1);
	   }

	   retval = PAPI_add_event( EventSet, code );
	   if ( retval != PAPI_OK ) {
	      fprintf( stderr, 
                       "Error: event %s cannot be counted with others\n",
		       argv[i] );
	      usage( argv );
	      exit(1);
	   }
	}
     }

}


int
main( int argc, char **argv)
{
	int retval;
	const PAPI_component_info_t *comp;

	int numc = 0;

	retval = PAPI_library_init( PAPI_VER_CURRENT );
	if ( retval != PAPI_VER_CURRENT ) {
		fprintf(stderr,"Error!  PAPI_library_init\n");
		return retval;
	}

	/* report any return codes less than 0? */
	/* Why? */
#if 0
	retval = PAPI_set_debug( PAPI_VERB_ECONT );
	if ( retval != PAPI_OK ) {
	   test_fail( __FILE__, __LINE__, "PAPI_set_debug", retval );
	}
#endif

	/* Create EventSet to use */
	EventSet = PAPI_NULL;

	retval = PAPI_create_eventset( &EventSet  );
	if ( retval != PAPI_OK ) {
		fprintf(stderr,"Error!  PAPI_create_eventset\n");
		return retval;
	}

	/* Get number of components */
	numc = PAPI_num_components(  );

	/* parse command line arguments */
        parse_command_line(argc,argv,numc);

	/* print XML header */
	fprintf( stdout, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
	fprintf( stdout, "<eventinfo>\n" );


	/* print hardware info */
	papi_xml_hwinfo( stdout );

	/* If a specific component specified, only print events from there */
	if ( cidx >= 0 ) {
	   comp = PAPI_get_component_info( cidx );

	   fprintf( stdout, "<component index=\"%d\" type=\"%s\" id=\"%s\">\n",
			    cidx, cidx ? "Unknown" : "CPU", comp->name );

	   if ( native )
	      enum_native_events( stdout, cidx);
	   if ( preset )
	      enum_preset_events( stdout, cidx);

	   fprintf( stdout, "</component>\n" );
	}
	else {
	   /* Otherwise, print info for all components */
	   for ( cidx = 0; cidx < numc; cidx++ ) {
	       comp = PAPI_get_component_info( cidx );

	       fprintf( stdout, "<component index=\"%d\" type=\"%s\" id=\"%s\">\n",
				cidx, cidx ? "Unknown" : "CPU", comp->name );

	       if ( native )
		  enum_native_events( stdout, cidx );
	       if ( preset )
		  enum_preset_events( stdout, cidx );

	       fprintf( stdout, "</component>\n" );

	       /* clean out eventset */
	       retval = PAPI_cleanup_eventset( EventSet );
	       if ( retval != PAPI_OK ) {
			fprintf(stderr,"Error!  PAPI_cleanup_eventset\n");
			return retval;
		}
	       retval = PAPI_destroy_eventset( &EventSet );
	       if ( retval != PAPI_OK ) {
			fprintf(stderr,"Error!  PAPI_destroy_eventset\n");
			return retval;
		}
	       EventSet = PAPI_NULL;

		retval = PAPI_create_eventset( &EventSet  );
		if ( retval != PAPI_OK ) {
			fprintf(stderr,"Error!  PAPI_create_eventset\n");
			return retval;
		}

	       /* re-parse command line to set up any events specified */
	       parse_command_line (argc, argv, numc);
	   }
	}
	fprintf( stdout, "</eventinfo>\n" );

	return 0;
}