2017-01-04 09:21:59 +08:00
|
|
|
#ifndef ARENA_RESET_PROF_C_
|
2016-04-23 05:37:17 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
#endif
|
|
|
|
|
2017-01-04 09:21:59 +08:00
|
|
|
#include "test/extent_hooks.h"
|
|
|
|
|
2016-04-23 05:37:17 +08:00
|
|
|
static unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
get_nsizes_impl(const char *cmd) {
|
2016-04-23 05:37:17 +08:00
|
|
|
unsigned ret;
|
|
|
|
size_t z;
|
|
|
|
|
|
|
|
z = sizeof(unsigned);
|
2016-10-28 12:31:25 +08:00
|
|
|
assert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,
|
2016-04-23 05:37:17 +08:00
|
|
|
"Unexpected mallctl(\"%s\", ...) failure", cmd);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
get_nsmall(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return get_nsizes_impl("arenas.nbins");
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
get_nlarge(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return get_nsizes_impl("arenas.nlextents");
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
get_size_impl(const char *cmd, size_t ind) {
|
2016-04-23 05:37:17 +08:00
|
|
|
size_t ret;
|
|
|
|
size_t z;
|
|
|
|
size_t mib[4];
|
|
|
|
size_t miblen = 4;
|
|
|
|
|
|
|
|
z = sizeof(size_t);
|
|
|
|
assert_d_eq(mallctlnametomib(cmd, mib, &miblen),
|
|
|
|
0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
|
|
|
|
mib[2] = ind;
|
|
|
|
z = sizeof(size_t);
|
2016-10-28 12:31:25 +08:00
|
|
|
assert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),
|
2016-04-23 05:37:17 +08:00
|
|
|
0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
get_small_size(size_t ind) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return get_size_impl("arenas.bin.0.size", ind);
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
get_large_size(size_t ind) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return get_size_impl("arenas.lextent.0.size", ind);
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
2016-05-18 01:36:17 +08:00
|
|
|
/* Like ivsalloc(), but safe to call on discarded allocations. */
|
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
vsalloc(tsdn_t *tsdn, const void *ptr) {
|
2017-03-17 08:57:52 +08:00
|
|
|
rtree_ctx_t rtree_ctx_fallback;
|
|
|
|
rtree_ctx_t *rtree_ctx = tsdn_rtree_ctx(tsdn, &rtree_ctx_fallback);
|
|
|
|
|
2016-05-18 01:36:17 +08:00
|
|
|
extent_t *extent;
|
2017-03-17 08:57:52 +08:00
|
|
|
szind_t szind;
|
|
|
|
if (rtree_extent_szind_read(tsdn, &extents_rtree, rtree_ctx,
|
|
|
|
(uintptr_t)ptr, false, &extent, &szind)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-05-18 01:36:17 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
if (extent == NULL) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return 0;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-30 13:57:14 +08:00
|
|
|
if (extent_state_get(extent) != extent_state_active) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return 0;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-18 01:36:17 +08:00
|
|
|
|
2017-03-17 08:57:52 +08:00
|
|
|
if (szind == NSIZES) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index2size(szind);
|
2016-05-18 01:36:17 +08:00
|
|
|
}
|
|
|
|
|
2017-01-04 09:21:59 +08:00
|
|
|
static unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
do_arena_create(extent_hooks_t *h) {
|
2017-01-04 09:21:59 +08:00
|
|
|
unsigned arena_ind;
|
|
|
|
size_t sz = sizeof(unsigned);
|
|
|
|
assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz,
|
|
|
|
(void *)(h != NULL ? &h : NULL), (h != NULL ? sizeof(h) : 0)), 0,
|
|
|
|
"Unexpected mallctl() failure");
|
2017-01-20 10:15:45 +08:00
|
|
|
return arena_ind;
|
2017-01-04 09:21:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
do_arena_reset_pre(unsigned arena_ind, void ***ptrs, unsigned *nptrs) {
|
2017-01-20 13:41:41 +08:00
|
|
|
#define NLARGE 32
|
2017-01-04 09:21:59 +08:00
|
|
|
unsigned nsmall, nlarge, i;
|
|
|
|
size_t sz;
|
2016-04-26 03:51:17 +08:00
|
|
|
int flags;
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2016-04-23 05:37:17 +08:00
|
|
|
|
2016-04-26 03:51:17 +08:00
|
|
|
flags = MALLOCX_ARENA(arena_ind) | MALLOCX_TCACHE_NONE;
|
|
|
|
|
2016-04-23 05:37:17 +08:00
|
|
|
nsmall = get_nsmall();
|
2016-06-01 05:50:21 +08:00
|
|
|
nlarge = get_nlarge() > NLARGE ? NLARGE : get_nlarge();
|
2017-01-04 09:21:59 +08:00
|
|
|
*nptrs = nsmall + nlarge;
|
|
|
|
*ptrs = (void **)malloc(*nptrs * sizeof(void *));
|
|
|
|
assert_ptr_not_null(*ptrs, "Unexpected malloc() failure");
|
2016-04-23 05:37:17 +08:00
|
|
|
|
|
|
|
/* Allocate objects with a wide range of sizes. */
|
|
|
|
for (i = 0; i < nsmall; i++) {
|
|
|
|
sz = get_small_size(i);
|
2017-01-04 09:21:59 +08:00
|
|
|
(*ptrs)[i] = mallocx(sz, flags);
|
|
|
|
assert_ptr_not_null((*ptrs)[i],
|
2016-04-26 03:51:17 +08:00
|
|
|
"Unexpected mallocx(%zu, %#x) failure", sz, flags);
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
2016-06-01 05:50:21 +08:00
|
|
|
for (i = 0; i < nlarge; i++) {
|
|
|
|
sz = get_large_size(i);
|
2017-01-04 09:21:59 +08:00
|
|
|
(*ptrs)[nsmall + i] = mallocx(sz, flags);
|
|
|
|
assert_ptr_not_null((*ptrs)[i],
|
2016-04-26 03:51:17 +08:00
|
|
|
"Unexpected mallocx(%zu, %#x) failure", sz, flags);
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn = tsdn_fetch();
|
2016-04-23 05:37:17 +08:00
|
|
|
|
|
|
|
/* Verify allocations. */
|
2017-01-04 09:21:59 +08:00
|
|
|
for (i = 0; i < *nptrs; i++) {
|
|
|
|
assert_zu_gt(ivsalloc(tsdn, (*ptrs)[i]), 0,
|
2016-04-23 05:37:17 +08:00
|
|
|
"Allocation should have queryable size");
|
|
|
|
}
|
2017-01-04 09:21:59 +08:00
|
|
|
}
|
2016-04-23 05:37:17 +08:00
|
|
|
|
2017-01-04 09:21:59 +08:00
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
do_arena_reset_post(void **ptrs, unsigned nptrs) {
|
2017-01-04 09:21:59 +08:00
|
|
|
tsdn_t *tsdn;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
tsdn = tsdn_fetch();
|
2016-04-23 05:37:17 +08:00
|
|
|
|
|
|
|
/* Verify allocations no longer exist. */
|
|
|
|
for (i = 0; i < nptrs; i++) {
|
2016-05-18 01:36:17 +08:00
|
|
|
assert_zu_eq(vsalloc(tsdn, ptrs[i]), 0,
|
2016-04-23 05:37:17 +08:00
|
|
|
"Allocation should no longer exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(ptrs);
|
|
|
|
}
|
2017-01-04 09:21:59 +08:00
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
do_arena_reset_destroy(const char *name, unsigned arena_ind) {
|
2017-01-04 09:21:59 +08:00
|
|
|
size_t mib[3];
|
|
|
|
size_t miblen;
|
|
|
|
|
|
|
|
miblen = sizeof(mib)/sizeof(size_t);
|
|
|
|
assert_d_eq(mallctlnametomib(name, mib, &miblen), 0,
|
|
|
|
"Unexpected mallctlnametomib() failure");
|
|
|
|
mib[1] = (size_t)arena_ind;
|
|
|
|
assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,
|
|
|
|
"Unexpected mallctlbymib() failure");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
do_arena_reset(unsigned arena_ind) {
|
2017-01-04 09:21:59 +08:00
|
|
|
do_arena_reset_destroy("arena.0.reset", arena_ind);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
do_arena_destroy(unsigned arena_ind) {
|
2017-01-04 09:21:59 +08:00
|
|
|
do_arena_reset_destroy("arena.0.destroy", arena_ind);
|
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_arena_reset) {
|
2017-01-04 09:21:59 +08:00
|
|
|
unsigned arena_ind;
|
|
|
|
void **ptrs;
|
|
|
|
unsigned nptrs;
|
|
|
|
|
|
|
|
arena_ind = do_arena_create(NULL);
|
|
|
|
do_arena_reset_pre(arena_ind, &ptrs, &nptrs);
|
|
|
|
do_arena_reset(arena_ind);
|
|
|
|
do_arena_reset_post(ptrs, nptrs);
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
arena_i_initialized(unsigned arena_ind, bool refresh) {
|
2017-01-04 09:21:59 +08:00
|
|
|
bool initialized;
|
|
|
|
size_t mib[3];
|
|
|
|
size_t miblen, sz;
|
|
|
|
|
|
|
|
if (refresh) {
|
|
|
|
uint64_t epoch = 1;
|
|
|
|
assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch,
|
|
|
|
sizeof(epoch)), 0, "Unexpected mallctl() failure");
|
|
|
|
}
|
|
|
|
|
|
|
|
miblen = sizeof(mib)/sizeof(size_t);
|
|
|
|
assert_d_eq(mallctlnametomib("arena.0.initialized", mib, &miblen), 0,
|
|
|
|
"Unexpected mallctlnametomib() failure");
|
|
|
|
mib[1] = (size_t)arena_ind;
|
|
|
|
sz = sizeof(initialized);
|
|
|
|
assert_d_eq(mallctlbymib(mib, miblen, (void *)&initialized, &sz, NULL,
|
|
|
|
0), 0, "Unexpected mallctlbymib() failure");
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return initialized;
|
2017-01-04 09:21:59 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_arena_destroy_initial) {
|
2017-01-04 09:21:59 +08:00
|
|
|
assert_false(arena_i_initialized(MALLCTL_ARENAS_DESTROYED, false),
|
|
|
|
"Destroyed arena stats should not be initialized");
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_arena_destroy_hooks_default) {
|
2017-01-04 09:21:59 +08:00
|
|
|
unsigned arena_ind, arena_ind_another, arena_ind_prev;
|
|
|
|
void **ptrs;
|
|
|
|
unsigned nptrs;
|
|
|
|
|
|
|
|
arena_ind = do_arena_create(NULL);
|
|
|
|
do_arena_reset_pre(arena_ind, &ptrs, &nptrs);
|
|
|
|
|
|
|
|
assert_false(arena_i_initialized(arena_ind, false),
|
|
|
|
"Arena stats should not be initialized");
|
|
|
|
assert_true(arena_i_initialized(arena_ind, true),
|
|
|
|
"Arena stats should be initialized");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create another arena before destroying one, to better verify arena
|
|
|
|
* index reuse.
|
|
|
|
*/
|
|
|
|
arena_ind_another = do_arena_create(NULL);
|
|
|
|
|
|
|
|
do_arena_destroy(arena_ind);
|
|
|
|
|
|
|
|
assert_false(arena_i_initialized(arena_ind, true),
|
|
|
|
"Arena stats should not be initialized");
|
|
|
|
assert_true(arena_i_initialized(MALLCTL_ARENAS_DESTROYED, false),
|
|
|
|
"Destroyed arena stats should be initialized");
|
|
|
|
|
|
|
|
do_arena_reset_post(ptrs, nptrs);
|
|
|
|
|
|
|
|
arena_ind_prev = arena_ind;
|
|
|
|
arena_ind = do_arena_create(NULL);
|
|
|
|
do_arena_reset_pre(arena_ind, &ptrs, &nptrs);
|
|
|
|
assert_u_eq(arena_ind, arena_ind_prev,
|
|
|
|
"Arena index should have been recycled");
|
|
|
|
do_arena_destroy(arena_ind);
|
|
|
|
do_arena_reset_post(ptrs, nptrs);
|
|
|
|
|
|
|
|
do_arena_destroy(arena_ind_another);
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Actually unmap extents, regardless of config_munmap, so that attempts to
|
|
|
|
* access a destroyed arena's memory will segfault.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
extent_dalloc_unmap(extent_hooks_t *extent_hooks, void *addr, size_t size,
|
2017-01-16 08:56:30 +08:00
|
|
|
bool committed, unsigned arena_ind) {
|
2017-01-04 09:21:59 +08:00
|
|
|
TRACE_HOOK("%s(extent_hooks=%p, addr=%p, size=%zu, committed=%s, "
|
|
|
|
"arena_ind=%u)\n", __func__, extent_hooks, addr, size, committed ?
|
|
|
|
"true" : "false", arena_ind);
|
|
|
|
assert_ptr_eq(extent_hooks, &hooks,
|
|
|
|
"extent_hooks should be same as pointer used to set hooks");
|
|
|
|
assert_ptr_eq(extent_hooks->dalloc, extent_dalloc_unmap,
|
|
|
|
"Wrong hook function");
|
|
|
|
called_dalloc = true;
|
2017-01-16 08:56:30 +08:00
|
|
|
if (!try_dalloc) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2017-01-04 09:21:59 +08:00
|
|
|
pages_unmap(addr, size);
|
|
|
|
did_dalloc = true;
|
2017-01-20 10:15:45 +08:00
|
|
|
return false;
|
2017-01-04 09:21:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static extent_hooks_t hooks_orig;
|
|
|
|
|
|
|
|
static extent_hooks_t hooks_unmap = {
|
|
|
|
extent_alloc_hook,
|
|
|
|
extent_dalloc_unmap, /* dalloc */
|
|
|
|
extent_commit_hook,
|
|
|
|
extent_decommit_hook,
|
|
|
|
extent_purge_lazy_hook,
|
|
|
|
extent_purge_forced_hook,
|
|
|
|
extent_split_hook,
|
|
|
|
extent_merge_hook
|
|
|
|
};
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_arena_destroy_hooks_unmap) {
|
2017-01-04 09:21:59 +08:00
|
|
|
unsigned arena_ind;
|
|
|
|
void **ptrs;
|
|
|
|
unsigned nptrs;
|
|
|
|
|
|
|
|
extent_hooks_prep();
|
|
|
|
try_decommit = false;
|
|
|
|
memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
|
|
|
|
memcpy(&hooks, &hooks_unmap, sizeof(extent_hooks_t));
|
|
|
|
|
|
|
|
did_alloc = false;
|
|
|
|
arena_ind = do_arena_create(&hooks);
|
|
|
|
do_arena_reset_pre(arena_ind, &ptrs, &nptrs);
|
|
|
|
|
|
|
|
assert_true(did_alloc, "Expected alloc");
|
|
|
|
|
|
|
|
assert_false(arena_i_initialized(arena_ind, false),
|
|
|
|
"Arena stats should not be initialized");
|
|
|
|
assert_true(arena_i_initialized(arena_ind, true),
|
|
|
|
"Arena stats should be initialized");
|
|
|
|
|
|
|
|
did_dalloc = false;
|
|
|
|
do_arena_destroy(arena_ind);
|
|
|
|
assert_true(did_dalloc, "Expected dalloc");
|
|
|
|
|
|
|
|
assert_false(arena_i_initialized(arena_ind, true),
|
|
|
|
"Arena stats should not be initialized");
|
|
|
|
assert_true(arena_i_initialized(MALLCTL_ARENAS_DESTROYED, false),
|
|
|
|
"Destroyed arena stats should be initialized");
|
|
|
|
|
|
|
|
do_arena_reset_post(ptrs, nptrs);
|
|
|
|
|
|
|
|
memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
|
|
|
|
}
|
2016-04-23 05:37:17 +08:00
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
2017-01-04 09:21:59 +08:00
|
|
|
test_arena_reset,
|
|
|
|
test_arena_destroy_initial,
|
|
|
|
test_arena_destroy_hooks_default,
|
2017-01-20 10:15:45 +08:00
|
|
|
test_arena_destroy_hooks_unmap);
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|