11.3. Configuring HugeTLB at boot time
The HugeTLB enables the use of huge pages by reserving them at boot time, thereby minimizing memory fragmentation and ensuring that sufficient resources are available for workloads that can benefit from larger memory pages.
You can reserve Huge Translation Lookaside Buffer (HugeTLB) pages at the earliest stage of boot process by using kernel command-line parameters. This method provides the highest chance of successfully reserving the required number and size of huge pages, because memory is allocated during the kernel boot.
Prefer reserving HugeTLB pages by using kernel boot parameters, as this method ensures allocation of larger contiguous memory regions compared to using a systemd unit.
The examples in the procedure demonstrate how to use the command-line options for configuring HugeTLB pages. These examples do not necessarily apply to your system configuration. Review your system requirements and objectives before applying these settings in your environment.
Prerequisites
- You must have root privileges on your system.
Procedure
Update the kernel command line to include HugeLTB options.
To reserve HugeTLB pages of the default size for your architecture, enter:
# grubby --update-kernel=ALL --args="hugepages=10"This command reserves 10 HugeTLB pages of the default pool size. For example, on
x86_64, the default pool size is2 MB. On systems with Non-Uniform Memory Access (NUMA), the allocation is distributed evenly across NUMA nodes. If the system has two NUMA nodes, each node reserves five pages.To reserve different sizes of HugeTLB pages, specify the
hugepageszandhugepagesoptions in the kernel command line, enter:# grubby --update-kernel=ALL --args="hugepagesz=2M hugepages=10 hugepagesz=1G hugepages=1"This command reserves 10 pages of
2 MBeach and 1 page of1 GB.The system reserves the specified number and size of HugeTLB pages at boot time, ensuring that memory is allocated before the operating system begins normal operation.
重要The order of the options is significant. Each
hugepagesz=option must be immediately followed by its correspondinghugepages=option.
You can configure Huge Translation Lookaside Buffer (HugeTLB) pages during the user-space booting process by using a systemd service unit. This method reserves large memory pages after the kernel has initialized but before starting of most of user-space services. Although this approach is not as early as kernel command-line configuration, it can still be effective for ensuring that applications have access to the required huge pages during system operation.
Prerequisites
- You must have root privileges on your system.
Procedure
Create a new file called
hugetlb-gigantic-pages.servicein the/usr/lib/systemd/system/directory and add the following content:[Unit] Description=HugeTLB Gigantic Pages Reservation DefaultDependencies=no Before=dev-hugepages.mount ConditionPathExists=/sys/devices/system/node ConditionKernelCommandLine=hugepagesz=1G [Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/lib/systemd/hugetlb-reserve-pages.sh [Install] WantedBy=sysinit.targetCreate a new file called
hugetlb-reserve-pages.shin the/usr/lib/systemd/directory and add the following content:#!/bin/sh nodes_path=/sys/devices/system/node/ if [ ! -d $nodes_path ]; then echo "ERROR: $nodes_path does not exist" exit 1 fi reserve_pages() { echo $1 > $nodes_path/$2/hugepages/hugepages-1048576kB/nr_hugepages } reserve_pages <number_of_pages> <node>Replace
<number_of_pages>with the number of 1GB pages you want to reserve, and<node>with the node name on which to reserve these pages. For example, to reserve two 1 GB pages onnode0and one 1GB page onnode1, replace<number_of_pages>with2fornode0and1fornode1.Create an executable script:
# chmod +x /usr/lib/systemd/hugetlb-reserve-pages.shEnable early boot reservation:
# systemctl enable hugetlb-gigantic-pages.service注意-
You can try reserving more 1 GB pages at runtime by writing to the
nr_hugepagesattribute at any time. However, to prevent failures due to memory fragmentation, reserve 1 GB pages early during the boot process. - Reserving static huge pages can effectively reduce the amount of memory available to the system, and prevent it from using its full memory capacity. Although a properly sized pool of reserved huge pages can be beneficial to applications that use it, an oversized or unused pool of reserved huge pages will eventually be detrimental to the overall system performance. When setting a reserved huge page pool, ensure that the system can properly use its full memory capacity.
-
You can try reserving more 1 GB pages at runtime by writing to the