PAPI  5.7.0.0
benchPCP.c
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // This test does timing on reading an event; specifically a PCP event.
3 // The timing for the event can be read many times, and given as an average.
4 // The count and event to read are given on the command line in text, e.g.
5 // ./benchPCP 100 "perfevent.hwcounters.instructions.value:cpu0"
6 // will be read in a loop 100 times, the whole thing will be timed, and the
7 // time divided by 100 will be reported (avg time of a read). We do this to
8 // increase resolution; since our time is measured in microseconds (uS) by
9 // averaging many reads we can get greater accuracy.
10 //
11 // We also measure the time required to initiliaze PAPI (with the component),
12 // and report that.
13 //
14 // We will printf() both the initialization time and read time on the same line
15 // in CSV format. If no arguments are given, we will printf() a header CSV
16 // line. Otherwise there must be exactly two arguments. Errors are printed to
17 // 'stderr'. This scheme allows a shell loop to produce a csv file with a
18 // header and many samples, to be processed to produce descriptive statistics
19 // separately (by spreadsheet or another program).
20 //-----------------------------------------------------------------------------
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <math.h>
26 #include <papi.h>
27 #include "papi_test.h"
28 #include <sys/time.h>
29 
30 #define mConvertUsec(timeval_) ((double) (timeval_.tv_sec*1000000+timeval_.tv_usec)) /* avoid typos, make it a double. */
31 static struct timeval t1, t2;
32 int EVENTREADS=0;
33 
34 typedef union
35 {
36  long long ll;
37  unsigned long long ull;
38  double d;
39  void *vp;
40  unsigned char ch[8];
41 } convert_64_t;
42 
43 //-----------------------------------------------------------------------------
44 // MAIN.
45 //-----------------------------------------------------------------------------
46 
47 int main(int argc, char **argv) { // args to set two events.
48  int i,ret;
49  int EventSet = PAPI_NULL;
50  char errMsg[1024]; // space for an error message with more info.
51  char *pcpName; // The pcp name.
52  if (argc == 1) { // If no arguments given,
53  printf("Initialize, Event Read Avg uS\n"); // OUTPUT Header for CSV.
54  return 0; // done.
55  }
56 
57  if (argc != 3) {
58  fprintf(stderr, "%s:%i ERROR Invalid number of arguments; must be 0 or 2.\n", __FILE__, __LINE__); // report.
59  fprintf(stderr, "%s readsToAvg Event-Name\n", argv[0]);
60  exit(-1);
61  }
62 
63  // Get args.
64  EVENTREADS = atoi(argv[1]); // get event reads.
65  if (EVENTREADS < 1) {
66  fprintf(stderr, "%s:%i ERROR readsToAvg must be > 0.\n", __FILE__, __LINE__); // report.
67  fprintf(stderr, "%s readsToAvg Event-Name\n", argv[0]);
68  exit(-1);
69  }
70 
71  pcpName = argv[2]; // collect the pcp event name.
72 
73  gettimeofday(&t1, NULL);
75  gettimeofday(&t2, NULL);
76 
77  if (ret != PAPI_VER_CURRENT) { // if we failed,
78  printf("ERROR PAPI library init failed.\n"); // Show abort in file.
79  test_fail(__FILE__, __LINE__, "PAPI_library_init failed\n", ret); // report.
80  }
81 
82  printf("%9.1f,", (mConvertUsec(t2)-mConvertUsec(t1))); // OUTPUT PAPI library init time.
83 
84 // fprintf(stderr, "Benching Event Read with PAPI %d.%d.%d\n",
85 // PAPI_VERSION_MAJOR( PAPI_VERSION ),
86 // PAPI_VERSION_MINOR( PAPI_VERSION ),
87 // PAPI_VERSION_REVISION( PAPI_VERSION ) );
88 
89  // Library is initialized.
90  ret = PAPI_create_eventset(&EventSet); // Create an event.
91  if (ret != PAPI_OK) { // If that failed, report and exit.
92  fprintf(stderr, "ERROR PAPI_create_eventset failed.\n");
93  test_fail(__FILE__, __LINE__, "PAPI_create_eventset failed.\n", ret);
94  }
95 
96  ret=PAPI_add_named_event(EventSet,pcpName); // Try to add it for counting.
97  if (ret != PAPI_OK) { // If that failed, report it.
98  sprintf(errMsg, "PAPI_add_named_event('%s') failed.\n", pcpName);
99  printf("ERROR %s\n", errMsg);
100  test_fail( __FILE__, __LINE__, errMsg, ret);
101  }
102 
103  //----------------------------------------------------------------------------------------------
104  // Testing, we just read each event EVENTREADS times, and record the times read.
105  // We overwrite the previous result every time, we aren't interested in the values.
106  // 'volatile' to avoid compiler optimizing out the loop.
107  //----------------------------------------------------------------------------------------------
108  long long pcpValue;
109 
110  // Begin event timing.
111  ret = PAPI_start(EventSet); // start counting.
112  if (ret != PAPI_OK) { // If that failed, report it.
113  printf("ERROR PAPI_start EventSet failed.\n"); // Show abort in file.
114  test_fail( __FILE__, __LINE__, "PAPI_start_event(EventSet) failed.\n", ret); // report and exit.
115  }
116 
117  gettimeofday(&t1, NULL);
118  for (i=0; i<EVENTREADS; i++) { // For all the PCP iterations,
119  ret = PAPI_read(EventSet, &pcpValue); // .. read without a stop.
120  if (ret != PAPI_OK) { // .. If that failed, report it.
121  printf("ERROR PAPI_read EventSet failed.\n"); // Show abort in file.
122  test_fail( __FILE__, __LINE__, "PAPI_read(EventSet) failed.\n", ret);
123  }
124  }
125  gettimeofday(&t2, NULL);
126 
127  ret = PAPI_stop(EventSet, &pcpValue); // stop counting, get final value.
128  if (ret != PAPI_OK) { // If that failed, report it.
129  printf("ERROR PAPI_stop EventSet failed.\n"); // Show abort in file.
130  test_fail( __FILE__, __LINE__, "PAPI_stop_event(PAPIEventSet, &papiValues[FINAL]) failed.\n", ret);
131  }
132 
133  printf("%9.1f\n", (mConvertUsec(t2)-mConvertUsec(t1))/((double) EVENTREADS)); // compute average, finish line.
134 
135  ret = PAPI_cleanup_eventset(EventSet); // Try a cleanup.
136  if (ret != PAPI_OK) { // If that failed, report it.
137  printf("ERROR PAPI_cleanup_eventset failed.\n"); // Show abort in file.
138  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset(EventSet) failed.\n", ret);
139  }
140 
141  ret = PAPI_destroy_eventset(&EventSet); // Deallocate. No memory leaks!
142  if (ret != PAPI_OK) { // If that failed, report it.
143  printf("ERROR PAPI_destroy_eventset failed.\n"); // Show abort in file.
144  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset(EventSet) failed.\n", ret);
145  }
146 
147  //----------------------------------------------------------------------------------------------
148  // Done. cleanup.
149  //----------------------------------------------------------------------------------------------
150  PAPI_shutdown(); // get out of papi.
151 // fprintf(stderr, "PAPI Shutdown completed.\n"); // If we are verbose,
152 // test_pass( __FILE__ ); // Note the test passed.
153  return 0; // Exit with all okay.
154 } // END main.
#define PAPI_OK
Definition: fpapi.h:105
static struct timeval t1 t2
Definition: benchPCP.c:31
int atoi()
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
int EVENTREADS
Definition: benchPCP.c:32
#define mConvertUsec(timeval_)
Definition: benchPCP.c:30
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int EventSet
volatile double t1
Return codes and api definitions.
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 PAPI_shutdown(void)
Definition: papi.c:4461
int main(int argc, char **argv)
Definition: benchPCP.c:47
#define PAPI_NULL
Definition: fpapi.h:13
long long ret
Definition: iozone.c:1346
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2890
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
int gettimeofday(void *ptr1, void *ptr2)
void test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:468
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:2014
int PAPI_read(int EventSet, long long *values)
Definition: papi.c:2559
int PAPI_start(int EventSet)
Definition: papi.c:2096
void exit()
int i
Definition: fileop.c:140