This repository has been archived by the owner on Dec 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachetest.c
69 lines (59 loc) · 2.7 KB
/
cachetest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <sys/times.h>
#include <sys/types.h>
#include <time.h>
#define CACHE_MIN (1024) /* smallest cache */
#define CACHE_MAX (16*1024*1024) /* largest cache */
#define SAMPLE 10 /* to get a larger time sample */
#ifndef CLK_TCK
#define CLK_TCK 60 /* number clock ticks per second */
#endif
int x[CACHE_MAX]; /* array going to stride through */
double get_seconds() { /* routine to read time */
struct tms rusage;
times(&rusage); /* UNIX utility: time in clock ticks */
return (double) (rusage.tms_utime)/CLK_TCK;
}
int main() {
int register i, index, stride, limit;
int temp;
int steps, tsteps, csize;
double sec0, sec; /* timing variables */
for (csize=CACHE_MIN; csize <= CACHE_MAX; csize=csize*2)
for (stride=1; stride <= 128; stride=stride*2) {
sec = 0; /* initialize timer */
limit = csize-stride+1; /* cache size this loop */
steps = 0;
do { /* repeat until collect 1 second */
sec0 = get_seconds(); /* start timer */
for (i=SAMPLE*stride;i!=0;i=i-1) { /* larger sample */
for (index=0; index < limit; index=index+stride+4) {
x[index+0] = x[index+0] + 1; /* cache access */
x[index+1] = x[index+1] + 1; /* cache access */
x[index+2] = x[index+2] + 1; /* cache access */
x[index+3] = x[index+3] + 1; /* cache access */
}
steps = steps + 1; /* count while loop iterations */
sec = sec + (get_seconds() - sec0);/* end timer */
}
} while (sec < 1.0); /* until collect 1 second */
/* Repeat empty loop to loop subtract overhead */
tsteps = 0; /* used to match no. while iterations */
do { /* repeat until same no. iterations as above */
sec0 = get_seconds(); /* start timer */
for (i=SAMPLE*stride;i!=0;i=i-1) {/* larger sample */
for (index=0; index < limit; index=index+stride+4) {
temp = temp + index+0; /* dummy code */
temp = temp + index+1; /* dummy code */
temp = temp + index+2; /* dummy code */
temp = temp + index+3; /* dummy code */
}
tsteps = tsteps + 1; /* count while iterations */
sec = sec - (get_seconds() - sec0);/* - overhead */
}
} while (tsteps<steps); /* until = no. iterations */
printf("Size (bytes):%9d, Stride (bytes):%4d, read+write:%6.2f ns\n",
csize*sizeof(int), stride*sizeof(int),
(double) sec*1e9/(steps*SAMPLE*stride*((limit-1)/stride+1)));
}; /* end of both outer for loops */
}