huge page setting

Introduction

Huge Pages is a feature that assists with memory management at the operating system level and is particularly beneficial for databases like Oracle Database. Utilizing Huge Pages optimizes performance and reduces memory overhead.

Recommended RAM Size for Huge Pages

Huge Pages are generally recommended for systems with 16 GB of RAM or more. This is because on systems with large amounts of memory, managing memory with larger pages (e.g., 2 MB instead of 4 KB) is more efficient and reduces the number of Page Table entries.

Brief Explanation of Page Table

A Page Table is a data structure used by the operating system and processor hardware to manage virtual memory space. Its primary function is to map virtual memory addresses (used by applications) to physical addresses (in RAM).

Each entry in this table represents a page in virtual memory and includes information such as the physical address of the page and access permissions.

Role of Page Table and Impact of Huge Pages

In systems that use small, regular pages (e.g., 4 KB), the Page Table can become very large, as it requires numerous entries to map the addresses. Using Huge Pages (e.g., 2 MB instead of 4 KB) reduces the number of required entries in the Page Table, which in turn reduces system overhead and improves memory efficiency.

In summary, using Huge Pages reduces the number of Page Table entries and makes memory management more effective.

Benefits of Huge Pages

  1. Reduced Memory Overhead: Using larger pages reduces the number of Page Table entries, thereby lowering the CPU overhead for memory management.
  2. Reduced Fragmentation: Huge Pages help decrease memory fragmentation.
  3. Memory Stability: Memory allocated for Huge Pages is permanently kept in memory, providing greater stability for the database.
  4. Reduced Swap Usage: Huge Pages help the system use less swap memory, which in turn enhances performance.

How Huge Pages Benefit the Database

Huge Pages help Oracle Database manage shared memory more effectively. These large pages ensure faster access to memory for processes, leading to overall performance improvements for the database. Specifically, Oracle uses Huge Pages to manage the System Global Area (SGA).

Disabling Transparent Huge Pages in the Operating System

Transparent Huge Pages (THP) automatically allocates large pages, but for Oracle databases, this feature can decrease performance. To disable THP:

Check the current THP status:

cat /sys/kernel/mm/transparent_hugepage/enabled

Sample output:

always madvise [never]

Disabling Transparent Huge Pages Using Grubby

If the current status is always or madvise, use the following commands to disable it:

Identify the default kernel:

grubby --default-kernel

Example output:

/boot/vmlinuz-4.1.12-61.1.6.el7uek.x86_64

Execute the following command to disable transparent huge pages:

grubby --args="transparent_hugepage=never" --update-kernel /boot/vmlinuz-4.1.12-61.1.6.el7uek.x86_64

Verify that the parameter was correctly applied:

grubby --info /boot/vmlinuz-4.1.12-61.1.6.el7uek.x86_64

Alternative Method

You can add the following lines to the /etc/rc.local file and reboot the server:

if test -f /sys/kernel/mm/transparent_hugepage/enabled;
then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag;
then echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi

Checking the Current Status of Huge Pages

Check the current status of Huge Pages using:

 
grep Huge /proc/meminfo

Sample output:

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

In this example, the huge page size is 2 MB, and Huge Pages are not yet configured.

Checking and Setting memlock

The memlock parameter specifies the maximum amount of memory that can be locked in physical memory and should be at least 90% of the current RAM when Huge Pages are enabled. To check the current settings:

 
cat /etc/security/limits.conf | grep memlock

Output:

# - memlock - max locked-in-memory address space (KB)
oracle soft memlock 475423826
oracle hard memlock 475423826

Configuring tmpfs for Proper SGA Usage

tmpfs is a memory-based file system used for data that requires fast access. The mount point, /dev/shm, should be at least as large as SGA_MAX_SIZE.

To check the size of tmpfs:

df -h

Sample output:

tmpfs 128G 637M 128G 1% /dev/shm

To change the size of tmpfs:

Temporary setting:

mount -t tmpfs shmfs -o size=4g /dev/shm

Permanent setting: Add the following line to the /etc/fstab file:

shmfs /dev/shm tmpfs size=128g 0 0

Then run:

mount -a remount

Steps to Implement Huge Pages

  1. Backup PFILE and Set SGA (if needed):

     
    sqlplus / as sysdba
    create pfile='/home/oracle/pfile030202.ora' from spfile;
    alter system set sga_max_size=70g scope=spfile;
    alter system set sga_target=65g scope=spfile;
    alter system set use_large_pages=only scope=spfile;

    In our setup, the RAM size is 128 GB.

  2. Shutdown the Database: Shut down the database to apply the changes.

  3. Set Huge Page Value in sysctl.conf:

    • Method A: Calculate the required pages:

      (maximum sga in GB * 1024 /2) + 1%
    • Method B: Use the script hugepages_settings.sh:

      chmod +x hugepages_settings.sh ./hugepages_settings.sh

    Then, add the settings to the /etc/sysctl.conf file:

    vm.nr_hugepages=<recommended_value>

    Apply the changes:

    sysctl -p
  4. Restart the Database: Start the database. If there is a memory-related error, release the memory:

     
    echo 3 > /proc/sys/vm/drop_caches
  5. Verify New Huge Pages Status: Ensure the settings are correctly applied:

     
    grep Huge /proc/meminfo

    Example output:

     
    AnonHugePages: 0 kB
    ShmemHugePages: 0 kB
    HugePages_Total: 38400
    HugePages_Free: 34849
    HugePages_Rsvd: 32290
    HugePages_Surp: 0
    Hugepagesize: 2048 kB
  6. Optionally Free Swap Space: To free swap space:

     
    swapoff -a
    swapon -a

Additional Notes on tmpfs and memlock

tmpfs

tmpfs is a memory-based file system used for data requiring fast access. Its size must be large enough to accommodate SGA_MAX_SIZE, as Oracle uses /dev/shm to manage shared memory (SGA). If the size of tmpfs is less than SGA_MAX_SIZE, the database will encounter errors.

memlock

memlock is a setting that specifies the amount of memory that can be locked in physical memory. This setting should be at least 90% of the RAM when Huge Pages is enabled, as locking memory is essential for using Huge Pages. This ensures Oracle has the required memory permanently without interference from the operating system.