Debian Codegraph @

test_allocator.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #include "mmap_allocator.h"
2 #include "mmap_exception.h"
3 #include "mmappable_vector.h"
4 #include <stdio.h>
5 #include <string.h>
6 #include <vector>
7 #include <assert.h>
8 #include <memory>
9 #include <sys/mman.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <sys/time.h>
14
15 using namespace std;
16 using namespace mmap_allocator_namespace;
17
18 #define TESTFILE "testfile"
19 #define TESTFILE2 "testfile2"
20
21 void generate_test_file(int count, const char *fname)
22 {
23 FILE *f;
24 int i;
25
26 f = fopen(fname, "w+");
27 for (i=0;i<count;i++) {
28 fwrite(&i, 1, sizeof(i), f);
29 }
30 fclose(f);
31 }
32
33 void test_test_file(int count, bool expect_zeros)
34 {
35 FILE *f;
36 int i, j;
37
38 f = fopen(TESTFILE, "r");
39 for (i=0;i<count;i++) {
40 size_t r = fread(&j, 1, sizeof(j), f);
41 assert(j == (expect_zeros ? 0 : i));
42 }
43 fclose(f);
44 }
45
46 void do_throw(void)
47 {
48 throw mmap_allocator_exception("Test2");
49 }
50
51 void test_page_align_macros(void)
52 {
53 int p;
54
55 p = 0x8000;
56 assert(ALIGN_TO_PAGE(p) == 0x8000);
57 assert(UPPER_ALIGN_TO_PAGE(p) == 0x8000);
58 assert(OFFSET_INTO_PAGE(p) == 0x0);
59
60 p = 0x64ab;
61 assert(ALIGN_TO_PAGE(p) == 0x6000);
62 assert(UPPER_ALIGN_TO_PAGE(p) == 0x7000);
63 assert(OFFSET_INTO_PAGE(p) == 0x4ab);
64 }
65
66 void test_throw_catch(void)
67 {
68 try {
69 throw mmap_allocator_exception("Test");
70 assert(0);
71 } catch (mmap_allocator_exception e) {
72 fprintf(stderr, "Exception message (expected): %s\n", e.what());
73 assert(strcmp(e.what(), "Test") == 0);
74 }
75 try {
76 do_throw();
77 assert(0);
78 } catch (mmap_allocator_exception e) {
79 fprintf(stderr, "Exception message (expected): %s\n", e.what());
80 }
81 }
82
83 /* Test exceptions: No file, mmap error */
84
85 void test_exceptions(void)
86 {
87 bool exception_thrown;
88
89 generate_test_file(1024, TESTFILE);
90 exception_thrown = false;
91 try {
92 vector<int, mmap_allocator<int> > int_vec(1024, 0, mmap_allocator<int>("", READ_ONLY));
93 /* Default constructor used, allocate will fail */
94 assert(0);
95 } catch (mmap_allocator_exception e) {
96 fprintf(stderr, "Exception message (expected): %s\n", e.what());
97 exception_thrown = true;
98 }
99 assert(exception_thrown);
100
101 exception_thrown = false;
102 try {
103 vector<int, mmap_allocator<int> > int_vec_notexsting_file(1024, 0, mmap_allocator<int>("karin", READ_ONLY)); /* no such file or directory */
104 assert(0);
105 } catch (mmap_allocator_exception e) {
106 fprintf(stderr, "Exception message (expected): %s\n", e.what());
107 exception_thrown = true;
108 }
109 assert(exception_thrown);
110
111 exception_thrown = false;
112 try {
113 vector<int, mmap_allocator<int> > int_vec_wrong_alignment_file(512, 0, mmap_allocator<int>(TESTFILE, READ_WRITE_PRIVATE, 123)); /* wrong alignment */
114 /* No exception here expected */
115 } catch (mmap_allocator_exception &e) {
116 fprintf(stderr, "Exception message (not expected): %s\n", e.what());
117 exception_thrown = true;
118 }
119 assert(!exception_thrown);
120 }
121
122 void test_mmap(void)
123 {
124 int i;
125
126 fprintf(stderr, "Testing int_vec_default\n");
127 generate_test_file(1024, TESTFILE);
128 mmappable_vector<int, mmap_allocator<int> > int_vec_default = mmappable_vector<int, mmap_allocator<int> >(mmap_allocator<int>(TESTFILE, DEFAULT_STL_ALLOCATOR, 0));
129 int_vec_default.mmap_file(1024);
130 assert(int_vec_default.size() == 1024);
131 for (i=0;i<1024;i++) {
132 int_vec_default[i] = i; /* no segfault */
133 assert(int_vec_default[i] == i); /* just to be sure */
134 }
135 test_test_file(1024, false);
136
137 fprintf(stderr, "Testing int_vec_rw_private\n");
138 generate_test_file(1024, TESTFILE);
139 mmappable_vector<int, mmap_allocator<int> > int_vec_rw_private = mmappable_vector<int, mmap_allocator<int> >(mmap_allocator<int>(TESTFILE, READ_WRITE_PRIVATE, 0));
140 int_vec_rw_private.mmap_file(1024);
141 for (i=0;i<1024;i++) {
142 assert(int_vec_rw_private[i] == i);
143 }
144 test_test_file(1024, false);
145
146 fprintf(stderr, "Testing int_vec_ro\n");
147 mmappable_vector<int, mmap_allocator<int> > int_vec_ro = mmappable_vector<int, mmap_allocator<int> >(mmap_allocator<int>(TESTFILE, READ_ONLY, 0));
148 int_vec_ro.mmap_file(1024);
149 for (i=0;i<1024;i++) {
150 assert(int_vec_ro[i] == i);
151 }
152 test_test_file(1024, false);
153
154 fprintf(stderr, "Testing int_vec_shifted\n");
155 mmappable_vector<int, mmap_allocator<int> > int_vec_shifted = mmappable_vector<int, mmap_allocator<int> >(mmap_allocator<int>(TESTFILE, READ_ONLY, sizeof(int)));
156 int_vec_shifted.mmap_file(1024-1);
157 for (i=0;i<1024-1;i++) {
158 assert(int_vec_shifted[i] == i+1);
159 }
160 test_test_file(1024, false);
161
162 fprintf(stderr, "Testing int_vec_pointer\n");
163 mmappable_vector<int, mmap_allocator<int> > *int_vec_pointer = new mmappable_vector<int, mmap_allocator<int> >(mmap_allocator<int>(TESTFILE, READ_ONLY, 0));
164 int_vec_pointer->mmap_file(1024);
165 for (i=0;i<1024;i++) {
166 assert((*int_vec_pointer)[i] == i);
167 }
168 test_test_file(1024, false);
169 delete int_vec_pointer;
170
171 fprintf(stderr, "Testing int_vec_initialized_private\n");
172 mmappable_vector<int, mmap_allocator<int> > int_vec_initialized_private = mmappable_vector<int, mmap_allocator<int> >(1024, 0, mmap_allocator<int>(TESTFILE, READ_WRITE_PRIVATE, 0));
173 for (i=0;i<1024;i++) {
174 assert(int_vec_initialized_private[i] == 0);
175 }
176 test_test_file(1024, false);
177
178 fprintf(stderr, "Testing int_vec_initialized_shared\n");
179 mmappable_vector<int, mmap_allocator<int> > int_vec_initialized_shared = mmappable_vector<int, mmap_allocator<int> >(1024, 0, mmap_allocator<int>(TESTFILE, READ_WRITE_SHARED, 0));
180 for (i=0;i<1024;i++) {
181 assert(int_vec_initialized_shared[i] == 0);
182 }
183 test_test_file(1024, true);
184
185 fprintf(stderr, "Testing int_vec_big\n");
186 generate_test_file(1024*1024, TESTFILE);
187 mmappable_vector<int, mmap_allocator<int> > int_vec_big = mmappable_vector<int, mmap_allocator<int> >(mmap_allocator<int>(TESTFILE, READ_ONLY, 0, MAP_WHOLE_FILE | ALLOW_REMAP));
188 int_vec_big.mmap_file(1024*1024);
189 for (i=0;i<1024*1024;i++) {
190 if (int_vec_big[i] != i) { fprintf(stderr, "falsch: i=%d val=%d\n", i, int_vec_big[i]); }
191 assert(int_vec_big[i] == i);
192 }
193 test_test_file(1024*1024, false);
194
195 fprintf(stderr, "Testing int_vec_shifted_big\n");
196 generate_test_file(1024*1024, TESTFILE);
197 mmappable_vector<int, mmap_allocator<int> > int_vec_shifted_big = mmappable_vector<int, mmap_allocator<int> >(mmap_allocator<int>(TESTFILE, READ_ONLY, sizeof(i), MAP_WHOLE_FILE | ALLOW_REMAP));
198 int_vec_shifted_big.mmap_file(1024*1024-1);
199 for (i=0;i<1024*1024-1;i++) {
200 assert(int_vec_shifted_big[i] == i+1);
201 }
202 test_test_file(1024*1024, false);
203
204 fprintf(stderr, "Testing int_vec_big_minus_one\n");
205 generate_test_file(1024*1024-1, TESTFILE);
206 mmappable_vector<int, mmap_allocator<int> > int_vec_big_minus_one = mmappable_vector<int, mmap_allocator<int> >(mmap_allocator<int>(TESTFILE, READ_ONLY, 0, MAP_WHOLE_FILE | ALLOW_REMAP));
207 int_vec_big_minus_one.mmap_file(1024*1024-1);
208 for (i=0;i<1024*1024-1;i++) {
209 assert(int_vec_big_minus_one[i] == i);
210 }
211 test_test_file(1024*1024-1, false);
212 }
213
214 void test_conversion(void)
215 {
216 mmappable_vector<int> mmap_vector;
217 vector<int> std_vector;
218 mmappable_vector<int> mmap_vector2;
219 int i;
220
221 fprintf(stderr, "Testing conversion between STL vector and mmap vector.\n");
222 generate_test_file(1024, TESTFILE);
223 mmap_vector.mmap_file(TESTFILE, READ_ONLY, 0, 1024);
224 for (i=0;i<1024;i++) {
225 assert(mmap_vector[i] == i);
226 }
227
228 std_vector = to_std_vector(mmap_vector);
229 for (i=0;i<1024;i++) {
230 assert(std_vector[i] == i);
231 }
232 for (i=0;i<1024;i++) {
233 std_vector[i] *= 2;
234 }
235 mmap_vector2 = to_mmappable_vector(std_vector);
236 for (i=0;i<1024;i++) {
237 assert(mmap_vector2[i] == i*2);
238 }
239 }
240
241
242 void test_mmap_file_pool(void)
243 {
244 generate_test_file(1024, TESTFILE);
245 int *f = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 0, 1024, false, false);
246 int *f2 = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 0, 1024, false, false);
247 int i;
248
249 assert(f == f2);
250
251 for (i=0;i<1024;i++) {
252 assert(f[i] == i);
253 }
254 the_pool.munmap_file(string(TESTFILE), READ_ONLY, 0, 1024);
255 the_pool.munmap_file(string(TESTFILE), READ_ONLY, 0, 1024);
256 }
257
258 void test_mapping_smaller_area(void)
259 {
260 fprintf(stderr, "Testing mapping of areas that fit in already mapped areas\n");
261 generate_test_file(2048, TESTFILE);
262
263 int *f = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 0, 8192, false, false);
264 int *first_page = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 0, 4096, false, false);
265 int *second_page = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 4096, 4096, false, false);
266
267 assert(f == first_page);
268 assert(f+1024 == second_page);
269
270 the_pool.munmap_file(string(TESTFILE), READ_ONLY, 0, 8192);
271 the_pool.munmap_file(string(TESTFILE), READ_ONLY, 0, 4096);
272 the_pool.munmap_file(string(TESTFILE), READ_ONLY, 4096, 4096);
273 }
274
275
276 void test_mapping_smaller_area_whole_file_flag(void)
277 {
278 fprintf(stderr, "Testing whole file flag\n");
279 generate_test_file(2048, TESTFILE);
280
281 int *f = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 0, 1, true, false);
282 int *first_page = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 0, 4096, false, false);
283 int *second_page = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 4096, 4096, false, false);
284
285 assert(f == first_page);
286 assert(f+1024 == second_page);
287
288 the_pool.munmap_file(string(TESTFILE), READ_ONLY, 0, 8192);
289 the_pool.munmap_file(string(TESTFILE), READ_ONLY, 0, 4096);
290 the_pool.munmap_file(string(TESTFILE), READ_ONLY, 4096, 4096);
291 }
292
293
294 void test_mapping_smaller_area_whole_file_flag_allocator(void)
295 {
296 int i;
297
298 fprintf(stderr, "Testing whole file flag via allocator\n");
299 generate_test_file(2048, TESTFILE);
300
301 fprintf(stderr, "Testing int_vec_ro\n");
302 mmappable_vector<int, mmap_allocator<int> > int_vec_ro = mmappable_vector<int, mmap_allocator<int> >(mmap_allocator<int>(TESTFILE, READ_ONLY, 0, MAP_WHOLE_FILE));
303 int_vec_ro.mmap_file(1024);
304 for (i=0;i<1024;i++) {
305 assert(int_vec_ro[i] == i);
306 }
307
308 mmappable_vector<int, mmap_allocator<int> > int_vec_ro_second_page = mmappable_vector<int, mmap_allocator<int> >(mmap_allocator<int>(TESTFILE, READ_ONLY, 4096, MAP_WHOLE_FILE));
309 int_vec_ro_second_page.mmap_file(1024);
310 for (i=0;i<1024;i++) {
311 assert(int_vec_ro_second_page[i] == i+1024);
312 }
313 }
314
315
316 void test_mapping_smaller_area_whole_file_flag_allocator_deleting_first_vec(void)
317 {
318 int i;
319
320 fprintf(stderr, "Testing whole file flag via allocator, deleting first mapping before allocating second page\n");
321 generate_test_file(2048, TESTFILE);
322
323 fprintf(stderr, "Testing int_vec_ro\n");
324 mmappable_vector<int, mmap_allocator<int> > *int_vec_ro_p = new mmappable_vector<int, mmap_allocator<int> >(mmap_allocator<int>(TESTFILE, READ_ONLY, 0, MAP_WHOLE_FILE));
325 int_vec_ro_p->mmap_file(1024);
326 for (i=0;i<1024;i++) {
327 assert((*int_vec_ro_p)[i] == i);
328 }
329 delete int_vec_ro_p;
330
331 mmappable_vector<int, mmap_allocator<int> > *int_vec_ro_second_page_p = new mmappable_vector<int, mmap_allocator<int> >(mmap_allocator<int>(TESTFILE, READ_ONLY, 4096, MAP_WHOLE_FILE));
332 int_vec_ro_second_page_p->mmap_file(1024);
333 for (i=0;i<1024;i++) {
334 assert((*int_vec_ro_second_page_p)[i] == i+1024);
335 }
336 delete int_vec_ro_second_page_p;
337 }
338
339
340 void test_new_interface(void)
341 {
342 int i;
343
344 fprintf(stderr, "Testing new interface\n");
345
346 generate_test_file(1024, TESTFILE);
347
348 mmappable_vector<int> vec;
349 vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024);
350
351 for (i=0;i<1024;i++) {
352 assert(vec[i] == i);
353 }
354 try {
355 vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024);
356 assert(0);
357 } catch (mmap_allocator_exception e) {
358 fprintf(stderr, "Exception message (expected): %s\n", e.what());
359 }
360 vec.munmap_file();
361
362 generate_test_file(2048, TESTFILE);
363 vec.mmap_file(TESTFILE, READ_ONLY, 4096, 1024);
364 for (i=0;i<1024;i++) {
365 assert(vec[i] == i+1024);
366 }
367 }
368
369 void test_cache_bug(void)
370 {
371 mmappable_vector<int> vec;
372 int i;
373
374 fprintf(stderr, "Testing if wrong offset bug in pool is fixed.\n");
375 generate_test_file(2048, TESTFILE);
376 vec.mmap_file(TESTFILE, READ_ONLY, 4096, 1024);
377
378 for (i=0;i<1024;i++) {
379 assert(vec[i] == i+1024);
380 }
381 }
382
383 void test_private_file_pool(void)
384 {
385 mmappable_vector<int> vec;
386 mmappable_vector<int> vec2;
387 int i;
388
389 fprintf(stderr, "Testing if bypass_file_pool works.\n");
390 generate_test_file(1024, TESTFILE);
391 vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024, BYPASS_FILE_POOL);
392 for (i=0;i<1024;i++) {
393 assert(vec[i] == i);
394 }
395
396 vec2.mmap_file(TESTFILE, READ_ONLY, 0, 1024, BYPASS_FILE_POOL);
397 for (i=0;i<1024;i++) {
398 assert(vec[i] == i);
399 }
400 assert(&vec[0] != &vec2[0]);
401 }
402
403 #define FILESIZE (1024*1024*16)
404
405 void read_large_file(enum access_mode mode)
406 {
407 struct timeval t, t2;
408 mmappable_vector<int> vec;
409 int i;
410
411 gettimeofday(&t, NULL);
412
413 vec.mmap_file(TESTFILE, mode, 0, FILESIZE);
414 for (i=0;i<FILESIZE;i++) {
415 assert(vec[i] == i);
416 }
417 gettimeofday(&t2, NULL);
418 fprintf(stderr, "Mode: %d Time: %lu.%06lu\n", mode, (t2.tv_sec - t.tv_sec)-(t2.tv_usec < t.tv_usec), (t2.tv_usec < t.tv_usec)*1000000 + (t2.tv_usec - t.tv_usec));
419 }
420
421 void test_large_file(void)
422 {
423 fprintf(stderr, "Testing large file.\n");
424 generate_test_file(FILESIZE, TESTFILE); /* 1G */
425
426 read_large_file(READ_ONLY);
427 read_large_file(READ_WRITE_PRIVATE);
428 read_large_file(READ_WRITE_SHARED);
429 }
430
431 void test_multiple_open(void)
432 {
433 generate_test_file(1024, TESTFILE);
434 generate_test_file(1024, TESTFILE2);
435 mmappable_vector<int> vec1, vec2, vec3, vec4;
436
437 fprintf(stderr, "Testing multiple open (you need to strace this).\n");
438 vec1.mmap_file(TESTFILE, READ_ONLY, 0, 1024, MAP_WHOLE_FILE);
439 vec2.mmap_file(TESTFILE, READ_ONLY, 0, 1024, MAP_WHOLE_FILE);
440 vec3.mmap_file(TESTFILE2, READ_ONLY, 0, 1024, MAP_WHOLE_FILE);
441 vec4.mmap_file(TESTFILE2, READ_ONLY, 0, 1024, MAP_WHOLE_FILE);
442 }
443
444 void test_keep_forever(void)
445 {
446 generate_test_file(1024, TESTFILE);
447 mmappable_vector<int> vec1, vec2, vec3, vec4;
448
449 fprintf(stderr, "Testing multiple open (you need to strace this).\n");
450 {
451 mmappable_vector<int> vec;
452 fprintf(stderr, "Testing mapping without KEEP_FOREVER (you need to strace this: there should be a close).\n");
453 vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024, MAP_WHOLE_FILE);
454 }
455 {
456 mmappable_vector<int> vec;
457 fprintf(stderr, "Testing mapping without KEEP_FOREVER (you need to strace this: the file should be reopened).\n");
458 vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024, MAP_WHOLE_FILE);
459 }
460 {
461 mmappable_vector<int> vec;
462 fprintf(stderr, "Testing mapping with KEEP_FOREVER (you need to strace this: there should be NO close).\n");
463 vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024, MAP_WHOLE_FILE | KEEP_FOREVER);
464 }
465 {
466 mmappable_vector<int> vec;
467 fprintf(stderr, "Testing mapping with KEEP_FOREVER (you need to strace this: the file shouldn't be reopened).\n");
468 vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024, MAP_WHOLE_FILE | KEEP_FOREVER);
469 }
470 }
471
472
473 void test_allocate_0_bytes(void) /* shouldn't segfault */
474 {
475 fprintf(stderr, "Testing vectors of mmappable_vectors.\n");
476
477 vector<mmappable_vector<int> > vecs;
478 vecs.resize(2);
479 for (int i=0; i<2; i++) {
480 vecs[i].mmap_file(TESTFILE, READ_ONLY, 0, 1024, 0);
481 for (int j=0;j<1024;j++) {
482 assert(vecs[i][j] == j);
483 }
484 }
485 }
486
487
488 int main(int argc, char ** argv)
489 {
490 set_verbosity(1);
491
•492 //test_page_align_macros();
493 test_throw_catch();
494 test_exceptions();
495 test_mmap_file_pool();
496 test_mmap();
497 test_conversion();
498 test_cache_bug();
499 test_mapping_smaller_area();
500 test_mapping_smaller_area_whole_file_flag();
501 test_mapping_smaller_area_whole_file_flag_allocator();
502 test_mapping_smaller_area_whole_file_flag_allocator_deleting_first_vec();
503 test_new_interface();
504 test_private_file_pool();
505 test_large_file();
506 test_multiple_open();
507 test_keep_forever();
508 test_allocate_0_bytes();
509 }