PAPI  5.1.0.2
command_line.c
Go to the documentation of this file.
00001 /* file command_line.c
00002  * This simply tries to add the events listed on the command line one at a time
00003  * then starts and stops the counters and prints the results
00004 */
00005 
00028 #include "papi_test.h"
00029 
00030 static void
00031 print_help( char **argv )
00032 {
00033     printf( "Usage: %s [options] [EVENTNAMEs]\n", argv[0] );
00034     printf( "Options:\n\n" );
00035     printf( "General command options:\n" );
00036     printf( "\t-u          Display output values as unsigned integers\n" );
00037     printf( "\t-x          Display output values as hexadecimal\n" );
00038     printf( "\t-h          Print this help message\n" );
00039     printf( "\tEVENTNAMEs  Specify one or more preset or native events\n" );
00040     printf( "\n" );
00041     printf( "This utility performs work while measuring the specified events.\n" );
00042     printf( "It can be useful for sanity checks on given events and sets of events.\n" );
00043 }
00044 
00045 
00046 int
00047 main( int argc, char **argv )
00048 {
00049     int retval;
00050     int num_events;
00051     long long *values;
00052     char *success;
00053     PAPI_event_info_t info;
00054     int EventSet = PAPI_NULL;
00055     int i, j, data_type, event;
00056     int u_format = 0;
00057     int hex_format = 0;
00058 
00059     tests_quiet( argc, argv );  /* Set TESTS_QUIET variable */
00060 
00061 
00062     if ( ( retval =
00063            PAPI_library_init( PAPI_VER_CURRENT ) ) != PAPI_VER_CURRENT )
00064         test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
00065 
00066     if ( ( retval = PAPI_create_eventset( &EventSet ) ) != PAPI_OK )
00067         test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
00068 
00069     if ( TESTS_QUIET )
00070         i = 2;
00071     else
00072         i = 1;
00073 
00074     num_events = argc - i;
00075 
00076     /* Automatically pass if no events, for run_tests.sh */
00077     if ( num_events == 0 )
00078         test_pass( __FILE__, NULL, 0 );
00079 
00080     values =
00081         ( long long * ) malloc( sizeof ( long long ) * ( size_t ) num_events );
00082     success = ( char * ) malloc( ( size_t ) argc );
00083 
00084     if ( success == NULL || values == NULL )
00085         test_fail_exit( __FILE__, __LINE__, "malloc", PAPI_ESYS );
00086 
00087     for ( ; i < argc; i++ ) {
00088         if ( strstr( argv[i], "-h" ) ) {
00089             print_help( argv );
00090             exit( 1 );
00091         } else if ( strstr( argv[i], "-u" ) ) {
00092             u_format = 1;
00093             num_events--;
00094         } else if ( strstr( argv[i], "-x" ) ) {
00095             hex_format = 1;
00096             num_events--;
00097         } else {
00098             if ( ( retval = PAPI_add_named_event( EventSet, argv[i] ) ) != PAPI_OK ) {
00099                 printf( "Failed adding: %s\nbecause: %s\n", argv[i], 
00100                     PAPI_strerror(retval));
00101                 success[i] = 0;
00102             } else {
00103                 success[i] = 1;
00104                 printf( "Successfully added: %s\n", argv[i] );
00105             }
00106         }
00107     }
00108     printf( "\n" );
00109 
00110     do_flops( 1 );
00111     do_flush(  );
00112 
00113     if ( ( retval = PAPI_start( EventSet ) ) != PAPI_OK ) {
00114        test_fail_exit( __FILE__, __LINE__, "PAPI_start", retval );
00115     }
00116 
00117     do_flops( NUM_FLOPS );
00118     do_misses( 1, L1_MISS_BUFFER_SIZE_INTS );
00119 
00120     if ( ( retval = PAPI_stop( EventSet, values ) ) != PAPI_OK )
00121         test_fail_exit( __FILE__, __LINE__, "PAPI_stop", retval );
00122 
00123     for ( i = 1, j = 0; i <= num_events; i++ ) {
00124         if ( success[i] ) {
00125             if (! (u_format || hex_format) ) {
00126                 retval = PAPI_event_name_to_code( argv[i], &event );
00127                 if (retval == PAPI_OK) {
00128                     retval = PAPI_get_event_info(event, &info);
00129                     if (retval == PAPI_OK) data_type = info.data_type;
00130                     else data_type = PAPI_DATATYPE_INT64;
00131                 }
00132                 switch (data_type) {
00133                   case PAPI_DATATYPE_UINT64:
00134                     printf( "%s : \t%llu(u)", argv[i], (unsigned long long)values[j++] );
00135                     break;
00136                   case PAPI_DATATYPE_FP64:
00137                     printf( "%s : \t%0.3f", argv[i], *((double *)(&values[j])) );
00138                     j++;
00139                     break;
00140                   case PAPI_DATATYPE_BIT64:
00141                     printf( "%s : \t0x%llX", argv[i], values[j++] );
00142                     break;
00143                   case PAPI_DATATYPE_INT64:
00144                   default:
00145                     printf( "%s : \t%lld", argv[i], values[j++] );
00146                     break;
00147                 }
00148                 if (retval == PAPI_OK)  printf( " %s", info.units );
00149                 printf( "\n" );
00150             }
00151             if (u_format) printf( "%s : \t%llu(u)\n", argv[i], (unsigned long long)values[j++] );
00152             if (u_format && hex_format) j--;
00153             if (hex_format) printf( "%s : \t0x%llX\n", argv[i], values[j++] );
00154         } else {
00155             printf( "%s : \t---------\n", argv[i] );
00156         }
00157     }
00158 
00159     printf( "\n----------------------------------\n" );
00160     printf
00161         ( "Verification: Checks for valid event name.\n This utility lets you add events from the command line interface to see if they work.\n" );
00162     test_pass( __FILE__, NULL, 0 );
00163     exit( 1 );
00164 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines