PAPI  5.7.0.0
papi_l2_dcw.c
Go to the documentation of this file.
1 /* This code attempts to test the L2 Data Cache Writes */
2 /* performance counter PAPI_L2_DCW */
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 
20 int 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,warnings=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_DCW 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 
49  retval=PAPI_create_eventset(&eventset);
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_DCW");
55  if (retval!=PAPI_OK) {
56  test_skip( __FILE__, __LINE__, "adding PAPI_L2_DCW", 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_DCW", 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));
117 
118  if (!quiet) {
119  printf("\tShould be roughly "
120  "arraysize/L1_linesize/double_size (%d/%d/%zu): "
121  "%lld\n\n",
122  arraysize,l1_linesize,sizeof(double),
123  expected);
124  }
125 
126  error=display_error(average,high,low,expected,quiet);
127 
128  if ((error > 10.0) || (error<-10.0)) {
129  if (!quiet) printf("Instruction count off by more than 1%%\n");
130  errors++;
131  }
132 
133  if (low<expected/10) {
134  if (!quiet) printf("WARNING: Average OK but low value seems suspicious\n");
135  warnings++;
136  }
137 
138  if (!quiet) printf("\n");
139 
140  /******************/
141  /* Testing Reads */
142  /******************/
143 
144  if (!quiet) {
145  printf("\nRead Test: Summing an array of %d doubles:\n",
146  arraysize);
147  }
148 
149  high=0; low=0; total=0;
150 
151  for(i=0;i<num_runs;i++) {
152 
153  PAPI_reset(eventset);
154  PAPI_start(eventset);
155 
156  aSumm+=cache_read_test(array,arraysize);
157 
158  retval=PAPI_stop(eventset,&count);
159 
160  if (retval!=PAPI_OK) {
161  test_fail( __FILE__, __LINE__,
162  "reading PAPI_L2_DCW", retval );
163  }
164 
165  if (count>high) high=count;
166  if ((low==0) || (count<low)) low=count;
167  total+=count;
168  }
169 
170  average=(total/num_runs);
171 
172  expected=(arraysize/(l1_linesize/sizeof(double)))/10;
173 
174  if (!quiet) {
175  printf("\tShould be very low as we are measuring writes\n");
176  }
177 
178  if (!quiet) {
179  printf("Average writes: %lld\n",average);
180  }
181 
182 
183 
184  if (average>expected) {
185  if (!quiet) printf("ERROR: Write count unexpectedly high\n");
186  errors++;
187  }
188 
189  if (!quiet) {
190  printf("\n");
191  }
192 
193  /* FIXME: warn for now, as fail on broadwell and more recent */
194  if (errors) {
195  test_warn( __FILE__, __LINE__, "Error too high", 1 );
196  }
197 
198  if (warnings) {
199  test_warn(__FILE__, __LINE__, "Average results OK but some measurements low",1);
200  }
201 
202  test_pass(__FILE__);
203 
204  return 0;
205 }
#define PAPI_OK
Definition: fpapi.h:105
#define NUM_RUNS
Definition: papi_l2_dcw.c:18
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
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
double display_error(long long average, long long high, long long low, long long expected, int quiet)
Definition: display_error.c:7
static double array[ARRAYSIZE]
Definition: papi_l1_dca.c:23
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int retval
Definition: zero_fork.c:53
void test_warn(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:524
Return codes and api definitions.
#define L2_CACHE
Definition: cache_helper.h:3
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
int quiet
Definition: rapl_overflow.c:18
int cache_write_test(double *array, int size)
Definition: cache_testcode.c:6
#define PAPI_NULL
Definition: fpapi.h:13
long long get_linesize(int type)
Definition: cache_helper.c:110
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
#define L1D_CACHE
Definition: cache_helper.h:2
int main(int argc, char **argv)
Definition: papi_l2_dcw.c:20
long long get_cachesize(int type)
Definition: cache_helper.c:78
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
long long get_entries(int type)
Definition: cache_helper.c:94
static long count
int i
Definition: fileop.c:140
double cache_read_test(double *array, int size)