PAPI  5.7.0.0
papi_br_msp.c
Go to the documentation of this file.
1 /* This file attempts to test the mispredicted branches */
2 /* performance event as counted by PAPI_BR_MSP */
3 
4 /* by Vince Weaver, <vincent.weaver@maine.edu> */
5 
6 
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <string.h>
11 
12 #include "papi.h"
13 #include "papi_test.h"
14 
15 #include "display_error.h"
16 #include "testcode.h"
17 
18 
19 
20 int main(int argc, char **argv) {
21 
22  int num_runs=100,i;
23  int num_random_branches=500000;
24  long long high=0,low=0,average=0,expected=1500000;
25 
26  long long count,total=0;
27  int quiet=0,retval,ins_result;
28  int total_eventset=PAPI_NULL,miss_eventset=PAPI_NULL;
29 
30  quiet=tests_quiet(argc,argv);
31 
32  if (!quiet) {
33  printf("\nTesting the PAPI_BR_MSP event.\n");
34  }
35 
36  /* Init the PAPI library */
38  if ( retval != PAPI_VER_CURRENT ) {
39  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
40  }
41 
42  /* Create total eventset */
43  retval=PAPI_create_eventset(&total_eventset);
44  if (retval!=PAPI_OK) {
45  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
46  }
47 
48  retval=PAPI_add_named_event(total_eventset,"PAPI_BR_INS");
49  if (retval!=PAPI_OK) {
50  if (!quiet) printf("Could not add PAPI_BR_INS\n");
51  test_skip( __FILE__, __LINE__, "adding PAPI_BR_INS", retval );
52  }
53 
54  /* Create miss eventset */
55  retval=PAPI_create_eventset(&miss_eventset);
56  if (retval!=PAPI_OK) {
57  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
58  }
59 
60  retval=PAPI_add_named_event(miss_eventset,"PAPI_BR_MSP");
61  if (retval!=PAPI_OK) {
62  test_fail( __FILE__, __LINE__, "adding PAPI_BR_MSP", retval );
63  }
64 
65  if (!quiet) {
66  printf("\nPart 1: Testing that easy to predict loop has few misses\n");
67  printf("Testing a loop with %lld branches (%d times):\n",
68  expected,num_runs);
69  printf("\tOn a simple loop like this, "
70  "miss rate should be very small.\n");
71  }
72 
73  for(i=0;i<num_runs;i++) {
74 
75  PAPI_reset(miss_eventset);
76  PAPI_start(miss_eventset);
77 
78  ins_result=branches_testcode();
79 
80  retval=PAPI_stop(miss_eventset,&count);
81 
82  if (ins_result==CODE_UNIMPLEMENTED) {
83  fprintf(stderr,"\tCode unimplemented\n");
84  test_skip( __FILE__, __LINE__, "unimplemented", 0);
85  }
86 
87  if (retval!=PAPI_OK) {
88  test_fail( __FILE__, __LINE__,
89  "reading PAPI_TOT_INS", retval );
90  }
91 
92  if (count>high) high=count;
93  if ((low==0) || (count<low)) low=count;
94  total+=count;
95  }
96 
97  average=(total/num_runs);
98 
99  if (!quiet) printf("\tAverage number of branch misses: %lld\n",average);
100 
101  if (average>1000) {
102  if (!quiet) printf("Branch miss rate too high\n");
103  test_fail( __FILE__, __LINE__, "Error too high", 1 );
104  }
105 
106  /*******************/
107 
108  if (!quiet) {
109  printf("\nPart 2\n");
110  }
111 
112  high=0; low=0; total=0;
113 
114  for(i=0;i<num_runs;i++) {
115  PAPI_reset(total_eventset);
116  PAPI_start(total_eventset);
117 
118  ins_result=random_branches_testcode(num_random_branches,1);
119 
120  retval=PAPI_stop(total_eventset,&count);
121 
122  if (ins_result==CODE_UNIMPLEMENTED) {
123  fprintf(stderr,"\tCode unimplemented\n");
124  test_skip( __FILE__, __LINE__, "unimplemented", 0);
125  }
126 
127  if (retval!=PAPI_OK) {
128  test_fail( __FILE__, __LINE__,
129  "reading PAPI_TOT_INS", retval );
130  }
131 
132  if (count>high) high=count;
133  if ((low==0) || (count<low)) low=count;
134  total+=count;
135  }
136 
137  average=total/num_runs;
138 
139  expected=average;
140  if (!quiet) {
141  printf("\nTesting a function that branches "
142  "based on a random number\n");
143  printf(" The loop has %lld branches\n",expected);
144  printf(" %d are random branches.\n",num_random_branches);
145  }
146 
147  high=0; low=0; total=0;
148 
149  for(i=0;i<num_runs;i++) {
150  PAPI_reset(miss_eventset);
151  PAPI_start(miss_eventset);
152 
153  ins_result=random_branches_testcode(num_random_branches,1);
154 
155  retval=PAPI_stop(miss_eventset,&count);
156 
157  if (ins_result==CODE_UNIMPLEMENTED) {
158  fprintf(stderr,"\tCode unimplemented\n");
159  test_skip( __FILE__, __LINE__, "unimplemented", 0);
160  }
161 
162  if (retval!=PAPI_OK) {
163  test_fail( __FILE__, __LINE__,
164  "reading eventset", retval );
165  }
166 
167  if (count>high) high=count;
168  if ((low==0) || (count<low)) low=count;
169  total+=count;
170  }
171 
172  average=total/num_runs;
173 
174  if (!quiet) {
175  printf("\nOut of %lld branches, %lld were mispredicted\n",expected,average);
176  printf("Assuming a good random number generator and no freaky luck\n");
177  printf("The mispredicts should be roughly between %d and %d\n",
178  num_random_branches/4,(num_random_branches/4)*3);
179  }
180 
181  if ( average < (num_random_branches/4)) {
182  if (!quiet) printf("Mispredicts too low\n");
183  test_fail( __FILE__, __LINE__, "Error too low", 1 );
184  }
185 
186  if (average > (num_random_branches/4)*3) {
187  if (!quiet) printf("Mispredicts too high\n");
188  test_fail( __FILE__, __LINE__, "Error too high", 1 );
189  }
190 
191  if (!quiet) printf("\n");
192 
193  test_pass( __FILE__ );
194 
195  PAPI_shutdown();
196 
197  return 0;
198 
199 }
#define PAPI_OK
Definition: fpapi.h:105
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
int random_branches_testcode(int number, int quiet)
void test_pass(const char *filename)
Definition: test_utils.c:432
static int expected[NUM_THREADS]
int PAPI_reset(int EventSet)
Definition: papi.c:2459
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
#define CODE_UNIMPLEMENTED
Definition: testcode.h:2
int retval
Definition: zero_fork.c:53
Return codes and api definitions.
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:561
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 quiet
Definition: rapl_overflow.c:18
#define PAPI_NULL
Definition: fpapi.h:13
int main(int argc, char **argv)
Definition: papi_br_msp.c:20
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
int branches_testcode(void)
static int total
Definition: rapl_overflow.c:9
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:468
int PAPI_start(int EventSet)
Definition: papi.c:2096
static long count
int i
Definition: fileop.c:140