System Performance Tuning คือการปรับค่าต่าง ๆ ของ Server เพื่อให้ใช้ทรัพยากรได้อย่างมีประสิทธิภาพสูงสุด บน Cloud VPS ที่มีทรัพยากรจำกัด การ Tune ค่าที่เหมาะสมสามารถเพิ่ม Throughput, ลด Latency และทำให้รองรับ Load สูงได้โดยไม่ต้องอัพเกรด Spec
บทความนี้ครอบคลุม Tools สำหรับ Profiling ระบบ, การ Tune CPU Governor, Memory และ Swap, การปรับ I/O Scheduler, Network Stack Tuning, การตั้งค่า Limits สำหรับ High-Concurrency Application และ Script ตรวจสอบ Performance อัตโนมัติ
ตรวจสอบ Performance Baseline
ก่อน Tune ควรวัด Baseline เพื่อเปรียบเทียบหลัง Tune ว่าดีขึ้นจริง
# CPU Usage Overview
top # Interactive process viewer
htop # Enhanced top (ติดตั้งด้วย apt/dnf install htop)
mpstat -P ALL 1 5 # CPU Stats แต่ละ Core ทุก 1 วินาที 5 ครั้ง
# Memory Usage
free -h # ดู RAM และ Swap
vmstat 1 5 # Virtual Memory Stats ทุก 1 วินาที
cat /proc/meminfo # รายละเอียด Memory
# Disk I/O
iostat -xz 1 5 # I/O Stats รายละเอียด
iotop # I/O ตาม Process (ต้องติดตั้ง: apt/dnf install iotop)
# Network
ss -s # Socket Summary
sar -n DEV 1 5 # Network Stats (ต้องติดตั้ง sysstat)
nethogs # Network Usage ตาม Process
# Load Average (1m, 5m, 15m)
uptime
cat /proc/loadavg
# benchmark เบื้องต้น
# CPU benchmark
sysbench cpu --cpu-max-prime=20000 run
# Memory bandwidth
sysbench memory --memory-block-size=1K --memory-total-size=10G run
# Disk write speed
dd if=/dev/zero of=/tmp/testfile bs=1M count=1000 oflag=direct
# ลบไฟล์ทดสอบ
rm /tmp/testfile
# Disk read speed
hdparm -Tt /dev/sda
# Network latency (ไปยัง Gateway)
ping -c 20 $(ip route | grep default | awk '{print $3}') | tail -3
CPU Governor และ Frequency Scaling
# ดู CPU Governor ปัจจุบัน
cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
# หรือ
cpupower frequency-info # ต้องติดตั้ง: apt install linux-tools-common
# Governor ที่มีให้เลือก:
# performance — รัน CPU ที่ความเร็วสูงสุดตลอดเวลา (เหมาะ Production)
# powersave — รักษา CPU ที่ความเร็วต่ำสุด (ประหยัดพลังงาน)
# ondemand — ปรับความเร็วตาม Load (Ubuntu default)
# schedutil — ปรับตาม Scheduler (kernel 4.7+)
# ตั้ง Governor เป็น performance (เหมาะสำหรับ VPS ที่ต้องการ CPU สูงสุด)
sudo cpupower frequency-set -g performance
# หรือตั้งทุก CPU Core
for cpu in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
echo performance | sudo tee "$cpu"
done
# ตั้งให้คงอยู่หลัง Reboot (Ubuntu)
sudo apt install cpufrequtils -y
echo 'GOVERNOR="performance"' | sudo tee /etc/default/cpufrequtils
sudo systemctl restart cpufrequtils
Memory Tuning — Swappiness และ Huge Pages
# Swappiness — ควบคุมว่า Kernel จะใช้ Swap เมื่อไหร่
# ค่า 0-100: ค่าน้อย = ใช้ RAM ให้นานที่สุดก่อนใช้ Swap
# Default: 60 (ดีสำหรับ Desktop)
# แนะนำสำหรับ Server: 10
sudo sysctl -w vm.swappiness=10
echo "vm.swappiness = 10" | sudo tee -a /etc/sysctl.d/99-perf.conf
# VFS Cache Pressure — ควบคุมการ Cache inode/dentry
# ลดลงเพื่อให้ Cache ไม่ถูก evict เร็วเกิน
sudo sysctl -w vm.vfs_cache_pressure=50
echo "vm.vfs_cache_pressure = 50" | sudo tee -a /etc/sysctl.d/99-perf.conf
# Dirty Ratio — ควบคุม Buffer Write
# dirty_ratio: % ของ RAM ที่ยอมให้เป็น dirty page ก่อน Force flush
# dirty_background_ratio: % ที่เริ่ม flush ใน background
sudo sysctl -w vm.dirty_ratio=15
sudo sysctl -w vm.dirty_background_ratio=5
echo "vm.dirty_ratio = 15" | sudo tee -a /etc/sysctl.d/99-perf.conf
echo "vm.dirty_background_ratio = 5" | sudo tee -a /etc/sysctl.d/99-perf.conf
# Apply ทั้งหมด
sudo sysctl --system
# Transparent Huge Pages (THP)
# แนะนำ: madvise สำหรับ General Server
# Database (MySQL, PostgreSQL, Redis) แนะนำ: never
cat /sys/kernel/mm/transparent_hugepage/enabled
# [always] madvise never ← แสดงค่าปัจจุบัน
# ตั้งค่า
echo madvise | sudo tee /sys/kernel/mm/transparent_hugepage/enabled
echo defer+madvise | sudo tee /sys/kernel/mm/transparent_hugepage/defrag
# ตั้งค่าถาวร (เพิ่มใน /etc/rc.local หรือ systemd service)
sudo tee /etc/systemd/system/disable-thp.service <<'EOF'
[Unit]
Description=Disable Transparent Huge Pages
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/sh -c "echo madvise > /sys/kernel/mm/transparent_hugepage/enabled"
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable --now disable-thp.service
Disk I/O Scheduler
I/O Scheduler กำหนดลำดับการเข้าถึง Disk การเลือก Scheduler ที่เหมาะสมช่วยเพิ่ม IOPS และลด Latency
# ดู I/O Scheduler ปัจจุบัน
cat /sys/block/sda/queue/scheduler
# ตัวอย่าง: [none] mq-deadline kyber bfq
# Scheduler ที่มี:
# none (noop) — ไม่มี Reordering เหมาะสำหรับ SSD/NVMe บน VPS
# mq-deadline — มี Deadline สำหรับแต่ละ Request เหมาะ HDD
# bfq (Budget Fair Queuing) — เหมาะ Desktop, ให้ Fairness สูง
# kyber — Low-latency เหมาะ NVMe
# สำหรับ VPS ที่ใช้ Virtual Disk (SSD-based)
echo none | sudo tee /sys/block/sda/queue/scheduler
# ตั้งค่าถาวรผ่าน udev rule
sudo tee /etc/udev/rules.d/60-io-scheduler.rules <<'EOF'
# SSD/NVMe
ACTION=="add|change", KERNEL=="sd[a-z]|nvme[0-9]n[0-9]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="none"
# HDD
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="mq-deadline"
EOF
# Reload udev rules
sudo udevadm control --reload-rules
sudo udevadm trigger
# เพิ่ม Read-ahead สำหรับ Sequential Read
# ดู Read-ahead ปัจจุบัน (ค่าหน่วยเป็น 512-byte sectors)
blockdev --getra /dev/sda
# ตั้ง Read-ahead (256 = 128KB, เหมาะสำหรับ Log files, DB sequential scan)
sudo blockdev --setra 256 /dev/sda
# ดู Queue Depth
cat /sys/block/sda/queue/nr_requests
# เพิ่มสำหรับ SSD ที่รับ Parallel I/O ได้ดี
echo 256 | sudo tee /sys/block/sda/queue/nr_requests
Network Stack Tuning
# Network Performance Tuning สำหรับ High-Traffic Web Server
sudo tee /etc/sysctl.d/99-network-perf.conf <<'EOF'
# TCP Buffer sizes (Read/Write)
net.core.rmem_default = 262144
net.core.wmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
# Connection Queue
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.core.netdev_max_backlog = 65535
# TCP Keepalive
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 5
# TIME_WAIT
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_max_tw_buckets = 1440000
net.ipv4.tcp_tw_reuse = 1
# TCP Fast Open (ลด Handshake latency)
net.ipv4.tcp_fastopen = 3
# Congestion Control
net.ipv4.tcp_congestion_control = bbr
net.core.default_qdisc = fq
EOF
sudo sysctl --system
# เปิดใช้ BBR Congestion Control (ต้องการ Kernel 4.9+)
# ตรวจสอบว่า BBR พร้อมใช้
modprobe tcp_bbr
lsmod | grep bbr
# ตรวจสอบ Congestion Control ที่ใช้งาน
sysctl net.ipv4.tcp_congestion_control
# ดูตัวเลือกที่มี
sysctl net.ipv4.tcp_available_congestion_control
# ทดสอบ Network Throughput ด้วย iperf3
# บน Server ที่ 1 (ทำหน้าที่เป็น Server)
iperf3 -s
# บน Server ที่ 2 (ทำหน้าที่เป็น Client)
iperf3 -c <server-ip> -t 30 -P 4
File Descriptor และ Process Limits
High-Concurrency Application เช่น Nginx, Node.js, Redis ต้องการ File Descriptor จำนวนมาก การตั้งค่า Limits ที่ไม่พอเป็นสาเหตุ “Too many open files” ที่พบบ่อยบน Production Server
# ดู Limit ปัจจุบัน
ulimit -a # Soft limits ของ shell ปัจจุบัน
ulimit -Hn # Hard limit ของ file descriptors
# ดู System-wide File Descriptor Count
cat /proc/sys/fs/file-max
cat /proc/sys/fs/file-nr # opened / not-used / max
# เพิ่ม System-wide File Max
sudo sysctl -w fs.file-max=2097152
echo "fs.file-max = 2097152" | sudo tee -a /etc/sysctl.d/99-perf.conf
# ตั้งค่า Limits สำหรับทุก User และ Process
sudo tee -a /etc/security/limits.conf <<'EOF'
# High-concurrency limits
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
root soft nofile 65535
root hard nofile 65535
EOF
# ตั้งค่าผ่าน systemd (สำหรับ Service ที่จัดการโดย systemd)
# /etc/systemd/system/nginx.service.d/limits.conf
sudo mkdir -p /etc/systemd/system/nginx.service.d
sudo tee /etc/systemd/system/nginx.service.d/limits.conf <<'EOF'
[Service]
LimitNOFILE=65535
LimitNPROC=65535
EOF
sudo systemctl daemon-reload
sudo systemctl restart nginx
NUMA และ CPU Affinity
# ตรวจสอบ NUMA topology
numactl --hardware
lscpu | grep -i numa
# รัน Process บน NUMA Node เฉพาะ (ลด Cross-NUMA Memory Access)
numactl --cpunodebind=0 --membind=0 /usr/sbin/nginx
# CPU Affinity — ผูก Process ไว้กับ CPU Core เฉพาะ
# ดู CPU ที่ Process ใช้
taskset -cp $(pgrep nginx | head -1)
# ผูก Process กับ Core 0-3
taskset -cp 0-3 $(pgrep nginx | head -1)
# รัน Process ใหม่ด้วย CPU Affinity
taskset -c 0-3 /usr/sbin/nginx
# IRQ Affinity — กระจาย Network Interrupt ไปหลาย Core
# ดู IRQ ของ Network Interface
cat /proc/interrupts | grep eth0
# ตั้ง IRQ Affinity (เช่น Core 0-1 สำหรับ eth0)
echo "3" | sudo tee /proc/irq/<IRQ_NUM>/smp_affinity
Script ตรวจสอบ Performance อัตโนมัติ
#!/bin/bash
# /usr/local/bin/perf-check.sh
# ตรวจสอบ Performance ของ Server และแจ้งเตือนถ้าผิดปกติ
ALERT_EMAIL="[email protected]"
HOSTNAME=$(hostname)
WARN_LOAD=4.0 # Load Average สูงสุดที่ยอมรับได้ (ควรไม่เกินจำนวน vCPU)
WARN_MEM=90 # % Memory Usage สูงสุด
WARN_DISK=85 # % Disk Usage สูงสุด
echo "=== Performance Check: ${HOSTNAME} at $(date) ==="
# Load Average
LOAD=$(awk '{print $1}' /proc/loadavg)
echo "Load Average (1m): ${LOAD}"
if (( $(echo "$LOAD > $WARN_LOAD" | bc -l) )); then
echo "⚠️ HIGH LOAD: ${LOAD}"
echo "High load on ${HOSTNAME}: ${LOAD}" \
| mail -s "[Alert] High Load: ${HOSTNAME}" "$ALERT_EMAIL"
fi
# Memory Usage
MEM_TOTAL=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
MEM_AVAIL=$(awk '/MemAvailable/ {print $2}' /proc/meminfo)
MEM_USED_PCT=$(( (MEM_TOTAL - MEM_AVAIL) * 100 / MEM_TOTAL ))
echo "Memory Used: ${MEM_USED_PCT}%"
if [ "$MEM_USED_PCT" -gt "$WARN_MEM" ]; then
echo "⚠️ HIGH MEMORY: ${MEM_USED_PCT}%"
free -h | mail -s "[Alert] High Memory: ${HOSTNAME}" "$ALERT_EMAIL"
fi
# Disk Usage
df -h | grep -vE '^(tmpfs|devtmpfs|udev)' | awk 'NR>1 {
gsub(/%/,"",$5)
if ($5+0 > '"$WARN_DISK"') print "⚠️ DISK " $6 " at " $5 "%"
}'
# Top CPU Processes
echo ""
echo "Top CPU Processes:"
ps aux --sort=-%cpu | head -6 | awk '{printf " %-10s %5s%% %s\n", $1, $3, $11}'
echo ""
echo "=== End of Performance Check ==="
ตรวจสอบและ Profile Application
# perf — Linux Performance Analysis Tool
# ติดตั้ง
sudo apt install linux-perf -y # Ubuntu
sudo dnf install perf -y # RHEL/Rocky
# CPU Profiling ของ Process เฉพาะ (10 วินาที)
sudo perf stat -p $(pgrep nginx | head -1) sleep 10
# Record CPU Samples
sudo perf record -g -p $(pgrep nginx | head -1) sleep 30
sudo perf report
# Flamegraph (ต้องติดตั้ง flamegraph tools)
sudo perf script | stackcollapse-perf.pl | flamegraph.pl > flamegraph.svg
# strace — Trace System Calls
sudo strace -c -p $(pgrep nginx | head -1)
# lsof — ดู File Descriptor ที่เปิดอยู่
sudo lsof -p $(pgrep mysql | head -1) | wc -l # จำนวน FD ของ MySQL
sudo lsof -i :80 | head -20 # Connection บน Port 80
# ss — Socket Statistics ละเอียด
ss -tnp state established # TCP connections
ss -s # Summary
ss -tnp | awk '{print $6}' | sort | uniq -c | sort -rn | head -10 # Top remote IPs
Tuning Quick Reference
# สรุปค่า sysctl ที่แนะนำสำหรับ Web Server บน VPS
sudo tee /etc/sysctl.d/99-webserver-perf.conf <<'EOF'
# Memory
vm.swappiness = 10
vm.vfs_cache_pressure = 50
vm.dirty_ratio = 15
vm.dirty_background_ratio = 5
# File System
fs.file-max = 2097152
# Network - Connection
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.core.netdev_max_backlog = 65535
# Network - Buffer
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
# Network - TIME_WAIT
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_reuse = 1
# Network - Congestion (BBR)
net.ipv4.tcp_congestion_control = bbr
net.core.default_qdisc = fq
EOF
sudo sysctl --system
echo "✅ Web Server Performance Tuning Applied"
สรุป
System Performance Tuning บน Cloud VPS เริ่มจากการวัด Baseline ด้วย top, vmstat, iostat เพื่อรู้ว่า Bottleneck อยู่ที่ไหน จากนั้นปรับ CPU Governor เป็น performance, ลด Swappiness เป็น 10, ตั้งค่า Dirty Ratio เพื่อควบคุม Buffer Write และปิด Transparent Huge Pages สำหรับ Database Network Tuning ที่สำคัญคือเพิ่ม TCP Buffer, เปิด BBR Congestion Control และเพิ่ม Connection Queue สำหรับ High-Concurrency Application ต้องเพิ่ม File Descriptor Limit ทั้งระดับ System และ systemd Service การ Tune ที่ดีต้องวัดผลทุกครั้งเพื่อยืนยันว่าได้ผลจริง
แนะนำบริการ DE
การทำ Performance Tuning บน Server ต้องการสิทธิ์ Root เพื่อแก้ไข sysctl parameters, I/O Scheduler และ System Limits Cloud VPS ของ DE ให้ Root Access เต็มรูปแบบ รองรับการปรับค่าทุกระดับตั้งแต่ Kernel Parameters จนถึง Service Configuration เหมาะสำหรับผู้ที่ต้องการ Tune Server ให้รองรับ Traffic สูง
สำหรับผู้ที่ต้องการโฮสต์เว็บไซต์โดยไม่ต้อง Tune Server เอง Cloud Hosting ของ DE มี Infrastructure ที่ปรับแต่งมาพร้อมสำหรับ Web Hosting โดยเฉพาะ รองรับ Traffic ได้ดีโดยไม่ต้องตั้งค่าเพิ่มเติม

