PAPI  5.7.0.0
papi_command_line.c
Go to the documentation of this file.
1 /* file papi_command_line.c
2  * This simply tries to add the events listed on the command line one at a time
3  * then starts and stops the counters and prints the results
4 */
5 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "papi.h"
37 #include "do_loops.h"
38 
39 static void
40 print_help( char **argv )
41 {
42  printf( "Usage: %s [options] [EVENTNAMEs]\n", argv[0] );
43  printf( "Options:\n\n" );
44  printf( "General command options:\n" );
45  printf( "\t-u Display output values as unsigned integers\n" );
46  printf( "\t-x Display output values as hexadecimal\n" );
47  printf( "\t-h Print this help message\n" );
48  printf( "\tEVENTNAMEs Specify one or more preset or native events\n" );
49  printf( "\n" );
50  printf( "This utility performs work while measuring the specified events.\n" );
51  printf( "It can be useful for sanity checks on given events and sets of events.\n" );
52 }
53 
54 
55 int
56 main( int argc, char **argv )
57 {
58  int retval;
59  int num_events;
60  long long *values;
61  char *success;
62  PAPI_event_info_t info;
63  int EventSet = PAPI_NULL;
64  int i, j, event, data_type = PAPI_DATATYPE_INT64;
65  int u_format = 0;
66  int hex_format = 0;
67 
68  printf( "\nThis utility lets you add events from the command line "
69  "interface to see if they work.\n\n" );
70 
72  if (retval != PAPI_VER_CURRENT ) {
73  fprintf(stderr,"Error! PAPI_library_init\n");
74  exit(retval );
75  }
76 
78  if (retval != PAPI_OK ) {
79  fprintf(stderr,"Error! PAPI_create_eventset\n");
80  exit(retval );
81  }
82 
83  values =
84  ( long long * ) malloc( sizeof ( long long ) * ( size_t ) argc );
85  success = ( char * ) malloc( ( size_t ) argc );
86 
87  if ( success == NULL || values == NULL ) {
88  fprintf(stderr,"Error allocating memory!\n");
89  exit(1);
90  }
91 
92  for ( num_events = 0, i = 1; i < argc; i++ ) {
93  if ( !strcmp( argv[i], "-h" ) ) {
94  print_help( argv );
95  exit( 1 );
96  } else if ( !strcmp( argv[i], "-u" ) ) {
97  u_format = 1;
98  } else if ( !strcmp( argv[i], "-x" ) ) {
99  hex_format = 1;
100  } else {
101  if ( ( retval = PAPI_add_named_event( EventSet, argv[i] ) ) != PAPI_OK ) {
102  printf( "Failed adding: %s\nbecause: %s\n", argv[i],
104  } else {
105  success[num_events++] = i;
106  printf( "Successfully added: %s\n", argv[i] );
107  }
108  }
109  }
110 
111  /* Automatically pass if no events, for run_tests.sh */
112  if ( num_events == 0 ) {
113  printf("No events specified!\n");
114  printf("Try running something like: %s PAPI_TOT_CYC\n\n",
115  argv[0]);
116  return 0;
117  }
118 
119 
120  printf( "\n" );
121 
122  do_flops( 1 );
123  do_flush( );
124 
126  if (retval != PAPI_OK ) {
127  fprintf(stderr,"Error! PAPI_start\n");
128  exit( retval );
129  }
130 
131  do_flops( NUM_FLOPS );
133 
135  if (retval != PAPI_OK ) {
136  fprintf(stderr,"Error! PAPI_stop\n");
137  exit( retval );
138  }
139 
140  for ( j = 0; j < num_events; j++ ) {
141  i = success[j];
142  if (! (u_format || hex_format) ) {
143  retval = PAPI_event_name_to_code( argv[i], &event );
144  if (retval == PAPI_OK) {
145  retval = PAPI_get_event_info(event, &info);
146  if (retval == PAPI_OK) data_type = info.data_type;
148  }
149  switch (data_type) {
151  printf( "%s : \t%llu(u)", argv[i], (unsigned long long)values[j] );
152  break;
153  case PAPI_DATATYPE_FP64:
154  printf( "%s : \t%0.3f", argv[i], *((double *)(&values[j])) );
155  break;
156  case PAPI_DATATYPE_BIT64:
157  printf( "%s : \t%#llX", argv[i], values[j] );
158  break;
159  case PAPI_DATATYPE_INT64:
160  default:
161  printf( "%s : \t%lld", argv[i], values[j] );
162  break;
163  }
164  if (retval == PAPI_OK) printf( " %s", info.units );
165  printf( "\n" );
166  }
167  if (u_format) printf( "%s : \t%llu(u)\n", argv[i], (unsigned long long)values[j] );
168  if (hex_format) printf( "%s : \t%#llX\n", argv[i], values[j] );
169  }
170 
171  printf( "\n----------------------------------\n" );
172 
173  return 0;
174 
175 }
#define PAPI_OK
Definition: fpapi.h:105
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
int main(int argc, char **argv)
char units[PAPI_MIN_STR_LEN]
Definition: papi.h:976
static int num_events
long unsigned int size_t
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int EventSet
int PAPI_event_name_to_code(const char *in, int *out)
Definition: papi.c:1004
int retval
Definition: zero_fork.c:53
static void print_help(char **argv)
#define NUM_FLOPS
Definition: sdsc-mpx.c:24
Return codes and api definitions.
int PAPI_get_event_info(int EventCode, PAPI_event_info_t *info)
Definition: papi.c:835
int PAPI_add_named_event(int EventSet, const char *EventName)
Definition: papi.c:1876
int PAPI_library_init(int version)
Definition: papi.c:500
void do_misses(int n, int bytes)
Definition: do_loops.c:120
#define PAPI_NULL
Definition: fpapi.h:13
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
void do_flops(int n)
Definition: multiplex.c:23
void do_flush(void)
Definition: do_loops.c:172
char * PAPI_strerror(int errorCode)
Definition: papi.c:4603
int data_type[MAX_EVENTS]
Definition: powercap_plot.c:16
int PAPI_start(int EventSet)
Definition: papi.c:2096
#define L1_MISS_BUFFER_SIZE_INTS
Definition: do_loops.h:11
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
void exit()
int i
Definition: fileop.c:140