bash
top -d 1 -n 10 # 1秒刷新,运行10次
bash
vmstat 1 5 # 每秒1次,共5次
bash
mpstat -P ALL 1 # 监控所有CPU核心
bash
iostat -dx 1 # 详细磁盘统计
bash
free -h -s 3 # 人类可读格式,每3秒刷新
bash
dstat -cdngy 1 # 监控CPU、磁盘、网络、内存等
bash
sar -u 1 3 # CPU使用率
sar -r 1 3 # 内存使用
bash
nmon -f -s 30 -c 120 # 每30秒采集一次,共120次
bash
perf top # 实时性能分析
perf stat -p <PID> # 进程统计
perf record -g -p <PID> # 记录调用图
bash
strace -c -p <PID> # 统计系统调用
bash
taskset -c 0,1 ./program # 绑定到CPU0和1
bash
service irqbalance status
bash
cpupower frequency-set -g performance # 高性能模式
bash
pmap -x <PID>
bash
slabtop -o # 按占用排序
bash
echo 10 > /proc/sys/vm/swappiness # 降低交换倾向
bash
echo never > /sys/kernel/mm/transparent_hugepage/enabled
bash
echo -17 > /proc/<PID>/oom_adj # 防止进程被OOM杀死
bash
iotop -o # 只显示有I/O活动的进程
bash
blktrace -d /dev/sda -o trace
bash
echo deadline > /sys/block/sda/queue/scheduler
bash
blockdev --setra 1024 /dev/sda
bash
mount -o noatime,nodiratime,data=writeback /dev/sda1 /mnt
bash
iftop -n -i eth0
bash
nethogs eth0
bash
ss -s # 摘要统计
ss -tulnp # 监听端口
bash
echo "net.ipv4.tcp_fin_timeout = 30" >> /etc/sysctl.conf
echo "net.core.somaxconn = 4096" >> /etc/sysctl.conf
sysctl -p
bash
ethtool -l eth0 # 查看队列
ethtool -L eth0 combined 8 # 设置8个队列
bash
echo 1 > /proc/irq/<irq_num>/smp_affinity
bash
echo "fs.file-max = 655350" >> /etc/sysctl.conf
bash
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
bash
echo "net.ipv4.ip_local_port_range = 1024 65000" >> /etc/sysctl.conf
bash
echo never > /sys/kernel/mm/transparent_hugepage/enabled
bash
echo 10 > /proc/sys/vm/dirty_ratio
ini
[mysqld]
innodb_buffer_pool_size = 4G
innodb_flush_log_at_trx_commit = 2
ini
maxmemory 4gb
maxmemory-policy allkeys-lru
nginx
worker_processes auto;
worker_connections 10240;
keepalive_timeout 65;
apache
StartServers 8
MinSpareServers 5
MaxSpareServers 20
bash
sysbench cpu --cpu-max-prime=20000 run
sysbench memory --memory-block-size=1K run
bash
fio --name=randread --ioengine=libaio --rw=randread --bs=4k --numjobs=4 --size=1G --runtime=60 --time_based --end_fsync=1
bash
iperf3 -s # 服务端
iperf3 -c <server_ip> -t 30 # 客户端
bash
netserver # 服务端
netperf -H <server_ip> -l 60 # 客户端
USE方法(Utilization, Saturation, Errors)
RED方法(Rate, Errors, Duration)
性能调优步骤:
#!/bin/bash
echo "===== CPU Top 10 ====="
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head -11
echo "===== Memory Top 10 ====="
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -11
echo "===== Disk Usage ====="
df -h
echo "===== Memory Usage ====="
free -h
echo "===== Load Average ====="
uptime
#!/bin/bash
echo "===== TCP Connections ====="
ss -s | grep -A 10 "TCP:"
echo "===== Established Connections by IP ====="
netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
Linux性能优化是一个系统工程,需要: 1. 全面监控系统资源使用情况 2. 准确识别性能瓶颈 3. 有针对性地实施优化措施 4. 持续监控优化效果 5. 建立性能基准和预警机制
掌握这些工具和方法,能够有效提升Linux系统的性能和稳定性。建议收藏本指南,在实际工作中根据具体场景灵活应用。