Chapter 12.2 - Performance Tuning
Learning Objectives
By the end of this chapter, you will be able to:
- Identify bottlenecks
- Optimize CPU performance
- Optimize memory
- Optimize disk I/O
- Configure kernel parameters
- Monitor performance
Identifying Issues
Diagnostic Tools
# CPU
top, htop, mpstat
cat /proc/stat
# Memory
free -h
vmstat
cat /proc/meminfo
# Disk
iostat -x
iotop
df -h
# Network
iftop, nethogs
ss -i
Key Metrics
# Load average
uptime
# Interpretation: < number of CPUs
# CPU idle
mpstat 1
# Goal: > 20% idle
# Available memory
free -h
# Goal: > 10% free
# I/O wait
iostat -x 1
# Goal: < 5%
CPU Optimization
Process Priorities
# Adjust nice values
nice -n 10 command # Low priority
nice -n -10 command # High priority (root)
# Renice existing process
renice 10 PID
renice 10 -u user1
CPU Affinity
# Assign process to specific CPU
taskset -c 0,1 command
# Existing process
taskset -cp 0,1 PID
CPU Limits
# cgroups v2
systemd-run --slice=my-slice --property=CPUQuota=50% command
# cpuset
cset set -c 0-3 -s my-set
cset proc -m -p PID -s my-set
Memory Optimization
Swap
# View swap
free -h
swapon --show
# Adjust swappiness
# /etc/sysctl.conf
vm.swappiness=10 # 0-100, lower = less swap
# Apply
sudo sysctl -p
Cache and Buffer
# Clear cache (if necessary)
sync
echo 3 | sudo tee /proc/sys/vm/drop_caches
# Check usage
free -h
Memory Limits
# ulimit
ulimit -v 1000000 # 1GB in KB
# cgroups
systemd-run --slice=my-slice --property=MemoryLimit=1G command
I/O Optimization
File Systems
# Choose appropriate FS
# - ext4: General
# - xfs: Large files
# - btrfs: Snapshots
# Mount options
# /etc/fstab
/dev/sda1 /data xfs defaults,noatime 0 2
# noatime: No atime update (performance)
I/O Scheduler
# View current scheduler
cat /sys/block/sda/queue/scheduler
# Change scheduler
echo deadline > /sys/block/sda/queue/scheduler
# Permanent: kernel parameter
# elevator=deadline
RAID and LVM
# RAID for performance
# - RAID 0: Performance (no redundancy)
# - RAID 10: Performance + redundancy
# LVM for flexibility
# - Volume extension
# - Snapshots
Kernel Parameters
sysctl
# View parameters
sysctl -a
# Modify temporarily
sudo sysctl vm.swappiness=10
# Permanent: /etc/sysctl.conf
vm.swappiness=10
net.core.rmem_max=16777216
net.core.wmem_max=16777216
# Apply
sudo sysctl -p
Network Parameters
# /etc/sysctl.conf
# TCP optimization
net.core.somaxconn=1024
net.ipv4.tcp_max_syn_backlog=2048
net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_keepalive_time=300
Memory Parameters
# /etc/sysctl.conf
vm.swappiness=10
vm.dirty_ratio=15
vm.dirty_background_ratio=5
Service Optimization
Unnecessary Services
# View active services
systemctl list-units --type=service --state=running
# Disable unnecessary services
sudo systemctl disable service-name
sudo systemctl stop service-name
Resource Limits
# Unit file systemd
[Service]
MemoryLimit=512M
CPUQuota=50%
IOWeight=100
Performance Monitoring
Monitoring Scripts
#!/bin/bash
# Monitoring performance
echo "=== CPU ==="
top -bn1 | grep "Cpu(s)"
mpstat 1 1
echo "=== Memory ==="
free -h
vmstat -s
echo "=== Disk ==="
iostat -x 1 1
df -h
echo "=== Network ==="
ss -i
Performance Alerts
#!/bin/bash
# Performance alerts
# CPU
CPU_IDLE=$(mpstat 1 1 | tail -1 | awk '{print $NF}')
if (( $(echo "$CPU_IDLE < 20" | bc -l) )); then
echo "ALERT: CPU idle at ${CPU_IDLE}%"
fi
# Memory
MEM_FREE=$(free | grep Mem | awk '{printf "%.0f", $4/$2 * 100}')
if [ "$MEM_FREE" -lt 10 ]; then
echo "ALERT: Memory free at ${MEM_FREE}%"
fi
Summary
In this chapter, you learned:
Diagnostic: Tools for identifying bottlenecks
CPU: Priorities, affinity, limits
Memory: Swap, cache, limits
I/O: FS, scheduler, RAID
Kernel: sysctl, network/memory parameters
Services: Disable unnecessary, limits
Monitoring: Scripts, performance alerts
Next Steps
Chapter 12.3: Complete Automation
End of Course: Recap
Chapter created on: December 2024