PAPI 7.1.0.0
Loading...
Searching...
No Matches
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
20int 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 double rate = 100.0*(double)average/(double)num_random_branches;
176 printf("\nOut of %d random branches %lld were mispredicted,\n",num_random_branches,average);
177 printf("resulting in a misprediction rate = %.1lf%%.\n",rate);
178 printf("Assuming a good random number generator and no freaky luck\n");
179 printf("the misprediction rate should be around 50%%, and the\n");
180 printf("mispredicts should at least be between %d and %d.\n",
181 num_random_branches/4,(num_random_branches/4)*3);
182 }
183
184 if ( average < (num_random_branches/4)) {
185 if (!quiet) printf("Mispredicts too low\n");
186 test_fail( __FILE__, __LINE__, "Error too low", 1 );
187 }
188
189 if (average > (num_random_branches/4)*3) {
190 if (!quiet) printf("Mispredicts too high\n");
191 test_fail( __FILE__, __LINE__, "Error too high", 1 );
192 }
193
194 if (!quiet) printf("\n");
195
196 test_pass( __FILE__ );
197
199
200 return 0;
201
202}
int i
int branches_testcode(void)
int random_branches_testcode(int number, int quiet)
static long count
add PAPI preset or native hardware event by name to an EventSet
Create a new empty PAPI EventSet.
initialize the PAPI library.
Reset the hardware event counts in an event set.
Finish using PAPI and free all related resources.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
static int expected[NUM_THREADS]
#define PAPI_VER_CURRENT
Definition: f90papi.h:54
#define PAPI_OK
Definition: f90papi.h:73
#define PAPI_NULL
Definition: f90papi.h:78
Return codes and api definitions.
FILE * stderr
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void PAPI_NORETURN test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:491
void PAPI_NORETURN test_pass(const char *filename)
Definition: test_utils.c:432
void PAPI_NORETURN test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:584
int main()
Definition: pernode.c:20
int quiet
Definition: rapl_overflow.c:19
static int total
Definition: rapl_overflow.c:9
#define CODE_UNIMPLEMENTED
Definition: testcode.h:2
int retval
Definition: zero_fork.c:53