Skip to content

Instantly share code, notes, and snippets.

@pankkor
Last active October 22, 2023 22:13
Show Gist options
  • Save pankkor/8e6b87f914f5a6697469af85fbbbf1bf to your computer and use it in GitHub Desktop.
Save pankkor/8e6b87f914f5a6697469af85fbbbf1bf to your computer and use it in GitHub Desktop.
Windows large pages, Linux huge pages, macOS super pages support

Large, Huge, Super Pages

Linux Huge Pages

Transparent Huge Pages (THP) via madvice

Not covered here, see https://www.kernel.org/doc/Documentation/vm/transhuge.txt

Allocating huge pages using mmap

See https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt

Huge pages cannot be swapped out under memory pressure.

Verify that kernel supports Huge pages cat /proc/meminfo:

HugePages_Total: uuu
HugePages_Free:  vvv
HugePages_Rsvd:  www
HugePages_Surp:  xxx
Hugepagesize:    yyy kB
Hugetlb:         zzz kB

Hugepagesize is most probably 2MB.

Set up size of kernel huge pages pool (512 pages * 2MB = 1GB)

sudo sysctl -w vm.nr_hugepages=512

It's better be done early at the startup. This could fail due to memory fragmentation. Add this to /etc/sysctl.conf:

vm.nr_hugepages = 512

macOS Super Pages

macOS has no superpages support (aka Huge Pages (Linux), aka Large Pages (Windows)) on Apple silicon.

Theoretically you could use VM_FLAGS_SUPERPAGE_SIZE_2MB or VM_FLAGS_SUPERPAGE_SIZE_ANY flags, which are defined in <mach/mach_vm.h>

You could profive those flags to mac_vm_allocate() directly, or pass them in the highest bits of file desciptor of mmap():

mmap(0, superpage_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, VM_FLAGS_SUPERPAGE_SIZE_2MB, 0);

Example from tests/superpages/testsp.c

Unfortunately that doesn't work on M1/M2. The only platform that has superpages support is x86_64. It supports 2MB pages. None of the other platforms seems to support it.

osfmk/vm/vm_map.c

arm/pmap/pmap.h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment