Skip to content

Commit 78cb21e

Browse files
committed
Add configurable hugepage optimization to system allocator
- Add madvise_hugepage_mmap_enabled parameter (default false) - Remove size gating per @ckennelly's feedback (TCMalloc operates in hugepage chunks) - Advisory-only madvise calls with error restoration - Runtime configurable via TCMalloc_Internal_SetMadviseHugepageMmapEnabled() - No behavior change unless explicitly enabled The optimization opts into transparent hugepages when the system is configured for transparent_hugepage=madvise. This can improve memory performance by reducing TLB pressure for large allocations. Follows TCMalloc parameter patterns with atomic storage and weak function accessors for runtime configuration.
1 parent 7c90c4d commit 78cb21e

4 files changed

Lines changed: 25 additions & 0 deletions

File tree

tcmalloc/internal/parameter_accessors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ ABSL_ATTRIBUTE_WEAK void TCMalloc_Internal_SetUseUserspaceCollapseHeuristics(
109109
ABSL_ATTRIBUTE_WEAK bool TCMalloc_Internal_GetHeapPartitioning();
110110
ABSL_ATTRIBUTE_WEAK bool TCMalloc_Internal_GetBackSmallAllocations();
111111
ABSL_ATTRIBUTE_WEAK void TCMalloc_Internal_SetBackSmallAllocations(bool v);
112+
ABSL_ATTRIBUTE_WEAK bool TCMalloc_Internal_GetMadviseHugepageMmapEnabled();
113+
ABSL_ATTRIBUTE_WEAK void TCMalloc_Internal_SetMadviseHugepageMmapEnabled(bool v);
112114
ABSL_ATTRIBUTE_WEAK int32_t TCMalloc_Internal_GetBackSizeThresholdBytes();
113115
ABSL_ATTRIBUTE_WEAK void TCMalloc_Internal_SetBackSizeThresholdBytes(int32_t v);
114116
ABSL_ATTRIBUTE_WEAK bool TCMalloc_Internal_GetEnableUnfilteredCollapse();

tcmalloc/internal/system_allocator.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,13 @@ 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 if (Parameters::madvise_hugepage_mmap_enabled()) {
381+
// Opt-in to transparent hugepages when system is
382+
// configured for transparent_hugepage=madvise. This can improve memory
383+
// performance by reducing TLB pressure.
384+
// This is only advisory, so ignore the error.
385+
ErrnoRestorer errno_restorer;
386+
(void)madvise(result_ptr, actual_size, MADV_HUGEPAGE);
380387
}
381388
free_size_ -= actual_size;
382389
return {result_ptr, actual_size};

tcmalloc/parameters.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ ABSL_CONST_INIT std::atomic<int32_t> Parameters::back_size_threshold_bytes_(
220220
kPageSize);
221221
ABSL_CONST_INIT std::atomic<bool> Parameters::enable_unfiltered_collapse_(
222222
false);
223+
ABSL_CONST_INIT std::atomic<bool> Parameters::madvise_hugepage_mmap_enabled_(
224+
false);
223225

224226
static std::atomic<bool>& heap_partitioning_enabled() {
225227
ABSL_CONST_INIT static absl::once_flag flag;
@@ -318,6 +320,10 @@ bool TCMalloc_Internal_GetBackSmallAllocations() {
318320
return Parameters::back_small_allocations();
319321
}
320322

323+
bool TCMalloc_Internal_GetMadviseHugepageMmapEnabled() {
324+
return Parameters::madvise_hugepage_mmap_enabled();
325+
}
326+
321327
int ABSL_ATTRIBUTE_WEAK default_want_disable_dynamic_slabs();
322328

323329
// TODO(b/271475288): remove the default_want_disable_dynamic_slabs opt-out
@@ -620,6 +626,10 @@ void TCMalloc_Internal_SetBackSmallAllocations(bool v) {
620626
Parameters::back_small_allocations_.store(v, std::memory_order_relaxed);
621627
}
622628

629+
void TCMalloc_Internal_SetMadviseHugepageMmapEnabled(bool v) {
630+
Parameters::madvise_hugepage_mmap_enabled_.store(v, std::memory_order_relaxed);
631+
}
632+
623633
void TCMalloc_Internal_SetBackSizeThresholdBytes(int32_t v) {
624634
Parameters::back_size_threshold_bytes_.store(v, std::memory_order_relaxed);
625635
}

tcmalloc/parameters.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ class Parameters {
191191
TCMalloc_Internal_SetPerCpuCachesDynamicSlabShrinkThreshold(value);
192192
}
193193

194+
static bool madvise_hugepage_mmap_enabled() {
195+
return madvise_hugepage_mmap_enabled_.load(std::memory_order_relaxed);
196+
}
197+
194198
static bool heap_partitioning();
195199

196200
static central_freelist_internal::PriorityListLength priority_list_length();
@@ -231,6 +235,7 @@ class Parameters {
231235
friend void ::TCMalloc_Internal_SetBackSmallAllocations(bool v);
232236
friend void ::TCMalloc_Internal_SetBackSizeThresholdBytes(int32_t v);
233237
friend void ::TCMalloc_Internal_SetEnableUnfilteredCollapse(bool v);
238+
friend void ::TCMalloc_Internal_SetMadviseHugepageMmapEnabled(bool v);
234239

235240
static std::atomic<MallocExtension::BytesPerSecond> background_release_rate_;
236241
static std::atomic<int64_t> guarded_sampling_interval_;
@@ -252,6 +257,7 @@ class Parameters {
252257
static std::atomic<bool> back_small_allocations_;
253258
static std::atomic<int32_t> back_size_threshold_bytes_;
254259
static std::atomic<bool> enable_unfiltered_collapse_;
260+
static std::atomic<bool> madvise_hugepage_mmap_enabled_;
255261
};
256262

257263
} // namespace tcmalloc_internal

0 commit comments

Comments
 (0)