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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sched.h>
#include <assert.h>
#include <string.h>
#include <stdint.h>
#include <sys/fcntl.h>
#include <sys/mman.h>
#include <inttypes.h>
/* Test tool for validating Litmus's uncache device. */
/* Tool also capable basic cache vs. sysmem statistics. */
/* Compile with '-O2' for significaintly greater margins */
/* in performance between cache and sysmem: */
/* (Intel Xeon X5650) */
/* -g -> uncache is 30x slower */
/* -O2 -> uncache is >100x slower */
int PAGE_SIZE;
#define NR_PAGES 16
#define UNCACHE_DEV "/dev/litmus/uncache"
/* volatile forces a read from memory (or cache) on every reference. Note
that volatile does not keep data out of the cache! */
typedef volatile char* pbuf_t;
/* hit the first byte in each page.
addr must be page aligned. */
inline int linear_write(pbuf_t addr, int size, char val)
{
pbuf_t end = addr + size;
pbuf_t step;
int nr_pages = (unsigned long)(end - addr)/PAGE_SIZE;
int times = nr_pages * PAGE_SIZE;
int i;
for (i = 0; i < times; ++i)
for(step = addr; step < end; step += PAGE_SIZE)
*step = val;
return 0;
}
inline int linear_read(pbuf_t addr, int size, char val)
{
pbuf_t end = addr + size;
pbuf_t step;
int nr_pages = (unsigned long)(end - addr)/PAGE_SIZE;
int times = nr_pages * PAGE_SIZE;
int i;
for (i = 0; i < times; ++i)
for(step = addr; step < end; step += PAGE_SIZE) {
if (*step != val)
return -1;
}
return 0;
}
/* write to *data nr times. */
inline int hammer_write(pbuf_t data, char val, int nr)
{
int i;
for (i = 0; i < nr; ++i)
*data = val;
return 0;
}
/* read from *data nr times. */
inline int hammer_read(pbuf_t data, char val, int nr)
{
int i;
for (i = 0; i < nr; ++i) {
if (*data != val)
return -1;
}
return 0;
}
inline int test(pbuf_t data, int size, int trials)
{
int HAMMER_TIME = 10000; /* can't cache this! */
char VAL = 0x55;
int t;
for(t = 0; t < trials; ++t) {
#if 0
if (linear_write(data, size, VAL) != 0) {
printf("failed linear_write()\n");
return -1;
}
if (linear_read(data, size, VAL) != 0) {
printf("failed linear_read()\n");
return -1;
}
#endif
/* hammer at the first byte in the array */
if (hammer_write(data, VAL, HAMMER_TIME) != 0) {
printf("failed hammer_write()\n");
return -1;
}
if (hammer_read(data, VAL, HAMMER_TIME) != 0) {
printf("failed hammer_read()\n");
return -1;
}
}
return 0;
}
inline void timespec_normalize(struct timespec* ts, time_t sec, int64_t nsec)
{
while(nsec > 1000000000LL) {
asm("" : "+rm"(nsec));
nsec -= 1000000000LL;
++sec;
}
while(nsec < 0) {
asm("" : "+rm"(nsec));
nsec += 1000000000LL;
--sec;
}
ts->tv_sec = sec;
ts->tv_nsec = nsec;
}
inline struct timespec timespec_sub(struct timespec lhs, struct timespec rhs)
{
struct timespec delta;
timespec_normalize(&delta, lhs.tv_sec - rhs.tv_sec, lhs.tv_nsec - rhs.tv_nsec);
return delta;
}
inline struct timespec timespec_add(struct timespec lhs, struct timespec rhs)
{
struct timespec delta;
timespec_normalize(&delta, lhs.tv_sec + rhs.tv_sec, lhs.tv_nsec + rhs.tv_nsec);
return delta;
}
inline int64_t timespec_to_us(struct timespec ts)
{
int64_t t;
t = ts.tv_sec * 1000000LL;
t += ts.tv_nsec / 1000LL;
return t;
}
/* hammers away at the first byte in each mmaped page and
times how long it took. */
int do_data(int do_uncache, int64_t* time)
{
int size;
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE;
pbuf_t data;
struct sched_param fifo_params;
struct timespec start, end;
int64_t elapsed;
int trials = 1000;
printf("Running data access test.\n");
mlockall(MCL_CURRENT | MCL_FUTURE);
memset(&fifo_params, 0, sizeof(fifo_params));
fifo_params.sched_priority = sched_get_priority_max(SCHED_FIFO);
size = PAGE_SIZE*NR_PAGES;
printf("Allocating %d %s pages.\n", NR_PAGES, (do_uncache) ?
"uncacheable" : "cacheable");
if (do_uncache) {
int fd = open(UNCACHE_DEV, O_RDWR);
data = mmap(NULL, size, prot, flags, fd, 0);
close(fd);
}
else {
/* Accessed data will probably fit in L1, so this will go VERY fast.
Code should also have little-to-no pipeline stalls. */
flags |= MAP_ANONYMOUS;
data = mmap(NULL, size, prot, flags, -1, 0);
}
if (data == MAP_FAILED) {
printf("Failed to alloc data! "
"Are you running Litmus? "
"Is Litmus broken?\n");
return -1;
}
else {
printf("Data allocated at %p.\n", data);
}
printf("Beginning tests...\n");
if (sched_setscheduler(getpid(), SCHED_FIFO, &fifo_params)) {
printf("(Could not become SCHED_FIFO task.) Are you running as root?\n");
}
/* observations suggest that no warmup phase is needed. */
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start);
if (test(data, size, trials) != 0) {
printf("Test failed!\n");
munmap((char*)data, size);
return -1;
}
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
elapsed = timespec_to_us(timespec_sub(end, start));
printf("%s Time: %"PRIi64"us\n", (do_uncache) ?
"Uncache" : "Cache", elapsed);
munmap((char*)data, size);
if(time)
*time = elapsed;
return 0;
}
/* compares runtime of cached vs. uncached */
int do_data_compare()
{
const double thresh = 1.3;
int ret = 0;
double ratio;
int64_t cache_time = 0, uncache_time = 0;
printf("Timing cached pages...\n");
ret = do_data(0, &cache_time);
if (ret != 0)
goto out;
printf("Timing uncached pages...\n");
ret = do_data(1, &uncache_time);
if (ret != 0)
goto out;
ratio = (double)uncache_time/(double)cache_time;
printf("Uncached/Cached Ratio: %f\n", ratio);
if (ratio < thresh) {
printf("Ratio is unexpectedly small (< %f)! "
" Uncache broken? Are you on kvm?\n", thresh);
ret = -1;
}
out:
return ret;
}
/* tries to max out uncache allocations.
under normal conditions (non-mlock),
pages should spill into swap. uncache
pages are not locked in memory. */
int do_max_alloc(void)
{
int fd;
int good = 1;
int count = 0;
uint64_t mmap_size = PAGE_SIZE; /* start at one page per mmap */
/* half of default limit on ubuntu. (see /proc/sys/vm/max_map_count) */
int max_mmaps = 32765;
volatile char** maps = calloc(max_mmaps, sizeof(pbuf_t));
if (!maps) {
printf("failed to alloc pointers for pages\n");
return -1;
}
printf("Testing max amount of uncache data. System may get wonkie (OOM Killer)!\n");
fd = open(UNCACHE_DEV, O_RDWR);
do {
int i;
int nr_pages = mmap_size/PAGE_SIZE;
printf("Testing mmaps of %d pages.\n", nr_pages);
count = 0;
for (i = 0; (i < max_mmaps) && good; ++i) {
pbuf_t data = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_POPULATE, fd, 0);
if (data != MAP_FAILED) {
maps[i] = data;
++count;
}
else {
perror(NULL);
good = 0;
}
}
for (i = 0; i < count; ++i) {
if (maps[i])
munmap((char*)(maps[i]), mmap_size);
}
memset(maps, 0, sizeof(maps[0])*max_mmaps);
mmap_size *= 2; /* let's do it again with bigger allocations */
}while(good);
free(maps);
close(fd);
printf("Maxed out allocs with %d mmaps of %"PRIu64" pages in size.\n",
count, mmap_size/PAGE_SIZE);
return 0;
}
typedef enum
{
UNCACHE,
CACHE,
COMPARE,
MAX_ALLOC
} test_t;
#define OPTSTR "ucxa"
int main(int argc, char** argv)
{
int ret;
test_t test = UNCACHE;
int opt;
PAGE_SIZE = sysconf(_SC_PAGE_SIZE);
while((opt = getopt(argc, argv, OPTSTR)) != -1) {
switch(opt) {
case 'c':
test = CACHE;
break;
case 'u':
test = UNCACHE;
break;
case 'x':
test = COMPARE;
break;
case 'a':
test = MAX_ALLOC;
break;
case ':':
printf("missing option\n");
exit(-1);
case '?':
default:
printf("bad argument\n");
exit(-1);
}
}
printf("Page Size: %d\n", PAGE_SIZE);
switch(test)
{
case CACHE:
ret = do_data(0, NULL);
break;
case UNCACHE:
ret = do_data(1, NULL);
break;
case COMPARE:
ret = do_data_compare();
break;
case MAX_ALLOC:
ret = do_max_alloc();
break;
default:
printf("invalid test\n");
ret = -1;
break;
}
if (ret != 0) {
printf("Test failed.\n");
}
return ret;
}
|