1 #include "test/jemalloc_test.h" 2 3 #include "jemalloc/internal/pa.h" 4 5 static void * 6 alloc_hook(extent_hooks_t *extent_hooks, void *new_addr, size_t size, 7 size_t alignment, bool *zero, bool *commit, unsigned arena_ind) { 8 void *ret = pages_map(new_addr, size, alignment, commit); 9 return ret; 10 } 11 12 static bool 13 merge_hook(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a, 14 void *addr_b, size_t size_b, bool committed, unsigned arena_ind) { 15 return !maps_coalesce; 16 } 17 18 static bool 19 split_hook(extent_hooks_t *extent_hooks, void *addr, size_t size, 20 size_t size_a, size_t size_b, bool committed, unsigned arena_ind) { 21 return !maps_coalesce; 22 } 23 24 static void 25 init_test_extent_hooks(extent_hooks_t *hooks) { 26 /* 27 * The default hooks are mostly fine for testing. A few of them, 28 * though, access globals (alloc for dss setting in an arena, split and 29 * merge touch the global emap to find head state. The first of these 30 * can be fixed by keeping that state with the hooks, where it logically 31 * belongs. The second, though, we can only fix when we use the extent 32 * hook API. 33 */ 34 memcpy(hooks, &ehooks_default_extent_hooks, sizeof(extent_hooks_t)); 35 hooks->alloc = &alloc_hook; 36 hooks->merge = &merge_hook; 37 hooks->split = &split_hook; 38 } 39 40 typedef struct test_data_s test_data_t; 41 struct test_data_s { 42 pa_shard_t shard; 43 pa_central_t central; 44 base_t *base; 45 emap_t emap; 46 pa_shard_stats_t stats; 47 malloc_mutex_t stats_mtx; 48 extent_hooks_t hooks; 49 }; 50 51 test_data_t *init_test_data(ssize_t dirty_decay_ms, ssize_t muzzy_decay_ms) { 52 test_data_t *test_data = calloc(1, sizeof(test_data_t)); 53 assert_ptr_not_null(test_data, ""); 54 init_test_extent_hooks(&test_data->hooks); 55 56 base_t *base = base_new(TSDN_NULL, /* ind */ 1, &test_data->hooks, 57 /* metadata_use_hooks */ true); 58 assert_ptr_not_null(base, ""); 59 60 test_data->base = base; 61 bool err = emap_init(&test_data->emap, test_data->base, 62 /* zeroed */ true); 63 assert_false(err, ""); 64 65 nstime_t time; 66 nstime_init(&time, 0); 67 68 err = pa_central_init(&test_data->central, base, opt_hpa, 69 &hpa_hooks_default); 70 assert_false(err, ""); 71 72 const size_t pa_oversize_threshold = 8 * 1024 * 1024; 73 err = pa_shard_init(TSDN_NULL, &test_data->shard, &test_data->central, 74 &test_data->emap, test_data->base, /* ind */ 1, &test_data->stats, 75 &test_data->stats_mtx, &time, pa_oversize_threshold, dirty_decay_ms, 76 muzzy_decay_ms); 77 assert_false(err, ""); 78 79 return test_data; 80 } 81 82 void destroy_test_data(test_data_t *data) { 83 base_delete(TSDN_NULL, data->base); 84 free(data); 85 } 86 87 static void * 88 do_alloc_free_purge(void *arg) { 89 test_data_t *test_data = (test_data_t *)arg; 90 for (int i = 0; i < 10 * 1000; i++) { 91 bool deferred_work_generated = false; 92 edata_t *edata = pa_alloc(TSDN_NULL, &test_data->shard, PAGE, 93 PAGE, /* slab */ false, /* szind */ 0, /* zero */ false, 94 /* guarded */ false, &deferred_work_generated); 95 assert_ptr_not_null(edata, ""); 96 pa_dalloc(TSDN_NULL, &test_data->shard, edata, 97 &deferred_work_generated); 98 malloc_mutex_lock(TSDN_NULL, 99 &test_data->shard.pac.decay_dirty.mtx); 100 pac_decay_all(TSDN_NULL, &test_data->shard.pac, 101 &test_data->shard.pac.decay_dirty, 102 &test_data->shard.pac.stats->decay_dirty, 103 &test_data->shard.pac.ecache_dirty, true); 104 malloc_mutex_unlock(TSDN_NULL, 105 &test_data->shard.pac.decay_dirty.mtx); 106 } 107 return NULL; 108 } 109 110 TEST_BEGIN(test_alloc_free_purge_thds) { 111 test_data_t *test_data = init_test_data(0, 0); 112 thd_t thds[4]; 113 for (int i = 0; i < 4; i++) { 114 thd_create(&thds[i], do_alloc_free_purge, test_data); 115 } 116 for (int i = 0; i < 4; i++) { 117 thd_join(thds[i], NULL); 118 } 119 } 120 TEST_END 121 122 int 123 main(void) { 124 return test( 125 test_alloc_free_purge_thds); 126 } 127