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 return NULL. 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, NULL is returned.
  • errno will be set based on what the failure of mmap() 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 🧚✨