Home | History | Annotate | Line # | Download | only in unit
      1      1.1  christos #include "test/jemalloc_test.h"
      2      1.1  christos 
      3      1.1  christos #include "test/extent_hooks.h"
      4      1.1  christos 
      5      1.1  christos static extent_hooks_t hooks_null = {
      6      1.1  christos 	extent_alloc_hook,
      7      1.1  christos 	NULL, /* dalloc */
      8      1.1  christos 	NULL, /* destroy */
      9      1.1  christos 	NULL, /* commit */
     10      1.1  christos 	NULL, /* decommit */
     11      1.1  christos 	NULL, /* purge_lazy */
     12      1.1  christos 	NULL, /* purge_forced */
     13      1.1  christos 	NULL, /* split */
     14      1.1  christos 	NULL /* merge */
     15      1.1  christos };
     16      1.1  christos 
     17      1.1  christos static extent_hooks_t hooks_not_null = {
     18      1.1  christos 	extent_alloc_hook,
     19      1.1  christos 	extent_dalloc_hook,
     20      1.1  christos 	extent_destroy_hook,
     21      1.1  christos 	NULL, /* commit */
     22      1.1  christos 	extent_decommit_hook,
     23      1.1  christos 	extent_purge_lazy_hook,
     24      1.1  christos 	extent_purge_forced_hook,
     25      1.1  christos 	NULL, /* split */
     26      1.1  christos 	NULL /* merge */
     27      1.1  christos };
     28      1.1  christos 
     29      1.1  christos TEST_BEGIN(test_base_hooks_default) {
     30      1.1  christos 	base_t *base;
     31      1.1  christos 	size_t allocated0, allocated1, resident, mapped, n_thp;
     32      1.1  christos 
     33      1.1  christos 	tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
     34  1.1.1.2  christos 	base = base_new(tsdn, 0,
     35  1.1.1.2  christos 	    (extent_hooks_t *)&ehooks_default_extent_hooks,
     36  1.1.1.2  christos 	    /* metadata_use_hooks */ true);
     37      1.1  christos 
     38      1.1  christos 	if (config_stats) {
     39      1.1  christos 		base_stats_get(tsdn, base, &allocated0, &resident, &mapped,
     40      1.1  christos 		    &n_thp);
     41  1.1.1.2  christos 		expect_zu_ge(allocated0, sizeof(base_t),
     42      1.1  christos 		    "Base header should count as allocated");
     43      1.1  christos 		if (opt_metadata_thp == metadata_thp_always) {
     44  1.1.1.2  christos 			expect_zu_gt(n_thp, 0,
     45      1.1  christos 			    "Base should have 1 THP at least.");
     46      1.1  christos 		}
     47      1.1  christos 	}
     48      1.1  christos 
     49  1.1.1.2  christos 	expect_ptr_not_null(base_alloc(tsdn, base, 42, 1),
     50      1.1  christos 	    "Unexpected base_alloc() failure");
     51      1.1  christos 
     52      1.1  christos 	if (config_stats) {
     53      1.1  christos 		base_stats_get(tsdn, base, &allocated1, &resident, &mapped,
     54      1.1  christos 		    &n_thp);
     55  1.1.1.2  christos 		expect_zu_ge(allocated1 - allocated0, 42,
     56      1.1  christos 		    "At least 42 bytes were allocated by base_alloc()");
     57      1.1  christos 	}
     58      1.1  christos 
     59      1.1  christos 	base_delete(tsdn, base);
     60      1.1  christos }
     61      1.1  christos TEST_END
     62      1.1  christos 
     63      1.1  christos TEST_BEGIN(test_base_hooks_null) {
     64      1.1  christos 	extent_hooks_t hooks_orig;
     65      1.1  christos 	base_t *base;
     66      1.1  christos 	size_t allocated0, allocated1, resident, mapped, n_thp;
     67      1.1  christos 
     68      1.1  christos 	extent_hooks_prep();
     69      1.1  christos 	try_dalloc = false;
     70      1.1  christos 	try_destroy = true;
     71      1.1  christos 	try_decommit = false;
     72      1.1  christos 	try_purge_lazy = false;
     73      1.1  christos 	try_purge_forced = false;
     74      1.1  christos 	memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
     75      1.1  christos 	memcpy(&hooks, &hooks_null, sizeof(extent_hooks_t));
     76      1.1  christos 
     77      1.1  christos 	tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
     78  1.1.1.2  christos 	base = base_new(tsdn, 0, &hooks, /* metadata_use_hooks */ true);
     79  1.1.1.2  christos 	expect_ptr_not_null(base, "Unexpected base_new() failure");
     80      1.1  christos 
     81      1.1  christos 	if (config_stats) {
     82      1.1  christos 		base_stats_get(tsdn, base, &allocated0, &resident, &mapped,
     83      1.1  christos 		    &n_thp);
     84  1.1.1.2  christos 		expect_zu_ge(allocated0, sizeof(base_t),
     85      1.1  christos 		    "Base header should count as allocated");
     86      1.1  christos 		if (opt_metadata_thp == metadata_thp_always) {
     87  1.1.1.2  christos 			expect_zu_gt(n_thp, 0,
     88      1.1  christos 			    "Base should have 1 THP at least.");
     89      1.1  christos 		}
     90      1.1  christos 	}
     91      1.1  christos 
     92  1.1.1.2  christos 	expect_ptr_not_null(base_alloc(tsdn, base, 42, 1),
     93      1.1  christos 	    "Unexpected base_alloc() failure");
     94      1.1  christos 
     95      1.1  christos 	if (config_stats) {
     96      1.1  christos 		base_stats_get(tsdn, base, &allocated1, &resident, &mapped,
     97      1.1  christos 		    &n_thp);
     98  1.1.1.2  christos 		expect_zu_ge(allocated1 - allocated0, 42,
     99      1.1  christos 		    "At least 42 bytes were allocated by base_alloc()");
    100      1.1  christos 	}
    101      1.1  christos 
    102      1.1  christos 	base_delete(tsdn, base);
    103      1.1  christos 
    104      1.1  christos 	memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
    105      1.1  christos }
    106      1.1  christos TEST_END
    107      1.1  christos 
    108      1.1  christos TEST_BEGIN(test_base_hooks_not_null) {
    109      1.1  christos 	extent_hooks_t hooks_orig;
    110      1.1  christos 	base_t *base;
    111      1.1  christos 	void *p, *q, *r, *r_exp;
    112      1.1  christos 
    113      1.1  christos 	extent_hooks_prep();
    114      1.1  christos 	try_dalloc = false;
    115      1.1  christos 	try_destroy = true;
    116      1.1  christos 	try_decommit = false;
    117      1.1  christos 	try_purge_lazy = false;
    118      1.1  christos 	try_purge_forced = false;
    119      1.1  christos 	memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
    120      1.1  christos 	memcpy(&hooks, &hooks_not_null, sizeof(extent_hooks_t));
    121      1.1  christos 
    122      1.1  christos 	tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
    123      1.1  christos 	did_alloc = false;
    124  1.1.1.2  christos 	base = base_new(tsdn, 0, &hooks, /* metadata_use_hooks */ true);
    125  1.1.1.2  christos 	expect_ptr_not_null(base, "Unexpected base_new() failure");
    126  1.1.1.2  christos 	expect_true(did_alloc, "Expected alloc");
    127      1.1  christos 
    128      1.1  christos 	/*
    129      1.1  christos 	 * Check for tight packing at specified alignment under simple
    130      1.1  christos 	 * conditions.
    131      1.1  christos 	 */
    132      1.1  christos 	{
    133      1.1  christos 		const size_t alignments[] = {
    134      1.1  christos 			1,
    135      1.1  christos 			QUANTUM,
    136      1.1  christos 			QUANTUM << 1,
    137      1.1  christos 			CACHELINE,
    138      1.1  christos 			CACHELINE << 1,
    139      1.1  christos 		};
    140      1.1  christos 		unsigned i;
    141      1.1  christos 
    142      1.1  christos 		for (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {
    143      1.1  christos 			size_t alignment = alignments[i];
    144      1.1  christos 			size_t align_ceil = ALIGNMENT_CEILING(alignment,
    145      1.1  christos 			    QUANTUM);
    146      1.1  christos 			p = base_alloc(tsdn, base, 1, alignment);
    147  1.1.1.2  christos 			expect_ptr_not_null(p,
    148      1.1  christos 			    "Unexpected base_alloc() failure");
    149  1.1.1.2  christos 			expect_ptr_eq(p,
    150      1.1  christos 			    (void *)(ALIGNMENT_CEILING((uintptr_t)p,
    151      1.1  christos 			    alignment)), "Expected quantum alignment");
    152      1.1  christos 			q = base_alloc(tsdn, base, alignment, alignment);
    153  1.1.1.2  christos 			expect_ptr_not_null(q,
    154      1.1  christos 			    "Unexpected base_alloc() failure");
    155  1.1.1.2  christos 			expect_ptr_eq((void *)((uintptr_t)p + align_ceil), q,
    156      1.1  christos 			    "Minimal allocation should take up %zu bytes",
    157      1.1  christos 			    align_ceil);
    158      1.1  christos 			r = base_alloc(tsdn, base, 1, alignment);
    159  1.1.1.2  christos 			expect_ptr_not_null(r,
    160      1.1  christos 			    "Unexpected base_alloc() failure");
    161  1.1.1.2  christos 			expect_ptr_eq((void *)((uintptr_t)q + align_ceil), r,
    162      1.1  christos 			    "Minimal allocation should take up %zu bytes",
    163      1.1  christos 			    align_ceil);
    164      1.1  christos 		}
    165      1.1  christos 	}
    166      1.1  christos 
    167      1.1  christos 	/*
    168      1.1  christos 	 * Allocate an object that cannot fit in the first block, then verify
    169      1.1  christos 	 * that the first block's remaining space is considered for subsequent
    170      1.1  christos 	 * allocation.
    171      1.1  christos 	 */
    172  1.1.1.2  christos 	expect_zu_ge(edata_bsize_get(&base->blocks->edata), QUANTUM,
    173      1.1  christos 	    "Remainder insufficient for test");
    174      1.1  christos 	/* Use up all but one quantum of block. */
    175  1.1.1.2  christos 	while (edata_bsize_get(&base->blocks->edata) > QUANTUM) {
    176      1.1  christos 		p = base_alloc(tsdn, base, QUANTUM, QUANTUM);
    177  1.1.1.2  christos 		expect_ptr_not_null(p, "Unexpected base_alloc() failure");
    178      1.1  christos 	}
    179  1.1.1.2  christos 	r_exp = edata_addr_get(&base->blocks->edata);
    180  1.1.1.2  christos 	expect_zu_eq(base->extent_sn_next, 1, "One extant block expected");
    181      1.1  christos 	q = base_alloc(tsdn, base, QUANTUM + 1, QUANTUM);
    182  1.1.1.2  christos 	expect_ptr_not_null(q, "Unexpected base_alloc() failure");
    183  1.1.1.2  christos 	expect_ptr_ne(q, r_exp, "Expected allocation from new block");
    184  1.1.1.2  christos 	expect_zu_eq(base->extent_sn_next, 2, "Two extant blocks expected");
    185      1.1  christos 	r = base_alloc(tsdn, base, QUANTUM, QUANTUM);
    186  1.1.1.2  christos 	expect_ptr_not_null(r, "Unexpected base_alloc() failure");
    187  1.1.1.2  christos 	expect_ptr_eq(r, r_exp, "Expected allocation from first block");
    188  1.1.1.2  christos 	expect_zu_eq(base->extent_sn_next, 2, "Two extant blocks expected");
    189      1.1  christos 
    190      1.1  christos 	/*
    191      1.1  christos 	 * Check for proper alignment support when normal blocks are too small.
    192      1.1  christos 	 */
    193      1.1  christos 	{
    194      1.1  christos 		const size_t alignments[] = {
    195      1.1  christos 			HUGEPAGE,
    196      1.1  christos 			HUGEPAGE << 1
    197      1.1  christos 		};
    198      1.1  christos 		unsigned i;
    199      1.1  christos 
    200      1.1  christos 		for (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {
    201      1.1  christos 			size_t alignment = alignments[i];
    202      1.1  christos 			p = base_alloc(tsdn, base, QUANTUM, alignment);
    203  1.1.1.2  christos 			expect_ptr_not_null(p,
    204      1.1  christos 			    "Unexpected base_alloc() failure");
    205  1.1.1.2  christos 			expect_ptr_eq(p,
    206      1.1  christos 			    (void *)(ALIGNMENT_CEILING((uintptr_t)p,
    207      1.1  christos 			    alignment)), "Expected %zu-byte alignment",
    208      1.1  christos 			    alignment);
    209      1.1  christos 		}
    210      1.1  christos 	}
    211      1.1  christos 
    212      1.1  christos 	called_dalloc = called_destroy = called_decommit = called_purge_lazy =
    213      1.1  christos 	    called_purge_forced = false;
    214      1.1  christos 	base_delete(tsdn, base);
    215  1.1.1.2  christos 	expect_true(called_dalloc, "Expected dalloc call");
    216  1.1.1.2  christos 	expect_true(!called_destroy, "Unexpected destroy call");
    217  1.1.1.2  christos 	expect_true(called_decommit, "Expected decommit call");
    218  1.1.1.2  christos 	expect_true(called_purge_lazy, "Expected purge_lazy call");
    219  1.1.1.2  christos 	expect_true(called_purge_forced, "Expected purge_forced call");
    220      1.1  christos 
    221      1.1  christos 	try_dalloc = true;
    222      1.1  christos 	try_destroy = true;
    223      1.1  christos 	try_decommit = true;
    224      1.1  christos 	try_purge_lazy = true;
    225      1.1  christos 	try_purge_forced = true;
    226      1.1  christos 	memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
    227      1.1  christos }
    228      1.1  christos TEST_END
    229      1.1  christos 
    230  1.1.1.2  christos TEST_BEGIN(test_base_ehooks_get_for_metadata_default_hook) {
    231  1.1.1.2  christos 	extent_hooks_prep();
    232  1.1.1.2  christos 	memcpy(&hooks, &hooks_not_null, sizeof(extent_hooks_t));
    233  1.1.1.2  christos 	base_t *base;
    234  1.1.1.2  christos 	tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
    235  1.1.1.2  christos 	base = base_new(tsdn, 0, &hooks, /* metadata_use_hooks */ false);
    236  1.1.1.2  christos 	ehooks_t *ehooks = base_ehooks_get_for_metadata(base);
    237  1.1.1.2  christos 	expect_true(ehooks_are_default(ehooks),
    238  1.1.1.2  christos 		"Expected default extent hook functions pointer");
    239  1.1.1.2  christos 	base_delete(tsdn, base);
    240  1.1.1.2  christos }
    241  1.1.1.2  christos TEST_END
    242  1.1.1.2  christos 
    243  1.1.1.2  christos 
    244  1.1.1.2  christos TEST_BEGIN(test_base_ehooks_get_for_metadata_custom_hook) {
    245  1.1.1.2  christos 	extent_hooks_prep();
    246  1.1.1.2  christos 	memcpy(&hooks, &hooks_not_null, sizeof(extent_hooks_t));
    247  1.1.1.2  christos 	base_t *base;
    248  1.1.1.2  christos 	tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
    249  1.1.1.2  christos 	base = base_new(tsdn, 0, &hooks, /* metadata_use_hooks */ true);
    250  1.1.1.2  christos 	ehooks_t *ehooks = base_ehooks_get_for_metadata(base);
    251  1.1.1.2  christos 	expect_ptr_eq(&hooks, ehooks_get_extent_hooks_ptr(ehooks),
    252  1.1.1.2  christos 		"Expected user-specified extend hook functions pointer");
    253  1.1.1.2  christos 	base_delete(tsdn, base);
    254  1.1.1.2  christos }
    255  1.1.1.2  christos TEST_END
    256  1.1.1.2  christos 
    257      1.1  christos int
    258      1.1  christos main(void) {
    259      1.1  christos 	return test(
    260      1.1  christos 	    test_base_hooks_default,
    261      1.1  christos 	    test_base_hooks_null,
    262  1.1.1.2  christos 	    test_base_hooks_not_null,
    263  1.1.1.2  christos             test_base_ehooks_get_for_metadata_default_hook,
    264  1.1.1.2  christos             test_base_ehooks_get_for_metadata_custom_hook);
    265      1.1  christos }
    266