aboutsummaryrefslogtreecommitdiff
path: root/test/unit/arena_reset.c
blob: 52170cc4a5bbe668dc9d94532aec1da2abef6756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#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);
	assert_d_eq(mallctl(cmd, &ret, &z, NULL, 0), 0,
	    "Unexpected mallctl(\"%s\", ...) failure", cmd);

	return (ret);
}

static unsigned
get_nsmall(void)
{

	return (get_nsizes_impl("arenas.nbins"));
}

static unsigned
get_nlarge(void)
{

	return (get_nsizes_impl("arenas.nlruns"));
}

static unsigned
get_nhuge(void)
{

	return (get_nsizes_impl("arenas.nhchunks"));
}

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);
	assert_d_eq(mallctlbymib(mib, miblen, &ret, &z, NULL, 0),
	    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
get_large_size(size_t ind)
{

	return (get_size_impl("arenas.lrun.0.size", ind));
}

static size_t
get_huge_size(size_t ind)
{

	return (get_size_impl("arenas.hchunk.0.size", ind));
}

TEST_BEGIN(test_arena_reset)
{
#define	NHUGE	4
	unsigned arena_ind, nsmall, nlarge, nhuge, nptrs, i;
	size_t sz, miblen;
	void **ptrs;
	size_t mib[3];
	tsd_t *tsd;

	test_skip_if((config_valgrind && unlikely(in_valgrind)) || (config_fill
	    && unlikely(opt_quarantine)));

	sz = sizeof(unsigned);
	assert_d_eq(mallctl("arenas.extend", &arena_ind, &sz, NULL, 0), 0,
	    "Unexpected mallctl() failure");

	nsmall = get_nsmall();
	nlarge = get_nlarge();
	nhuge = get_nhuge() > NHUGE ? NHUGE : get_nhuge();
	nptrs = nsmall + nlarge + nhuge;
	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);
		ptrs[i] = mallocx(sz, MALLOCX_ARENA(arena_ind));
		assert_ptr_not_null(ptrs[i],
		    "Unexpected mallocx(%zu, MALLOCX_ARENA(%u)) failure", sz,
		    arena_ind);
	}
	for (i = 0; i < nlarge; i++) {
		sz = get_large_size(i);
		ptrs[nsmall + i] = mallocx(sz, MALLOCX_ARENA(arena_ind));
		assert_ptr_not_null(ptrs[i],
		    "Unexpected mallocx(%zu, MALLOCX_ARENA(%u)) failure", sz,
		    arena_ind);
	}
	for (i = 0; i < nhuge; i++) {
		sz = get_huge_size(i);
		ptrs[nsmall + nlarge + i] = mallocx(sz,
		    MALLOCX_ARENA(arena_ind));
		assert_ptr_not_null(ptrs[i],
		    "Unexpected mallocx(%zu, MALLOCX_ARENA(%u)) failure", sz,
		    arena_ind);
	}

	tsd = tsd_fetch();

	/* Verify allocations. */
	for (i = 0; i < nptrs; i++) {
		assert_zu_gt(ivsalloc(tsd, ptrs[i], false), 0,
		    "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++) {
		assert_zu_eq(ivsalloc(tsd, ptrs[i], false), 0,
		    "Allocation should no longer exist");
	}

	free(ptrs);
}
TEST_END

int
main(void)
{

	return (test(
	    test_arena_reset));
}