1#include "test/jemalloc_test.h"
2
3/* Immediately purge to minimize fragmentation. */
4const char *malloc_conf = "decay_time:-1";
5
6/*
7 * Size class that is a divisor of the page size, ideally 4+ regions per run.
8 */
9#if LG_PAGE <= 14
10#define	SZ	(ZU(1) << (LG_PAGE - 2))
11#else
12#define	SZ	4096
13#endif
14
15/*
16 * Number of slabs to consume at high water mark.  Should be at least 2 so that
17 * if mmap()ed memory grows downward, downward growth of mmap()ed memory is
18 * tested.
19 */
20#define	NSLABS	8
21
22static unsigned
23binind_compute(void)
24{
25	size_t sz;
26	unsigned nbins, i;
27
28	sz = sizeof(nbins);
29	assert_d_eq(mallctl("arenas.nbins", (void *)&nbins, &sz, NULL, 0), 0,
30	    "Unexpected mallctl failure");
31
32	for (i = 0; i < nbins; i++) {
33		size_t mib[4];
34		size_t miblen = sizeof(mib)/sizeof(size_t);
35		size_t size;
36
37		assert_d_eq(mallctlnametomib("arenas.bin.0.size", mib,
38		    &miblen), 0, "Unexpected mallctlnametomb failure");
39		mib[2] = (size_t)i;
40
41		sz = sizeof(size);
42		assert_d_eq(mallctlbymib(mib, miblen, (void *)&size, &sz, NULL,
43		    0), 0, "Unexpected mallctlbymib failure");
44		if (size == SZ)
45			return (i);
46	}
47
48	test_fail("Unable to compute nregs_per_run");
49	return (0);
50}
51
52static size_t
53nregs_per_run_compute(void)
54{
55	uint32_t nregs;
56	size_t sz;
57	unsigned binind = binind_compute();
58	size_t mib[4];
59	size_t miblen = sizeof(mib)/sizeof(size_t);
60
61	assert_d_eq(mallctlnametomib("arenas.bin.0.nregs", mib, &miblen), 0,
62	    "Unexpected mallctlnametomb failure");
63	mib[2] = (size_t)binind;
64	sz = sizeof(nregs);
65	assert_d_eq(mallctlbymib(mib, miblen, (void *)&nregs, &sz, NULL,
66	    0), 0, "Unexpected mallctlbymib failure");
67	return (nregs);
68}
69
70static unsigned
71arenas_create_mallctl(void)
72{
73	unsigned arena_ind;
74	size_t sz;
75
76	sz = sizeof(arena_ind);
77	assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
78	    0, "Error in arenas.create");
79
80	return (arena_ind);
81}
82
83static void
84arena_reset_mallctl(unsigned arena_ind)
85{
86	size_t mib[3];
87	size_t miblen = sizeof(mib)/sizeof(size_t);
88
89	assert_d_eq(mallctlnametomib("arena.0.reset", mib, &miblen), 0,
90	    "Unexpected mallctlnametomib() failure");
91	mib[1] = (size_t)arena_ind;
92	assert_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,
93	    "Unexpected mallctlbymib() failure");
94}
95
96TEST_BEGIN(test_pack)
97{
98	unsigned arena_ind = arenas_create_mallctl();
99	size_t nregs_per_run = nregs_per_run_compute();
100	size_t nregs = nregs_per_run * NSLABS;
101	VARIABLE_ARRAY(void *, ptrs, nregs);
102	size_t i, j, offset;
103
104	/* Fill matrix. */
105	for (i = offset = 0; i < NSLABS; i++) {
106		for (j = 0; j < nregs_per_run; j++) {
107			void *p = mallocx(SZ, MALLOCX_ARENA(arena_ind) |
108			    MALLOCX_TCACHE_NONE);
109			assert_ptr_not_null(p,
110			    "Unexpected mallocx(%zu, MALLOCX_ARENA(%u) |"
111			    " MALLOCX_TCACHE_NONE) failure, run=%zu, reg=%zu",
112			    SZ, arena_ind, i, j);
113			ptrs[(i * nregs_per_run) + j] = p;
114		}
115	}
116
117	/*
118	 * Free all but one region of each run, but rotate which region is
119	 * preserved, so that subsequent allocations exercise the within-run
120	 * layout policy.
121	 */
122	offset = 0;
123	for (i = offset = 0;
124	    i < NSLABS;
125	    i++, offset = (offset + 1) % nregs_per_run) {
126		for (j = 0; j < nregs_per_run; j++) {
127			void *p = ptrs[(i * nregs_per_run) + j];
128			if (offset == j)
129				continue;
130			dallocx(p, MALLOCX_ARENA(arena_ind) |
131			    MALLOCX_TCACHE_NONE);
132		}
133	}
134
135	/*
136	 * Logically refill matrix, skipping preserved regions and verifying
137	 * that the matrix is unmodified.
138	 */
139	offset = 0;
140	for (i = offset = 0;
141	    i < NSLABS;
142	    i++, offset = (offset + 1) % nregs_per_run) {
143		for (j = 0; j < nregs_per_run; j++) {
144			void *p;
145
146			if (offset == j)
147				continue;
148			p = mallocx(SZ, MALLOCX_ARENA(arena_ind) |
149			    MALLOCX_TCACHE_NONE);
150			assert_ptr_eq(p, ptrs[(i * nregs_per_run) + j],
151			    "Unexpected refill discrepancy, run=%zu, reg=%zu\n",
152			    i, j);
153		}
154	}
155
156	/* Clean up. */
157	arena_reset_mallctl(arena_ind);
158}
159TEST_END
160
161int
162main(void)
163{
164	return (test(
165	    test_pack));
166}
167