-
Notifications
You must be signed in to change notification settings - Fork 9
/
lat_mem.c
205 lines (172 loc) · 4.55 KB
/
lat_mem.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "lat_mem.h"
#include "bw_mem.h"
extern int TEMP;
static int ITER = 10000;
static int get_iteration() {
return ITER;
}
void do_l1_prepare(lat_mem_state_t *state) {
size_t buflen = 1024*1024*256;
state->buf = (char *)malloc(buflen);
if(!state->buf) {
perror("malloc buf error");
exit(1);
}
memset(state->buf, 0, buflen);
char *buf = state->buf;
size_t stride = state->stride;
int i = 0;
for(i = stride; i < state->buflen; i += stride) {
*(char **)&buf[i-stride] = (char *)&buf[i];
}
*(char **)&buf[i-stride] = (char *)&buf[0];
}
void do_l1_cooldown(lat_mem_state_t *state) {
if(state->buf)
free(state->buf);
}
#define ONE p = (char **)*p;
#define FIVE ONE ONE ONE ONE ONE
#define TEN FIVE FIVE
#define FIFTY TEN TEN TEN TEN TEN
#define HUNDRED FIFTY FIFTY
void do_l1_latency(unsigned long iter, lat_mem_state_t *state) {
char **p = (char **)&state->buf[0];
while(iter --) {
HUNDRED
}
TEMP = (long)p;
}
double get_cache_latency(lat_mem_state_t *state) {
struct timespec st, ed;
unsigned long elapse;
int loopcnt, loop = 30;
int index = loop >> 2;
int iter, iteration = get_iteration();
result_t *r = (result_t *)malloc(sizeof(result_t) * loop);
loopcnt = loop;
while(loopcnt --) {
if(state->prepare)
state->prepare(state);
clock_gettime(CLOCK_REALTIME, &st);
state->bench(iteration, state);
clock_gettime(CLOCK_REALTIME, &ed);
elapse = get_total_ns(&st, &ed);
save_result(r, loopcnt, elapse, iteration);
if(state->cooldown)
state->cooldown(state);
}
qsort(r, loop, sizeof(result_t ), cmp_result);
double latency = get_latency(r[index].elapse / 100, r[index].iter); // convert us to ns
return latency;
}
size_t l1_step(size_t k) {
if (k < 16*1024) {
k = k * 4;
} else if (k < 64*1024) {
k += 16*1024;
} else {
size_t s;
for (s = 128 * 4096; s <= k; s *= 2)
;
k += s / 4;
}
return (k);
}
double bench_l1_latency() {
lat_mem_state_t state;
memset(&state, 0, sizeof(lat_mem_state_t));
state.prepare = do_l1_prepare;
state.bench = do_l1_latency;
state.cooldown = do_l1_cooldown;
size_t strides[] = {64, 1024, 4096, 8192};
size_t buflen = 1024*1024; // 1024KB
printf("\tBandwidth of Latency of L1 Cache\n\n");
for(int i=0; i < sizeof(strides)/sizeof(size_t); i++) {
size_t stride = strides[i];
for(size_t len = 1024; len <= buflen; len = l1_step(len)) {
if(stride < len) {
state.buflen = len;
state.stride = stride;
printf("\tbuflen = %5luKB\tstride = %4lu\tL1 Cache Latency: %lfns\n", len >> 10, stride, get_cache_latency(&state));
}
}
printf("\n");
}
}
size_t l2_step(size_t k) {
if (k < 128*1024) {
k = k * 2;
} else if (k < 256*1024) {
k += 32*1024;
} else {
size_t s;
for (s = 512 * 4096; s <= k; s *= 2)
;
k += s / 4;
}
return (k);
}
double bench_l2_latency() {
lat_mem_state_t state;
memset(&state, 0, sizeof(lat_mem_state_t));
state.prepare = do_l1_prepare;
state.bench = do_l1_latency;
state.cooldown = do_l1_cooldown;
size_t strides[] = {4096, 8192, 16384};
size_t buflen = 8*1024*1024; // 1024KB
printf("\tBandwidth of Latency of L2 Cache\n\n");
for(int i=0; i < sizeof(strides)/sizeof(size_t); i++) {
size_t stride = strides[i];
for(size_t len = 32*1024; len <= buflen; len = l2_step(len)) {
if(stride < len) {
state.buflen = len;
state.stride = stride;
printf("\tbuflen = %5luKB\tstride = %4lu\tL2 Cache Latency: %lfns\n", len >> 10, stride, get_cache_latency(&state));
}
}
printf("\n");
}
}
size_t l3_step(size_t k) {
if (k < 128*1024) {
k = k * 2;
} else if (k < 256*1024) {
k += 32*1024;
} else {
size_t s;
for (s = 512 * 4096; s <= k; s *= 2)
;
k += s / 8;
}
return (k);
}
double bench_l3_latency() {
lat_mem_state_t state;
memset(&state, 0, sizeof(lat_mem_state_t));
state.prepare = do_l1_prepare;
state.bench = do_l1_latency;
state.cooldown = do_l1_cooldown;
size_t strides[] = {4096, 8192, 16384};
printf("\tBandwidth of Latency of L3 Cache\n\n");
size_t buflen = 32*1024*1024; // 1024KB
for(int i=0; i < sizeof(strides)/sizeof(size_t); i++) {
size_t stride = strides[i];
for(size_t len = 32*1024; len <= buflen; len = l3_step(len)) {
if(stride < len) {
state.buflen = len;
state.stride = stride;
printf("\tbuflen = %6luKB\tstride = %6lu\tL3 Cache Latency: %lfns\n", len >> 10, stride, get_cache_latency(&state));
}
}
printf("\n");
}
}
void bench_cache_latency() {
printf("\nbenchmark on Cache Latency\n");
printf("-------------------------------------\n");
bench_l1_latency();
bench_l2_latency();
bench_l3_latency();
printf("\n--------------end--------------------\n");
}