Transparent Huge Pages (THP) is a memory management system that Linux uses to improve the performance of applications by automatically using large memory pages. However, in some cases, especially with database systems like Couchbase, THP may lead to performance degradation. This article will guide you through the process of disabling THP on Rocky Linux, setting up this change as a permanent system service.
Step 1: Create a Service Unit File
Open the Terminal:
You can access the terminal through your Linux desktop if you're using one or connect via SSH if you are managing a remote server.
Create a New Service File:
Run the following command to create and edit a service file in the systemd directory
sudo vi /etc/systemd/system/disable-thp.service
Step 2: Configure the Service
In the text editor that opens, enter the following configuration. This script sets up a service that disables THP on startup
[Unit]
Description=Disable Transparent Huge Pages (THP)
DefaultDependencies=no
After=sysinit.target local-fs.target
Before=couchbase-server.service
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/enabled > /dev/null'
ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/defrag > /dev/null'
[Install]
WantedBy=basic.target
Explanation of the above config:
Description: Describes the service.
DefaultDependencies: Ensures that the service does not have implicit dependencies.
After/Before: Specifies the order of start-up in relation to other services.
ExecStart: Commands that disable THP.WantedBy: Determines when the service should be started during the boot process.
Save and close the file (:wq command in vi editor)
Step 3: Reload Systemctl to Recognize New Service
Update systemd to recognize the new service by running
sudo systemctl daemon-reload
Step 4: Start the Service and Verify Configuration
Start the service to apply the changes immediately:
sudo systemctl start disable-thp
Check if THP is successfully disabled:
cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag
The output for both commands should be never.
Step 5: Enable Service on Boot
To ensure THP remains disabled after every reboot, enable the service permanently:
sudo systemctl enable disable-thp
Step 6: Reboot and Validate
Optionally, reboot your system to ensure that the settings persist:
sudo systemctl reboot
After rebooting, verify the settings again:
cat /sys/kernel/mm/transparent_hugepage/enabled
cat /sys/kernel/mm/transparent_hugepage/defrag
Conclusion
Rocky Linux, like its upstream source Red Hat Enterprise Linux (RHEL) and its derivative CentOS, primarily uses systemd as its init system and service manager. The systemd has been the standard for managing system processes, service start-up sequences, and more since RHEL 7, and consequently, in Rocky Linux as well, starting from its initial release.
Disabling Transparent Huge Pages on Rocky Linux can help optimize the performance of applications that are sensitive to how memory is handled, such as databases. By setting up a systemd service, you ensure that THP is disabled consistently across reboots, providing a stable and predictable environment for your applications.
Comments
0 comments
Please sign in to leave a comment.