Skip to content

How do I configure HugePages on my Amazon EC2 Linux instance?

3 minute read
1

I want to configure HugePages on my Amazon Elastic Compute Cloud (Amazon EC2) Linux instance.

Resolution

Note: It's a best practice to configure HugePages in a test environment and benchmark performance before you deploy HugePages to production. HugePage size can only be 2 MB or 1 GB on x86_64 systems. For more information about HugePages, see Configuring HugePages on Linux on the Oracle website.

Prerequisites: To verify that your application or database supports HugePages, check with your vendor. Also, to determine the amount of available memory, run the following command to verify the total memory of the EC2 instance:

free -m 

To configure HugePages on your Linux instance, complete the following steps:

  1. Run the following command to verify that your kernel doesn't have HugePages activated:

    sudo cat /proc/sys/vm/nr_hugepages

    If the command output is 0, then HugePages isn't activated.

  2. To calculate the number of HugePages that you require, use the following formula:
    Number of HugePages = Required memory / HugePage size
    Note: Round up the result to make sure that you create enough pages.

  3. Run the following command to activate HugePages and set the kernel parameter value:

    sudo sysctl -w vm.nr_hugepages=2048

    Note: Replace 2048 with the number of HugePages that you calculated.
    Example output:

    vm.nr_hugepages = 2048
  4. To verify that the system allocates HugePages after reboot, add the following entry to the /etc/sysctl.conf file:

    sudo echo "vm.nr_hugepages=2048" >> /etc/sysctl.conf

    Note: Replace 2048 with the number of HugePages that you calculated.

  5. To verify your changes, run the following command:

    sudo cat /etc/sysctl.conf

    In the command output, verify that there's a value for vm.nr_hugepages.
    Example output:

    # sudo cat /etc/sysctl.conf
    # sysctl settings are defined through files in
    # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
    #
    # Vendors settings live in /usr/lib/sysctl.d/.
    # To override a whole file, create a new file with the same in
    # /etc/sysctl.d/ and put new settings there. To override
    # only specific settings, add a file with a lexically later
    # name in /etc/sysctl.d/ and put new settings there.
    #
    # For more information, see sysctl.conf(5) and sysctl.d(5).
    vm.zone_reclaim_mode=1
    vm.nr_hugepages=2048
  6. Reboot your instance.

  7. Rerun the following command to verify the HugePages value and configuration:

    sudo cat /proc/sys/vm/nr_hugepages

    If the output is the number of HugePages that you calculated, then HugePages is activated.
    Example output:

    # sudo cat /proc/sys/vm/nr_hugepages
    2048
  8. To check the available HugePages in /proc/meminfo, run the following command:

    sudo grep Huge /proc/meminfo

    Example output:

    # sudo grep Huge /proc/meminfo
    HugePages_Total:    2048
    HugePages_Free:     2048
    HugePages_Rsvd:        0
    HugePages_Surp:        0
    Hugepagesize:       2048 kB

    For definitions of each HugePages value, see HugePages on kernel.org.

To deactivate HugePages, complete the following steps:

  1. Run the following command to reset nr_hugepages to 0:

    sudo sysctl -w vm.nr_hugepages=0
  2. Remove the following entry from sysctl.conf:

    sudo echo "vm.nr_hugepages=2048" >> /etc/sysctl.conf
  3. Reboot your instance.

AWS OFFICIALUpdated 14 days ago