GET_HUGEPAGE_REGION(3)
π GET_HUGEPAGE_REGION(3) π
okay bestie, this is a page from the Library Functions Manual and it's about get_hugepage_region and free_hugepage_region β the duo that hands you memory backed by hugepages when it can. let's get into it π«Ά
β¨ NAME β¨
get_hugepage_region, free_hugepage_region β the iconic pair that allocates and frees regions of memory that use hugepages where possible π
πΈ SYNOPSIS πΈ
#include <hugetlbfs.h>
void *get_hugepage_region(size_t len, ghr_t flags);
void free_hugepage_region(void *ptr);
like. two functions. one header. so clean β¨
π¦ DESCRIPTION π¦
so get_hugepage_region() allocates a memory region that's len bytes in size, backed by hugepages. why do you care? bc hugepages may be of benefit to applications that use large amounts of address space and are suffering a performance hit due to TLB misses π. and how do you even know if it's helping? well β wall-clock time or oprofile can be used to determine if there's a performance benefit from using hugepages or not. vibe check your workload, basically.
now, unlike get_huge_pages(), len does not have to be hugepage-aligned β although memory may be wasted due to alignment. and the caller may also specify that base pages be used in the event there are no hugepages available. flexible queen behavior π
the flags argument changes the behaviour of the function. flags may be or'd together. so:
GHR_FALLBACKβ use base pages if there are an insufficient number of huge pages.GHR_STRICTβ use hugepages or returnNULL. no backup plan, no safety net, all or nothing πGHR_COLORβ when specified, bytes that would be wasted due to alignment are used to color the buffer by offsetting it by a random cacheline within the hugepage. this avoids a performance problem whereby multiple buffers use the same cache lines at the same offsets. so if it is not important that the start of the buffer be page-aligned, specify this flag. it's giving β¨ chaos but productive β¨GHR_DEFAULTβ the library chooses a sensible combination of flags for allocating a region of memory. the current default is:
GHR_FALLBACK | GHR_COLOR
and then free_hugepage_region() frees a region of memory allocated by get_hugepage_region().
π¨ okay listen: the behaviour of the function if another pointer is used, valid or otherwise, is undefined. undefined. so don't be cute with it, bestie. pass the pointer you got. periodt.
π RETURN VALUE π
- on success, a pointer is returned for to the allocated memory π
- on error,
NULLis returned. errnowill be set based on what the failure ofmmap()was due to.
π SEE ALSO π
go read them, iykyk π
π AUTHORS π
libhugetlbfs was written by various people on the libhugetlbfs-devel mailing list. a whole group chat of legends fr fr π«Ά
and that's the whole page, bestie!! this one's dated November 7, 2008 and it comes to you from libhugetlbfs β so if you're reading this on a modern system, just know the source and version are doing their little archival moment π stay hugepage-aligned (or don't, GHR_COLOR says it's fine) and slay π§β¨
π¦ where this page comes from
- debian bookworm: libhugetlbfs-dev 2.23-5 Β· LGPL-2.1-only
- debian trixie: libhugetlbfs-dev 2.24-1+b1 Β· LGPL-2.1-only
- nixpkgs unstable: libhugetlbfs 2.24
- opensuse-leap 16.0: libhugetlbfs-devel 2.24.0.g1322884-160000.3.2 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libhugetlbfs-devel 2.24.0.g1322884-160000.3.2 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libhugetlbfs-devel 2.24.0.g1322884-160000.3.2 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libhugetlbfs-devel 2.24.0.g1322884-160000.3.2 Β· LGPL-2.1-or-later
- ubuntu jammy: libhugetlbfs-dev 2.23-4 Β· LGPL-2.1-only
- ubuntu noble: libhugetlbfs-dev 2.23-5build1 Β· LGPL-2.1-only
- ubuntu questing: libhugetlbfs-dev 2.24-1
- ubuntu resolute: libhugetlbfs-dev 2.24-1build1 Β· LGPL-2.1-only
- ubuntu stonking: libhugetlbfs-dev 2.24-1build1 Β· LGPL-2.1-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms