mirror of
https://github.com/jemalloc/jemalloc.git
synced 2026-07-08 23:07:20 +03:00
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:
parent
ac279d7e71
commit
c067a55c79
33 changed files with 713 additions and 74 deletions
28
src/arena.c
28
src/arena.c
|
|
@ -145,8 +145,18 @@ arena_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,
|
|||
assert(nmalloc - ndalloc <= SIZE_T_MAX);
|
||||
size_t curlextents = (size_t)(nmalloc - ndalloc);
|
||||
lstats[i].curlextents += curlextents;
|
||||
astats->allocated_large +=
|
||||
curlextents * sz_index2size(SC_NBINS + i);
|
||||
|
||||
if (config_limit_usize_gap) {
|
||||
uint64_t active_bytes = locked_read_u64(tsdn,
|
||||
LOCKEDINT_MTX(arena->stats.mtx),
|
||||
&arena->stats.lstats[i].active_bytes);
|
||||
locked_inc_u64_unsynchronized(
|
||||
&lstats[i].active_bytes, active_bytes);
|
||||
astats->allocated_large += active_bytes;
|
||||
} else {
|
||||
astats->allocated_large +=
|
||||
curlextents * sz_index2size(SC_NBINS + i);
|
||||
}
|
||||
}
|
||||
|
||||
pa_shard_stats_merge(tsdn, &arena->pa_shard, &astats->pa_shard_stats,
|
||||
|
|
@ -315,6 +325,11 @@ arena_large_malloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t usize) {
|
|||
LOCKEDINT_MTX_LOCK(tsdn, arena->stats.mtx);
|
||||
locked_inc_u64(tsdn, LOCKEDINT_MTX(arena->stats.mtx),
|
||||
&arena->stats.lstats[hindex].nmalloc, 1);
|
||||
if (config_limit_usize_gap) {
|
||||
locked_inc_u64(tsdn, LOCKEDINT_MTX(arena->stats.mtx),
|
||||
&arena->stats.lstats[hindex].active_bytes,
|
||||
usize);
|
||||
}
|
||||
LOCKEDINT_MTX_UNLOCK(tsdn, arena->stats.mtx);
|
||||
}
|
||||
}
|
||||
|
|
@ -338,6 +353,11 @@ arena_large_dalloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t usize) {
|
|||
LOCKEDINT_MTX_LOCK(tsdn, arena->stats.mtx);
|
||||
locked_inc_u64(tsdn, LOCKEDINT_MTX(arena->stats.mtx),
|
||||
&arena->stats.lstats[hindex].ndalloc, 1);
|
||||
if (config_limit_usize_gap) {
|
||||
locked_dec_u64(tsdn, LOCKEDINT_MTX(arena->stats.mtx),
|
||||
&arena->stats.lstats[hindex].active_bytes,
|
||||
usize);
|
||||
}
|
||||
LOCKEDINT_MTX_UNLOCK(tsdn, arena->stats.mtx);
|
||||
}
|
||||
}
|
||||
|
|
@ -802,7 +822,7 @@ arena_reset(tsd_t *tsd, arena_t *arena) {
|
|||
assert(alloc_ctx.szind != SC_NSIZES);
|
||||
|
||||
if (config_stats || (config_prof && opt_prof)) {
|
||||
usize = sz_index2size(alloc_ctx.szind);
|
||||
usize = emap_alloc_ctx_usize_get(&alloc_ctx);
|
||||
assert(usize == isalloc(tsd_tsdn(tsd), ptr));
|
||||
}
|
||||
/* Remove large allocation from prof sample set. */
|
||||
|
|
@ -1346,7 +1366,7 @@ arena_malloc_hard(tsdn_t *tsdn, arena_t *arena, size_t size, szind_t ind,
|
|||
assert(sz_can_use_slab(size));
|
||||
return arena_malloc_small(tsdn, arena, ind, zero);
|
||||
} else {
|
||||
return large_malloc(tsdn, arena, sz_index2size(ind), zero);
|
||||
return large_malloc(tsdn, arena, sz_s2u(size), zero);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -168,6 +168,7 @@ CTL_PROTO(opt_prof_sys_thread_name)
|
|||
CTL_PROTO(opt_prof_time_res)
|
||||
CTL_PROTO(opt_lg_san_uaf_align)
|
||||
CTL_PROTO(opt_zero_realloc)
|
||||
CTL_PROTO(opt_limit_usize_gap)
|
||||
CTL_PROTO(opt_malloc_conf_symlink)
|
||||
CTL_PROTO(opt_malloc_conf_env_var)
|
||||
CTL_PROTO(opt_malloc_conf_global_var)
|
||||
|
|
@ -557,6 +558,7 @@ static const ctl_named_node_t opt_node[] = {
|
|||
{NAME("zero_realloc"), CTL(opt_zero_realloc)},
|
||||
{NAME("debug_double_free_max_scan"),
|
||||
CTL(opt_debug_double_free_max_scan)},
|
||||
{NAME("limit_usize_gap"), CTL(opt_limit_usize_gap)},
|
||||
{NAME("malloc_conf"), CHILD(named, opt_malloc_conf)}
|
||||
};
|
||||
|
||||
|
|
@ -2341,6 +2343,8 @@ CTL_RO_NL_CGEN(config_uaf_detection, opt_lg_san_uaf_align,
|
|||
opt_lg_san_uaf_align, ssize_t)
|
||||
CTL_RO_NL_GEN(opt_zero_realloc,
|
||||
zero_realloc_mode_names[opt_zero_realloc_action], const char *)
|
||||
CTL_RO_NL_CGEN(config_limit_usize_gap, opt_limit_usize_gap, opt_limit_usize_gap,
|
||||
bool)
|
||||
|
||||
/* malloc_conf options */
|
||||
CTL_RO_NL_CGEN(opt_malloc_conf_symlink, opt_malloc_conf_symlink,
|
||||
|
|
@ -3364,8 +3368,8 @@ arenas_bin_i_index(tsdn_t *tsdn, const size_t *mib,
|
|||
}
|
||||
|
||||
CTL_RO_NL_GEN(arenas_nlextents, SC_NSIZES - SC_NBINS, unsigned)
|
||||
CTL_RO_NL_GEN(arenas_lextent_i_size, sz_index2size(SC_NBINS+(szind_t)mib[2]),
|
||||
size_t)
|
||||
CTL_RO_NL_GEN(arenas_lextent_i_size,
|
||||
sz_index2size_unsafe(SC_NBINS+(szind_t)mib[2]), size_t)
|
||||
static const ctl_named_node_t *
|
||||
arenas_lextent_i_index(tsdn_t *tsdn, const size_t *mib,
|
||||
size_t miblen, size_t i) {
|
||||
|
|
|
|||
119
src/eset.c
119
src/eset.c
|
|
@ -155,6 +155,71 @@ eset_remove(eset_t *eset, edata_t *edata) {
|
|||
cur_extents_npages - (size >> LG_PAGE), ATOMIC_RELAXED);
|
||||
}
|
||||
|
||||
edata_t *
|
||||
eset_enumerate_alignment_search(eset_t *eset, size_t size, pszind_t bin_ind,
|
||||
size_t alignment) {
|
||||
if (edata_heap_empty(&eset->bins[bin_ind].heap)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
edata_t *edata = NULL;
|
||||
edata_heap_enumerate_helper_t helper;
|
||||
edata_heap_enumerate_prepare(&eset->bins[bin_ind].heap, &helper,
|
||||
ESET_ENUMERATE_MAX_NUM, sizeof(helper.bfs_queue)/sizeof(void *));
|
||||
while ((edata =
|
||||
edata_heap_enumerate_next(&eset->bins[bin_ind].heap, &helper)) !=
|
||||
NULL) {
|
||||
uintptr_t base = (uintptr_t)edata_base_get(edata);
|
||||
size_t candidate_size = edata_size_get(edata);
|
||||
if (candidate_size < size) {
|
||||
continue;
|
||||
}
|
||||
|
||||
uintptr_t next_align = ALIGNMENT_CEILING((uintptr_t)base,
|
||||
PAGE_CEILING(alignment));
|
||||
if (base > next_align || base + candidate_size <= next_align) {
|
||||
/* Overflow or not crossing the next alignment. */
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t leadsize = next_align - base;
|
||||
if (candidate_size - leadsize >= size) {
|
||||
return edata;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
edata_t *
|
||||
eset_enumerate_search(eset_t *eset, size_t size, pszind_t bin_ind,
|
||||
bool exact_only, edata_cmp_summary_t *ret_summ) {
|
||||
if (edata_heap_empty(&eset->bins[bin_ind].heap)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
edata_t *ret = NULL, *edata = NULL;
|
||||
edata_heap_enumerate_helper_t helper;
|
||||
edata_heap_enumerate_prepare(&eset->bins[bin_ind].heap, &helper,
|
||||
ESET_ENUMERATE_MAX_NUM, sizeof(helper.bfs_queue)/sizeof(void *));
|
||||
while ((edata =
|
||||
edata_heap_enumerate_next(&eset->bins[bin_ind].heap, &helper)) !=
|
||||
NULL) {
|
||||
if ((!exact_only && edata_size_get(edata) >= size) ||
|
||||
(exact_only && edata_size_get(edata) == size)) {
|
||||
edata_cmp_summary_t temp_summ =
|
||||
edata_cmp_summary_get(edata);
|
||||
if (ret == NULL || edata_cmp_summary_comp(temp_summ,
|
||||
*ret_summ) < 0) {
|
||||
ret = edata;
|
||||
*ret_summ = temp_summ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find an extent with size [min_size, max_size) to satisfy the alignment
|
||||
* requirement. For each size, try only the first extent in the heap.
|
||||
|
|
@ -162,8 +227,19 @@ eset_remove(eset_t *eset, edata_t *edata) {
|
|||
static edata_t *
|
||||
eset_fit_alignment(eset_t *eset, size_t min_size, size_t max_size,
|
||||
size_t alignment) {
|
||||
pszind_t pind = sz_psz2ind(sz_psz_quantize_ceil(min_size));
|
||||
pszind_t pind_max = sz_psz2ind(sz_psz_quantize_ceil(max_size));
|
||||
pszind_t pind = sz_psz2ind(sz_psz_quantize_ceil(min_size));
|
||||
pszind_t pind_max = sz_psz2ind(sz_psz_quantize_ceil(max_size));
|
||||
|
||||
/* See comments in eset_first_fit for why we enumerate search below. */
|
||||
pszind_t pind_prev = sz_psz2ind(sz_psz_quantize_floor(min_size));
|
||||
if (sz_limit_usize_gap_enabled() && pind != pind_prev) {
|
||||
edata_t *ret = NULL;
|
||||
ret = eset_enumerate_alignment_search(eset, min_size, pind_prev,
|
||||
alignment);
|
||||
if (ret != NULL) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
for (pszind_t i =
|
||||
(pszind_t)fb_ffs(eset->bitmap, ESET_NPSIZES, (size_t)pind);
|
||||
|
|
@ -211,8 +287,43 @@ eset_first_fit(eset_t *eset, size_t size, bool exact_only,
|
|||
pszind_t pind = sz_psz2ind(sz_psz_quantize_ceil(size));
|
||||
|
||||
if (exact_only) {
|
||||
return edata_heap_empty(&eset->bins[pind].heap) ? NULL :
|
||||
edata_heap_first(&eset->bins[pind].heap);
|
||||
if (sz_limit_usize_gap_enabled()) {
|
||||
pszind_t pind_prev =
|
||||
sz_psz2ind(sz_psz_quantize_floor(size));
|
||||
return eset_enumerate_search(eset, size, pind_prev,
|
||||
/* exact_only */ true, &ret_summ);
|
||||
} else {
|
||||
return edata_heap_empty(&eset->bins[pind].heap) ? NULL:
|
||||
edata_heap_first(&eset->bins[pind].heap);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Each element in the eset->bins is a heap corresponding to a size
|
||||
* class. When sz_limit_usize_gap_enabled() is false, all heaps after
|
||||
* pind (including pind itself) will surely satisfy the rquests while
|
||||
* heaps before pind cannot satisfy the request because usize is
|
||||
* calculated based on size classes then. However, when
|
||||
* sz_limit_usize_gap_enabled() is true, usize is calculated by ceiling
|
||||
* user requested size to the closest multiple of PAGE. This means in
|
||||
* the heap before pind, i.e., pind_prev, there may exist extents able
|
||||
* to satisfy the request and we should enumerate the heap when
|
||||
* pind_prev != pind.
|
||||
*
|
||||
* For example, when PAGE=4KB and the user requested size is 1MB + 4KB,
|
||||
* usize would be 1.25MB when sz_limit_usize_gap_enabled() is false.
|
||||
* pind points to the heap containing extents ranging in
|
||||
* [1.25MB, 1.5MB). Thus, searching starting from pind will not miss
|
||||
* any candidates. When sz_limit_usize_gap_enabled() is true, the
|
||||
* usize would be 1MB + 4KB and pind still points to the same heap.
|
||||
* In this case, the heap pind_prev points to, which contains extents
|
||||
* in the range [1MB, 1.25MB), may contain candidates satisfying the
|
||||
* usize and thus should be enumerated.
|
||||
*/
|
||||
pszind_t pind_prev = sz_psz2ind(sz_psz_quantize_floor(size));
|
||||
if (sz_limit_usize_gap_enabled() && pind != pind_prev){
|
||||
ret = eset_enumerate_search(eset, size, pind_prev,
|
||||
/* exact_only */ false, &ret_summ);
|
||||
}
|
||||
|
||||
for (pszind_t i =
|
||||
|
|
|
|||
|
|
@ -706,7 +706,7 @@ hpa_alloc_batch_psset(tsdn_t *tsdn, hpa_shard_t *shard, size_t size,
|
|||
bool *deferred_work_generated) {
|
||||
assert(size <= HUGEPAGE);
|
||||
assert(size <= shard->opts.slab_max_alloc ||
|
||||
size == sz_index2size(sz_size2index(size)));
|
||||
size == sz_s2u(size));
|
||||
bool oom = false;
|
||||
|
||||
size_t nsuccess = hpa_try_alloc_batch_no_grow(tsdn, shard, size, &oom,
|
||||
|
|
|
|||
|
|
@ -123,6 +123,13 @@ zero_realloc_action_t opt_zero_realloc_action =
|
|||
|
||||
atomic_zu_t zero_realloc_count = ATOMIC_INIT(0);
|
||||
|
||||
bool opt_limit_usize_gap =
|
||||
#ifdef LIMIT_USIZE_GAP
|
||||
true;
|
||||
#else
|
||||
false;
|
||||
#endif
|
||||
|
||||
const char *const zero_realloc_mode_names[] = {
|
||||
"alloc",
|
||||
"free",
|
||||
|
|
@ -1578,8 +1585,8 @@ malloc_conf_init_helper(sc_data_t *sc_data, unsigned bin_shard_sizes[SC_NBINS],
|
|||
"hpa_sec_nshards", 0, 0, CONF_CHECK_MIN,
|
||||
CONF_DONT_CHECK_MAX, true);
|
||||
CONF_HANDLE_SIZE_T(opt_hpa_sec_opts.max_alloc,
|
||||
"hpa_sec_max_alloc", PAGE, 0, CONF_CHECK_MIN,
|
||||
CONF_DONT_CHECK_MAX, true);
|
||||
"hpa_sec_max_alloc", PAGE, USIZE_GROW_SLOW_THRESHOLD,
|
||||
CONF_CHECK_MIN, CONF_CHECK_MAX, true);
|
||||
CONF_HANDLE_SIZE_T(opt_hpa_sec_opts.max_bytes,
|
||||
"hpa_sec_max_bytes", PAGE, 0, CONF_CHECK_MIN,
|
||||
CONF_DONT_CHECK_MAX, true);
|
||||
|
|
@ -1763,6 +1770,11 @@ malloc_conf_init_helper(sc_data_t *sc_data, unsigned bin_shard_sizes[SC_NBINS],
|
|||
"san_guard_large", 0, SIZE_T_MAX,
|
||||
CONF_DONT_CHECK_MIN, CONF_DONT_CHECK_MAX, false)
|
||||
|
||||
if (config_limit_usize_gap) {
|
||||
CONF_HANDLE_BOOL(opt_limit_usize_gap,
|
||||
"limit_usize_gap");
|
||||
}
|
||||
|
||||
CONF_ERROR("Invalid conf pair", k, klen, v, vlen);
|
||||
#undef CONF_ERROR
|
||||
#undef CONF_CONTINUE
|
||||
|
|
@ -2182,6 +2194,17 @@ static bool
|
|||
malloc_init_hard(void) {
|
||||
tsd_t *tsd;
|
||||
|
||||
if (config_limit_usize_gap) {
|
||||
assert(TCACHE_MAXCLASS_LIMIT <= USIZE_GROW_SLOW_THRESHOLD);
|
||||
assert(SC_LOOKUP_MAXCLASS <= USIZE_GROW_SLOW_THRESHOLD);
|
||||
/*
|
||||
* This asserts an extreme case where TINY_MAXCLASS is larger
|
||||
* than LARGE_MINCLASS. It could only happen if some constants
|
||||
* are configured miserably wrong.
|
||||
*/
|
||||
assert(SC_LG_TINY_MAXCLASS <=
|
||||
(size_t)1ULL << (LG_PAGE + SC_LG_NGROUP));
|
||||
}
|
||||
#if defined(_WIN32) && _WIN32_WINNT < 0x0600
|
||||
_init_init_lock();
|
||||
#endif
|
||||
|
|
@ -2376,7 +2399,8 @@ aligned_usize_get(size_t size, size_t alignment, size_t *usize, szind_t *ind,
|
|||
if (unlikely(*ind >= SC_NSIZES)) {
|
||||
return true;
|
||||
}
|
||||
*usize = sz_index2size(*ind);
|
||||
*usize = sz_limit_usize_gap_enabled()? sz_s2u(size):
|
||||
sz_index2size(*ind);
|
||||
assert(*usize > 0 && *usize <= SC_LARGE_MAXCLASS);
|
||||
return false;
|
||||
}
|
||||
|
|
@ -2924,7 +2948,7 @@ ifree(tsd_t *tsd, void *ptr, tcache_t *tcache, bool slow_path) {
|
|||
&alloc_ctx);
|
||||
assert(alloc_ctx.szind != SC_NSIZES);
|
||||
|
||||
size_t usize = sz_index2size(alloc_ctx.szind);
|
||||
size_t usize = emap_alloc_ctx_usize_get(&alloc_ctx);
|
||||
if (config_prof && opt_prof) {
|
||||
prof_free(tsd, ptr, usize, &alloc_ctx);
|
||||
}
|
||||
|
|
@ -2956,35 +2980,41 @@ isfree(tsd_t *tsd, void *ptr, size_t usize, tcache_t *tcache, bool slow_path) {
|
|||
assert(malloc_initialized() || IS_INITIALIZER);
|
||||
|
||||
emap_alloc_ctx_t alloc_ctx;
|
||||
szind_t szind = sz_size2index(usize);
|
||||
if (!config_prof) {
|
||||
alloc_ctx.szind = sz_size2index(usize);
|
||||
alloc_ctx.slab = (alloc_ctx.szind < SC_NBINS);
|
||||
emap_alloc_ctx_init(&alloc_ctx, szind, (szind < SC_NBINS),
|
||||
usize);
|
||||
} else {
|
||||
if (likely(!prof_sample_aligned(ptr))) {
|
||||
/*
|
||||
* When the ptr is not page aligned, it was not sampled.
|
||||
* usize can be trusted to determine szind and slab.
|
||||
*/
|
||||
alloc_ctx.szind = sz_size2index(usize);
|
||||
alloc_ctx.slab = (alloc_ctx.szind < SC_NBINS);
|
||||
emap_alloc_ctx_init(&alloc_ctx, szind,
|
||||
(szind < SC_NBINS), usize);
|
||||
} else if (opt_prof) {
|
||||
/*
|
||||
* Small sampled allocs promoted can still get correct
|
||||
* usize here. Check comments in edata_usize_get.
|
||||
*/
|
||||
emap_alloc_ctx_lookup(tsd_tsdn(tsd), &arena_emap_global,
|
||||
ptr, &alloc_ctx);
|
||||
|
||||
if (config_opt_safety_checks) {
|
||||
/* Small alloc may have !slab (sampled). */
|
||||
size_t true_size =
|
||||
emap_alloc_ctx_usize_get(&alloc_ctx);
|
||||
if (unlikely(alloc_ctx.szind !=
|
||||
sz_size2index(usize))) {
|
||||
safety_check_fail_sized_dealloc(
|
||||
/* current_dealloc */ true, ptr,
|
||||
/* true_size */ sz_index2size(
|
||||
alloc_ctx.szind),
|
||||
/* true_size */ true_size,
|
||||
/* input_size */ usize);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
alloc_ctx.szind = sz_size2index(usize);
|
||||
alloc_ctx.slab = (alloc_ctx.szind < SC_NBINS);
|
||||
emap_alloc_ctx_init(&alloc_ctx, szind,
|
||||
(szind < SC_NBINS), usize);
|
||||
}
|
||||
}
|
||||
bool fail = maybe_check_alloc_ctx(tsd, ptr, &alloc_ctx);
|
||||
|
|
@ -3486,7 +3516,7 @@ do_rallocx(void *ptr, size_t size, int flags, bool is_realloc) {
|
|||
emap_alloc_ctx_lookup(tsd_tsdn(tsd), &arena_emap_global, ptr,
|
||||
&alloc_ctx);
|
||||
assert(alloc_ctx.szind != SC_NSIZES);
|
||||
old_usize = sz_index2size(alloc_ctx.szind);
|
||||
old_usize = emap_alloc_ctx_usize_get(&alloc_ctx);
|
||||
assert(old_usize == isalloc(tsd_tsdn(tsd), ptr));
|
||||
if (aligned_usize_get(size, alignment, &usize, NULL, false)) {
|
||||
goto label_oom;
|
||||
|
|
@ -3756,7 +3786,7 @@ je_xallocx(void *ptr, size_t size, size_t extra, int flags) {
|
|||
emap_alloc_ctx_lookup(tsd_tsdn(tsd), &arena_emap_global, ptr,
|
||||
&alloc_ctx);
|
||||
assert(alloc_ctx.szind != SC_NSIZES);
|
||||
old_usize = sz_index2size(alloc_ctx.szind);
|
||||
old_usize = emap_alloc_ctx_usize_get(&alloc_ctx);
|
||||
assert(old_usize == isalloc(tsd_tsdn(tsd), ptr));
|
||||
/*
|
||||
* The API explicitly absolves itself of protecting against (size +
|
||||
|
|
|
|||
|
|
@ -513,7 +513,13 @@ void prof_unbias_map_init(void) {
|
|||
/* See the comment in prof_sample_new_event_wait */
|
||||
#ifdef JEMALLOC_PROF
|
||||
for (szind_t i = 0; i < SC_NSIZES; i++) {
|
||||
double sz = (double)sz_index2size(i);
|
||||
/*
|
||||
* When limit_usize_gap is enabled, the unbiased calculation
|
||||
* here is not as accurate as it was because usize now changes
|
||||
* in a finer grain while the unbiased_sz is still calculated
|
||||
* using the old way.
|
||||
*/
|
||||
double sz = (double)sz_index2size_unsafe(i);
|
||||
double rate = (double)(ZU(1) << lg_prof_sample);
|
||||
double div_val = 1.0 - exp(-sz / rate);
|
||||
double unbiased_sz = sz / div_val;
|
||||
|
|
|
|||
34
src/psset.c
34
src/psset.c
|
|
@ -337,18 +337,50 @@ psset_update_end(psset_t *psset, hpdata_t *ps) {
|
|||
hpdata_assert_consistent(ps);
|
||||
}
|
||||
|
||||
hpdata_t *
|
||||
psset_enumerate_search(psset_t *psset, pszind_t pind, size_t size) {
|
||||
if (hpdata_age_heap_empty(&psset->pageslabs[pind])) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hpdata_t *ps = NULL;
|
||||
hpdata_age_heap_enumerate_helper_t helper;
|
||||
hpdata_age_heap_enumerate_prepare(&psset->pageslabs[pind], &helper,
|
||||
PSSET_ENUMERATE_MAX_NUM, sizeof(helper.bfs_queue) / sizeof(void *));
|
||||
|
||||
while ((ps = hpdata_age_heap_enumerate_next(&psset->pageslabs[pind],
|
||||
&helper))) {
|
||||
if (hpdata_longest_free_range_get(ps) >= size) {
|
||||
return ps;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hpdata_t *
|
||||
psset_pick_alloc(psset_t *psset, size_t size) {
|
||||
assert((size & PAGE_MASK) == 0);
|
||||
assert(size <= HUGEPAGE);
|
||||
|
||||
pszind_t min_pind = sz_psz2ind(sz_psz_quantize_ceil(size));
|
||||
hpdata_t *ps = NULL;
|
||||
|
||||
/* See comments in eset_first_fit for why we enumerate search below. */
|
||||
pszind_t pind_prev = sz_psz2ind(sz_psz_quantize_floor(size));
|
||||
if (sz_limit_usize_gap_enabled() && pind_prev < min_pind) {
|
||||
ps = psset_enumerate_search(psset, pind_prev, size);
|
||||
if (ps != NULL) {
|
||||
return ps;
|
||||
}
|
||||
}
|
||||
|
||||
pszind_t pind = (pszind_t)fb_ffs(psset->pageslab_bitmap, PSSET_NPSIZES,
|
||||
(size_t)min_pind);
|
||||
if (pind == PSSET_NPSIZES) {
|
||||
return hpdata_empty_list_first(&psset->empty);
|
||||
}
|
||||
hpdata_t *ps = hpdata_age_heap_first(&psset->pageslabs[pind]);
|
||||
ps = hpdata_age_heap_first(&psset->pageslabs[pind]);
|
||||
if (ps == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,13 @@ bool
|
|||
sec_init(tsdn_t *tsdn, sec_t *sec, base_t *base, pai_t *fallback,
|
||||
const sec_opts_t *opts) {
|
||||
assert(opts->max_alloc >= PAGE);
|
||||
/*
|
||||
* Same as tcache, sec do not cache allocs/dallocs larger than
|
||||
* USIZE_GROW_SLOW_THRESHOLD because the usize above this increases
|
||||
* by PAGE and the number of usizes is too large.
|
||||
*/
|
||||
assert(!sz_limit_usize_gap_enabled() ||
|
||||
opts->max_alloc <= USIZE_GROW_SLOW_THRESHOLD);
|
||||
|
||||
size_t max_alloc = PAGE_FLOOR(opts->max_alloc);
|
||||
pszind_t npsizes = sz_psz2ind(max_alloc) + 1;
|
||||
|
|
|
|||
|
|
@ -1047,7 +1047,8 @@ tcache_bin_flush_impl_large(tsd_t *tsd, tcache_t *tcache, cache_bin_t *cache_bin
|
|||
ndeferred++;
|
||||
continue;
|
||||
}
|
||||
if (large_dalloc_safety_checks(edata, ptr, binind)) {
|
||||
if (large_dalloc_safety_checks(edata, ptr,
|
||||
sz_index2size(binind))) {
|
||||
/* See the comment in isfree. */
|
||||
continue;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue