2016-04-23 05:37:17 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
|
|
|
#ifdef JEMALLOC_PROF
|
|
|
|
const char *malloc_conf = "prof:true,lg_prof_sample:0";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static unsigned
|
|
|
|
get_nsizes_impl(const char *cmd)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
|
|
|
get_nsmall(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (get_nsizes_impl("arenas.nbins"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
2016-06-01 05:50:21 +08:00
|
|
|
get_nlarge(void)
|
2016-04-23 05:37:17 +08:00
|
|
|
{
|
|
|
|
|
2016-06-01 05:50:21 +08:00
|
|
|
return (get_nsizes_impl("arenas.nlextents"));
|
2016-04-23 05:37:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
get_size_impl(const char *cmd, size_t ind)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
get_small_size(size_t ind)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (get_size_impl("arenas.bin.0.size", ind));
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2016-06-01 05:50:21 +08:00
|
|
|
get_large_size(size_t ind)
|
2016-04-23 05:37:17 +08:00
|
|
|
{
|
|
|
|
|
2016-06-01 05:50:21 +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
|
|
|
|
vsalloc(tsdn_t *tsdn, const void *ptr)
|
|
|
|
{
|
|
|
|
extent_t *extent;
|
|
|
|
|
2016-06-02 03:10:39 +08:00
|
|
|
extent = extent_lookup(tsdn, ptr, false);
|
2016-05-18 01:36:17 +08:00
|
|
|
if (extent == NULL)
|
|
|
|
return (0);
|
|
|
|
if (!extent_active_get(extent))
|
|
|
|
return (0);
|
|
|
|
|
2016-05-28 15:17:28 +08:00
|
|
|
return (isalloc(tsdn, extent, ptr));
|
2016-05-18 01:36:17 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 05:37:17 +08:00
|
|
|
TEST_BEGIN(test_arena_reset)
|
|
|
|
{
|
2016-06-01 05:50:21 +08:00
|
|
|
#define NLARGE 32
|
|
|
|
unsigned arena_ind, nsmall, nlarge, nptrs, i;
|
2016-04-23 05:37:17 +08:00
|
|
|
size_t sz, miblen;
|
|
|
|
void **ptrs;
|
2016-04-26 03:51:17 +08:00
|
|
|
int flags;
|
2016-04-23 05:37:17 +08:00
|
|
|
size_t mib[3];
|
2016-05-11 13:21:10 +08:00
|
|
|
tsdn_t *tsdn;
|
2016-04-23 05:37:17 +08:00
|
|
|
|
|
|
|
sz = sizeof(unsigned);
|
2017-01-04 00:21:29 +08:00
|
|
|
assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
|
2016-10-28 12:31:25 +08:00
|
|
|
0, "Unexpected mallctl() failure");
|
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();
|
|
|
|
nptrs = nsmall + nlarge;
|
2016-04-23 05:37:17 +08:00
|
|
|
ptrs = (void **)malloc(nptrs * sizeof(void *));
|
|
|
|
assert_ptr_not_null(ptrs, "Unexpected malloc() failure");
|
|
|
|
|
|
|
|
/* Allocate objects with a wide range of sizes. */
|
|
|
|
for (i = 0; i < nsmall; i++) {
|
|
|
|
sz = get_small_size(i);
|
2016-04-26 03:51:17 +08:00
|
|
|
ptrs[i] = mallocx(sz, flags);
|
2016-04-23 05:37:17 +08:00
|
|
|
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);
|
2016-05-28 15:17:28 +08:00
|
|
|
ptrs[nsmall + i] = mallocx(sz, flags);
|
2016-04-23 05:37:17 +08:00
|
|
|
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. */
|
|
|
|
for (i = 0; i < nptrs; i++) {
|
2016-05-28 15:17:28 +08:00
|
|
|
assert_zu_gt(ivsalloc(tsdn, ptrs[i]), 0,
|
2016-04-23 05:37:17 +08:00
|
|
|
"Allocation should have queryable size");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset. */
|
|
|
|
miblen = sizeof(mib)/sizeof(size_t);
|
|
|
|
assert_d_eq(mallctlnametomib("arena.0.reset", 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");
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (test(
|
|
|
|
test_arena_reset));
|
|
|
|
}
|