PAPI 7.1.0.0
Loading...
Searching...
No Matches
papi_l2_dcr.c
Go to the documentation of this file.
1/* This code attempts to test the L2 Data Cache Reads */
2/* performance counter PAPI_L2_DCR */
3
4/* by Vince Weaver, vincent.weaver@maine.edu */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <unistd.h>
9
10#include "papi.h"
11#include "papi_test.h"
12
13#include "cache_helper.h"
14#include "display_error.h"
15
16#include "testcode.h"
17
18#define NUM_RUNS 100
19
20int main(int argc, char **argv) {
21
22 int i;
23 int eventset=PAPI_NULL;
24 int num_runs=NUM_RUNS;
25 long long high,low,average,expected;
26 long long count,total;
27
28 int retval;
29 int l1_size,l2_size,l1_linesize,l2_linesize,l2_entries;
30 int arraysize;
31 int quiet,errors=0;
32
33 double error;
34 double *array;
35 double aSumm = 0.0;
36
37 quiet=tests_quiet(argc,argv);
38
39 if (!quiet) {
40 printf("Testing the PAPI_L2_DCR event\n");
41 }
42
43 /* Init the PAPI library */
45 if (retval != PAPI_VER_CURRENT) {
46 test_fail(__FILE__,__LINE__,"PAPI_library_init",retval);
47 }
48
50 if (retval!=PAPI_OK) {
51 test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
52 }
53
54 retval=PAPI_add_named_event(eventset,"PAPI_L2_DCR");
55 if (retval!=PAPI_OK) {
56 test_skip( __FILE__, __LINE__, "adding PAPI_L2_DCR", retval );
57 }
58
59 l1_size=get_cachesize(L1D_CACHE);
60 l1_linesize=get_linesize(L1D_CACHE);
61 l2_size=get_cachesize(L2_CACHE);
62 l2_linesize=get_linesize(L2_CACHE);
63 l2_entries=get_entries(L2_CACHE);
64
65 if (!quiet) {
66 printf("\tDetected %dk L1 DCache, %dB linesize\n",
67 l1_size/1024,l1_linesize);
68 printf("\tDetected %dk L2 DCache, %dB linesize, %d entries\n",
69 l2_size/1024,l2_linesize,l2_entries);
70 }
71
72 arraysize=l2_size/sizeof(double);
73
74 if (!quiet) {
75 printf("\tAllocating %zu bytes of memory (%d doubles)\n",
76 arraysize*sizeof(double),arraysize);
77 }
78
79 array=calloc(arraysize,sizeof(double));
80 if (array==NULL) {
81 test_fail(__FILE__,__LINE__,"Can't allocate memory",0);
82 }
83
84 /******************/
85 /* Testing Writes */
86 /******************/
87
88 if (!quiet) {
89 printf("\nWrite Test: Initializing an array of %d doubles:\n",
90 arraysize);
91 }
92
93 high=0; low=0; total=0;
94
95 for(i=0;i<num_runs;i++) {
96
97 PAPI_reset(eventset);
98 PAPI_start(eventset);
99
100 cache_write_test(array,arraysize);
101
102 retval=PAPI_stop(eventset,&count);
103
104 if (retval!=PAPI_OK) {
105 test_fail( __FILE__, __LINE__,
106 "reading PAPI_L2_DCR", retval );
107 }
108
109 if (count>high) high=count;
110 if ((low==0) || (count<low)) low=count;
111 total+=count;
112 }
113
114 average=(total/num_runs);
115
116 expected=(arraysize/(l1_linesize/sizeof(double)))/10;
117
118 if (!quiet) {
119 printf("\tShould be low, as we only measure reads:\n\n");
120 }
121
122 if (!quiet) {
123 printf("\tMeasured average of: %lld\n",average);
124 }
125
126 if (average>expected) {
127 if (!quiet) printf("Instruction count bigger than expected\n");
128 errors++;
129 }
130
131 if (!quiet) printf("\n");
132
133 /******************/
134 /* Testing Reads */
135 /******************/
136
137 if (!quiet) {
138 printf("\nRead Test: Summing an array of %d doubles:\n",
139 arraysize);
140 }
141
142 high=0; low=0; total=0;
143
144 for(i=0;i<num_runs;i++) {
145
146 PAPI_reset(eventset);
147 PAPI_start(eventset);
148
149 aSumm+=cache_read_test(array,arraysize);
150
151 retval=PAPI_stop(eventset,&count);
152
153 if (retval!=PAPI_OK) {
154 test_fail( __FILE__, __LINE__,
155 "reading PAPI_L2_DCR", retval );
156 }
157
158 if (count>high) high=count;
159 if ((low==0) || (count<low)) low=count;
160 total+=count;
161 }
162
163 average=(total/num_runs);
164
165 expected=arraysize/(l1_linesize/sizeof(double));
166
167 if (!quiet) {
168 printf("\tShould be roughly "
169 "arraysize/L1_linesize/double_size (%d/%d/%zu): "
170 "%lld\n\n",
171 arraysize,l1_linesize,sizeof(double),
172 expected);
173 }
174
175 error=display_error(average,high,low,expected,quiet);
176
177 if ((error > 1.0) || (error<-1.0)) {
178 if (!quiet) printf("Instruction count off by more than 1%%\n");
179 errors++;
180 }
181
182 if (!quiet) {
183 printf("\n");
184 }
185
186 /* FIXME: Warn, as we fail on broadwell and more recent */
187 if (errors) {
188 test_warn( __FILE__, __LINE__, "Error too high", 1 );
189 }
190
191 test_pass(__FILE__);
192
193 return 0;
194}
int i
long long get_cachesize(int type)
Definition: cache_helper.c:78
long long get_linesize(int type)
Definition: cache_helper.c:110
long long get_entries(int type)
Definition: cache_helper.c:94
#define L2_CACHE
Definition: cache_helper.h:3
#define L1D_CACHE
Definition: cache_helper.h:2
int cache_write_test(double *array, int size)
Definition: cache_testcode.c:6
double cache_read_test(double *array, int size)
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.
Start counting hardware events in an event set.
Stop counting hardware events in an event set.
static int expected[NUM_THREADS]
double display_error(long long average, long long high, long long low, long long expected, int quiet)
Definition: display_error.c:7
#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.
static double array[ARRAYSIZE]
Definition: papi_l1_dca.c:23
#define NUM_RUNS
Definition: papi_l2_dcr.c:18
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 test_warn(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:547
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
int retval
Definition: zero_fork.c:53