#include "mmap_allocator.h" #include "mmap_exception.h" #include "mmappable_vector.h" #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace mmap_allocator_namespace; #define TESTFILE "testfile" #define TESTFILE2 "testfile2" void generate_test_file(int count, const char *fname) { FILE *f; int i; f = fopen(fname, "w+"); for (i=0;i > int_vec(1024, 0, mmap_allocator("", READ_ONLY)); /* Default constructor used, allocate will fail */ assert(0); } catch (mmap_allocator_exception e) { fprintf(stderr, "Exception message (expected): %s\n", e.what()); exception_thrown = true; } assert(exception_thrown); exception_thrown = false; try { vector > int_vec_notexsting_file(1024, 0, mmap_allocator("karin", READ_ONLY)); /* no such file or directory */ assert(0); } catch (mmap_allocator_exception e) { fprintf(stderr, "Exception message (expected): %s\n", e.what()); exception_thrown = true; } assert(exception_thrown); exception_thrown = false; try { vector > int_vec_wrong_alignment_file(512, 0, mmap_allocator(TESTFILE, READ_WRITE_PRIVATE, 123)); /* wrong alignment */ /* No exception here expected */ } catch (mmap_allocator_exception &e) { fprintf(stderr, "Exception message (not expected): %s\n", e.what()); exception_thrown = true; } assert(!exception_thrown); } void test_mmap(void) { int i; fprintf(stderr, "Testing int_vec_default\n"); generate_test_file(1024, TESTFILE); mmappable_vector > int_vec_default = mmappable_vector >(mmap_allocator(TESTFILE, DEFAULT_STL_ALLOCATOR, 0)); int_vec_default.mmap_file(1024); assert(int_vec_default.size() == 1024); for (i=0;i<1024;i++) { int_vec_default[i] = i; /* no segfault */ assert(int_vec_default[i] == i); /* just to be sure */ } test_test_file(1024, false); fprintf(stderr, "Testing int_vec_rw_private\n"); generate_test_file(1024, TESTFILE); mmappable_vector > int_vec_rw_private = mmappable_vector >(mmap_allocator(TESTFILE, READ_WRITE_PRIVATE, 0)); int_vec_rw_private.mmap_file(1024); for (i=0;i<1024;i++) { assert(int_vec_rw_private[i] == i); } test_test_file(1024, false); fprintf(stderr, "Testing int_vec_ro\n"); mmappable_vector > int_vec_ro = mmappable_vector >(mmap_allocator(TESTFILE, READ_ONLY, 0)); int_vec_ro.mmap_file(1024); for (i=0;i<1024;i++) { assert(int_vec_ro[i] == i); } test_test_file(1024, false); fprintf(stderr, "Testing int_vec_shifted\n"); mmappable_vector > int_vec_shifted = mmappable_vector >(mmap_allocator(TESTFILE, READ_ONLY, sizeof(int))); int_vec_shifted.mmap_file(1024-1); for (i=0;i<1024-1;i++) { assert(int_vec_shifted[i] == i+1); } test_test_file(1024, false); fprintf(stderr, "Testing int_vec_pointer\n"); mmappable_vector > *int_vec_pointer = new mmappable_vector >(mmap_allocator(TESTFILE, READ_ONLY, 0)); int_vec_pointer->mmap_file(1024); for (i=0;i<1024;i++) { assert((*int_vec_pointer)[i] == i); } test_test_file(1024, false); delete int_vec_pointer; fprintf(stderr, "Testing int_vec_initialized_private\n"); mmappable_vector > int_vec_initialized_private = mmappable_vector >(1024, 0, mmap_allocator(TESTFILE, READ_WRITE_PRIVATE, 0)); for (i=0;i<1024;i++) { assert(int_vec_initialized_private[i] == 0); } test_test_file(1024, false); fprintf(stderr, "Testing int_vec_initialized_shared\n"); mmappable_vector > int_vec_initialized_shared = mmappable_vector >(1024, 0, mmap_allocator(TESTFILE, READ_WRITE_SHARED, 0)); for (i=0;i<1024;i++) { assert(int_vec_initialized_shared[i] == 0); } test_test_file(1024, true); fprintf(stderr, "Testing int_vec_big\n"); generate_test_file(1024*1024, TESTFILE); mmappable_vector > int_vec_big = mmappable_vector >(mmap_allocator(TESTFILE, READ_ONLY, 0, MAP_WHOLE_FILE | ALLOW_REMAP)); int_vec_big.mmap_file(1024*1024); for (i=0;i<1024*1024;i++) { if (int_vec_big[i] != i) { fprintf(stderr, "falsch: i=%d val=%d\n", i, int_vec_big[i]); } assert(int_vec_big[i] == i); } test_test_file(1024*1024, false); fprintf(stderr, "Testing int_vec_shifted_big\n"); generate_test_file(1024*1024, TESTFILE); mmappable_vector > int_vec_shifted_big = mmappable_vector >(mmap_allocator(TESTFILE, READ_ONLY, sizeof(i), MAP_WHOLE_FILE | ALLOW_REMAP)); int_vec_shifted_big.mmap_file(1024*1024-1); for (i=0;i<1024*1024-1;i++) { assert(int_vec_shifted_big[i] == i+1); } test_test_file(1024*1024, false); fprintf(stderr, "Testing int_vec_big_minus_one\n"); generate_test_file(1024*1024-1, TESTFILE); mmappable_vector > int_vec_big_minus_one = mmappable_vector >(mmap_allocator(TESTFILE, READ_ONLY, 0, MAP_WHOLE_FILE | ALLOW_REMAP)); int_vec_big_minus_one.mmap_file(1024*1024-1); for (i=0;i<1024*1024-1;i++) { assert(int_vec_big_minus_one[i] == i); } test_test_file(1024*1024-1, false); } void test_conversion(void) { mmappable_vector mmap_vector; vector std_vector; mmappable_vector mmap_vector2; int i; fprintf(stderr, "Testing conversion between STL vector and mmap vector.\n"); generate_test_file(1024, TESTFILE); mmap_vector.mmap_file(TESTFILE, READ_ONLY, 0, 1024); for (i=0;i<1024;i++) { assert(mmap_vector[i] == i); } std_vector = to_std_vector(mmap_vector); for (i=0;i<1024;i++) { assert(std_vector[i] == i); } for (i=0;i<1024;i++) { std_vector[i] *= 2; } mmap_vector2 = to_mmappable_vector(std_vector); for (i=0;i<1024;i++) { assert(mmap_vector2[i] == i*2); } } void test_mmap_file_pool(void) { generate_test_file(1024, TESTFILE); int *f = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 0, 1024, false, false); int *f2 = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 0, 1024, false, false); int i; assert(f == f2); for (i=0;i<1024;i++) { assert(f[i] == i); } the_pool.munmap_file(string(TESTFILE), READ_ONLY, 0, 1024); the_pool.munmap_file(string(TESTFILE), READ_ONLY, 0, 1024); } void test_mapping_smaller_area(void) { fprintf(stderr, "Testing mapping of areas that fit in already mapped areas\n"); generate_test_file(2048, TESTFILE); int *f = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 0, 8192, false, false); int *first_page = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 0, 4096, false, false); int *second_page = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 4096, 4096, false, false); assert(f == first_page); assert(f+1024 == second_page); the_pool.munmap_file(string(TESTFILE), READ_ONLY, 0, 8192); the_pool.munmap_file(string(TESTFILE), READ_ONLY, 0, 4096); the_pool.munmap_file(string(TESTFILE), READ_ONLY, 4096, 4096); } void test_mapping_smaller_area_whole_file_flag(void) { fprintf(stderr, "Testing whole file flag\n"); generate_test_file(2048, TESTFILE); int *f = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 0, 1, true, false); int *first_page = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 0, 4096, false, false); int *second_page = (int*)the_pool.mmap_file(string(TESTFILE), READ_ONLY, 4096, 4096, false, false); assert(f == first_page); assert(f+1024 == second_page); the_pool.munmap_file(string(TESTFILE), READ_ONLY, 0, 8192); the_pool.munmap_file(string(TESTFILE), READ_ONLY, 0, 4096); the_pool.munmap_file(string(TESTFILE), READ_ONLY, 4096, 4096); } void test_mapping_smaller_area_whole_file_flag_allocator(void) { int i; fprintf(stderr, "Testing whole file flag via allocator\n"); generate_test_file(2048, TESTFILE); fprintf(stderr, "Testing int_vec_ro\n"); mmappable_vector > int_vec_ro = mmappable_vector >(mmap_allocator(TESTFILE, READ_ONLY, 0, MAP_WHOLE_FILE)); int_vec_ro.mmap_file(1024); for (i=0;i<1024;i++) { assert(int_vec_ro[i] == i); } mmappable_vector > int_vec_ro_second_page = mmappable_vector >(mmap_allocator(TESTFILE, READ_ONLY, 4096, MAP_WHOLE_FILE)); int_vec_ro_second_page.mmap_file(1024); for (i=0;i<1024;i++) { assert(int_vec_ro_second_page[i] == i+1024); } } void test_mapping_smaller_area_whole_file_flag_allocator_deleting_first_vec(void) { int i; fprintf(stderr, "Testing whole file flag via allocator, deleting first mapping before allocating second page\n"); generate_test_file(2048, TESTFILE); fprintf(stderr, "Testing int_vec_ro\n"); mmappable_vector > *int_vec_ro_p = new mmappable_vector >(mmap_allocator(TESTFILE, READ_ONLY, 0, MAP_WHOLE_FILE)); int_vec_ro_p->mmap_file(1024); for (i=0;i<1024;i++) { assert((*int_vec_ro_p)[i] == i); } delete int_vec_ro_p; mmappable_vector > *int_vec_ro_second_page_p = new mmappable_vector >(mmap_allocator(TESTFILE, READ_ONLY, 4096, MAP_WHOLE_FILE)); int_vec_ro_second_page_p->mmap_file(1024); for (i=0;i<1024;i++) { assert((*int_vec_ro_second_page_p)[i] == i+1024); } delete int_vec_ro_second_page_p; } void test_new_interface(void) { int i; fprintf(stderr, "Testing new interface\n"); generate_test_file(1024, TESTFILE); mmappable_vector vec; vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024); for (i=0;i<1024;i++) { assert(vec[i] == i); } try { vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024); assert(0); } catch (mmap_allocator_exception e) { fprintf(stderr, "Exception message (expected): %s\n", e.what()); } vec.munmap_file(); generate_test_file(2048, TESTFILE); vec.mmap_file(TESTFILE, READ_ONLY, 4096, 1024); for (i=0;i<1024;i++) { assert(vec[i] == i+1024); } } void test_cache_bug(void) { mmappable_vector vec; int i; fprintf(stderr, "Testing if wrong offset bug in pool is fixed.\n"); generate_test_file(2048, TESTFILE); vec.mmap_file(TESTFILE, READ_ONLY, 4096, 1024); for (i=0;i<1024;i++) { assert(vec[i] == i+1024); } } void test_private_file_pool(void) { mmappable_vector vec; mmappable_vector vec2; int i; fprintf(stderr, "Testing if bypass_file_pool works.\n"); generate_test_file(1024, TESTFILE); vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024, BYPASS_FILE_POOL); for (i=0;i<1024;i++) { assert(vec[i] == i); } vec2.mmap_file(TESTFILE, READ_ONLY, 0, 1024, BYPASS_FILE_POOL); for (i=0;i<1024;i++) { assert(vec[i] == i); } assert(&vec[0] != &vec2[0]); } #define FILESIZE (1024*1024*16) void read_large_file(enum access_mode mode) { struct timeval t, t2; mmappable_vector vec; int i; gettimeofday(&t, NULL); vec.mmap_file(TESTFILE, mode, 0, FILESIZE); for (i=0;i vec1, vec2, vec3, vec4; fprintf(stderr, "Testing multiple open (you need to strace this).\n"); vec1.mmap_file(TESTFILE, READ_ONLY, 0, 1024, MAP_WHOLE_FILE); vec2.mmap_file(TESTFILE, READ_ONLY, 0, 1024, MAP_WHOLE_FILE); vec3.mmap_file(TESTFILE2, READ_ONLY, 0, 1024, MAP_WHOLE_FILE); vec4.mmap_file(TESTFILE2, READ_ONLY, 0, 1024, MAP_WHOLE_FILE); } void test_keep_forever(void) { generate_test_file(1024, TESTFILE); mmappable_vector vec1, vec2, vec3, vec4; fprintf(stderr, "Testing multiple open (you need to strace this).\n"); { mmappable_vector vec; fprintf(stderr, "Testing mapping without KEEP_FOREVER (you need to strace this: there should be a close).\n"); vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024, MAP_WHOLE_FILE); } { mmappable_vector vec; fprintf(stderr, "Testing mapping without KEEP_FOREVER (you need to strace this: the file should be reopened).\n"); vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024, MAP_WHOLE_FILE); } { mmappable_vector vec; fprintf(stderr, "Testing mapping with KEEP_FOREVER (you need to strace this: there should be NO close).\n"); vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024, MAP_WHOLE_FILE | KEEP_FOREVER); } { mmappable_vector vec; fprintf(stderr, "Testing mapping with KEEP_FOREVER (you need to strace this: the file shouldn't be reopened).\n"); vec.mmap_file(TESTFILE, READ_ONLY, 0, 1024, MAP_WHOLE_FILE | KEEP_FOREVER); } } void test_allocate_0_bytes(void) /* shouldn't segfault */ { fprintf(stderr, "Testing vectors of mmappable_vectors.\n"); vector > vecs; vecs.resize(2); for (int i=0; i<2; i++) { vecs[i].mmap_file(TESTFILE, READ_ONLY, 0, 1024, 0); for (int j=0;j<1024;j++) { assert(vecs[i][j] == j); } } } int main(int argc, char ** argv) { set_verbosity(1); //test_page_align_macros(); test_throw_catch(); test_exceptions(); test_mmap_file_pool(); test_mmap(); test_conversion(); test_cache_bug(); test_mapping_smaller_area(); test_mapping_smaller_area_whole_file_flag(); test_mapping_smaller_area_whole_file_flag_allocator(); test_mapping_smaller_area_whole_file_flag_allocator_deleting_first_vec(); test_new_interface(); test_private_file_pool(); test_large_file(); test_multiple_open(); test_keep_forever(); test_allocate_0_bytes(); }