2019-04-10 02:01:26 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
|
|
|
#define TEST_UTIL_EINVAL(node, a, b, c, d, why_inval) do { \
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl("experimental.utilization." node, \
|
2019-04-10 02:01:26 +08:00
|
|
|
a, b, c, d), EINVAL, "Should fail when " why_inval); \
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(out_sz, out_sz_ref, \
|
2019-04-10 02:01:26 +08:00
|
|
|
"Output size touched when given invalid arguments"); \
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(memcmp(out, out_ref, out_sz_ref), 0, \
|
2019-04-10 02:01:26 +08:00
|
|
|
"Output content touched when given invalid arguments"); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define TEST_UTIL_QUERY_EINVAL(a, b, c, d, why_inval) \
|
|
|
|
TEST_UTIL_EINVAL("query", a, b, c, d, why_inval)
|
|
|
|
#define TEST_UTIL_BATCH_EINVAL(a, b, c, d, why_inval) \
|
|
|
|
TEST_UTIL_EINVAL("batch_query", a, b, c, d, why_inval)
|
|
|
|
|
|
|
|
#define TEST_UTIL_VALID(node) do { \
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl("experimental.utilization." node, \
|
2019-04-10 02:01:26 +08:00
|
|
|
out, &out_sz, in, in_sz), 0, \
|
|
|
|
"Should return 0 on correct arguments"); \
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(out_sz, out_sz_ref, "incorrect output size"); \
|
|
|
|
expect_d_ne(memcmp(out, out_ref, out_sz_ref), 0, \
|
2019-04-10 02:01:26 +08:00
|
|
|
"Output content should be changed"); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define TEST_UTIL_BATCH_VALID TEST_UTIL_VALID("batch_query")
|
|
|
|
|
2019-05-14 05:59:33 +08:00
|
|
|
#define TEST_MAX_SIZE (1 << 20)
|
|
|
|
|
2019-04-10 02:01:26 +08:00
|
|
|
TEST_BEGIN(test_query) {
|
2019-05-14 05:59:33 +08:00
|
|
|
size_t sz;
|
|
|
|
/*
|
|
|
|
* Select some sizes that can span both small and large sizes, and are
|
|
|
|
* numerically unrelated to any size boundaries.
|
|
|
|
*/
|
|
|
|
for (sz = 7; sz <= TEST_MAX_SIZE && sz <= SC_LARGE_MAXCLASS;
|
|
|
|
sz += (sz <= SC_SMALL_MAXCLASS ? 1009 : 99989)) {
|
|
|
|
void *p = mallocx(sz, 0);
|
|
|
|
void **in = &p;
|
|
|
|
size_t in_sz = sizeof(const void *);
|
|
|
|
size_t out_sz = sizeof(void *) + sizeof(size_t) * 5;
|
|
|
|
void *out = mallocx(out_sz, 0);
|
|
|
|
void *out_ref = mallocx(out_sz, 0);
|
|
|
|
size_t out_sz_ref = out_sz;
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(p,
|
2019-05-14 05:59:33 +08:00
|
|
|
"test pointer allocation failed");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(out,
|
2019-05-14 05:59:33 +08:00
|
|
|
"test output allocation failed");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(out_ref,
|
2019-05-14 05:59:33 +08:00
|
|
|
"test reference output allocation failed");
|
2019-04-10 02:01:26 +08:00
|
|
|
|
|
|
|
#define SLABCUR_READ(out) (*(void **)out)
|
|
|
|
#define COUNTS(out) ((size_t *)((void **)out + 1))
|
|
|
|
#define NFREE_READ(out) COUNTS(out)[0]
|
|
|
|
#define NREGS_READ(out) COUNTS(out)[1]
|
|
|
|
#define SIZE_READ(out) COUNTS(out)[2]
|
|
|
|
#define BIN_NFREE_READ(out) COUNTS(out)[3]
|
|
|
|
#define BIN_NREGS_READ(out) COUNTS(out)[4]
|
|
|
|
|
2019-05-14 05:59:33 +08:00
|
|
|
SLABCUR_READ(out) = NULL;
|
|
|
|
NFREE_READ(out) = NREGS_READ(out) = SIZE_READ(out) = -1;
|
|
|
|
BIN_NFREE_READ(out) = BIN_NREGS_READ(out) = -1;
|
|
|
|
memcpy(out_ref, out, out_sz);
|
|
|
|
|
|
|
|
/* Test invalid argument(s) errors */
|
|
|
|
TEST_UTIL_QUERY_EINVAL(NULL, &out_sz, in, in_sz,
|
|
|
|
"old is NULL");
|
|
|
|
TEST_UTIL_QUERY_EINVAL(out, NULL, in, in_sz,
|
|
|
|
"oldlenp is NULL");
|
|
|
|
TEST_UTIL_QUERY_EINVAL(out, &out_sz, NULL, in_sz,
|
|
|
|
"newp is NULL");
|
|
|
|
TEST_UTIL_QUERY_EINVAL(out, &out_sz, in, 0,
|
|
|
|
"newlen is zero");
|
|
|
|
in_sz -= 1;
|
|
|
|
TEST_UTIL_QUERY_EINVAL(out, &out_sz, in, in_sz,
|
|
|
|
"invalid newlen");
|
|
|
|
in_sz += 1;
|
|
|
|
out_sz_ref = out_sz -= 2 * sizeof(size_t);
|
|
|
|
TEST_UTIL_QUERY_EINVAL(out, &out_sz, in, in_sz,
|
|
|
|
"invalid *oldlenp");
|
|
|
|
out_sz_ref = out_sz += 2 * sizeof(size_t);
|
|
|
|
|
|
|
|
/* Examine output for valid call */
|
|
|
|
TEST_UTIL_VALID("query");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_le(sz, SIZE_READ(out),
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent size should be at least allocation size");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(SIZE_READ(out) & (PAGE - 1), 0,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent size should be a multiple of page size");
|
|
|
|
if (sz <= SC_SMALL_MAXCLASS) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_le(NFREE_READ(out), NREGS_READ(out),
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent free count exceeded region count");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_le(NREGS_READ(out), SIZE_READ(out),
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent region count exceeded size");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_ne(NREGS_READ(out), 0,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent region count must be positive");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_true(NFREE_READ(out) == 0 || (SLABCUR_READ(out)
|
2019-10-29 00:24:42 +08:00
|
|
|
!= NULL && SLABCUR_READ(out) <= p),
|
2019-05-14 05:59:33 +08:00
|
|
|
"Allocation should follow first fit principle");
|
|
|
|
if (config_stats) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_le(BIN_NFREE_READ(out),
|
2019-05-14 05:59:33 +08:00
|
|
|
BIN_NREGS_READ(out),
|
|
|
|
"Bin free count exceeded region count");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_ne(BIN_NREGS_READ(out), 0,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Bin region count must be positive");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_le(NFREE_READ(out),
|
2019-05-14 05:59:33 +08:00
|
|
|
BIN_NFREE_READ(out),
|
|
|
|
"Extent free count exceeded bin free count");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_le(NREGS_READ(out),
|
2019-05-14 05:59:33 +08:00
|
|
|
BIN_NREGS_READ(out),
|
|
|
|
"Extent region count exceeded "
|
|
|
|
"bin region count");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(BIN_NREGS_READ(out)
|
2019-05-14 05:59:33 +08:00
|
|
|
% NREGS_READ(out), 0,
|
|
|
|
"Bin region count isn't a multiple of "
|
|
|
|
"extent region count");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_le(
|
2019-05-14 05:59:33 +08:00
|
|
|
BIN_NFREE_READ(out) - NFREE_READ(out),
|
|
|
|
BIN_NREGS_READ(out) - NREGS_READ(out),
|
|
|
|
"Free count in other extents in the bin "
|
|
|
|
"exceeded region count in other extents "
|
|
|
|
"in the bin");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_le(NREGS_READ(out) - NFREE_READ(out),
|
2019-05-14 05:59:33 +08:00
|
|
|
BIN_NREGS_READ(out) - BIN_NFREE_READ(out),
|
|
|
|
"Extent utilized count exceeded "
|
|
|
|
"bin utilized count");
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(NFREE_READ(out), 0,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent free count should be zero");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(NREGS_READ(out), 1,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent region count should be one");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_null(SLABCUR_READ(out),
|
2019-05-14 05:59:33 +08:00
|
|
|
"Current slab must be null for large size classes");
|
|
|
|
if (config_stats) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(BIN_NFREE_READ(out), 0,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Bin free count must be zero for "
|
|
|
|
"large sizes");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(BIN_NREGS_READ(out), 0,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Bin region count must be zero for "
|
|
|
|
"large sizes");
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 02:01:26 +08:00
|
|
|
|
|
|
|
#undef BIN_NREGS_READ
|
|
|
|
#undef BIN_NFREE_READ
|
|
|
|
#undef SIZE_READ
|
|
|
|
#undef NREGS_READ
|
|
|
|
#undef NFREE_READ
|
|
|
|
#undef COUNTS
|
|
|
|
#undef SLABCUR_READ
|
|
|
|
|
2019-05-14 05:59:33 +08:00
|
|
|
free(out_ref);
|
|
|
|
free(out);
|
|
|
|
free(p);
|
|
|
|
}
|
2019-04-10 02:01:26 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
TEST_BEGIN(test_batch) {
|
2019-05-14 05:59:33 +08:00
|
|
|
size_t sz;
|
|
|
|
/*
|
|
|
|
* Select some sizes that can span both small and large sizes, and are
|
|
|
|
* numerically unrelated to any size boundaries.
|
|
|
|
*/
|
|
|
|
for (sz = 17; sz <= TEST_MAX_SIZE && sz <= SC_LARGE_MAXCLASS;
|
|
|
|
sz += (sz <= SC_SMALL_MAXCLASS ? 1019 : 99991)) {
|
|
|
|
void *p = mallocx(sz, 0);
|
|
|
|
void *q = mallocx(sz, 0);
|
|
|
|
void *in[] = {p, q};
|
|
|
|
size_t in_sz = sizeof(const void *) * 2;
|
|
|
|
size_t out[] = {-1, -1, -1, -1, -1, -1};
|
|
|
|
size_t out_sz = sizeof(size_t) * 6;
|
|
|
|
size_t out_ref[] = {-1, -1, -1, -1, -1, -1};
|
|
|
|
size_t out_sz_ref = out_sz;
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(p, "test pointer allocation failed");
|
|
|
|
expect_ptr_not_null(q, "test pointer allocation failed");
|
2019-05-14 05:59:33 +08:00
|
|
|
|
|
|
|
/* Test invalid argument(s) errors */
|
|
|
|
TEST_UTIL_BATCH_EINVAL(NULL, &out_sz, in, in_sz,
|
|
|
|
"old is NULL");
|
|
|
|
TEST_UTIL_BATCH_EINVAL(out, NULL, in, in_sz,
|
|
|
|
"oldlenp is NULL");
|
|
|
|
TEST_UTIL_BATCH_EINVAL(out, &out_sz, NULL, in_sz,
|
|
|
|
"newp is NULL");
|
|
|
|
TEST_UTIL_BATCH_EINVAL(out, &out_sz, in, 0,
|
|
|
|
"newlen is zero");
|
|
|
|
in_sz -= 1;
|
|
|
|
TEST_UTIL_BATCH_EINVAL(out, &out_sz, in, in_sz,
|
|
|
|
"newlen is not an exact multiple");
|
|
|
|
in_sz += 1;
|
|
|
|
out_sz_ref = out_sz -= 2 * sizeof(size_t);
|
|
|
|
TEST_UTIL_BATCH_EINVAL(out, &out_sz, in, in_sz,
|
|
|
|
"*oldlenp is not an exact multiple");
|
|
|
|
out_sz_ref = out_sz += 2 * sizeof(size_t);
|
|
|
|
in_sz -= sizeof(const void *);
|
|
|
|
TEST_UTIL_BATCH_EINVAL(out, &out_sz, in, in_sz,
|
|
|
|
"*oldlenp and newlen do not match");
|
|
|
|
in_sz += sizeof(const void *);
|
2019-04-10 02:01:26 +08:00
|
|
|
|
|
|
|
/* Examine output for valid calls */
|
|
|
|
#define TEST_EQUAL_REF(i, message) \
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(memcmp(out + (i) * 3, out_ref + (i) * 3, 3), 0, message)
|
2019-04-10 02:01:26 +08:00
|
|
|
|
|
|
|
#define NFREE_READ(out, i) out[(i) * 3]
|
|
|
|
#define NREGS_READ(out, i) out[(i) * 3 + 1]
|
|
|
|
#define SIZE_READ(out, i) out[(i) * 3 + 2]
|
|
|
|
|
2019-05-14 05:59:33 +08:00
|
|
|
out_sz_ref = out_sz /= 2;
|
|
|
|
in_sz /= 2;
|
|
|
|
TEST_UTIL_BATCH_VALID;
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_le(sz, SIZE_READ(out, 0),
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent size should be at least allocation size");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(SIZE_READ(out, 0) & (PAGE - 1), 0,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent size should be a multiple of page size");
|
|
|
|
if (sz <= SC_SMALL_MAXCLASS) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_le(NFREE_READ(out, 0), NREGS_READ(out, 0),
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent free count exceeded region count");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_le(NREGS_READ(out, 0), SIZE_READ(out, 0),
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent region count exceeded size");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_ne(NREGS_READ(out, 0), 0,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent region count must be positive");
|
|
|
|
} else {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(NFREE_READ(out, 0), 0,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent free count should be zero");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(NREGS_READ(out, 0), 1,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent region count should be one");
|
|
|
|
}
|
|
|
|
TEST_EQUAL_REF(1,
|
|
|
|
"Should not overwrite content beyond what's needed");
|
|
|
|
in_sz *= 2;
|
|
|
|
out_sz_ref = out_sz *= 2;
|
|
|
|
|
|
|
|
memcpy(out_ref, out, 3 * sizeof(size_t));
|
|
|
|
TEST_UTIL_BATCH_VALID;
|
|
|
|
TEST_EQUAL_REF(0, "Statistics should be stable across calls");
|
|
|
|
if (sz <= SC_SMALL_MAXCLASS) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_le(NFREE_READ(out, 1), NREGS_READ(out, 1),
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent free count exceeded region count");
|
|
|
|
} else {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(NFREE_READ(out, 0), 0,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent free count should be zero");
|
|
|
|
}
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(NREGS_READ(out, 0), NREGS_READ(out, 1),
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent region count should be same for same region size");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(SIZE_READ(out, 0), SIZE_READ(out, 1),
|
2019-05-14 05:59:33 +08:00
|
|
|
"Extent size should be same for same region size");
|
2019-04-10 02:01:26 +08:00
|
|
|
|
|
|
|
#undef SIZE_READ
|
|
|
|
#undef NREGS_READ
|
|
|
|
#undef NFREE_READ
|
|
|
|
|
|
|
|
#undef TEST_EQUAL_REF
|
|
|
|
|
2019-05-14 05:59:33 +08:00
|
|
|
free(q);
|
|
|
|
free(p);
|
|
|
|
}
|
2019-04-10 02:01:26 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_lt(SC_SMALL_MAXCLASS, TEST_MAX_SIZE,
|
2019-05-14 05:59:33 +08:00
|
|
|
"Test case cannot cover large classes");
|
2019-04-10 02:01:26 +08:00
|
|
|
return test(test_query, test_batch);
|
|
|
|
}
|