Home | History | Annotate | Line # | Download | only in unit
inspect.c revision 1.1.1.2
      1      1.1  christos #include "test/jemalloc_test.h"
      2      1.1  christos 
      3  1.1.1.2  christos #define TEST_UTIL_EINVAL(node, a, b, c, d, why_inval)                          \
      4  1.1.1.2  christos 	do {                                                                   \
      5  1.1.1.2  christos 		assert_d_eq(                                                   \
      6  1.1.1.2  christos 		    mallctl("experimental.utilization." node, a, b, c, d),     \
      7  1.1.1.2  christos 		    EINVAL, "Should fail when " why_inval);                    \
      8  1.1.1.2  christos 		assert_zu_eq(out_sz, out_sz_ref,                               \
      9  1.1.1.2  christos 		    "Output size touched when given invalid arguments");       \
     10  1.1.1.2  christos 		assert_d_eq(memcmp(out, out_ref, out_sz_ref), 0,               \
     11  1.1.1.2  christos 		    "Output content touched when given invalid arguments");    \
     12  1.1.1.2  christos 	} while (0)
     13      1.1  christos 
     14  1.1.1.2  christos #define TEST_UTIL_QUERY_EINVAL(a, b, c, d, why_inval)                          \
     15      1.1  christos 	TEST_UTIL_EINVAL("query", a, b, c, d, why_inval)
     16  1.1.1.2  christos #define TEST_UTIL_BATCH_EINVAL(a, b, c, d, why_inval)                          \
     17      1.1  christos 	TEST_UTIL_EINVAL("batch_query", a, b, c, d, why_inval)
     18      1.1  christos 
     19  1.1.1.2  christos #define TEST_UTIL_VALID(node)                                                  \
     20  1.1.1.2  christos 	do {                                                                   \
     21  1.1.1.2  christos 		assert_d_eq(mallctl("experimental.utilization." node, out,     \
     22  1.1.1.2  christos 		                &out_sz, in, in_sz),                           \
     23  1.1.1.2  christos 		    0, "Should return 0 on correct arguments");                \
     24  1.1.1.2  christos 		expect_zu_eq(out_sz, out_sz_ref, "incorrect output size");     \
     25  1.1.1.2  christos 		expect_d_ne(memcmp(out, out_ref, out_sz_ref), 0,               \
     26  1.1.1.2  christos 		    "Output content should be changed");                       \
     27  1.1.1.2  christos 	} while (0)
     28      1.1  christos 
     29      1.1  christos #define TEST_UTIL_BATCH_VALID TEST_UTIL_VALID("batch_query")
     30      1.1  christos 
     31      1.1  christos #define TEST_MAX_SIZE (1 << 20)
     32      1.1  christos 
     33      1.1  christos TEST_BEGIN(test_query) {
     34      1.1  christos 	size_t sz;
     35      1.1  christos 	/*
     36      1.1  christos 	 * Select some sizes that can span both small and large sizes, and are
     37      1.1  christos 	 * numerically unrelated to any size boundaries.
     38      1.1  christos 	 */
     39      1.1  christos 	for (sz = 7; sz <= TEST_MAX_SIZE && sz <= SC_LARGE_MAXCLASS;
     40  1.1.1.2  christos 	     sz += (sz <= SC_SMALL_MAXCLASS ? 1009 : 99989)) {
     41  1.1.1.2  christos 		void  *p = mallocx(sz, 0);
     42      1.1  christos 		void **in = &p;
     43      1.1  christos 		size_t in_sz = sizeof(const void *);
     44      1.1  christos 		size_t out_sz = sizeof(void *) + sizeof(size_t) * 5;
     45  1.1.1.2  christos 		void  *out = mallocx(out_sz, 0);
     46  1.1.1.2  christos 		void  *out_ref = mallocx(out_sz, 0);
     47      1.1  christos 		size_t out_sz_ref = out_sz;
     48      1.1  christos 
     49  1.1.1.2  christos 		assert_ptr_not_null(p, "test pointer allocation failed");
     50  1.1.1.2  christos 		assert_ptr_not_null(out, "test output allocation failed");
     51  1.1.1.2  christos 		assert_ptr_not_null(
     52  1.1.1.2  christos 		    out_ref, "test reference output allocation failed");
     53      1.1  christos 
     54      1.1  christos #define SLABCUR_READ(out) (*(void **)out)
     55      1.1  christos #define COUNTS(out) ((size_t *)((void **)out + 1))
     56      1.1  christos #define NFREE_READ(out) COUNTS(out)[0]
     57      1.1  christos #define NREGS_READ(out) COUNTS(out)[1]
     58      1.1  christos #define SIZE_READ(out) COUNTS(out)[2]
     59      1.1  christos #define BIN_NFREE_READ(out) COUNTS(out)[3]
     60      1.1  christos #define BIN_NREGS_READ(out) COUNTS(out)[4]
     61      1.1  christos 
     62      1.1  christos 		SLABCUR_READ(out) = NULL;
     63      1.1  christos 		NFREE_READ(out) = NREGS_READ(out) = SIZE_READ(out) = -1;
     64      1.1  christos 		BIN_NFREE_READ(out) = BIN_NREGS_READ(out) = -1;
     65      1.1  christos 		memcpy(out_ref, out, out_sz);
     66      1.1  christos 
     67      1.1  christos 		/* Test invalid argument(s) errors */
     68  1.1.1.2  christos 		TEST_UTIL_QUERY_EINVAL(NULL, &out_sz, in, in_sz, "old is NULL");
     69  1.1.1.2  christos 		TEST_UTIL_QUERY_EINVAL(out, NULL, in, in_sz, "oldlenp is NULL");
     70  1.1.1.2  christos 		TEST_UTIL_QUERY_EINVAL(
     71  1.1.1.2  christos 		    out, &out_sz, NULL, in_sz, "newp is NULL");
     72  1.1.1.2  christos 		TEST_UTIL_QUERY_EINVAL(out, &out_sz, in, 0, "newlen is zero");
     73      1.1  christos 		in_sz -= 1;
     74  1.1.1.2  christos 		TEST_UTIL_QUERY_EINVAL(
     75  1.1.1.2  christos 		    out, &out_sz, in, in_sz, "invalid newlen");
     76      1.1  christos 		in_sz += 1;
     77      1.1  christos 		out_sz_ref = out_sz -= 2 * sizeof(size_t);
     78  1.1.1.2  christos 		TEST_UTIL_QUERY_EINVAL(
     79  1.1.1.2  christos 		    out, &out_sz, in, in_sz, "invalid *oldlenp");
     80      1.1  christos 		out_sz_ref = out_sz += 2 * sizeof(size_t);
     81      1.1  christos 
     82      1.1  christos 		/* Examine output for valid call */
     83      1.1  christos 		TEST_UTIL_VALID("query");
     84      1.1  christos 		expect_zu_le(sz, SIZE_READ(out),
     85      1.1  christos 		    "Extent size should be at least allocation size");
     86      1.1  christos 		expect_zu_eq(SIZE_READ(out) & (PAGE - 1), 0,
     87      1.1  christos 		    "Extent size should be a multiple of page size");
     88      1.1  christos 
     89      1.1  christos 		/*
     90      1.1  christos 		 * We don't do much bin checking if prof is on, since profiling
     91      1.1  christos 		 * can produce extents that are for small size classes but not
     92      1.1  christos 		 * slabs, which interferes with things like region counts.
     93      1.1  christos 		 */
     94      1.1  christos 		if (!opt_prof && sz <= SC_SMALL_MAXCLASS) {
     95      1.1  christos 			expect_zu_le(NFREE_READ(out), NREGS_READ(out),
     96      1.1  christos 			    "Extent free count exceeded region count");
     97      1.1  christos 			expect_zu_le(NREGS_READ(out), SIZE_READ(out),
     98      1.1  christos 			    "Extent region count exceeded size");
     99      1.1  christos 			expect_zu_ne(NREGS_READ(out), 0,
    100      1.1  christos 			    "Extent region count must be positive");
    101  1.1.1.2  christos 			expect_true(NFREE_READ(out) == 0
    102  1.1.1.2  christos 			        || (SLABCUR_READ(out) != NULL
    103  1.1.1.2  christos 			            && SLABCUR_READ(out) <= p),
    104      1.1  christos 			    "Allocation should follow first fit principle");
    105      1.1  christos 
    106      1.1  christos 			if (config_stats) {
    107      1.1  christos 				expect_zu_le(BIN_NFREE_READ(out),
    108      1.1  christos 				    BIN_NREGS_READ(out),
    109      1.1  christos 				    "Bin free count exceeded region count");
    110      1.1  christos 				expect_zu_ne(BIN_NREGS_READ(out), 0,
    111      1.1  christos 				    "Bin region count must be positive");
    112      1.1  christos 				expect_zu_le(NFREE_READ(out),
    113      1.1  christos 				    BIN_NFREE_READ(out),
    114      1.1  christos 				    "Extent free count exceeded bin free count");
    115      1.1  christos 				expect_zu_le(NREGS_READ(out),
    116      1.1  christos 				    BIN_NREGS_READ(out),
    117      1.1  christos 				    "Extent region count exceeded "
    118      1.1  christos 				    "bin region count");
    119  1.1.1.2  christos 				expect_zu_eq(
    120  1.1.1.2  christos 				    BIN_NREGS_READ(out) % NREGS_READ(out), 0,
    121      1.1  christos 				    "Bin region count isn't a multiple of "
    122      1.1  christos 				    "extent region count");
    123      1.1  christos 				expect_zu_le(
    124      1.1  christos 				    BIN_NFREE_READ(out) - NFREE_READ(out),
    125      1.1  christos 				    BIN_NREGS_READ(out) - NREGS_READ(out),
    126      1.1  christos 				    "Free count in other extents in the bin "
    127      1.1  christos 				    "exceeded region count in other extents "
    128      1.1  christos 				    "in the bin");
    129      1.1  christos 				expect_zu_le(NREGS_READ(out) - NFREE_READ(out),
    130      1.1  christos 				    BIN_NREGS_READ(out) - BIN_NFREE_READ(out),
    131      1.1  christos 				    "Extent utilized count exceeded "
    132      1.1  christos 				    "bin utilized count");
    133      1.1  christos 			}
    134      1.1  christos 		} else if (sz > SC_SMALL_MAXCLASS) {
    135      1.1  christos 			expect_zu_eq(NFREE_READ(out), 0,
    136      1.1  christos 			    "Extent free count should be zero");
    137      1.1  christos 			expect_zu_eq(NREGS_READ(out), 1,
    138      1.1  christos 			    "Extent region count should be one");
    139      1.1  christos 			expect_ptr_null(SLABCUR_READ(out),
    140      1.1  christos 			    "Current slab must be null for large size classes");
    141      1.1  christos 			if (config_stats) {
    142      1.1  christos 				expect_zu_eq(BIN_NFREE_READ(out), 0,
    143      1.1  christos 				    "Bin free count must be zero for "
    144      1.1  christos 				    "large sizes");
    145      1.1  christos 				expect_zu_eq(BIN_NREGS_READ(out), 0,
    146      1.1  christos 				    "Bin region count must be zero for "
    147      1.1  christos 				    "large sizes");
    148      1.1  christos 			}
    149      1.1  christos 		}
    150      1.1  christos 
    151      1.1  christos #undef BIN_NREGS_READ
    152      1.1  christos #undef BIN_NFREE_READ
    153      1.1  christos #undef SIZE_READ
    154      1.1  christos #undef NREGS_READ
    155      1.1  christos #undef NFREE_READ
    156      1.1  christos #undef COUNTS
    157      1.1  christos #undef SLABCUR_READ
    158      1.1  christos 
    159      1.1  christos 		free(out_ref);
    160      1.1  christos 		free(out);
    161      1.1  christos 		free(p);
    162      1.1  christos 	}
    163      1.1  christos }
    164      1.1  christos TEST_END
    165      1.1  christos 
    166      1.1  christos TEST_BEGIN(test_batch) {
    167      1.1  christos 	size_t sz;
    168      1.1  christos 	/*
    169      1.1  christos 	 * Select some sizes that can span both small and large sizes, and are
    170      1.1  christos 	 * numerically unrelated to any size boundaries.
    171      1.1  christos 	 */
    172      1.1  christos 	for (sz = 17; sz <= TEST_MAX_SIZE && sz <= SC_LARGE_MAXCLASS;
    173  1.1.1.2  christos 	     sz += (sz <= SC_SMALL_MAXCLASS ? 1019 : 99991)) {
    174  1.1.1.2  christos 		void  *p = mallocx(sz, 0);
    175  1.1.1.2  christos 		void  *q = mallocx(sz, 0);
    176  1.1.1.2  christos 		void  *in[] = {p, q};
    177      1.1  christos 		size_t in_sz = sizeof(const void *) * 2;
    178      1.1  christos 		size_t out[] = {-1, -1, -1, -1, -1, -1};
    179      1.1  christos 		size_t out_sz = sizeof(size_t) * 6;
    180      1.1  christos 		size_t out_ref[] = {-1, -1, -1, -1, -1, -1};
    181      1.1  christos 		size_t out_sz_ref = out_sz;
    182      1.1  christos 
    183      1.1  christos 		assert_ptr_not_null(p, "test pointer allocation failed");
    184      1.1  christos 		assert_ptr_not_null(q, "test pointer allocation failed");
    185      1.1  christos 
    186      1.1  christos 		/* Test invalid argument(s) errors */
    187  1.1.1.2  christos 		TEST_UTIL_BATCH_EINVAL(NULL, &out_sz, in, in_sz, "old is NULL");
    188  1.1.1.2  christos 		TEST_UTIL_BATCH_EINVAL(out, NULL, in, in_sz, "oldlenp is NULL");
    189  1.1.1.2  christos 		TEST_UTIL_BATCH_EINVAL(
    190  1.1.1.2  christos 		    out, &out_sz, NULL, in_sz, "newp is NULL");
    191  1.1.1.2  christos 		TEST_UTIL_BATCH_EINVAL(out, &out_sz, in, 0, "newlen is zero");
    192      1.1  christos 		in_sz -= 1;
    193  1.1.1.2  christos 		TEST_UTIL_BATCH_EINVAL(
    194  1.1.1.2  christos 		    out, &out_sz, in, in_sz, "newlen is not an exact multiple");
    195      1.1  christos 		in_sz += 1;
    196      1.1  christos 		out_sz_ref = out_sz -= 2 * sizeof(size_t);
    197      1.1  christos 		TEST_UTIL_BATCH_EINVAL(out, &out_sz, in, in_sz,
    198      1.1  christos 		    "*oldlenp is not an exact multiple");
    199      1.1  christos 		out_sz_ref = out_sz += 2 * sizeof(size_t);
    200      1.1  christos 		in_sz -= sizeof(const void *);
    201      1.1  christos 		TEST_UTIL_BATCH_EINVAL(out, &out_sz, in, in_sz,
    202      1.1  christos 		    "*oldlenp and newlen do not match");
    203      1.1  christos 		in_sz += sizeof(const void *);
    204      1.1  christos 
    205  1.1.1.2  christos 		/* Examine output for valid calls */
    206  1.1.1.2  christos #define TEST_EQUAL_REF(i, message)                                             \
    207      1.1  christos 	assert_d_eq(memcmp(out + (i) * 3, out_ref + (i) * 3, 3), 0, message)
    208      1.1  christos 
    209      1.1  christos #define NFREE_READ(out, i) out[(i) * 3]
    210      1.1  christos #define NREGS_READ(out, i) out[(i) * 3 + 1]
    211      1.1  christos #define SIZE_READ(out, i) out[(i) * 3 + 2]
    212      1.1  christos 
    213      1.1  christos 		out_sz_ref = out_sz /= 2;
    214      1.1  christos 		in_sz /= 2;
    215      1.1  christos 		TEST_UTIL_BATCH_VALID;
    216      1.1  christos 		expect_zu_le(sz, SIZE_READ(out, 0),
    217      1.1  christos 		    "Extent size should be at least allocation size");
    218      1.1  christos 		expect_zu_eq(SIZE_READ(out, 0) & (PAGE - 1), 0,
    219      1.1  christos 		    "Extent size should be a multiple of page size");
    220      1.1  christos 		/*
    221      1.1  christos 		 * See the corresponding comment in test_query; profiling breaks
    222      1.1  christos 		 * our slab count expectations.
    223      1.1  christos 		 */
    224      1.1  christos 		if (sz <= SC_SMALL_MAXCLASS && !opt_prof) {
    225      1.1  christos 			expect_zu_le(NFREE_READ(out, 0), NREGS_READ(out, 0),
    226      1.1  christos 			    "Extent free count exceeded region count");
    227      1.1  christos 			expect_zu_le(NREGS_READ(out, 0), SIZE_READ(out, 0),
    228      1.1  christos 			    "Extent region count exceeded size");
    229      1.1  christos 			expect_zu_ne(NREGS_READ(out, 0), 0,
    230      1.1  christos 			    "Extent region count must be positive");
    231      1.1  christos 		} else if (sz > SC_SMALL_MAXCLASS) {
    232      1.1  christos 			expect_zu_eq(NFREE_READ(out, 0), 0,
    233      1.1  christos 			    "Extent free count should be zero");
    234      1.1  christos 			expect_zu_eq(NREGS_READ(out, 0), 1,
    235      1.1  christos 			    "Extent region count should be one");
    236      1.1  christos 		}
    237  1.1.1.2  christos 		TEST_EQUAL_REF(
    238  1.1.1.2  christos 		    1, "Should not overwrite content beyond what's needed");
    239      1.1  christos 		in_sz *= 2;
    240      1.1  christos 		out_sz_ref = out_sz *= 2;
    241      1.1  christos 
    242      1.1  christos 		memcpy(out_ref, out, 3 * sizeof(size_t));
    243      1.1  christos 		TEST_UTIL_BATCH_VALID;
    244      1.1  christos 		TEST_EQUAL_REF(0, "Statistics should be stable across calls");
    245      1.1  christos 		if (sz <= SC_SMALL_MAXCLASS) {
    246      1.1  christos 			expect_zu_le(NFREE_READ(out, 1), NREGS_READ(out, 1),
    247      1.1  christos 			    "Extent free count exceeded region count");
    248      1.1  christos 		} else {
    249      1.1  christos 			expect_zu_eq(NFREE_READ(out, 0), 0,
    250      1.1  christos 			    "Extent free count should be zero");
    251      1.1  christos 		}
    252      1.1  christos 		expect_zu_eq(NREGS_READ(out, 0), NREGS_READ(out, 1),
    253      1.1  christos 		    "Extent region count should be same for same region size");
    254      1.1  christos 		expect_zu_eq(SIZE_READ(out, 0), SIZE_READ(out, 1),
    255      1.1  christos 		    "Extent size should be same for same region size");
    256      1.1  christos 
    257      1.1  christos #undef SIZE_READ
    258      1.1  christos #undef NREGS_READ
    259      1.1  christos #undef NFREE_READ
    260      1.1  christos 
    261      1.1  christos #undef TEST_EQUAL_REF
    262      1.1  christos 
    263      1.1  christos 		free(q);
    264      1.1  christos 		free(p);
    265      1.1  christos 	}
    266      1.1  christos }
    267      1.1  christos TEST_END
    268      1.1  christos 
    269      1.1  christos int
    270      1.1  christos main(void) {
    271      1.1  christos 	assert_zu_lt(SC_SMALL_MAXCLASS + 100000, TEST_MAX_SIZE,
    272      1.1  christos 	    "Test case cannot cover large classes");
    273      1.1  christos 	return test(test_query, test_batch);
    274      1.1  christos }
    275