From 2f5727413a74794198e7f4b01814d7111c47dddb Mon Sep 17 00:00:00 2001
From: James Ralph <ralph@icl.utk.edu>
Date: Tue, 15 Apr 2014 10:55:49 -0400
Subject: [PATCH 04/44] native_avail: Add a --validate flag

--validate attempts to decide if each event can be added to an eventset.

Patch by Steve Kaufmann
-----------------------
I have modified papi_native_avail to accept a -v (validate) option that
when set will mark an event as actually available or not if it is
attempted to be added to an event set. I did this rather quickly but it
conveys an idea that would inform users that, yes this event name
exists but if you try to access it on the hardware that you are running
papi_native_avail on, you will not get it. For whatever reason. If you
think it a useful feature perhaps consider it for inclusion in papi.
---
 src/utils/native_avail.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/utils/native_avail.c b/src/utils/native_avail.c
index 51afa08..2073ed7 100644
--- a/src/utils/native_avail.c
+++ b/src/utils/native_avail.c
@@ -53,6 +53,7 @@ typedef struct command_flags
 	int named;
 	int include;
 	int xclude;
+	int validate;
 	char *name, *istr, *xstr;
 	int darr;
 	int dear;
@@ -72,6 +73,7 @@ print_help( char **argv )
 	printf( "\nOptions:\n" );
 	printf( "   --help, -h   print this help message\n" );
 	printf( "   -d           display detailed information about native events\n" );
+	printf( "   --validate   attempts to add each event\n");
 	printf( "   -e EVENTNAME display detailed information about named native event\n" );
 	printf( "   -i EVENTSTR  include only event names that contain EVENTSTR\n" );
 	printf( "   -x EVENTSTR  exclude any event names that contain EVENTSTR\n" );
@@ -144,9 +146,11 @@ parse_args( int argc, char **argv, command_flags_t * f )
 				printf( "Invalid argument for -x\n");
 				exit(1);
 			}
-		} else if ( !strcmp( argv[i], "-h" ) || !strcmp( argv[i], "--help" ) )
+		} else if ( !strcmp( argv[i], "-h" ) || !strcmp( argv[i], "--help" ) ) {
 			f->help = 1;
-		else {
+		} else if ( !strcmp( argv[i], "--validate" ) ) {
+			f->validate = 1;
+		} else {
 			printf( "%s is not supported\n", argv[i] );
 			exit(1);
 		}
@@ -167,17 +171,25 @@ space_pad( char *str, int spaces )
 }
 
 static void
-print_event( PAPI_event_info_t * info, int offset )
+print_event( PAPI_event_info_t * info, int offset, int validate )
 {
-	unsigned int i, j = 0;
+	unsigned int i, j = 0, na = 0;
 	char str[EVT_LINE + EVT_LINE];
+	int EventSet = PAPI_NULL;
+
+	if (validate && PAPI_create_eventset (&EventSet) == PAPI_OK) {
+		if (PAPI_add_named_event (EventSet, info->symbol) != PAPI_OK) {
+			na = 1;
+		}
+		PAPI_destroy_eventset (&EventSet);
+	}
 
 	/* indent by offset */
 	if ( offset ) {
-	   printf( "|     %-73s|\n", info->symbol );
+	   printf( "|     %-69s%4s|\n", info->symbol, (na ? "<NA>" : "") );
 	}
 	else {
-	   printf( "| %-77s|\n", info->symbol );
+	   printf( "| %-73s%4s|\n", info->symbol, (na ? "<NA>" : "") );
 	}
 
 	while ( j <= strlen( info->long_descr ) ) {
@@ -275,7 +287,6 @@ main( int argc, char **argv )
 		test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 2 );
 	}
 
-
 	/* Do this code if the event name option was specified on the commandline */
 	if ( flags.named ) {
 	   if ( PAPI_event_name_to_code( flags.name, &i ) == PAPI_OK ) {
@@ -359,7 +370,7 @@ main( int argc, char **argv )
 			  /* count only events that are actually processed */
 			  j++;
 
-			  print_event( &info, 0 );
+			  print_event( &info, 0, flags.validate );
 
 			  if (flags.details) {
 				if (info.units[0]) printf( "|     Units: %-67s|\n", 
@@ -395,7 +406,7 @@ main( int argc, char **argv )
 				   retval = PAPI_get_event_info( k, &info );
 				   if ( retval == PAPI_OK ) {
 					  if ( parse_unit_masks( &info ) )
-						 print_event( &info, 2 );
+						 print_event( &info, 2, 0 );
 				   }
 					} while ( PAPI_enum_cmp_event( &k, PAPI_NTV_ENUM_UMASKS, cid ) == PAPI_OK );
 				 }
-- 
2.1.1

