Blame src/utils/papi_event_chooser.c

Packit Service a1973e
/** file event_chooser.c
Packit Service a1973e
  * @brief papi_event_chooser utility.
Packit Service a1973e
  *	@page papi_event_chooser
Packit Service a1973e
  *	@section NAME
Packit Service a1973e
  *		papi_event_chooser - given a list of named events,
Packit Service a1973e
  *		lists other events that can be counted with them.
Packit Service a1973e
  *
Packit Service a1973e
  *	@section Synopsis
Packit Service a1973e
  *		papi_event_chooser NATIVE | PRESET < event > < event > ...
Packit Service a1973e
  *
Packit Service a1973e
  *	@section Description
Packit Service a1973e
  *		papi_event_chooser is a PAPI utility program that reports information 
Packit Service a1973e
  *		about the current PAPI installation and supported preset events.
Packit Service a1973e
  *
Packit Service a1973e
  *	@section Options
Packit Service a1973e
  *		This utility has no command line options.
Packit Service a1973e
  *
Packit Service a1973e
  *	@section Bugs
Packit Service a1973e
  *		There are no known bugs in this utility.
Packit Service a1973e
  *		If you find a bug, it should be reported to the
Packit Service a1973e
  *		PAPI Mailing List at <ptools-perfapi@icl.utk.edu>.
Packit Service a1973e
 */
