-
Notifications
You must be signed in to change notification settings - Fork 15
/
mmap_cache_test.c
384 lines (293 loc) · 8.18 KB
/
mmap_cache_test.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
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
383
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#if !defined(WIN32) || defined(CYGWIN)
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#ifdef DEBUG
#define ASSERT(x) assert(x)
#include <assert.h>
#else
#define ASSERT(x)
#endif
#ifndef WIN32
#include <sys/wait.h>
#else
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
double last_rand;
double drand48(void) {
last_rand = rand() / (double)(RAND_MAX+1);
ASSERT(last_rand < 1);
return last_rand;
}
#endif
#include <time.h>
#include "mmap_cache.h"
void * Get(mmap_cache * cache, void * key_ptr, int key_len, int * val_len) {
int found;
void * val_ptr, * val_rtn_ptr = 0;
MU32 hash_page, hash_slot, flags;
/* Hash key to get page and slot */
mmc_hash(cache, key_ptr, key_len, &hash_page, &hash_slot);
/* Get and lock the page */
mmc_lock(cache, hash_page);
/* Get value data pointer */
found = mmc_read(cache, hash_slot, key_ptr, key_len, &val_ptr, val_len, &flags);
/* If not found, use undef */
if (found == -1) {
} else {
/* Put into our own memory */
val_rtn_ptr = (void *)malloc(*val_len);
memcpy(val_rtn_ptr, val_ptr, *val_len);
}
mmc_unlock(cache);
return val_rtn_ptr;
}
void Set(mmap_cache * cache, void * key_ptr, int key_len, void * val_ptr, int val_len) {
MU32 hash_page, hash_slot, flags = 0, new_num_slots, ** expunge_items = 0;
int num_expunge;
/* Hash key to get page and slot */
mmc_hash(cache, key_ptr, key_len, &hash_page, &hash_slot);
/* Get and lock the page */
mmc_lock(cache, hash_page);
num_expunge = mmc_calc_expunge(cache, 2, key_len + val_len, &new_num_slots, &expunge_items);
if (expunge_items) {
mmc_do_expunge(cache, num_expunge, new_num_slots, expunge_items);
}
/* Get value data pointer */
mmc_write(cache, hash_slot, key_ptr, key_len, val_ptr, val_len, 60, flags);
mmc_unlock(cache);
}
char * rand_str(int nchar) {
unsigned char * buf = (unsigned char *)malloc(nchar + 1);
int i;
for (i = 0; i < nchar; i++) {
buf[i] = (char)(rand() % 26) + 'A';
}
buf[i] = 0;
return (char *)buf;
}
char buf[65537];
int BasicTests(mmap_cache * cache) {
int val_len, i;
void * val_ptr;
printf("Basic tests\n");
/* Test empty */
ASSERT(!Get(cache, "", 0, &val_len));
ASSERT(!Get(cache, " ", 0, &val_len));
for (i = 0; i < 65536; i++) { buf[i] = ' '; }
ASSERT(!Get(cache, buf, 1024, &val_len));
ASSERT(!Get(cache, buf, 65536, &val_len));
/* Test basic store/get on key sizes */
Set(cache, "", 0, "abc", 3);
ASSERT(!memcmp(val_ptr = Get(cache, "", 0, &val_len), "abc", 3) && val_len == 3);
free(val_ptr);
Set(cache, " ", 1, "def", 3);
ASSERT(!memcmp(val_ptr = Get(cache, " ", 1, &val_len), "def", 3) && val_len == 3);
free(val_ptr);
Set(cache, buf, 1024, "ghi", 3);
ASSERT(!memcmp(val_ptr = Get(cache, buf, 1024, &val_len), "ghi", 3) && val_len == 3);
free(val_ptr);
/* Bigger than page size - shouldn't work */
Set(cache, buf, 65536, "jkl", 3);
ASSERT(!Get(cache, buf, 65536, &val_len));
/* Test basic store/get on value sizes */
Set(cache, "abc", 3, "", 0);
ASSERT((val_ptr = Get(cache, "abc", 3, &val_len)) && val_len == 0);
free(val_ptr);
Set(cache, "def", 3, "x", 1);
ASSERT(!memcmp(val_ptr = Get(cache, "def", 3, &val_len), "x", 1) && val_len == 1);
free(val_ptr);
for (i = 0; i < 1024; i++) { buf[i] = 'y'; }
buf[0] = 'z'; buf[1023] = 'w';
Set(cache, "ghi", 3, buf, 1024);
ASSERT(!memcmp(val_ptr = Get(cache, "ghi", 3, &val_len), buf, 1024) && val_len == 1024);
free(val_ptr);
/* Bigger than page size - shouldn't work */
Set(cache, "jkl", 3, buf, 65536);
ASSERT(!Get(cache, "jkl", 3, &val_len));
return 0;
}
int LinearTests(mmap_cache * cache) {
int i, gl;
char * str1, * str2, * str3;
printf("Linear tests\n");
for (i = 0; i < 100000; i++) {
str1 = rand_str(10);
str2 = rand_str(10);
Set(cache, str1, strlen(str1)+1, str2, strlen(str2)+1);
str3 = Get(cache, str1, strlen(str1)+1, &gl);
ASSERT(strlen(str2)+1 == gl);
ASSERT(!memcmp(str2, str3, strlen(str2)+1));
free(str1);
free(str2);
free(str3);
if (i % 1000 == 0) {
printf("%d\n", i);
}
}
}
int EdgeTests() {
return 0;
}
typedef struct key_list {
int n_keys;
int buf_size;
char ** keys;
} key_list;
key_list * kl_new() {
key_list * kl = (key_list *)malloc(sizeof(key_list));
kl->buf_size = 8;
kl->keys = (char **)malloc(sizeof(char *) * kl->buf_size);
kl->n_keys = 0;
return kl;
}
void kl_push(key_list * kl, char * key) {
if (kl->n_keys < kl->buf_size) {
kl->keys[kl->n_keys++] = key;
return;
}
kl->buf_size *= 2;
kl->keys = (char **)realloc(kl->keys, sizeof(char *) * kl->buf_size);
kl->keys[kl->n_keys++] = key;
return;
}
void kl_free(key_list * kl) {
int i;
for (i = 0; i < kl->n_keys; i++) {
free(kl->keys[i]);
}
}
int urand_fh = 0;
void RandSeed() {
#ifdef WIN32
//randomize();
#else
char buf[8];
if (!urand_fh) {
urand_fh = open("/dev/urandom", O_RDONLY);
}
read(urand_fh, buf, 8);
srand48(*(long int *)buf);
#endif
}
int RepeatMixTests(mmap_cache * cache, double ratio, key_list * kl) {
int i, val_len;
int read = 0, read_hit = 0;
char valbuf[256];
printf("Repeat mix tests\n");
for (i = 0; i < 10000; i++) {
/* Read/write ratio */
if (drand48() < ratio) {
/* Pick a key from known written ones */
char * k = kl->keys[(int)(drand48() * kl->n_keys)];
void * v = Get(cache, k, strlen(k), &val_len);
read++;
/* Skip if not found in cache */
if (!v) { continue; }
read_hit++;
/* Offset of 10 past first chars of value are key */
memcpy(valbuf, v+10, strlen(k));
valbuf[strlen(k)] = '\0';
ASSERT(!memcmp(valbuf, k, strlen(k)));
free(v);
} else {
char * k = rand_str(10 + (int)(drand48() * 10));
char * v = rand_str(10);
char * ve = rand_str((int)(drand48() * 200));
strcpy(valbuf, v);
strcat(valbuf, k);
strcat(valbuf, ve);
kl_push(kl, k);
Set(cache, k, strlen(k), valbuf, strlen(valbuf));
free(ve);
free(v);
}
}
if (read) {
printf("Read hit pct: %5.3f\n", (double)read_hit/read);
}
return 1;
}
void IteratorTests(mmap_cache * cache) {
MU32 * entry_ptr;
void * key_ptr, * val_ptr;
int key_len, val_len;
MU32 last_access, expire_time, flags;
mmap_cache_it * it = mmc_iterate_new(cache);
printf("Iterator tests\n");
while ((entry_ptr = mmc_iterate_next(it))) {
mmc_get_details(cache, entry_ptr,
&key_ptr, &key_len, &val_ptr, &val_len,
&last_access, &expire_time, &flags);
ASSERT(key_len >= 10 && key_len <= 20);
ASSERT(val_len >= 20 && val_len <= 240);
ASSERT(last_access >= 1000000 && last_access <= time(0));
}
mmc_iterate_close(it);
}
int ForkTests(mmap_cache * cache, key_list * kl) {
#ifndef WIN32
int pid, j, k, kid, kids[20], nkids = 0, status;
struct timeval timeout = { 0, 1000 };
for (j = 0; j < 8; j++) {
if (!(pid = fork())) {
RandSeed();
RepeatMixTests(cache, 0.4, kl);
exit(0);
}
kids[nkids++] = pid;
select(0, 0, 0, 0, &timeout);
}
do {
kid = waitpid(-1, &status, 0);
for (j = 0, k = 0; j < nkids; j++) {
if (kids[j] != kid) { k++; }
kids[j] = kids[k];
}
nkids--;
} while (kid > 0 && nkids);
return 0;
#else
#endif
}
int main(int argc, char ** argv) {
int res;
key_list * kl;
mmap_cache * cache;
cache = mmc_new();
mmc_set_param(cache, "init_file", "1");
res = mmc_init(cache);
kl = kl_new();
BasicTests(cache);
LinearTests(cache);
mmc_close(cache);
cache = mmc_new();
mmc_set_param(cache, "init_file", "1");
res = mmc_init(cache);
RepeatMixTests(cache, 0.0, kl);
RepeatMixTests(cache, 0.5, kl);
RepeatMixTests(cache, 0.8, kl);
IteratorTests(cache);
ForkTests(cache, kl);
kl_free(kl);
mmc_close(cache);
cache = mmc_new();
mmc_set_param(cache, "init_file", "1");
mmc_set_param(cache, "page_size", "8192");
res = mmc_init(cache);
kl = kl_new();
BasicTests(cache);
RepeatMixTests(cache, 0.0, kl);
RepeatMixTests(cache, 0.5, kl);
RepeatMixTests(cache, 0.8, kl);
ForkTests(cache, kl);
kl_free(kl);
mmc_close(cache);
return 0;
}