PAPI  5.7.0.0
papi_event_chooser.c
Go to the documentation of this file.
1 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "papi.h"
29 #include "print_header.h"
30 
32 int retval;
33 
34 static char *
36 {
37  if ( strlen( info->derived ) == 0 )
38  return ( "No" );
39  else if ( strcmp( info->derived, "NOT_DERIVED" ) == 0 )
40  return ( "No" );
41  else if ( strcmp( info->derived, "DERIVED_CMPD" ) == 0 )
42  return ( "No" );
43  else
44  return ( "Yes" );
45 }
46 
47 static int
48 add_remove_event( int EventSet, int evt )
49 {
50  int retval;
51 
52  if ( ( retval = PAPI_add_event( EventSet, evt ) ) != PAPI_OK ) {
53  //printf( "Error adding event.\n" );
54  } else {
55  if ( ( retval = PAPI_remove_event( EventSet, evt ) ) != PAPI_OK ) {
56  printf( "Error removing event.\n" );
57  }
58  }
59  return retval;
60 }
61 
62 static int
63 show_event_info( int evt )
64 {
65  int k;
66  int retval;
67  PAPI_event_info_t info;
68 
69  if ( ( retval = PAPI_get_event_info( evt, &info ) ) == PAPI_OK ) {
70  printf( "%s\t%#x\n |%s|\n",
71  info.symbol, info.event_code, info.long_descr );
72 
73  for( k = 0; k < ( int ) info.count; k++ ) {
74  if ( strlen( info.name[k] ) ) {
75  printf( " |Register Value[%d]: %#-10x %s|\n",
76  k, info.code[k], info.name[k] );
77  }
78  }
79  }
80  return retval;
81 }
82 
83 
84 static int
85 native( int cidx )
86 {
87  int i, j, k;
88  int retval, added;
89  PAPI_event_info_t info;
90 
91  j = 0;
92 
93  /* For platform independence, always ASK FOR the first event */
94  /* Don't just assume it'll be the first numeric value */
95  i = 0 | PAPI_NATIVE_MASK;
97  if (retval==PAPI_ENOEVNT) {
98  printf("Cannot find first event in component %d\n",cidx);
99  }
100 
101  do {
102  k = i;
103 
105  if ( ( added = add_remove_event( EventSet, k ) ) == PAPI_OK ) {
106  show_event_info( i );
107  do {
108  retval = PAPI_get_event_info( k, &info );
109  if ( retval == PAPI_OK ) {
110  printf( " %#-10x%s |%s|\n", info.event_code,
111  strchr( info.symbol, ':' ),
112  strchr( info.long_descr, ':' ) + 1 );
113  }
114  } while ( PAPI_enum_cmp_event( &k, PAPI_NTV_ENUM_UMASKS, cidx ) ==
115  PAPI_OK );
116  j++;
117  }
118  } else {
119  if ( ( added = add_remove_event( EventSet, i ) ) == PAPI_OK ) {
120  show_event_info( i );
121  j++;
122  }
123  }
124 
125  if ( added == PAPI_OK ) {
126  /* modifier = PAPI_NTV_ENUM_GROUPS returns event codes with a
127  groups id for each group in which this
128  native event lives, in bits 16 - 23 of event code
129  terminating with PAPI_ENOEVNT at the end of the list.
130  */
131  k = i;
133  printf( "Groups: " );
134  do {
135  printf( "%4d", ( ( k & PAPI_NTV_GROUP_AND_MASK ) >>
136  PAPI_NTV_GROUP_SHIFT ) - 1 );
137  } while ( PAPI_enum_cmp_event( &k, PAPI_NTV_ENUM_GROUPS, cidx ) ==
138  PAPI_OK );
139  printf( "\n" );
140  }
141 
142  printf( "---------------------------------------------"
143  "----------------------------\n" );
144  }
145  } while ( PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx ) == PAPI_OK );
146 
147  printf( "------------------------------------------"
148  "-------------------------------\n" );
149  printf( "Total events reported: %d\n", j );
150 
151  exit( 0 );
152 }
153 
154 static int
155 preset( void )
156 {
157  int i, j = 0;
158  int retval;
159  PAPI_event_info_t info;
160 
161  printf( " Name Code " );
162  printf( "Deriv Description (Note)\n" );
163 
164  /* For consistency, always ASK FOR the first event */
165  i = 0 | PAPI_PRESET_MASK;
167 
168  do {
170  if ( retval == PAPI_OK ) {
171  if ( PAPI_get_event_info( i, &info ) == PAPI_OK ) {
172  printf( "%-13s%#x %-5s%s",
173  info.symbol,
174  info.event_code, is_derived( &info ), info.long_descr );
175  if ( info.note[0] )
176  printf( " (%s)", info.note );
177  printf( "\n" );
178  }
179  if ( ( retval = PAPI_remove_event( EventSet, i ) ) != PAPI_OK )
180  printf( "Error in PAPI_remove_event\n" );
181  j++;
182  }
183  } while ( PAPI_enum_event( &i, PAPI_PRESET_ENUM_AVAIL ) == PAPI_OK );
184 
185  printf
186  ( "-------------------------------------------------------------------------\n" );
187  printf( "Total events reported: %d\n", j );
188 
189  exit( 0 );
190 }
191 
192 int
193 main( int argc, char **argv )
194 {
195  int i;
196  int pevent,cevent;
197  int cidx;
198 
199  const PAPI_hw_info_t *hwinfo = NULL;
200 
201  if ( argc < 3 ) {
202  goto use_exit;
203  }
204 
205  /* Init PAPI library */
207  if ( retval != PAPI_VER_CURRENT ) {
208  fprintf(stderr,"Error! PAPI_library_init\n");
209  return retval;
210  }
211 
213  if ( retval != PAPI_OK ) {
214  fprintf(stderr,"Error! PAPI_set_debug\n");
215  return retval;
216  }
217 
218  retval = papi_print_header( "Event Chooser: Available events "
219  "which can be added with given events.\n",
220  &hwinfo );
221  if ( retval != PAPI_OK ) {
222  fprintf(stderr, "Error! PAPI_get_hardware_info\n");
223  return 2;
224  }
225 
227  if ( retval != PAPI_OK ) {
228  fprintf( stderr, "PAPI_create_eventset error\n" );
229  return 1;
230  }
231 
232  retval = PAPI_event_name_to_code( argv[2], &cevent );
233  if ( retval != PAPI_OK ) {
234  fprintf( stderr, "Event %s can't be found\n", argv[2] );
235  return 1;
236  }
237  cidx = PAPI_get_event_component(cevent);
238 
239  for( i = 2; i < argc; i++ ) {
240  retval = PAPI_event_name_to_code( argv[i], &pevent );
241  if ( retval != PAPI_OK ) {
242  fprintf( stderr, "Event %s can't be found\n", argv[i] );
243  return 1;
244  }
245  retval = PAPI_add_event( EventSet, pevent );
246  if ( retval != PAPI_OK ) {
247  fprintf( stderr, "Event %s can't be counted with others %d\n",
248  argv[i], retval );
249  return 1;
250  }
251  }
252 
253  if ( !strcmp( "NATIVE", argv[1] ) ) {
254  native( cidx );
255  }
256  else if ( !strcmp( "PRESET", argv[1] ) ) {
257  preset( );
258  }
259  else {
260  goto use_exit;
261  }
262  return 0;
263 
264 use_exit:
265  fprintf( stderr,
266  "Usage: papi_event_chooser NATIVE|PRESET evt1 evt2 ... \n" );
267  return 1;
268 }
#define PAPI_OK
Definition: fpapi.h:105
unsigned int count
Definition: papi.h:988
#define PAPI_NATIVE_MASK
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1663
Hardware info structure.
Definition: papi.h:781
static int show_event_info(int evt)
int main(int argc, char **argv)
int PAPI_remove_event(int EventSet, int EventCode)
Definition: papi.c:1758
int PAPI_enum_event(int *EventCode, int modifier)
Definition: papi.c:1152
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
char long_descr[PAPI_HUGE_STR_LEN]
Definition: papi.h:970
static char * is_derived(PAPI_event_info_t *info)
int PAPI_event_name_to_code(const char *in, int *out)
Definition: papi.c:1004
#define PAPI_PRESET_MASK
unsigned int code[PAPI_MAX_INFO_TERMS]
Definition: papi.h:1004
char derived[PAPI_MIN_STR_LEN]
Definition: papi.h:996
Return codes and api definitions.
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:835
static int cidx
int PAPI_library_init(int version)
Definition: papi.c:500
#define PAPI_NTV_GROUP_SHIFT
Definition: papi.h:527
char symbol[PAPI_HUGE_STR_LEN]
Definition: papi.h:967
#define PAPI_VERB_ECONT
Definition: fpapi.h:39
#define PAPI_NULL
Definition: fpapi.h:13
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
static int add_remove_event(int EventSet, int evt)
static int native(int cidx)
char name[PAPI_MAX_INFO_TERMS][PAPI_2MAX_STR_LEN]
Definition: papi.h:1010
#define PAPI_NTV_GROUP_AND_MASK
Definition: papi.h:526
#define PAPI_ENOEVNT
Definition: fpapi.h:112
int retval
static int preset(void)
int EventSet
unsigned int event_code
Definition: papi.h:965
char note[PAPI_HUGE_STR_LEN]
Definition: papi.h:1014
void exit()
int PAPI_get_event_component(int EventCode)
Definition: papi.c:6583
int PAPI_set_debug(int level)
Definition: papi.c:3126
int i
Definition: fileop.c:140