A Complete Guide to Linux System Performance Tuning - Part 1

System tuning in Linux is both an art and a science that involves optimizing various components of your system to achieve maximum performance. This comprehensive guide will walk you through the essential aspects of Linux system tuning, from basic concepts to advanced techniques.

Understanding System Performance Metrics

Before diving into tuning techniques, it's crucial to understand what metrics to monitor:

  • CPU usage and load average

  • Memory utilization and swap usage

  • Disk I/O performance

  • Network throughput

  • Process response times

1. CPU Tuning

CPU Frequency Scaling

Modern processors support dynamic frequency scaling to balance performance and power consumption. The CPU governor controls this behavior:

# View current CPU frequency settings
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

# Set performance governor for maximum performance
echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

Process Priority Management

Use nice and renice to adjust process priorities:

  • Values range from -20 (highest priority) to 19 (lowest priority)

  • Only root can assign negative nice values

# Start a process with modified priority
nice -n 10 ./my_process

# Modify priority of running process
renice -n 5 -p [PID]

2. Memory Management

Swap Configuration

Proper swap configuration is crucial for system performance:

# Check current swappiness value
cat /proc/sys/vm/swappiness

# Adjust swappiness (temporary)
sudo sysctl vm.swappiness=10

# Make it permanent
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf

Memory Limits

Configure system-wide memory limits in /etc/security/limits.conf:

* soft nofile 65535
* hard nofile 65535

3. Disk I/O Optimization

I/O Schedulers

Choose the appropriate I/O scheduler based on your workload:

# Check current scheduler
cat /sys/block/sda/queue/scheduler

# Change scheduler
echo deadline | sudo tee /sys/block/sda/queue/scheduler

File System Tuning

Optimize mount options in /etc/fstab:

# Example for SSD
UUID=xxx / ext4 noatime,discard,errors=remount-ro 0 1

Key mount options:

  • noatime: Disable access time updates

  • discard: Enable TRIM for SSDs

  • data=writeback: Faster but less safe journaling

4. Network Performance

TCP Stack Tuning

Optimize TCP stack parameters in /etc/sysctl.conf:

# Increase TCP window size
net.core.wmem_max = 12582912
net.core.rmem_max = 12582912
net.ipv4.tcp_wmem = 10240 87380 12582912
net.ipv4.tcp_rmem = 10240 87380 12582912

# Enable TCP fast open
net.ipv4.tcp_fastopen = 3

# Increase connection backlog
net.core.somaxconn = 65535

Network Interface Tuning

Optimize network interface settings:

# Enable jumbo frames
sudo ip link set eth0 mtu 9000

# Adjust ring buffer sizes
sudo ethtool -G eth0 rx 4096 tx 4096

5. System Resource Limits

File Descriptor Limits

Increase system-wide file descriptor limits in /etc/sysctl.conf:

fs.file-max = 2097152

6. Monitoring and Profiling Tools

Essential Monitoring Tools

  1. top/htop: Real-time system monitoring

  2. iostat: I/O statistics

  3. vmstat: Virtual memory statistics

  4. netstat/ss: Network statistics

  5. sar: System activity reporter

Performance Profiling

Use specialized tools for detailed analysis:

  • perf: Linux performance events subsystem

  • strace: System call tracer

  • ftrace: Function tracer

  • bpftrace: Advanced tracing tool

7. Automated Tuning Tools

tuned Daemon

Red Hat's tuned daemon provides automatic system tuning:

# Install tuned
sudo apt install tuned

# Start and enable the service
sudo systemctl enable --now tuned

# List available profiles
tuned-adm list

# Set a profile
tuned-adm profile throughput-performance

Best Practices and Guidelines

  1. Baseline Measurements

    • Always measure performance before making changes

    • Document all modifications

    • Test one change at a time

  2. Security Considerations

    • Balance performance with security requirements

    • Avoid disabling security features without understanding implications

    • Maintain proper logging and monitoring

  3. Documentation

    • Maintain detailed documentation of all tuning changes

    • Include reasoning behind each modification

    • Document rollback procedures

  4. Testing

    • Test changes in development environment first

    • Perform gradual rollouts in production

    • Have rollback plans ready

Conclusion

System tuning is an iterative process that requires careful planning, testing, and monitoring. Start with measuring your system's current performance, identify bottlenecks, and make targeted improvements. Remember that no single configuration works for all workloads – tune your system based on your specific requirements and usage patterns.

Regular monitoring and adjustment of these parameters ensure optimal system performance over time. As workloads change, be prepared to revisit and modify your tuning parameters accordingly.

Remember to always:

  • Backup your system before making changes

  • Document all modifications

  • Test changes in a non-production environment first

  • Monitor the impact of changes

  • Maintain security while optimizing performance

By following these guidelines and understanding the various components that affect system performance, you can create a well-tuned Linux system that meets your performance requirements while maintaining stability and reliability.

0
Subscribe to my newsletter

Read articles from Srinath Thilakarathne directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Srinath Thilakarathne
Srinath Thilakarathne