Introducing a new usize calculation policy

Converting size to usize is what jemalloc has been done by ceiling
size to the closest size class. However, this causes lots of memory
wastes with HPA enabled.  This commit changes how usize is calculated so
that the gap between two contiguous usize is no larger than a page.
Specifically, this commit includes the following changes:

1. Adding a build-time config option (--enable-limit-usize-gap) and a
runtime one (limit_usize_gap) to guard the changes.
When build-time
config is enabled, some minor CPU overhead is expected because usize
will be stored and accessed apart from index.  When runtime option is
also enabled (it can only be enabled with the build-time config
enabled). a new usize calculation approach wil be employed.  This new
calculation will ceil size to the closest multiple of PAGE for all sizes
larger than USIZE_GROW_SLOW_THRESHOLD instead of using the size classes.
Note when the build-time config is enabled, the runtime option is
default on.

2. Prepare tcache for size to grow by PAGE over GROUP*PAGE.
To prepare for the upcoming changes where size class grows by PAGE when
larger than NGROUP * PAGE, disable the tcache when it is larger than 2 *
NGROUP * PAGE. The threshold for tcache is set higher to prevent perf
regression as much as possible while usizes between NGROUP * PAGE and 2 *
NGROUP * PAGE happen to grow by PAGE.

3. Prepare pac and hpa psset for size to grow by PAGE over GROUP*PAGE
For PAC, to avoid having too many bins, arena bins still have the same
layout.  This means some extra search is needed for a page-level request that
is not aligned with the orginal size class: it should also search the heap
before the current index since the previous heap might also be able to
have some allocations satisfying it.  The same changes apply to HPA's
psset.
This search relies on the enumeration of the heap because not all allocs in
the previous heap are guaranteed to satisfy the request.  To balance the
memory and CPU overhead, we currently enumerate at most a fixed number
of nodes before concluding none can satisfy the request during an
enumeration.

4. Add bytes counter to arena large stats.
To prepare for the upcoming usize changes, stats collected by
multiplying alive allocations and the bin size is no longer accurate.
Thus, add separate counters to record the bytes malloced and dalloced.

5. Change structs use when freeing to avoid using index2size for large sizes.
  - Change the definition of emap_alloc_ctx_t
  - Change the read of both from edata_t.
  - Change the assignment and usage of emap_alloc_ctx_t.
  - Change other callsites of index2size.
Note for the changes in the data structure, i.e., emap_alloc_ctx_t,
will be used when the build-time config (--enable-limit-usize-gap) is
enabled but they will store the same value as index2size(szind) if the
runtime option (opt_limit_usize_gap) is not enabled.

6. Adapt hpa to the usize changes.
Change the settings in sec to limit is usage for sizes larger than
USIZE_GROW_SLOW_THRESHOLD and modify corresponding tests.

7. Modify usize calculation and corresponding tests.
Change the sz_s2u_compute. Note sz_index2size is not always safe now
while sz_size2index still works as expected.
This commit is contained in:
guangli-dai 2024-03-26 14:35:29 -07:00 committed by Qi Wang
parent ac279d7e71
commit c067a55c79
33 changed files with 713 additions and 74 deletions

View file

@ -49,7 +49,7 @@ TEST_BEGIN(test_grow_and_shrink) {
size_t tsz;
#define NCYCLES 3
unsigned i, j;
#define NSZS 1024
#define NSZS 64
size_t szs[NSZS];
#define MAXSZ ZU(12 * 1024 * 1024)

View file

@ -43,6 +43,7 @@ for t in $@; do
# per test shell script to ignore the @JEMALLOC_CPREFIX@ detail).
enable_fill=@enable_fill@ \
enable_prof=@enable_prof@ \
limit_usize_gap=@limit_usize_gap@ \
. @srcroot@${t}.sh && \
export_malloc_conf && \
$JEMALLOC_TEST_PREFIX ${t}@exe@ @abs_srcroot@ @abs_objroot@

View file

@ -78,7 +78,8 @@ vsalloc(tsdn_t *tsdn, const void *ptr) {
return 0;
}
return sz_index2size(full_alloc_ctx.szind);
return config_limit_usize_gap? edata_usize_get(full_alloc_ctx.edata):
sz_index2size(full_alloc_ctx.szind);
}
static unsigned

View file