Packit Service a1973e
Packit Service a1973e
#include <stdio.h>
Packit Service a1973e
#include <stdlib.h>
Packit Service a1973e
#include <string.h>
Packit Service a1973e
Packit Service a1973e
#include "papi.h"
Packit Service a1973e
#include "print_header.h"
Packit Service a1973e
Packit Service a1973e
int EventSet = PAPI_NULL;
Packit Service a1973e
int retval;
Packit Service a1973e
Packit Service a1973e
static char *
Packit Service a1973e
is_derived( PAPI_event_info_t * info )
Packit Service a1973e
{
Packit Service a1973e
	if ( strlen( info->derived ) == 0 )
Packit Service a1973e
		return ( "No" );
Packit Service a1973e
	else if ( strcmp( info->derived, "NOT_DERIVED" ) == 0 )
Packit Service a1973e
		return ( "No" );
Packit Service a1973e
	else if ( strcmp( info->derived, "DERIVED_CMPD" ) == 0 )
Packit Service a1973e
		return ( "No" );
Packit Service a1973e
	else
Packit Service a1973e
		return ( "Yes" );
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static int
Packit Service a1973e
add_remove_event( int EventSet, int evt )
Packit Service a1973e
{
Packit Service a1973e
    int retval;
Packit Service a1973e
Packit Service a1973e
    if ( ( retval = PAPI_add_event( EventSet, evt ) ) != PAPI_OK ) {
Packit Service a1973e
       //printf( "Error adding event.\n" );
Packit Service a1973e
    } else {
Packit Service a1973e
       if ( ( retval = PAPI_remove_event( EventSet, evt ) ) != PAPI_OK ) {
Packit Service a1973e
	  printf( "Error removing event.\n" );
Packit Service a1973e
       }
Packit Service a1973e
    }
Packit Service a1973e
    return retval;
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static int
Packit Service a1973e
show_event_info( int evt )
Packit Service a1973e
{
Packit Service a1973e
    int k;
Packit Service a1973e
    int retval;
Packit Service a1973e
    PAPI_event_info_t info;
Packit Service a1973e
Packit Service a1973e
    if ( ( retval = PAPI_get_event_info( evt, &info ) ) == PAPI_OK ) {
Packit Service a1973e
       printf( "%s\t%#x\n |%s|\n",
Packit Service a1973e
	       info.symbol, info.event_code, info.long_descr );
Packit Service a1973e
Packit Service a1973e
       for( k = 0; k < ( int ) info.count; k++ ) {
Packit Service a1973e
	  if ( strlen( info.name[k] ) ) {
Packit Service a1973e
	     printf( " |Register Value[%d]: %#-10x  %s|\n", 
Packit Service a1973e
                     k, info.code[k], info.name[k] );
Packit Service a1973e
	  }
Packit Service a1973e
       }
Packit Service a1973e
    }
Packit Service a1973e
    return retval;
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
Packit Service a1973e
static int
Packit Service a1973e
native( int cidx )
Packit Service a1973e
{
Packit Service a1973e
    int i, j, k;
Packit Service a1973e
    int retval, added;
Packit Service a1973e
    PAPI_event_info_t info;
Packit Service a1973e
Packit Service a1973e
    j = 0;
Packit Service a1973e
Packit Service a1973e
    /* For platform independence, always ASK FOR the first event */
Packit Service a1973e
    /* Don't just assume it'll be the first numeric value */
Packit Service a1973e
    i = 0 | PAPI_NATIVE_MASK;
Packit Service a1973e
    retval=PAPI_enum_cmp_event( &i, PAPI_ENUM_FIRST, cidx );
Packit Service a1973e
    if (retval==PAPI_ENOEVNT) {
Packit Service a1973e
       printf("Cannot find first event in component %d\n",cidx);
Packit Service a1973e
    }
Packit Service a1973e
Packit Service a1973e
    do {
Packit Service a1973e
       k = i;
Packit Service a1973e
Packit Service a1973e
       if ( PAPI_enum_cmp_event( &k, PAPI_NTV_ENUM_UMASKS, cidx) == PAPI_OK ) {
Packit Service a1973e
	  if ( ( added = add_remove_event( EventSet, k ) ) == PAPI_OK ) {
Packit Service a1973e
	     show_event_info( i );
Packit Service a1973e
	     do {
Packit Service a1973e
		retval = PAPI_get_event_info( k, &info );
Packit Service a1973e
		if ( retval == PAPI_OK ) {
Packit Service a1973e
		   printf( "    %#-10x%s  |%s|\n", info.event_code,
Packit Service a1973e
			   strchr( info.symbol, ':' ),
Packit Service a1973e
			   strchr( info.long_descr, ':' ) + 1 );
Packit Service a1973e
		}
Packit Service a1973e
	     } while ( PAPI_enum_cmp_event( &k, PAPI_NTV_ENUM_UMASKS, cidx ) ==
Packit Service a1973e
							  PAPI_OK );
Packit Service a1973e
	     j++;
Packit Service a1973e
	  }
Packit Service a1973e
       } else {
Packit Service a1973e
	  if ( ( added = add_remove_event( EventSet, i ) ) == PAPI_OK ) {
Packit Service a1973e
	     show_event_info( i );
Packit Service a1973e
	     j++;
Packit Service a1973e
	  }
Packit Service a1973e
       }
Packit Service a1973e
Packit Service a1973e
       if ( added == PAPI_OK ) {
Packit Service a1973e
	  /* modifier = PAPI_NTV_ENUM_GROUPS returns event codes with a
Packit Service a1973e
	     groups id for each group in which this
Packit Service a1973e
	     native event lives, in bits 16 - 23 of event code
Packit Service a1973e
	     terminating with PAPI_ENOEVNT at the end of the list.
Packit Service a1973e
	   */
Packit Service a1973e
	  k = i;
Packit Service a1973e
	  if ( PAPI_enum_cmp_event( &k, PAPI_NTV_ENUM_GROUPS, cidx ) == PAPI_OK ) {
Packit Service a1973e
	     printf( "Groups: " );
Packit Service a1973e
	     do {
Packit Service a1973e
		printf( "%4d", ( ( k & PAPI_NTV_GROUP_AND_MASK ) >>
Packit Service a1973e
					  PAPI_NTV_GROUP_SHIFT ) - 1 );
Packit Service a1973e
	     } while ( PAPI_enum_cmp_event( &k, PAPI_NTV_ENUM_GROUPS, cidx ) ==
Packit Service a1973e
							  PAPI_OK );
Packit Service a1973e
	     printf( "\n" );
Packit Service a1973e
	  }
Packit Service a1973e
Packit Service a1973e
	  printf( "---------------------------------------------"
Packit Service a1973e
		  "----------------------------\n" );
Packit Service a1973e
       }
Packit Service a1973e
    } while ( PAPI_enum_cmp_event( &i, PAPI_ENUM_EVENTS, cidx ) == PAPI_OK );
Packit Service a1973e
Packit Service a1973e
    printf( "------------------------------------------"
Packit Service a1973e
	    "-------------------------------\n" );
Packit Service a1973e
    printf( "Total events reported: %d\n", j );
Packit Service a1973e
Packit Service a1973e
    exit( 0 );
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
static int
Packit Service a1973e
preset( void )
Packit Service a1973e
{
Packit Service a1973e
	int i, j = 0;
Packit Service a1973e
	int retval;
Packit Service a1973e
	PAPI_event_info_t info;
Packit Service a1973e
Packit Service a1973e
	printf( "    Name        Code    " );
Packit Service a1973e
	printf( "Deriv Description (Note)\n" );
Packit Service a1973e
Packit Service a1973e
	/* For consistency, always ASK FOR the first event */
Packit Service a1973e
	i = 0 | PAPI_PRESET_MASK;
Packit Service a1973e
	PAPI_enum_event( &i, PAPI_ENUM_FIRST );
Packit Service a1973e
Packit Service a1973e
	do {
Packit Service a1973e
		retval = PAPI_add_event( EventSet, i );
Packit Service a1973e
		if ( retval == PAPI_OK ) {
Packit Service a1973e
			if ( PAPI_get_event_info( i, &info ) == PAPI_OK ) {
Packit Service a1973e
				printf( "%-13s%#x  %-5s%s",
Packit Service a1973e
						info.symbol,
Packit Service a1973e
						info.event_code, is_derived( &info ), info.long_descr );
Packit Service a1973e
				if ( info.note[0] )
Packit Service a1973e
					printf( " (%s)", info.note );
Packit Service a1973e
				printf( "\n" );
Packit Service a1973e
			}
Packit Service a1973e
			if ( ( retval = PAPI_remove_event( EventSet, i ) ) != PAPI_OK )
Packit Service a1973e
				printf( "Error in PAPI_remove_event\n" );
Packit Service a1973e
			j++;
Packit Service a1973e
		}
Packit Service a1973e
	} while ( PAPI_enum_event( &i, PAPI_PRESET_ENUM_AVAIL ) == PAPI_OK );
Packit Service a1973e
Packit Service a1973e
	printf
Packit Service a1973e
		( "-------------------------------------------------------------------------\n" );
Packit Service a1973e
	printf( "Total events reported: %d\n", j );
Packit Service a1973e
Packit Service a1973e
	exit( 0 );
Packit Service a1973e
}
Packit Service a1973e
Packit Service a1973e
int
Packit Service a1973e
main( int argc, char **argv )
Packit Service a1973e
{
Packit Service a1973e
    int i;
Packit Service a1973e
    int pevent,cevent;
Packit Service a1973e
    int cidx;
Packit Service a1973e
Packit Service a1973e
    const PAPI_hw_info_t *hwinfo = NULL;
Packit Service a1973e
Packit Service a1973e
    if ( argc < 3 ) {
Packit Service a1973e
       goto use_exit;
Packit Service a1973e
    }
Packit Service a1973e
Packit Service a1973e
    /* Init PAPI library */
Packit Service a1973e
    retval = PAPI_library_init( PAPI_VER_CURRENT );
Packit Service a1973e
    if ( retval != PAPI_VER_CURRENT ) {
Packit Service a1973e
	fprintf(stderr,"Error! PAPI_library_init\n");
Packit Service a1973e
	return retval;
Packit Service a1973e
    }
Packit Service a1973e
Packit Service a1973e
    retval = PAPI_set_debug( PAPI_VERB_ECONT );
Packit Service a1973e
    if ( retval != PAPI_OK ) {
Packit Service a1973e
	fprintf(stderr,"Error! PAPI_set_debug\n");
Packit Service a1973e
	return retval;
Packit Service a1973e
    }
Packit Service a1973e
Packit Service a1973e
    retval = papi_print_header( "Event Chooser: Available events "
Packit Service a1973e
				"which can be added with given events.\n",
Packit Service a1973e
		                &hwinfo );
Packit Service a1973e
    if ( retval != PAPI_OK ) {
Packit Service a1973e
       fprintf(stderr, "Error! PAPI_get_hardware_info\n");
Packit Service a1973e
	return 2;
Packit Service a1973e
    }
Packit Service a1973e
Packit Service a1973e
    retval = PAPI_create_eventset( &EventSet );
Packit Service a1973e
    if ( retval != PAPI_OK ) {
Packit Service a1973e
	fprintf( stderr, "PAPI_create_eventset error\n" );
Packit Service a1973e
	return 1;
Packit Service a1973e
    }
Packit Service a1973e
Packit Service a1973e
    retval = PAPI_event_name_to_code( argv[2], &cevent );
Packit Service a1973e
    if ( retval != PAPI_OK ) {
Packit Service a1973e
	fprintf( stderr, "Event %s can't be found\n", argv[2] );
Packit Service a1973e
	return 1;
Packit Service a1973e
    }
Packit Service a1973e
    cidx = PAPI_get_event_component(cevent);
Packit Service a1973e
Packit Service a1973e
    for( i = 2; i < argc; i++ ) {
Packit Service a1973e
       retval = PAPI_event_name_to_code( argv[i], &pevent );
Packit Service a1973e
       if ( retval != PAPI_OK ) {
Packit Service a1973e
	  fprintf( stderr, "Event %s can't be found\n", argv[i] );
Packit Service a1973e
	  return 1;
Packit Service a1973e
       }
Packit Service a1973e
       retval = PAPI_add_event( EventSet, pevent );
Packit Service a1973e
       if ( retval != PAPI_OK ) {
Packit Service a1973e
	  fprintf( stderr, "Event %s can't be counted with others %d\n",
Packit Service a1973e
		   argv[i], retval );
Packit Service a1973e
	  return 1;
Packit Service a1973e
       }
Packit Service a1973e
    }
Packit Service a1973e
Packit Service a1973e
    if ( !strcmp( "NATIVE", argv[1] ) ) {
Packit Service a1973e
       native( cidx );
Packit Service a1973e
    }
Packit Service a1973e
    else if ( !strcmp( "PRESET", argv[1] ) ) {
Packit Service a1973e
       preset(  );
Packit Service a1973e
    }
Packit Service a1973e
    else {
Packit Service a1973e
       goto use_exit;
Packit Service a1973e
    }
Packit Service a1973e
	return 0;
Packit Service a1973e
Packit Service a1973e
use_exit:
Packit Service a1973e
    fprintf( stderr,
Packit Service a1973e
	    "Usage: papi_event_chooser NATIVE|PRESET evt1 evt2 ... \n" );
Packit Service a1973e
	return 1;
Packit Service a1973e
}