Performance and optimization
Table of contents
- Key metrics
- Monitoring tools
- CPU optimization
- Memory optimization
- I/O optimization
- Practical exercises
1 - Key metrics
The 4 fundamental resources
Load Average
# Afficher le load
uptime
cat /proc/loadavg
# Exemple: 2.50 1.80 1.20
# = Load sur 1min, 5min, 15min
| Load | State (4 CPUs) |
|---|---|
| < 4.0 | Normal |
| 4.0 - 8.0 | Loaded |
| > 8.0 | Overloaded |
Rule of thumb
Load < number of CPUs = OK Load > number of CPUs = possible saturation
USE metrics (Utilization, Saturation, Errors)
| Resource | Utilization | Saturation | Errors |
|---|---|---|---|
| CPU | CPU time | Run queue | - |
| Memory | Mem used | Swapping | OOM |
| Disk | Disk busy | I/O wait | Errors |
| Network | TX/RX bytes | Drops | Errors |
🔝 Back to table of contents
2 - Monitoring tools
System overview
# top - Vue classique
top
# htop - Amélioré
htop
# glances - Tout en un
glances
# dstat - Combiné
dstat -cdngy
CPU
# Utilisation par CPU
mpstat -P ALL 1
# Processus consommateurs
ps aux --sort=-%cpu | head
# Profiling
perf top
perf record -g ./myapp
perf report
Memory
# Vue mémoire
free -h
# Détaillé
cat /proc/meminfo
# Par processus
ps aux --sort=-%mem | head
# Mapping mémoire
pmap -x PID
Disk
# Utilisation
df -h
du -sh /*
# I/O en temps réel
iotop
iostat -xz 1
# Latence
ioping /dev/sda
Network
# Connexions
ss -tunapl
netstat -tunapl
# Trafic en temps réel
iftop
nethogs
# Statistiques
sar -n DEV 1
🔝 Back to table of contents
3 - CPU optimization
Identify problems
# Processus en CPU wait
ps aux | awk '$8 ~ /D/'
# Interruptions
cat /proc/interrupts
watch -n1 'cat /proc/interrupts'
# Context switches
vmstat 1
Nice and priority
# Lancer avec priorité basse
nice -n 19 ./heavy_task.sh
# Modifier un processus existant
renice -n 10 -p PID
# Afficher les priorités
ps -eo pid,ni,comm | head
CPU Governor
# Voir le governor actuel
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
# Governors disponibles
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
# Changer (temporaire)
echo "performance" | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
# Permanent avec cpufrequtils
apt install cpufrequtils
cpufreq-set -g performance
CPU affinity
# Voir l'affinité
taskset -p PID
# Assigner à des CPUs spécifiques
taskset -c 0,1 ./myapp
taskset -c 2-3 -p PID
🔝 Back to table of contents
4 - Memory optimization
Understanding Linux memory
free -h
# total used free shared buff/cache available
# Mem: 16G 4G 2G 1G 10G 10G
# Swap: 2G 0B 2G
| Term | Description |
|---|---|
| used | Memory used by processes |
| free | Unused memory |
| buff/cache | File cache (reclaimable) |
| available | Available for apps |
vm parameters
# /etc/sysctl.d/99-memory.conf
# Swappiness (0-100, défaut 60)
# Plus bas = moins de swap
vm.swappiness = 10
# Pression sur le cache
vm.vfs_cache_pressure = 50
# Dirty pages - quand écrire sur disque
vm.dirty_ratio = 20
vm.dirty_background_ratio = 5
# Appliquer
sysctl -p /etc/sysctl.d/99-memory.conf
Drop caches (test only)
# Sync avant
sync
# Vider PageCache
echo 1 > /proc/sys/vm/drop_caches
# Vider dentries et inodes
echo 2 > /proc/sys/vm/drop_caches
# Vider tout
echo 3 > /proc/sys/vm/drop_caches
Huge Pages
# Vérifier le support
grep -i huge /proc/meminfo
# Configurer
echo 1024 > /proc/sys/vm/nr_hugepages
# Permanent
# /etc/sysctl.conf
vm.nr_hugepages = 1024
OOM Killer
# Ajuster le score OOM d'un processus
# -1000 = ne jamais tuer, +1000 = tuer en premier
echo -500 > /proc/PID/oom_score_adj
# Voir le score
cat /proc/PID/oom_score
🔝 Back to table of contents
5 - I/O optimization
I/O scheduler
# Voir le scheduler actuel
cat /sys/block/sda/queue/scheduler
# Schedulers disponibles
# - mq-deadline : SSD et HDD modernes
# - kyber : SSD rapides
# - bfq : Desktop, fairness
# - none : NVMe
# Changer
echo mq-deadline > /sys/block/sda/queue/scheduler
Queue parameters
# Read-ahead (Ko)
blockdev --getra /dev/sda
blockdev --setra 4096 /dev/sda
# Queue depth
cat /sys/block/sda/queue/nr_requests
echo 256 > /sys/block/sda/queue/nr_requests
Mount options
# /etc/fstab optimisé pour SSD
/dev/sda1 / ext4 noatime,nodiratime,discard 0 1
# Options
# noatime : pas de mise à jour access time
# nodiratime : idem pour répertoires
# discard : TRIM pour SSD
# barrier=0 : désactiver barriers (risqué)
I/O benchmark
# fio - outil de benchmark
apt install fio
# Test lecture séquentielle
fio --name=seqread --rw=read --bs=1M --size=1G --filename=/tmp/test
# Test écriture aléatoire
fio --name=randwrite --rw=randwrite --bs=4k --size=1G --filename=/tmp/test
# Test mixte
fio --name=mixed --rw=randrw --bs=4k --size=1G --filename=/tmp/test
Filesystem optimization
# Vérifier la fragmentation (ext4)
e4defrag -c /dev/sda1
# Défragmenter
e4defrag /dev/sda1
# Paramètres ext4
tune2fs -l /dev/sda1
# XFS
xfs_info /dev/sda1
xfs_fsr /mountpoint # Défragmentation
🔝 Back to table of contents
6 - Practical exercises
Exercise 1: Load analysis
A server has a load average of 8.5 on 4 CPUs. Analyze the situation:
Solution
# 1. Vérifier le load
uptime
# 2. Identifier les processus CPU
top -b -n1 | head -20
ps aux --sort=-%cpu | head
# 3. Vérifier I/O wait
vmstat 1 5
# Si wa% élevé -> problème I/O, pas CPU
# 4. Vérifier les processus en D state
ps aux | awk '$8 ~ /D/'
# Load 8.5 sur 4 CPUs = saturation
# Actions : identifier le goulot d'étranglement
Exercise 2: Swap optimization
Reduce swap usage on a database server:
Solution
# /etc/sysctl.d/99-db.conf
vm.swappiness = 1
vm.vfs_cache_pressure = 50
# Appliquer
sysctl -p /etc/sysctl.d/99-db.conf
# Vérifier
cat /proc/sys/vm/swappiness
Quiz
Q1. What does a load average of 4.0 mean on a machine with 2 CPUs?
Answer
The system is overloaded: on average 4 processes are waiting for a CPU when only 2 are available.
Q2. Which command lets you see I/O per process?
Answer
iotop
🔝 Back to table of contents
Key takeaways
- USE Method: Utilization, Saturation, Errors for each resource
- Load Average: compare it to the number of CPUs
vm.swappiness: lower it for servers- I/O scheduler:
mq-deadlinefor SSD/HDD,nonefor NVMe noatime: mount option for performance- Always measure before and after optimization