@ -5,7 +5,7 @@
#define SHARD_IND 111
#define ALLOC_MAX (HUGEPAGE / 4)
#define ALLOC_MAX (HUGEPAGE)
typedef struct test_data_s test_data_t;
struct test_data_s {

View file

@ -332,6 +332,7 @@ TEST_BEGIN(test_mallctl_opt) {
TEST_MALLCTL_OPT(bool, prof_sys_thread_name, prof);
TEST_MALLCTL_OPT(ssize_t, lg_san_uaf_align, uaf_detection);
TEST_MALLCTL_OPT(unsigned, debug_double_free_max_scan, always);
TEST_MALLCTL_OPT(bool, limit_usize_gap, limit_usize_gap);
#undef TEST_MALLCTL_OPT
}

View file

@ -2,8 +2,9 @@
#include "jemalloc/internal/ph.h"
#define BFS_ENUMERATE_MAX 30
typedef struct node_s node_t;
ph_structs(heap, node_t);
ph_structs(heap, node_t, BFS_ENUMERATE_MAX);
struct node_s {
#define NODE_MAGIC 0x9823af7e
@ -239,6 +240,22 @@ TEST_BEGIN(test_ph_random) {
expect_false(heap_empty(&heap),
"Heap should not be empty");
/* Enumerate nodes. */
heap_enumerate_helper_t helper;
uint16_t max_queue_size = sizeof(helper.bfs_queue)
/ sizeof(void *);
expect_u_eq(max_queue_size, BFS_ENUMERATE_MAX,
"Incorrect bfs queue length initialized");
assert(max_queue_size == BFS_ENUMERATE_MAX);
heap_enumerate_prepare(&heap, &helper,
BFS_ENUMERATE_MAX, max_queue_size);
size_t node_count = 0;
while(heap_enumerate_next(&heap, &helper)) {
node_count ++;
}
expect_lu_eq(node_count, j,
"Unexpected enumeration results.");
/* Remove nodes. */
switch (i % 6) {
case 0:

View file

@ -412,7 +412,8 @@ TEST_BEGIN(test_expand_shrink_delegate) {
bool deferred_work_generated = false;
test_sec_init(&sec, &ta.pai, /* nshards */ 1, /* max_alloc */ 10 * PAGE,
test_sec_init(&sec, &ta.pai, /* nshards */ 1,
/* max_alloc */ USIZE_GROW_SLOW_THRESHOLD,
/* max_bytes */ 1000 * PAGE);
edata_t *edata = pai_alloc(tsdn, &sec.pai, PAGE, PAGE,
/* zero */ false, /* guarded */ false, /* frequent_reuse */ false,

View file

@ -26,7 +26,8 @@ TEST_BEGIN(test_size_classes) {
size_t size_class, max_size_class;
szind_t index, gen_index, max_index;
max_size_class = get_max_size_class();
max_size_class = sz_limit_usize_gap_enabled()? SC_SMALL_MAXCLASS:
get_max_size_class();
max_index = sz_size2index(max_size_class);
for (index = 0, size_class = sz_index2size(index); index < max_index ||
@ -79,6 +80,40 @@ TEST_BEGIN(test_size_classes) {
}
TEST_END
TEST_BEGIN(test_grow_slow_size_classes) {
test_skip_if(!sz_limit_usize_gap_enabled());
size_t size = SC_LARGE_MINCLASS;
size_t target_usize = SC_LARGE_MINCLASS;
size_t max_size = get_max_size_class();
size_t increase[3] = {PAGE - 1, 1, 1};
while (size <= max_size) {
size_t usize = sz_s2u(size);
expect_zu_eq(usize, target_usize,
"sz_s2u() does not generate usize as expected.");
size += increase[0];
usize = sz_s2u(size);
target_usize += PAGE;
expect_zu_eq(usize, target_usize,
"sz_s2u() does not generate usize as expected.");
size += increase[1];
usize = sz_s2u(size);
expect_zu_eq(usize, target_usize,
"sz_s2u() does not generate usize as expected.");
size += increase[2];
usize = sz_s2u(size);
target_usize += PAGE;
expect_zu_eq(usize, target_usize,
"sz_s2u() does not generate usize as expected.");
if (target_usize << 1 < target_usize) {
break;
}
target_usize = target_usize << 1;
size = target_usize;
}
}
TEST_END
TEST_BEGIN(test_psize_classes) {
size_t size_class, max_psz;
pszind_t pind, max_pind;
@ -182,6 +217,7 @@ int
main(void) {
return test(
test_size_classes,
test_grow_slow_size_classes,
test_psize_classes,
test_overflow);
}

View file

@ -0,0 +1,5 @@
#!/bin/sh
if [ "x${limit_usize_gap}" = "x1" ] ; then
export MALLOC_CONF="limit_usize_gap:true"
fi

View file

@ -202,17 +202,22 @@ TEST_END
TEST_BEGIN(test_stats_arenas_large) {
void *p;
size_t sz, allocated;
size_t sz, allocated, allocated_before;
uint64_t epoch, nmalloc, ndalloc;
size_t malloc_size = (1U << (SC_LG_LARGE_MINCLASS + 1)) + 1;
int expected = config_stats ? 0 : ENOENT;
p = mallocx((1U << SC_LG_LARGE_MINCLASS), MALLOCX_ARENA(0));
sz = sizeof(size_t);
expect_d_eq(mallctl("stats.arenas.0.large.allocated",
(void *)&allocated_before, &sz, NULL, 0), expected,
"Unexpected mallctl() result");
p = mallocx(malloc_size, MALLOCX_ARENA(0));
expect_ptr_not_null(p, "Unexpected mallocx() failure");
expect_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
0, "Unexpected mallctl() failure");
sz = sizeof(size_t);
expect_d_eq(mallctl("stats.arenas.0.large.allocated",
(void *)&allocated, &sz, NULL, 0), expected,
"Unexpected mallctl() result");
@ -223,8 +228,10 @@ TEST_BEGIN(test_stats_arenas_large) {
&sz, NULL, 0), expected, "Unexpected mallctl() result");
if (config_stats) {
expect_zu_gt(allocated, 0,
expect_zu_ge(allocated_before, 0,
"allocated should be greater than zero");
expect_zu_ge(allocated - allocated_before, sz_s2u(malloc_size),
"the diff between allocated should be greater than the allocation made");
expect_u64_gt(nmalloc, 0,
"nmalloc should be greater than zero");
expect_u64_ge(nmalloc, ndalloc,