1. Overview
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.
2. Understanding Linux Performance Optimization
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:
- CPU performance: enhancing the CPU’s ability to handle tasks efficiently
- memory management: optimizing how memory is used and managed
- disk I/O: improving the speed of read/write operations on disk
- network performance: enhancing data transfer speeds over the network
- system services: disabling unnecessary services to free up resources
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.
3. CPU Performance Optimization
In this section, we’ll explore various methods to improve CPU performance in Linux.
3.1. Viewing and Changing CPU Governors
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:
- /sys/devices/system/cpu/cpu0/cpufreq/: this path points to the directory containing CPU frequency scaling information for the first CPU (cpu0)
- scaling_available_governors: this specific file within the directory lists all the available CPU frequency governors supported by our system
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:
- echo performance: outputs the value performance
- sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor: writes the output to the scaling_governor file for each CPU, changing the governor to “performance“
Finally, we alternated the governor from powersave to performance as the output shows.
3.2. Using Aliases for CPU Governor Management
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:
- creates an alias cpugetavail to display available CPU governors
- creates an alias cpushowcurrent to display the current CPU governor
- creates an alias cpusethigh to set the CPU governor to performance
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“.
4. Memory Management
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.
4.1. Adjusting swappines
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:
- echo ‘vm.swappiness=10’: outputs the new swappiness setting
- sudo tee -a /etc/sysctl.conf: appends the setting to the sysctl configuration file
Now, once we settle on the most appropriate value that matches our environment setup, we can make the change permanent.
4.2. Using HugePages
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
# 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.
6. Network Performance Optimization
In this section, we’ll focus on the network aspect and what can be changed to enhance overall performance.
6.1. Adjusting Network Buffers
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:
- sets the maximum receive buffer size to 16777216 bytes
- sets the maximum send buffer size to 16777216 bytes
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.
6.2. Disabling Unnecessary Services
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.
7. Conclusion
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.