Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: July 13, 2024
Linux is a versatile operating system that can be fine-tuned for various use cases, including high-performance computing. Whether we’re running a server, a virtual machine, or a personal computer, optimizing Linux for high performance can significantly enhance its efficiency and speed.
In this tutorial, we’ll explore different strategies to achieve this, complete with examples and code snippets in Bash. In addition, we’ll break down the concepts and commands in simple terms, making it easy for Linux administrators and new users to follow along.
Performance optimization is all about making a system run faster, smoother, and more efficiently. Before diving into the practical examples, it’s important to understand the key areas where performance optimization can make a difference:
By focusing on these key areas of performance optimization, we can ensure that our Linux system runs at its peak potential, delivering a more responsive and enjoyable user experience. Furthermore, we’ll cover each of these areas, providing concise and clear practical steps and code snippets.
In this section, we’ll explore various methods to improve CPU performance in Linux.
The CPU governor determines how the CPU frequency is scaled. The “performance” governor keeps the CPU at its highest frequency, which can significantly improve performance for compute-intensive tasks.
Let’s take a look at how to view the governor:
# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
performance powersave
From the output above, we conclude that the available governors are performance and powersave. However, let’s break down this command:
Next, let’s check on the currently configured governor:
# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
powersave
Hence, we now know that the CPU is currently set to the powersave governor.
To enhance the performance capabilities of the CPU, we need to change this from powersave to performance, and then validate the changes:
# echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
performance
First, we used the pipe symbol | to use the first output in our following command:
Finally, we alternated the governor from powersave to performance as the output shows.
Creating aliases can simplify the management of CPU governors. Aliases are shortcuts for longer commands, making them easier to use.
To begin with, we need to define the aliases in either the ~/.bashrc or ~/.zshrc file:
# alias cpugetavail='cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_available_governors'
# alias cpushowcurrent='cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor'
# alias cpusethigh='echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor'
Let’s understand what each of the above aliases does:
After adding the aliases, we need to reload our shell configuration by using the source command, and then view the available governors:
# source ~/.bashrc
cpugetavail
performance powersave
In this case, we used the ~/.bashrc not ~/.zshrc. Moreover, we can see that we have 2 CPU governors: performance and powersave.
After listing the available CPU governors, we can check the current configured governor and change it to what we need:
cpushowcurrent
powersave
cpusethigh
performance
perfromance
These outputs confirm the governors’ availability, current setting, and successful change to “performance“.
In this section, we’ll introduce a couple of methods to adjust memory management in our Linux environment that can help improve performance and boost our system’s capabilities.
swappiness determines how aggressively the kernel swaps memory pages. That being said, lowering the swappiness value can reduce the tendency to swap, which is beneficial for performance.
First, let’s check what is the configured value for swappines in our system:
# cat /proc/sys/vm/swappiness
60
A default value of 60 is typical, but we can lower this to improve performance:
# sudo sysctl vm.swappiness=10
Typically, sudo sysctl vm.swappiness=10 sets the swappiness value to 10. If this is against our system design, we can still adjust the value per our needs. For example, this should be decided by the system administrator and if our system can tolerate a value lower than 60 to enhance the overall performance capabilities.
Moreover, we can make the change permanent by adding to sysctl.conf file:
# echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
Let’s break down the above snippet:
Now, once we settle on the most appropriate value that matches our environment setup, we can make the change permanent.
In essence, HugePages can improve performance by reducing the overhead of memory management for large applications.
Moreover, we can easily check if such a setting is configured or not in our system:
# grep Huge /proc/meminfo
AnonHugePages: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
This output shows that HugePages are not currently configured. Hence, let’s configure it by setting a number for HugePages and verifying the changes are applied:
# echo 1024 | sudo tee /proc/sys/vm/nr_hugepages
# grep Huge /proc/meminfo
AnonHugePages: 0 kB
HugePages_Total: 1024
HugePages_Free: 1024
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Finally, we used the grep command to verify the output of Huge in the /proc/meminfo file to confirm the success of the operation.
In this section, let’s cover some techniques to optimize the input and output operations on our disks.
Now, it’s time to introduce the hdparm utility that can enable us to tune hard disk parameters for better performance. First, let’s check the configuration for our disk:
# hdparm -I /dev/sda
/dev/sda:
ATA Device Information (ATA / ATAPI):
Model : WDC WD10EZRX-00DC0L0
Serial No : ABC1234567890
Firmware Rev: 1.01A01
User Capacity: 1000GB (1993752516 sectors)
Logical block size: 512 bytes
...
Afterward, we can enable write caching to enhance performance:
# hdparm -W 1 /dev/sda
# hdparm -W /dev/sda
/dev/sda:
write-caching = 1 (on)
Here, we validate that we enabled the write cache functionality on the /dev/sda disk.
In this section, we’ll focus on the network aspect and what can be changed to enhance overall performance.
Since increasing network buffer sizes can improve network performance, we can adjust our needed values and make it permanent:
# echo 'net.core.rmem_max=16777216' | sudo tee -a /etc/sysctl.conf
# echo 'net.core.wmem_max=16777216' | sudo tee -a /etc/sysctl.conf
Next, let’s understand the parameter above:
This can have many deciding factors depending on every environment. Particularly, we need to align with our network administrators so that our network can tolerate such change.
Unnecessary services can consume system resources, so disabling them can improve overall performance.
As a start, we need to check what are the running services in our system:
# systemctl list-units --type=service --state=running
UNIT LOAD ACTIVE SUB DESCRIPTION
accounts-daemon.service loaded active running Accounts Service
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
bluetooth.service loaded active running Bluetooth service
...
For example, one of the services that we might not need for now is the Bluetooth service. Therefore, let’s understand how to disable it:
# systemctl disable bluetooth.service
# systemctl stop bluetooth.service
Either the disable or the stop keyword will do the job. However, we need to be careful when stopping poor disabling running services because we only need to get rid of the unused services and not just any service.
Optimizing Linux for high performance involves tweaking various system settings to maximize CPU, memory, disk I/O, and network performance.
By following the steps and examples provided in this article, we can significantly improve the efficiency and speed of our Linux system. Furthermore, it’s important to clarify that performance tuning is an ongoing process, and we should regularly monitor our system to ensure it continues to perform optimally.
By implementing these optimizations, we can make the most out of our Linux system, whether it’s a high-performance server, a virtual machine, or a personal computer.