Skip to content

Commit f0bf91a

Browse files
committed
Add size-gated hugepage optimization to system allocator
This patch adds optional hugepage optimization by applying madvise(MADV_HUGEPAGE) to allocations that are large enough to benefit from transparent hugepages. Current behavior: - Uses MADV_NOHUGEPAGE for infrequent access patterns, MADV_HUGEPAGE only for memory collapsing - No proactive hugepages for large allocations Enhanced behavior: - Adds MADV_HUGEPAGE for allocations >= kHugePageSize (2MB) - Only applies to allocations that can actually benefit from hugepages - Avoids kernel overhead on small allocations The size threshold ensures we only hint hugepage usage for allocations that can actually be backed by hugepages, avoiding unnecessary kernel overhead for smaller allocations.
1 parent 7c90c4d commit f0bf91a

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

tcmalloc/internal/system_allocator.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,15 @@ SystemAllocator<Topology, NormalPartitions>::MmapRegion::Alloc(
377377
// This is only advisory, so ignore the error.
378378
ErrnoRestorer errno_restorer;
379379
(void)madvise(result_ptr, actual_size, MADV_NOHUGEPAGE);
380+
} else {
381+
// Opt-in to transparent hugepages for large allocations when system is
382+
// configured for transparent_hugepage=madvise. This can improve memory
383+
// performance by reducing TLB pressure.
384+
if (actual_size >= kHugePageSize) {
385+
// This is only advisory, so ignore the error.
386+
ErrnoRestorer errno_restorer;
387+
(void)madvise(result_ptr, actual_size, MADV_HUGEPAGE);
388+
}
380389
}
381390
free_size_ -= actual_size;
382391
return {result_ptr, actual_size};

0 commit comments

Comments
 (0)