PAPI  5.7.0.0
papi_l1_dcm.c
Go to the documentation of this file.
1 /* This code attempts to test the L1 Data Cache Missses */
2 /* performance counter PAPI_L1_DCM */
3 
4 /* by Vince Weaver, vincent.weaver@maine.edu */
5 
6 
7 /* Due to prefetching it is hard to create a testcase short of */
8 /* just having random accesses. */
9 /* In addition, due to context switching the cache might be */
10 /* affected by other processes on a busy system. */
11 
12 /* Other tests to attempt */
13 /* Repeatedly reading same cache line should give very small error */
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 
19 #include "papi.h"
20 #include "papi_test.h"
21 
22 #include "cache_helper.h"
23 #include "display_error.h"
24 
25 #include "testcode.h"
26 
27 /* Is 5% too big? */
28 #define ALLOWED_ERROR 5.0
29 
30 #define NUM_RUNS 100
31 
32 #define ITERATIONS 1000000
33 
34 int main(int argc, char **argv) {
35 
36  int i;
37  int eventset=PAPI_NULL;
38  int num_runs=NUM_RUNS;
39  long long high,low,average,expected;
40  long long count,total;
41 
42  int retval;
43  int l1_size,l2_size,l1_linesize,l2_linesize,l2_entries;
44  int arraysize;
45  int quiet,errors=0;
46 
47  double error;
48  double *array;
49  double aSumm = 0.0;
50 
51  quiet=tests_quiet(argc,argv);
52 
53  if (!quiet) {
54  printf("Testing the PAPI_L1_DCM event\n");
55  }
56 
57  /* Init the PAPI library */
59  if (retval != PAPI_VER_CURRENT) {
60  test_fail(__FILE__,__LINE__,"PAPI_library_init",retval);
61  }
62 
63  retval=PAPI_create_eventset(&eventset);
64  if (retval!=PAPI_OK) {
65  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
66  }
67 
68  retval=PAPI_add_named_event(eventset,"PAPI_L1_DCM");
69  if (retval!=PAPI_OK) {
70  test_skip( __FILE__, __LINE__, "adding PAPI_L1_DCM", retval );
71  }
72 
73  l1_size=get_cachesize(L1D_CACHE);
74  l1_linesize=get_linesize(L1D_CACHE);
75  l2_size=get_cachesize(L2_CACHE);
76  l2_linesize=get_linesize(L2_CACHE);
77  l2_entries=get_entries(L2_CACHE);
78 
79  if (!quiet) {
80  printf("\tDetected %dk L1 DCache, %dB linesize\n",
81  l1_size/1024,l1_linesize);
82  printf("\tDetected %dk L2 DCache, %dB linesize, %d entries\n",
83  l2_size/1024,l2_linesize,l2_entries);
84  }
85 
86  arraysize=(l1_size/sizeof(double))*8;
87 
88  if (arraysize==0) {
89  if (!quiet) printf("Could not detect cache size\n");
90  test_skip(__FILE__,__LINE__,"No cache info",0);
91  }
92 
93  if (!quiet) {
94  printf("\tAllocating %zu bytes of memory (%d doubles)\n",
95  arraysize*sizeof(double),arraysize);
96  }
97 
98  array=calloc(arraysize,sizeof(double));
99  if (array==NULL) {
100  test_fail(__FILE__,__LINE__,"Can't allocate memory",0);
101  }
102 
103  /******************/
104  /* Testing Writes */
105  /******************/
106 
107  if (!quiet) {
108  printf("\nWrite Test: Writing an array of %d doubles %d random times:\n",
109  arraysize,ITERATIONS);
110  printf("It's expected that 7/8 of the accesses should be hits\n");
111  }
112 
113  high=0; low=0; total=0;
114 
115  for(i=0;i<num_runs;i++) {
116 
117  PAPI_reset(eventset);
118  PAPI_start(eventset);
119 
121 
122  retval=PAPI_stop(eventset,&count);
123 
124  if (retval!=PAPI_OK) {
125  test_fail( __FILE__, __LINE__,
126  "reading PAPI_L1_DCM", retval );
127  }
128 
129  if (count>high) high=count;
130  if ((low==0) || (count<low)) low=count;
131  total+=count;
132  }
133 
134  average=(total/num_runs);
135 
136  expected=(ITERATIONS*7)/8;
137 
138 // expected=arraysize/(l1_linesize);
139 
140  error=display_error(average,high,low,expected,quiet);
141 
142  if ((error > ALLOWED_ERROR) || (error<-ALLOWED_ERROR)) {
143  if (!quiet) {
144  printf("Instruction count off by more than "
145  "%.2lf%%\n",ALLOWED_ERROR);
146  }
147  errors++;
148  }
149 
150  if (!quiet) printf("\n");
151 
152  /******************/
153  /* Testing Reads */
154  /******************/
155 
156  if (!quiet) {
157  printf("\nRead Test: Summing %d random doubles from array "
158  "of size %d:\n",ITERATIONS,arraysize);
159  printf("It's expected that 7/8 of the accesses should be hits\n");
160  }
161 
162  high=0; low=0; total=0;
163 
164  for(i=0;i<num_runs;i++) {
165 
166  PAPI_reset(eventset);
167  PAPI_start(eventset);
168 
169  aSumm+=cache_random_read_test(array,arraysize,ITERATIONS);
170 
171  retval=PAPI_stop(eventset,&count);
172 
173  if (retval!=PAPI_OK) {
174  test_fail( __FILE__, __LINE__,
175  "reading PAPI_L1_DCM", retval );
176  }
177 
178  if (count>high) high=count;
179  if ((low==0) || (count<low)) low=count;
180  total+=count;
181  }
182 
183  average=(total/num_runs);
184 
185  expected=(ITERATIONS*7)/8;
186 
187 // expected=arraysize/(l1_linesize/sizeof(double));
188 
189  error=display_error(average,high,low,expected,quiet);
190 
191  if ((error > ALLOWED_ERROR) || (error<-ALLOWED_ERROR)) {
192  if (!quiet) {
193  printf("Instruction count off by more than "
194  "%.2lf%%\n",ALLOWED_ERROR);
195  }
196  errors++;
197  }
198 
199  if (!quiet) {
200  printf("\n");
201  }
202 
203  if (errors) {
204  test_fail( __FILE__, __LINE__, "Error too high", 1 );
205  }
206 
207 
208 
209  test_pass(__FILE__);
210 
211  return 0;
212 }
#define PAPI_OK
Definition: fpapi.h:105
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
int main(int argc, char **argv)
Definition: papi_l1_dcm.c:34
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
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 cache_random_write_test(double *array, int size, int count)
int PAPI_library_init(int version)
Definition: papi.c:500
int quiet
Definition: rapl_overflow.c:18
#define ALLOWED_ERROR
Definition: papi_l1_dcm.c:28
double cache_random_read_test(double *array, int size, int count)
#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
#define ITERATIONS
Definition: papi_l1_dcm.c:32
long long get_cachesize(int type)
Definition: cache_helper.c:78
#define NUM_RUNS
Definition: papi_l1_dcm.c:30
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