Line data Source code
1 1 : /** 2 : * @file test/mm/buddy/buddy_test.c 3 : * 4 : * @brief Test: rollbackable buddy system allocator 5 : * 6 : * A test of the buddy system allocator used to handle model's memory 7 : * 8 : * SPDX-FileCopyrightText: 2008-2021 HPDCS Group <rootsim@googlegroups.com> 9 : * SPDX-License-Identifier: GPL-3.0-only 10 : */ 11 : #include <test.h> 12 : #include <test_rng.h> 13 : 14 : #include <lp/lp.h> 15 : #include <mm/model_allocator.h> 16 : 17 : #include <stdlib.h> 18 : 19 0 : static __thread test_rng_state rng_state; 20 : 21 0 : __thread struct lp_ctx *current_lp; // needed by the model allocator 22 0 : __thread simtime_t current_gvt; // needed by the model allocator 23 : 24 0 : static int block_size_test(unsigned b_exp) 25 : { 26 : unsigned errs = 0; 27 : unsigned block_size = 1 << b_exp; 28 : unsigned allocations_cnt = 1 << (B_TOTAL_EXP - b_exp); 29 : test_rng_state rng_snap_a = rng_state; 30 : uint64_t **allocations = malloc(allocations_cnt * sizeof(uint64_t *)); 31 : 32 : for (unsigned i = 0; i < allocations_cnt; ++i) { 33 : allocations[i] = malloc_mt(block_size); 34 : 35 : if (allocations[i] == NULL) { 36 : ++errs; 37 : continue; 38 : } 39 : 40 : for (unsigned j = 0; j < block_size / sizeof(uint64_t); ++j) { 41 : allocations[i][j] = lcg_random_u(rng_state); 42 : } 43 : } 44 : 45 : errs += malloc_mt(block_size) != NULL; 46 : 47 : model_allocator_checkpoint_take(0); 48 : test_rng_state rng_snap_b = rng_state; 49 : 50 : for (unsigned i = 0; i < allocations_cnt; ++i) { 51 : for (unsigned j = 0; j < block_size / sizeof(uint64_t); ++j) { 52 : allocations[i][j] = lcg_random_u(rng_state); 53 : } 54 : } 55 : 56 : errs += malloc_mt(block_size) != NULL; 57 : 58 : model_allocator_checkpoint_take(B_LOG_FREQUENCY); 59 : 60 : 61 : for (unsigned i = 0; i < allocations_cnt; ++i) { 62 : for (unsigned j = 0; j < block_size / sizeof(uint64_t); ++j) { 63 : allocations[i][j] = lcg_random_u(rng_state); 64 : } 65 : } 66 : 67 : errs += malloc_mt(block_size) != NULL; 68 : 69 : model_allocator_checkpoint_take(B_LOG_FREQUENCY * 2 - 1); 70 : model_allocator_checkpoint_take(B_LOG_FREQUENCY * 2); 71 : 72 : model_allocator_checkpoint_restore(B_LOG_FREQUENCY); 73 : 74 : for (unsigned i = 0; i < allocations_cnt; ++i) { 75 : for (unsigned j = 0; j < block_size / sizeof(uint64_t); ++j) { 76 : errs += allocations[i][j] != lcg_random_u(rng_snap_b); 77 : } 78 : 79 : free_mt(allocations[i]); 80 : } 81 : 82 : model_allocator_checkpoint_restore(0); 83 : 84 : for (unsigned i = 0; i < allocations_cnt; ++i) { 85 : for (unsigned j = 0; j < block_size / sizeof(uint64_t); ++j) { 86 : errs += allocations[i][j] != lcg_random_u(rng_snap_a); 87 : } 88 : 89 : free_mt(allocations[i]); 90 : } 91 : 92 : free(allocations); 93 : return -(!!errs); 94 : } 95 : 96 0 : static int model_allocator_test(void) 97 : { 98 : current_lp = malloc(sizeof(*current_lp)); 99 : model_allocator_lp_init(); 100 : lcg_init(rng_state, (rid + 1) * 1713); 101 : 102 : unsigned errs = 0; 103 : for (unsigned j = B_BLOCK_EXP; j < B_TOTAL_EXP; ++j) { 104 : errs += block_size_test(j) < 0; 105 : } 106 : 107 : errs += malloc_mt(0) != NULL; 108 : errs += calloc_mt(0, sizeof(uint64_t)) != NULL; 109 : 110 : free_mt(NULL); 111 : 112 : uint64_t *mem = calloc_mt(1, sizeof(uint64_t)); 113 : errs += *mem; 114 : free_mt(mem); 115 : 116 : model_allocator_lp_fini(); 117 : free(current_lp); 118 : return -(!!errs); 119 : } 120 : 121 : const struct test_config test_config = { 122 : .threads_count = 4, 123 : .test_fnc = model_allocator_test 124 : }; 125 :