1 #include "test/jemalloc_test.h"
2 
TEST_BEGIN(test_small_extent_size)3 TEST_BEGIN(test_small_extent_size) {
4 	unsigned nbins, i;
5 	size_t sz, extent_size;
6 	size_t mib[4];
7 	size_t miblen = sizeof(mib) / sizeof(size_t);
8 
9 	/*
10 	 * Iterate over all small size classes, get their extent sizes, and
11 	 * verify that the quantized size is the same as the extent size.
12 	 */
13 
14 	sz = sizeof(unsigned);
15 	assert_d_eq(mallctl("arenas.nbins", (void *)&nbins, &sz, NULL, 0), 0,
16 	    "Unexpected mallctl failure");
17 
18 	assert_d_eq(mallctlnametomib("arenas.bin.0.slab_size", mib, &miblen), 0,
19 	    "Unexpected mallctlnametomib failure");
20 	for (i = 0; i < nbins; i++) {
21 		mib[2] = i;
22 		sz = sizeof(size_t);
23 		assert_d_eq(mallctlbymib(mib, miblen, (void *)&extent_size, &sz,
24 		    NULL, 0), 0, "Unexpected mallctlbymib failure");
25 		assert_zu_eq(extent_size,
26 		    extent_size_quantize_floor(extent_size),
27 		    "Small extent quantization should be a no-op "
28 		    "(extent_size=%zu)", extent_size);
29 		assert_zu_eq(extent_size,
30 		    extent_size_quantize_ceil(extent_size),
31 		    "Small extent quantization should be a no-op "
32 		    "(extent_size=%zu)", extent_size);
33 	}
34 }
35 TEST_END
36 
TEST_BEGIN(test_large_extent_size)37 TEST_BEGIN(test_large_extent_size) {
38 	bool cache_oblivious;
39 	unsigned nlextents, i;
40 	size_t sz, extent_size_prev, ceil_prev;
41 	size_t mib[4];
42 	size_t miblen = sizeof(mib) / sizeof(size_t);
43 
44 	/*
45 	 * Iterate over all large size classes, get their extent sizes, and
46 	 * verify that the quantized size is the same as the extent size.
47 	 */
48 
49 	sz = sizeof(bool);
50 	assert_d_eq(mallctl("config.cache_oblivious", (void *)&cache_oblivious,
51 	    &sz, NULL, 0), 0, "Unexpected mallctl failure");
52 
53 	sz = sizeof(unsigned);
54 	assert_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL,
55 	    0), 0, "Unexpected mallctl failure");
56 
57 	assert_d_eq(mallctlnametomib("arenas.lextent.0.size", mib, &miblen), 0,
58 	    "Unexpected mallctlnametomib failure");
59 	for (i = 0; i < nlextents; i++) {
60 		size_t lextent_size, extent_size, floor, ceil;
61 
62 		mib[2] = i;
63 		sz = sizeof(size_t);
64 		assert_d_eq(mallctlbymib(mib, miblen, (void *)&lextent_size,
65 		    &sz, NULL, 0), 0, "Unexpected mallctlbymib failure");
66 		extent_size = cache_oblivious ? lextent_size + PAGE :
67 		    lextent_size;
68 		floor = extent_size_quantize_floor(extent_size);
69 		ceil = extent_size_quantize_ceil(extent_size);
70 
71 		assert_zu_eq(extent_size, floor,
72 		    "Extent quantization should be a no-op for precise size "
73 		    "(lextent_size=%zu, extent_size=%zu)", lextent_size,
74 		    extent_size);
75 		assert_zu_eq(extent_size, ceil,
76 		    "Extent quantization should be a no-op for precise size "
77 		    "(lextent_size=%zu, extent_size=%zu)", lextent_size,
78 		    extent_size);
79 
80 		if (i > 0) {
81 			assert_zu_eq(extent_size_prev,
82 			    extent_size_quantize_floor(extent_size - PAGE),
83 			    "Floor should be a precise size");
84 			if (extent_size_prev < ceil_prev) {
85 				assert_zu_eq(ceil_prev, extent_size,
86 				    "Ceiling should be a precise size "
87 				    "(extent_size_prev=%zu, ceil_prev=%zu, "
88 				    "extent_size=%zu)", extent_size_prev,
89 				    ceil_prev, extent_size);
90 			}
91 		}
92 		if (i + 1 < nlextents) {
93 			extent_size_prev = floor;
94 			ceil_prev = extent_size_quantize_ceil(extent_size +
95 			    PAGE);
96 		}
97 	}
98 }
99 TEST_END
100 
TEST_BEGIN(test_monotonic)101 TEST_BEGIN(test_monotonic) {
102 #define SZ_MAX	ZU(4 * 1024 * 1024)
103 	unsigned i;
104 	size_t floor_prev, ceil_prev;
105 
106 	floor_prev = 0;
107 	ceil_prev = 0;
108 	for (i = 1; i <= SZ_MAX >> LG_PAGE; i++) {
109 		size_t extent_size, floor, ceil;
110 
111 		extent_size = i << LG_PAGE;
112 		floor = extent_size_quantize_floor(extent_size);
113 		ceil = extent_size_quantize_ceil(extent_size);
114 
115 		assert_zu_le(floor, extent_size,
116 		    "Floor should be <= (floor=%zu, extent_size=%zu, ceil=%zu)",
117 		    floor, extent_size, ceil);
118 		assert_zu_ge(ceil, extent_size,
119 		    "Ceiling should be >= (floor=%zu, extent_size=%zu, "
120 		    "ceil=%zu)", floor, extent_size, ceil);
121 
122 		assert_zu_le(floor_prev, floor, "Floor should be monotonic "
123 		    "(floor_prev=%zu, floor=%zu, extent_size=%zu, ceil=%zu)",
124 		    floor_prev, floor, extent_size, ceil);
125 		assert_zu_le(ceil_prev, ceil, "Ceiling should be monotonic "
126 		    "(floor=%zu, extent_size=%zu, ceil_prev=%zu, ceil=%zu)",
127 		    floor, extent_size, ceil_prev, ceil);
128 
129 		floor_prev = floor;
130 		ceil_prev = ceil;
131 	}
132 }
133 TEST_END
134 
135 int
main(void)136 main(void) {
137 	return test(
138 	    test_small_extent_size,
139 	    test_large_extent_size,
140 	    test_monotonic);
141 }
142