PAPI  5.7.0.0
papi_hybrid_native_avail.c
Go to the documentation of this file.
1 /* This file utility reports hardware info and native event availability on either the host
2  * CPU or on one of the attached MIC devices. It is based on the papi_native_avail utility,
3  * but uses offloading to run either on the host CPU or on a target device. */
50 //#pragma offload_attribute (push,target(mic))
51 //#include "papi_test.h"
52 //#pragma offload_attribute (pop)
53 
54 #include <stdlib.h>
55 #include <offload.h>
56 
57 #define EVT_LINE 80
58 
59 typedef struct command_flags
60 {
61  int help;
62  int details;
63  int named;
64  int include;
65  int xclude;
66  char *name, *istr, *xstr;
67  int darr;
68  int dear;
69  int iarr;
70  int iear;
71  int opcm;
72  int umask;
73  int groups;
74  int mic;
75  int devidx;
77 
78 static void
79 print_help( char **argv )
80 {
81  printf( "This is the PAPI native avail program.\n" );
82  printf( "It provides availability and detail information for PAPI native events.\n" );
83  printf( "Usage: %s [options]\n", argv[0] );
84  printf( "\nOptions:\n" );
85  printf( " --help, -h print this help message\n" );
86  printf( " -d display detailed information about native events\n" );
87  printf( " -e EVENTNAME display detailed information about named native event\n" );
88  printf( " -i EVENTSTR include only event names that contain EVENTSTR\n" );
89  printf( " -x EVENTSTR exclude any event names that contain EVENTSTR\n" );
90  printf( " --noumasks suppress display of Unit Mask information\n" );
91  printf( "\nProcessor-specific options\n");
92  printf( " --darr display events supporting Data Address Range Restriction\n" );
93  printf( " --dear display Data Event Address Register events only\n" );
94  printf( " --iarr display events supporting Instruction Address Range Restriction\n" );
95  printf( " --iear display Instruction Event Address Register events only\n" );
96  printf( " --opcm display events supporting OpCode Matching\n" );
97  printf( " --nogroups suppress display of Event grouping information\n" );
98  printf( " --mic <index> display events on the specified Xeon Phi device\n" );
99  printf( "\n" );
100 }
101 
102 static int
103 no_str_arg( char *arg )
104 {
105  return ( ( arg == NULL ) || ( strlen( arg ) == 0 ) || ( arg[0] == '-' ) );
106 }
107 
108 static void
109 parse_args( int argc, char **argv, command_flags_t * f )
110 {
111 
112  int i;
113 
114  /* Look for all currently defined commands */
115  memset( f, 0, sizeof ( command_flags_t ) );
116  f->umask = 1;
117  f->groups = 1;
118 
119  for ( i = 1; i < argc; i++ ) {
120  if ( !strcmp( argv[i], "--darr" ) )
121  f->darr = 1;
122  else if ( !strcmp( argv[i], "--dear" ) )
123  f->dear = 1;
124  else if ( !strcmp( argv[i], "--iarr" ) )
125  f->iarr = 1;
126  else if ( !strcmp( argv[i], "--iear" ) )
127  f->iear = 1;
128  else if ( !strcmp( argv[i], "--opcm" ) )
129  f->opcm = 1;
130  else if ( !strcmp( argv[i], "--noumasks" ) )
131  f->umask = 0;
132  else if ( !strcmp( argv[i], "--nogroups" ) )
133  f->groups = 0;
134  else if ( !strcmp( argv[i], "-d" ) )
135  f->details = 1;
136  else if ( !strcmp( argv[i], "--mic" ) )
137  {
138  f->mic = 1;
139  i++;
140  if ( i >= argc || no_str_arg( argv[i] ) ) {
141  printf( "Specify a device index for --mic\n");
142  exit(1);
143  }
144  f->devidx = strtol(argv[i], 0, 10);
145  } else if ( !strcmp( argv[i], "-e" ) ) {
146  f->named = 1;
147  i++;
148  f->name = argv[i];
149  if ( i >= argc || no_str_arg( f->name ) ) {
150  printf( "Invalid argument for -e\n");
151  exit(1);
152  }
153  } else if ( !strcmp( argv[i], "-i" ) ) {
154  f->include = 1;
155  i++;
156  f->istr = argv[i];
157  if ( i >= argc || no_str_arg( f->istr ) ) {
158  printf( "Invalid argument for -i\n");
159  exit(1);
160  }
161  } else if ( !strcmp( argv[i], "-x" ) ) {
162  f->xclude = 1;
163  i++;
164  f->xstr = argv[i];
165  if ( i >= argc || no_str_arg( f->xstr ) ) {
166  printf( "Invalid argument for -x\n");
167  exit(1);
168  }
169  } else if ( !strcmp( argv[i], "-h" ) || !strcmp( argv[i], "--help" ) )
170  f->help = 1;
171  else {
172  printf( "%s is not supported\n", argv[i] );
173  exit(1);
174  }
175  }
176 
177  /* if help requested, print and bail */
178  if ( f->help ) {
179  print_help( argv);
180  exit( 1 );
181  }
182 }
183 
184 static void
185 space_pad( char *str, int spaces )
186 {
187  while ( spaces-- > 0 )
188  strcat( str, " " );
189 }
190 
191 static void
193 {
194  unsigned int i, j = 0;
195  char str[EVT_LINE + EVT_LINE];
196 
197  /* indent by offset */
198  if ( offset ) {
199  printf( "| %-73s|\n", info->symbol );
200  }
201  else {
202  printf( "| %-77s|\n", info->symbol );
203  }
204 
205  while ( j <= strlen( info->long_descr ) ) {
206  i = EVT_LINE - 12 - 2;
207  if ( i > 0 ) {
208  str[0] = 0;
209  strcat(str,"| " );
210  space_pad( str, 11 );
211  strncat( str, &info->long_descr[j], i );
212  j += i;
213  i = ( unsigned int ) strlen( str );
214  space_pad( str, EVT_LINE - ( int ) i - 1 );
215  strcat( str, "|" );
216  }
217  printf( "%s\n", str );
218  }
219 }
220 
221 static int
223 {
224  char *pmask,*ptr;
225 
226  /* handle the PAPI component-style events which have a component:::event type */
227  if ((ptr=strstr(info->symbol, ":::"))) {
228  ptr+=3;
229  /* handle libpfm4-style events which have a pmu::event type event name */
230  } else if ((ptr=strstr(info->symbol, "::"))) {
231  ptr+=2;
232  }
233  else {
234  ptr=info->symbol;
235  }
236 
237  if ( ( pmask = strchr( ptr, ':' ) ) == NULL ) {
238  return ( 0 );
239  }
240  memmove( info->symbol, pmask, ( strlen( pmask ) + 1 ) * sizeof ( char ) );
241  pmask = strchr( info->long_descr, ':' );
242  if ( pmask == NULL )
243  info->long_descr[0] = 0;
244  else
245  memmove( info->long_descr, pmask + sizeof ( char ),
246  ( strlen( pmask ) + 1 ) * sizeof ( char ) );
247  return ( 1 );
248 }
249 
250 
251 int
252 main( int argc, char **argv )
253 {
254  int i, j = 0, k;
255  int retval;
256  PAPI_event_info_t info;
257  const PAPI_hw_info_t *hwinfo = NULL;
258  command_flags_t flags;
259  int enum_modifier;
260  int numcmp, cid;
261 
262  int num_devices = 0;
263  int target_idx = 0;
264  int offload_mode = 0;
265  int target_ok = 0;
266 
267  /* Parse the command-line arguments */
268  parse_args( argc, argv, &flags );
269 
270  if (flags.mic)
271  {
272  printf("Checking for Intel(R) Xeon Phi(TM) (Target CPU) devices...\n\n");
273 
274 #ifdef __INTEL_OFFLOAD
275  num_devices = _Offload_number_of_devices();
276 #endif
277  printf("Number of Target devices installed: %d\n\n",num_devices);
278 
279  if (flags.devidx >= num_devices) {
280  // Run in fallback-mode
281  printf("Requested device index %d is not available. Specify a device between 0 and %d\n\n",
282  flags.devidx, num_devices-1);
283  exit(1);
284  }
285  else {
286  offload_mode = 1;
287  target_idx = flags.devidx;
288  printf("PAPI will list the native events available on device mic%d\n\n", target_idx);
289  }
290  }
291 
292  /* Set enum modifier mask */
293  if ( flags.dear )
294  enum_modifier = PAPI_NTV_ENUM_DEAR;
295  else if ( flags.darr )
296  enum_modifier = PAPI_NTV_ENUM_DARR;
297  else if ( flags.iear )
298  enum_modifier = PAPI_NTV_ENUM_IEAR;
299  else if ( flags.iarr )
300  enum_modifier = PAPI_NTV_ENUM_IARR;
301  else if ( flags.opcm )
302  enum_modifier = PAPI_NTV_ENUM_OPCM;
303  else
304  enum_modifier = PAPI_ENUM_EVENTS;
305 
307 
308  /* Initialize before parsing the input arguments */
309 #ifdef __INTEL_OFFLOAD
310  __Offload_report(1);
311 #endif
312  #pragma offload target(mic: target_idx) if(offload_mode)
313 
315  if ( retval != PAPI_VER_CURRENT ) {
316  fprintf(stderr,"Error! PAPI_library_init\n");
317  exit(retval);
318  }
319 
320 
321  if ( !TESTS_QUIET ) {
322 #ifdef __INTEL_OFFLOAD
323  __Offload_report(1);
324 #endif
325  #pragma offload target(mic: target_idx) if(offload_mode)
327  if ( retval != PAPI_OK ) {
328  fprintf(stderr,"Error! PAPI_set_debug\n");
329  exit(retval);
330  }
331  }
332 
333 #ifdef __INTEL_OFFLOAD
334  __Offload_report(1);
335 #endif
336  #pragma offload target(mic: target_idx) if(offload_mode) nocopy(hwinfo)
337  {
338  retval = papi_print_header( "Available native events and hardware information.\n", &hwinfo );
339  fflush(stdout);
340  }
341  if ( retval != PAPI_OK ) {
342  fprintf(stderr,"Error! PAPI_get_hardware_info\n");
343  exit( 2 );
344  }
345 
346 
347  /* Do this code if the event name option was specified on the commandline */
348  if ( flags.named )
349  {
350  int papi_ok = 0;
351  char *ename = flags.name;
352  int elen = 0;
353  if (ename)
354  elen = strlen(ename) + 1;
355 #ifdef __INTEL_OFFLOAD
356  __Offload_report(1);
357 #endif
358  #pragma offload target(mic: target_idx) if(offload_mode) in(ename:length(elen)) out(i)
359  papi_ok = PAPI_event_name_to_code(ename, &i);
360 
361  if (papi_ok == PAPI_OK)
362  {
363 #ifdef __INTEL_OFFLOAD
364  __Offload_report(1);
365 #endif
366  #pragma offload target(mic: target_idx) if(offload_mode) out(info)
367  papi_ok = PAPI_get_event_info(i, &info);
368  }
369 
370  if (papi_ok == PAPI_OK)
371  {
372  printf( "%-30s%s\n",
373  "Event name:", info.symbol);
374  printf( "%-29s|%s|\n", "Description:", info.long_descr );
375 
376  /* if unit masks exist but none specified, process all */
377  if ( !strchr( flags.name, ':' ) )
378  {
379 #ifdef __INTEL_OFFLOAD
380  __Offload_report(1);
381 #endif
382  #pragma offload target(mic: target_idx) if(offload_mode) inout(i)
383  papi_ok = PAPI_enum_event( &i, PAPI_NTV_ENUM_UMASKS);
384  if (papi_ok == PAPI_OK )
385  {
386  printf( "\nUnit Masks:\n" );
387  do
388  {
389 #ifdef __INTEL_OFFLOAD
390  __Offload_report(1);
391 #endif
392  #pragma offload target(mic: target_idx) if(offload_mode) inout(i, info)
393  retval = PAPI_get_event_info( i, &info );
394  if ( retval == PAPI_OK ) {
395  if ( parse_unit_masks( &info ) ) {
396  printf( "%-29s|%s|%s|\n", " Mask Info:",
397  info.symbol, info.long_descr );
398  }
399  }
400 #ifdef __INTEL_OFFLOAD
401  __Offload_report(1);
402 #endif
403  #pragma offload target(mic: target_idx) if(offload_mode) inout(i, info)
405  } while (papi_ok == PAPI_OK);
406  }
407  }
408  } else {
409  printf("Sorry, an event by the name '%s' could not be found.\n",
410  flags.name);
411  printf("Is it typed correctly?\n\n");
412  exit( 1 );
413  }
414  }
415  else {
416 
417  /* Print *ALL* available events */
418 
419 #ifdef __INTEL_OFFLOAD
420  __Offload_report(1);
421 #endif
422  #pragma offload target(mic: target_idx) if(offload_mode)
423  numcmp = PAPI_num_components( );
424 
425  j = 0;
426 
427  for ( cid = 0; cid < numcmp; cid++ ) {
428 
429  PAPI_component_info_t component;
430 // if (offload_mode) // I must allocate local memory to receive the result
431 // component = (PAPI_component_info_t*)malloc(sizeof(PAPI_component_info_t));
432 // #pragma offload target(mic: target_idx) if(offload_mode) out(*component:length(sizeof(PAPI_component_info_t)) alloc_if(0) free_if(0))
433 #ifdef __INTEL_OFFLOAD
434  __Offload_report(1);
435 #endif
436  #pragma offload target(mic: target_idx) if(offload_mode) out(component)
437  {
438  memcpy(&component, PAPI_get_component_info(cid), sizeof(PAPI_component_info_t));
439  }
440 
441  /* Skip disabled components */
442  if (component.disabled) continue;
443 
444  printf( "===============================================================================\n" );
445  printf( " Native Events in Component: %s\n",component.name);
446  printf( "===============================================================================\n" );
447 
448  /* Always ASK FOR the first event */
449  /* Don't just assume it'll be the first numeric value */
450  i = 0 | PAPI_NATIVE_MASK;
451 
452 #ifdef __INTEL_OFFLOAD
453  __Offload_report(1);
454 #endif
455  #pragma offload target(mic: target_idx) if(offload_mode) inout(i)
457 
458  do
459  {
460  memset( &info, 0, sizeof ( info ) );
461 #ifdef __INTEL_OFFLOAD
462  __Offload_report(1);
463 #endif
464  #pragma offload target(mic: target_idx) if(offload_mode) inout(info)
465  retval = PAPI_get_event_info( i, &info );
466 
467  /* This event may not exist */
468  if ( retval != PAPI_OK )
469  goto endloop;
470 
471  /* Bail if event name doesn't contain include string */
472  if ( flags.include ) {
473  if ( !strstr( info.symbol, flags.istr ) ) {
474  goto endloop;
475  }
476  }
477 
478  /* Bail if event name does contain exclude string */
479  if ( flags.xclude ) {
480  if ( strstr( info.symbol, flags.xstr ) )
481  goto endloop;
482  }
483 
484  /* count only events that are actually processed */
485  j++;
486 
487  print_event( &info, 0 );
488 
489  if (flags.details) {
490  if (info.units[0]) printf( "| Units: %-67s|\n",
491  info.units );
492  }
493 
494 /* modifier = PAPI_NTV_ENUM_GROUPS returns event codes with a
495  groups id for each group in which this
496  native event lives, in bits 16 - 23 of event code
497  terminating with PAPI_ENOEVNT at the end of the list.
498 */
499 
500  /* This is an IBM Power issue */
501  if ( flags.groups ) {
502  int papi_ok = 0;
503  k = i;
504 #ifdef __INTEL_OFFLOAD
505  __Offload_report(1);
506 #endif
507  #pragma offload target(mic: target_idx) if(offload_mode) inout(k)
508  papi_ok = PAPI_enum_cmp_event(&k, PAPI_NTV_ENUM_GROUPS, cid);
509  if (papi_ok == PAPI_OK )
510  {
511  printf("Groups: ");
512  do {
513  printf( "%4d", ( ( k & PAPI_NTV_GROUP_AND_MASK ) >>
514  PAPI_NTV_GROUP_SHIFT ) - 1 );
515 #ifdef __INTEL_OFFLOAD
516  __Offload_report(1);
517 #endif
518  #pragma offload target(mic: target_idx) if(offload_mode) inout(k)
519  papi_ok = PAPI_enum_cmp_event(&k, PAPI_NTV_ENUM_GROUPS, cid);
520  } while (papi_ok==PAPI_OK );
521  printf( "\n" );
522  }
523  }
524 
525  /* Print umasks */
526  /* components that don't have them can just ignore */
527 
528  if ( flags.umask )
529  {
530  int papi_ok = 0;
531  k = i;
532 #ifdef __INTEL_OFFLOAD
533  __Offload_report(1);
534 #endif
535  #pragma offload target(mic: target_idx) if(offload_mode) inout(k)
536  papi_ok = PAPI_enum_cmp_event(&k, PAPI_NTV_ENUM_UMASKS, cid);
537  if (papi_ok == PAPI_OK )
538  {
539  do {
540 #ifdef __INTEL_OFFLOAD
541  __Offload_report(1);
542 #endif
543  #pragma offload target(mic: target_idx) if(offload_mode) inout(info)
544  retval = PAPI_get_event_info(k, &info);
545  if ( retval == PAPI_OK ) {
546  if (parse_unit_masks( &info ))
547  print_event(&info, 2);
548  }
549 #ifdef __INTEL_OFFLOAD
550  __Offload_report(1);
551 #endif
552  #pragma offload target(mic: target_idx) if(offload_mode) inout(k)
553  papi_ok = PAPI_enum_cmp_event(&k, PAPI_NTV_ENUM_UMASKS, cid);
554  } while (papi_ok == PAPI_OK);
555  }
556  }
557  printf( "--------------------------------------------------------------------------------\n" );
558 
559 endloop:
560 #ifdef __INTEL_OFFLOAD
561  __Offload_report(1);
562 #endif
563  #pragma offload target(mic: target_idx) if(offload_mode) inout(i)
564  retval=PAPI_enum_cmp_event(&i, enum_modifier, cid);
565  } while (retval == PAPI_OK );
566  }
567 
568  printf("\n");
569  printf( "Total events reported: %d\n", j );
570  }
571 
572  return 0;
573 }
#define PAPI_OK
Definition: fpapi.h:105
#define PAPI_NATIVE_MASK
double f(double a)
Definition: cpi.c:23
static const char * name
Definition: fork_overflow.c:31
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:796
Hardware info structure.
Definition: papi.h:781
char units[PAPI_MIN_STR_LEN]
Definition: papi.h:976
static void space_pad(char *str, int spaces)
static void print_help(char **argv)
#define EVT_LINE
static void parse_args(int argc, char **argv, command_flags_t *f)
off64_t offset
Definition: iozone.c:1279
int PAPI_num_components(void)
Definition: papi.c:4387
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
int PAPI_event_name_to_code(const char *in, int *out)
Definition: papi.c:1004
int retval
Definition: zero_fork.c:53
int main(int argc, char **argv)
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:835
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
char name[PAPI_MAX_STR_LEN]
Definition: papi.h:630
int PAPI_enum_cmp_event(int *EventCode, int modifier, int cidx)
Definition: papi.c:1357
static void print_event(PAPI_event_info_t *info, int offset)
char * help[]
Definition: iozone.c:129
static int parse_unit_masks(PAPI_event_info_t *info)
#define PAPI_NTV_GROUP_AND_MASK
Definition: papi.h:526
int TESTS_QUIET
Definition: test_utils.c:18
void exit()
static int no_str_arg(char *arg)
int PAPI_set_debug(int level)
Definition: papi.c:3126
int i
Definition: fileop.c:140