Hugepages
HugepageRegion
Linux-only hugepage-backed anonymous mmap helper. Allocates a
region backed by 2MB or 1GB hugepages (MAP_HUGETLB |
MAP_HUGE_2MB / MAP_HUGE_1GB). Useful for large rings where
TLB pressure measurably hurts: a 16MB region fits in 8 hugepages
vs 4096 4KB pages.
API
| Constant / item | Value |
|---|---|
HUGEPAGE_2MB | 2 * 1024 * 1024 |
HUGEPAGE_1GB | 1024 * 1024 * 1024 |
HugepageSize::Mb2 | 2 MB pages (most widely available). |
HugepageSize::Gb1 | 1 GB pages (require kernel hugetlbfs + reserved gigabyte pages at boot). |
| Call | Behavior |
|---|---|
HugepageRegion::allocate(pages: usize, size: HugepageSize) | Allocate pages * size.bytes() bytes backed by the requested hugepage size. Returns io::Result<Self>. |
region.as_mut_slice() -> &mut [u8] | Mutable byte slice. |
region.as_slice() -> &[u8] | Read-only byte slice. |
region.len() -> usize | Region size in bytes. |
region.is_empty() -> bool | Always false for a valid region. |
Drop runs munmap on the region.
HugepageRegion is anonymous: in-process and fork-shared only. The
cross-process counterpart is a file on a hugetlbfs mount, which
unrelated processes map by path.
| Call | Behavior |
|---|---|
SharedHugepageRegion::create(path, pages, size) | Obtain the region: initialize the file only when the path does not yet exist, otherwise attach with its contents in place. Racing creators elect one initializer; the rest wait on the exact byte size, which is the ready signal because a hugetlbfs region carries no header. A different page count is refused. |
SharedHugepageRegion::reset(path, pages, size) | Truncate and reinitialize, for a caller that owns the path. |
SharedHugepageRegion::open(path, pages, size) | Map an existing region. The exact byte size is checked, same as attach. |
region.as_mut_slice() / as_slice() / len() | As above. |
The elected creator owns the path and unlinks it on drop; an attacher
or an open does not, so a peer’s drop never removes the region out
from under the creator.
Falling back gracefully
allocate returns Err(io::Error) when the kernel does not have
the requested hugepage size reserved. Callers handle the fallback
by allocating via standard MmapOptions::map_anon for a 4KB-page
region.
To reserve hugepages on a Linux host:
echo 64 > /proc/sys/vm/nr_hugepages # 64 * 2MB = 128MBWhen to reach for this primitive
- Very large rings (multi-MB) where TLB miss latency measurably hurts the hot path.
- Workloads that benefit from kernel-level hugepage-aware memory management (THP off, manual hugepages on).
When not to reach for this
- Small rings (< 1MB). 4KB pages are fine; hugepages add no measurable benefit and pin scarce reserved pages.
- Hosts without reserved hugepages.
allocatefails immediately on those.
References
- Large Pages
- the Windows
sibling (
VirtualAlloc(MEM_LARGE_PAGES)for private regions plusSEC_LARGE_PAGESnamed sections for cross-process sharing, which this Linux module does not cover - hugepage cross-process sharing on Linux goes through hugetlbfs ormemfd_create(MFD_HUGETLB)). SpscRingCore,SharedRing- the ring primitives most sensitive to TLB pressure at large capacities.HugepageRegionimplementsRegionOwner, soSpscRingCore::create_in_region/SharedRing::create_in_regionandSharedRingMpmc::create_grid_in_regionlay a ring (or a whole many-lane grid) straight into the hugepage bytes. The Windows sibling with named cross-process sections islarge_pages.