How to check if kernel hugepages are enabled in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if kernel hugepages are enabled and configured on a Linux system. Hugepages are a kernel feature that can improve performance for memory-intensive applications by using larger memory pages.

Through a series of practical steps, you will explore different methods to verify hugepage settings. You will start by examining the /proc/meminfo file to view current hugepage statistics, including the total, free, and reserved hugepages, as well as the hugepage size. Next, you will use the sysctl command to check the vm.nr_hugepages kernel parameter, which indicates the desired number of hugepages. Finally, you will inspect the /sys/kernel/mm directory to gain further insight into the hugepage configuration within the kernel's memory management subsystem. By completing these steps, you will gain a solid understanding of how to determine the hugepage status on your Linux system.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/TextProcessingGroup(["Text Processing"]) linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/FileandDirectoryManagementGroup -.-> linux/cd("Directory Changing") linux/TextProcessingGroup -.-> linux/grep("Pattern Searching") linux/UserandGroupManagementGroup -.-> linux/env("Environment Managing") subgraph Lab Skills linux/ls -.-> lab-558793{{"How to check if kernel hugepages are enabled in Linux"}} linux/cat -.-> lab-558793{{"How to check if kernel hugepages are enabled in Linux"}} linux/cd -.-> lab-558793{{"How to check if kernel hugepages are enabled in Linux"}} linux/grep -.-> lab-558793{{"How to check if kernel hugepages are enabled in Linux"}} linux/env -.-> lab-558793{{"How to check if kernel hugepages are enabled in Linux"}} end

Check hugepages in /proc/meminfo

In this step, we will explore how to check the current hugepages configuration on your Linux system. Hugepages are a feature of the Linux kernel that allows the system to use larger memory pages than the default 4KB size. This can improve performance for applications that use large amounts of memory, such as databases and virtualization software.

We can find information about hugepages in the /proc/meminfo file. This file contains various memory-related statistics for the system.

Open the terminal if you haven't already. You can do this by clicking the Xfce Terminal icon on the left side of the desktop.

Now, let's view the contents of /proc/meminfo using the cat command. The cat command is used to display the content of files.

Type the following command in your terminal and press Enter:

cat /proc/meminfo

This command will print the entire content of the /proc/meminfo file to your terminal. You will see a lot of information about your system's memory.

To specifically look for hugepages information, we can use the grep command to filter the output of cat. The grep command searches for lines that match a specific pattern.

Type the following command and press Enter:

cat /proc/meminfo | grep HugePages

The | symbol is called a pipe. It takes the output of the command on the left (cat /proc/meminfo) and sends it as input to the command on the right (grep HugePages). This way, grep only searches within the output of cat.

You should see lines similar to this (the exact numbers may vary):

HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB

Let's break down what these lines mean:

  • HugePages_Total: The total number of hugepages configured on the system.
  • HugePages_Free: The number of hugepages that are currently free and available for use.
  • HugePages_Rsvd: The number of hugepages that are reserved for future use.
  • HugePages_Surp: The number of hugepages that are above the vm.nr_hugepages setting (we'll look at this in the next step).
  • Hugepagesize: The size of each hugepage. On most systems, this is 2048 kB (2MB).

In this environment, the default is likely 0 total hugepages, which is common for general-purpose systems.

Click Continue to proceed to the next step.

Verify hugepage settings with sysctl vm.nr_hugepages

In the previous step, we looked at /proc/meminfo to see the current hugepage usage. Now, let's use the sysctl command to check the kernel parameter that controls the total number of hugepages.

The sysctl command is used to view and modify kernel parameters at runtime. Kernel parameters are settings that affect how the Linux kernel behaves.

The specific parameter we are interested in is vm.nr_hugepages. This parameter determines the total number of hugepages that the kernel should reserve.

Type the following command in your terminal and press Enter:

sysctl vm.nr_hugepages

This command will display the current value of the vm.nr_hugepages kernel parameter.

You should see output similar to this:

vm.nr_hugepages = 0

This output confirms that the current setting for the total number of hugepages is 0. This matches what we saw in /proc/meminfo where HugePages_Total was also 0.

The sysctl command is a powerful tool for inspecting and changing many aspects of the kernel's behavior. While we are only looking at vm.nr_hugepages here, you can explore many other parameters using sysctl -a.

Understanding kernel parameters like vm.nr_hugepages is important for tuning system performance for specific workloads.

Click Continue to move on to the next step.

Inspect hugepage config in /sys/kernel/mm

In this final step, we will explore another location where hugepage configuration information is available: the /sys filesystem. The /sys filesystem provides an interface to kernel data structures, allowing us to inspect and sometimes modify kernel objects.

Hugepage-related information can be found under /sys/kernel/mm/transparent_hugepage. Transparent Hugepages (THP) is a feature that attempts to automatically use hugepages without requiring explicit configuration from applications.

Let's navigate to this directory using the cd command. cd stands for "change directory".

Type the following command and press Enter:

cd /sys/kernel/mm/transparent_hugepage

Now that we are in the /sys/kernel/mm/transparent_hugepage directory, let's list the files in this directory using the ls command. The ls command lists the contents of a directory.

Type the following command and press Enter:

ls

You should see output similar to this:

defrag  enabled  khugepaged  numa_defrag  shmem_enabled  split_huge_pmd_size  split_huge_pte_size

These files represent different configuration options and status indicators for Transparent Hugepages.

Let's look at the content of the enabled file using the cat command. This file shows whether Transparent Hugepages are enabled, disabled, or in a 'madvise' mode (where applications can advise the kernel on hugepage usage).

Type the following command and press Enter:

cat enabled

You should see output similar to this:

[always] madvise never

The output indicates the current setting. [always] means THP is currently enabled for all mappings.

Now let's look at the defrag file. This file controls whether the kernel should attempt to defragment memory to make hugepages available.

Type the following command and press Enter:

cat defrag

You should see output similar to this:

[always] defer defer+madvise madvise never

Again, [always] indicates the current setting for memory defragmentation related to THP.

The /sys filesystem is a valuable resource for understanding the kernel's internal state and configuration. Exploring directories like /sys/kernel/mm can provide deep insights into memory management.

You have now successfully explored hugepage information using three different methods: /proc/meminfo, sysctl, and the /sys filesystem.

Click Continue to complete the lab.

Summary

In this lab, we learned how to check if kernel hugepages are enabled and configured on a Linux system. We explored three methods to achieve this. First, we examined the /proc/meminfo file using cat and grep to view key hugepage statistics such as HugePages_Total, HugePages_Free, and Hugepagesize.

Next, we would typically verify hugepage settings using sysctl vm.nr_hugepages and inspect the hugepage configuration within the /sys/kernel/mm directory, although the detailed steps for these methods were not fully provided in the provided content. These steps collectively demonstrate how to determine the current state and configuration of hugepages on a Linux system.