dnf install -y unixbench lmbench sysbench iperf3dnf install -y tuned && systemctl enable --now tuned # 后文各场景使用 tuned-adm 切换 profile
unixbench # 标准跑法:1 份拷贝 + nproc 份拷贝unixbench -c 1 -i 3 execl spawn shell8 # 只跑单项unixbench --print-workdir # 查看结果所在目录
lmbench <程序名> 调用。lmbench lat_ctx -s 0 2 # 上下文切换延迟(微秒)lmbench lat_tcp -s; lmbench lat_tcp localhost; lmbench lat_tcp -S localhost # TCP 回环延迟(微秒)lmbench lat_proc exec # 进程创建延迟(微秒)lmbench bw_mem 1g rd # 内存读带宽(MB/s)
--db-driver=pgsql)。sysbench cpu --threads=$(nproc) runsysbench memory --memory-block-size=1M --memory-total-size=10G runsysbench fileio --file-total-size=4G --file-test-mode=rndrw preparesysbench fileio --file-total-size=4G --file-test-mode=rndrw --time=60 runsysbench fileio --file-total-size=4G cleanup
iperf3 -s # 服务端iperf3 -c <服务端IP> -t 60 # 客户端
always 模式可能导致内存整理开销增大。TencentOS Server 4 默认已是 madvise,先确认:cat /sys/kernel/mm/transparent_hugepage/enabled
never。持久化方式见"调优结果持久化"一节,不建议写在 rc.local 中。cat > /etc/sysctl.d/99-database-perf.conf << 'EOF'vm.swappiness = 5vm.dirty_ratio = 10vm.dirty_background_ratio = 3EOFsysctl -p /etc/sysctl.d/99-database-perf.conf
dnf install -y numactlnumactl --hardwarenumactl --cpunodebind=0 --membind=0 /usr/sbin/mysqld
numactl --interleave=all /usr/sbin/mysqld
shared_buffers = 16GB,使用 2MB 大页时约需 8192 个,再留少量余量:grep Hugepagesize /proc/meminfoecho "vm.nr_hugepages = 8400" > /etc/sysctl.d/99-hugepages.confsysctl -p /etc/sysctl.d/99-hugepages.confgrep HugePages_ /proc/meminfo # 确认 HugePages_Total 达到预期
huge_pages = on,Oracle 设置 USE_LARGE_PAGES = ONLY。none;机械盘使用 mq-deadline。# 内存紧张时优先回收其他进程的内存,保留数据库缓存systemctl set-property mysqld.service MemoryLow=32G# 备份、导出等后台任务使用最低 I/O 优先级ionice -c3 <备份命令>
# MySQL OLTP 混合读写(脚本位于 /usr/share/sysbench/,需先创建测试库)mysql -u<用户> -p -e "CREATE DATABASE sbtest"sysbench oltp_read_write --mysql-user=<用户> --mysql-password=<密码> \\--tables=10 --table-size=100000 preparesysbench oltp_read_write --mysql-user=<用户> --mysql-password=<密码> \\--tables=10 --table-size=100000 --threads=$(nproc) --time=60 run# Redis(redis-benchmark 随 redis 包安装)redis-benchmark -n 100000 -c 50 -t set,get# PostgreSQL(pgbench 由 postgresql-contrib 包提供)pgbench -i -s 100 <数据库名>pgbench -c $(nproc) -j $(nproc) -T 60 <数据库名>
dirty_ratio 调优效果有限。ionice 只在 bfq、mq-deadline 调度器下生效,none 下无效。vm.zone_reclaim_mode = 1:开启后本节点内存不足时,内核会先回收本节点的页面缓存,而不是从其他节点分配,导致缓存频繁被清掉,对数据库通常不利。cat > /etc/sysctl.d/99-web-perf.conf << 'EOF'net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.ipv4.tcp_rmem = 4096 131072 16777216net.ipv4.tcp_wmem = 4096 65536 16777216net.core.somaxconn = 65535net.ipv4.tcp_max_syn_backlog = 8192net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_fin_timeout = 30EOFsysctl -p /etc/sysctl.d/99-web-perf.conf
somaxconn 需要与 Nginx/Tomcat 的 listen backlog 配合,取两者较小值生效。fs.file-max 在 TencentOS Server 4 上默认已经足够大,不需要调整,需要调整的是进程级上限。systemd 管理的服务通过 unit 文件设置,/etc/security/limits.conf 对 systemd 服务不生效:mkdir -p /etc/systemd/system/nginx.service.dcat > /etc/systemd/system/nginx.service.d/limits.conf << 'EOF'[Service]LimitNOFILE=1048576EOFsystemctl daemon-reload && systemctl restart nginx
dnf install -y irqbalancesystemctl enable --now irqbalance
systemctl stop irqbalance # 否则 irqbalance 会覆盖手动设置grep eth0 /proc/interruptsecho 0-3 > /proc/irq/<IRQ号>/smp_affinity_list
# 增大网卡收发 ring buffer(不超过 ethtool -g 显示的最大值)ethtool -g eth0ethtool -G eth0 rx 4096 tx 4096# /proc/net/softnet_stat 每行对应一个 CPU,第 2 列(十六进制)持续增长说明软中断来不及处理而丢包,此时增大收包队列cat /proc/net/softnet_statsysctl -w net.core.netdev_max_backlog=16384
ethtool -l eth0 # 查看队列数echo ffff > /sys/class/net/eth0/queues/rx-0/rps_cpus # 每个 rx 队列都需设置,掩码按实际 CPU 数计算sysctl -w net.core.rps_sock_flow_entries=32768echo 4096 > /sys/class/net/eth0/queues/rx-0/rps_flow_cnt # rps_sock_flow_entries / 队列数
cat > /etc/sysctl.d/99-web-ports.conf << 'EOF'net.ipv4.ip_local_port_range = 10000 65000# 预留本机监听的业务端口,避免被出站连接占用(按实际端口替换)net.ipv4.ip_local_reserved_ports = 8080,9000-9100EOFsysctl -p /etc/sysctl.d/99-web-ports.conf
cat > /etc/sysctl.d/99-web-keepalive.conf << 'EOF'net.ipv4.tcp_keepalive_time = 600net.ipv4.tcp_keepalive_intvl = 30net.ipv4.tcp_keepalive_probes = 3EOFsysctl -p /etc/sysctl.d/99-web-keepalive.conf
SO_KEEPALIVE 的连接生效。tuned-adm profile throughput-performance。# HTTP 压测(在另一台机器上执行)dnf install -y httpd-toolsab -n 1000000 -c 1000 -k http://<服务端IP>/# 单机多核吞吐(关注多拷贝 Index)unixbench -c $(nproc) -i 3 syscall context1 pipe
tcp_tw_reuse = 1 仅对出站连接复用有效,不影响入站连接。setsockopt(SO_RCVBUF/SO_SNDBUF) 设置或依赖系统自动调整。tcp_tw_recycle 已在 Linux 4.12 中移除(NAT 环境下会导致丢包),TencentOS Server 4 上不存在该参数。BDP = 带宽(bps) × RTT(s)所需窗口大小 = BDP / 8
cat > /etc/sysctl.d/99-wan-perf.conf << 'EOF'# 将缓冲区上限设为 BDP 的 2 倍以上net.core.rmem_max = 67108864net.core.wmem_max = 67108864net.ipv4.tcp_rmem = 4096 131072 67108864net.ipv4.tcp_wmem = 4096 65536 67108864net.ipv4.tcp_window_scaling = 1# 长连接空闲后不重新慢启动,间歇性传输时能更快恢复到满速net.ipv4.tcp_slow_start_after_idle = 0EOFsysctl -p /etc/sysctl.d/99-wan-perf.conf
cat > /etc/sysctl.d/99-bbr.conf << 'EOF'net.core.default_qdisc = fqnet.ipv4.tcp_congestion_control = bbrEOFsysctl -p /etc/sysctl.d/99-bbr.confsysctl net.ipv4.tcp_congestion_control # 确认已切换为 bbr
tcp_bbr 模块默认未加载,tcp_available_congestion_control 中看不到 bbr 属正常现象,设置时内核会自动加载该模块。sysctl -w net.ipv4.tcp_mtu_probing=1
iperf3 -c <对端 IP> -t 60 # 单流带宽iperf3 -c <对端 IP> -t 60 -R # 反向ss -ti dst <对端 IP> # 查看 rwnd_limited / sndbuf_limited 占比(未受限时不显示)
rwnd_limited 和 sndbuf_limited 占比应明显下降,否则瓶颈不在窗口。net.ipv4.tcp_mem(单位为页)限制,可通过 cat /proc/net/sockstat 中 TCP 的 mem 值确认是否接近上限。dnf install -y tuned-profiles-cpu-partitioningvi /etc/tuned/cpu-partitioning-variables.conf# isolated_cores=2-15tuned-adm profile cpu-partitioningreboot
cpu-partitioning 会自动设置 nohz_full、rcu_nocbs 启动参数,并让 irqbalance 不再把中断分配到隔离核上。如果不使用该 profile、手动隔离 CPU,需要自行在 /etc/sysconfig/irqbalance 中排除隔离核:# /etc/sysconfig/irqbalanceIRQBALANCE_BANNED_CPULIST=2-15
systemctl restart irqbalance。-a),否则只有主线程生效:taskset -a -cp 2-15 <PID>
cat /sys/class/net/eth0/device/numa_node # 网卡所在节点,-1 表示无 NUMA 信息numactl --cpunodebind=<节点> --membind=<节点> <业务命令>
ethtool -c eth0 # 查看当前设置ethtool -C eth0 adaptive-rx off rx-usecs 0
ethtool -c 的输出为准。echo never > /sys/kernel/mm/transparent_hugepage/enabled(持久化见“调优结果持久化”一节)。latency-performance 或 network-latency profile。KVM 虚拟机内 cpuidle 驱动通常为 none,vCPU 空闲后唤醒较慢,可启用 TencentOS Server 4 内核已内置的 haltpoll(会增加宿主机侧 CPU 占用,适合独占物理核的实例):cat /sys/devices/system/cpu/cpuidle/current_driver # none 表示未启用grubby --update-kernel=ALL --args="cpuidle_haltpoll.force=1"reboot
chrt -f -p 50 <PID> # SCHED_FIFO 优先级 50
kernel.sched_rt_runtime_us 设为 -1,默认值为普通任务保留了 5% 的 CPU 时间,防止实时任务把系统卡死。taskset -c 2,3 lmbench lat_ctx -s 0 2 # 上下文切换延迟lmbench lat_tcp -s; taskset -c 2 lmbench lat_tcp localhost; lmbench lat_tcp -S localhost
tuned-adm profile throughput-performance。mkdir -p /buildmount -t tmpfs -o size=32G tmpfs /build
# CPUWeight 默认 100,调低后在 CPU 争抢时让出资源;TasksMax 防止进程数失控;MemoryHigh 超过后开始限流systemd-run --scope -p CPUWeight=20 -p TasksMax=8192 -p MemoryHigh=16G -- make -j$(nproc)# 或使用 nice/ionice 降低 CPU 和 I/O 优先级nice -n 10 ionice -c3 make -j$(nproc)
CPUQuota/CPUWeight 限制,把资源留给构建任务。unixbench -c 1 -c $(nproc) -i 3 execl spawn shell1 shell8lmbench lat_proc exec
tuned-adm profile virtual-host
dnf install -y libvirt numactlnumactl --hardware # 查看各节点的 CPU 与内存virsh vcpupin <虚拟机> <vCPU 编号> <物理 CPU> --configvirsh emulatorpin <虚拟机> <物理 CPU 列表> --config # QEMU 自身线程virsh numatune <虚拟机> --mode strict --nodeset 0 --config
<memoryBacking><hugepages/></memoryBacking>
<interface type='network'><model type='virtio'/><driver name='vhost' queues='4'/></interface>
ethtool -L eth0 combined 4 启用全部队列。cat /sys/kernel/mm/ksm/run # 0 表示未运行,无需处理echo 2 > /sys/kernel/mm/ksm/run # 停止 KSM 并拆分已合并的页面
unixbench -c 1 -c $(nproc) -i 3sysbench cpu --threads=$(nproc) run
cat /proc/pressure/cpu /proc/pressure/memory /proc/pressure/io
some 表示至少有一个任务因等待该资源而停顿的时间占比,full 表示所有非空闲任务同时停顿的时间占比(CPU 的 full 在系统级别恒为 0,只看 some);avg10、avg60、avg300 分别为 10 秒、60 秒、300 秒内的平均值。哪一项持续偏高,瓶颈就在哪一类资源上。dnf install -y tunedsystemctl enable --now tunedtuned-adm recommendtuned-adm profile $(tuned-adm recommend)tuned-adm activetuned-adm verify
Profile | 主要设置 | 适用场景 |
throughput-performance | governor=performance,readahead≥4096,dirty_ratio=40,swappiness=10 | 物理机通用高吞吐 |
virtual-guest | 继承 throughput-performance,dirty_ratio=30,swappiness=30 | 虚拟机通用 |
latency-performance | governor=performance,限制 C-state,dirty_ratio=10,swappiness=10 | 低延迟 |
network-latency | 继承 latency-performance,关闭 THP 和 NUMA 自动平衡,busy_poll=50 | 低延迟网络 |
hpc-compute | 继承 latency-performance,THP=always,关闭 NUMA 自动平衡 | HPC 计算 |
参数 | TencentOS Server 4 默认值 | 建议 |
vm.swappiness | 60 | 保持默认,除非明确需要减少 swap |
vm.dirty_ratio | 20 | 保持默认 |
net.ipv4.tcp_congestion_control | cubic | 保持默认,除非跨地域传输 |
THP | madvise | 保持默认,除非数据库或低延迟场景需要 never |
sysctl <参数名> 确认当前值。unixbench # 对比切换 profile 前后的单拷贝和多拷贝 Index
mkdir -p /etc/tuned/my-dbcat > /etc/tuned/my-db/tuned.conf << 'EOF'[main]summary=数据库业务调优include=throughput-performance[vm]transparent_hugepages=never[sysctl]vm.swappiness=5vm.dirty_ratio=10vm.dirty_background_ratio=3EOFtuned-adm profile my-dbtuned-adm verify
include 继承的父 profile 中的同名参数会被子 profile 覆盖。reapply_sysctl(见 /etc/tuned/tuned-main.conf),应用 profile 后会重新加载 /etc/sysctl.d/ 中的配置,同名参数以 sysctl.d 为准。改用 tuned 管理后,应删除前面各场景写入的同名 sysctl.d 文件,避免两处配置不一致。常见建议 | 在 TencentOS Server 4 上的情况 | 建议做法 |
通过 sysctl 调整 kernel.sched_latency_ns、kernel.sched_min_granularity_ns | 5.13 起这些参数已从 sysctl 移到 debugfs,只作调试用途;6.6 内核使用 EEVDF 调度器, min_granularity 已被 base_slice_ns 取代 | 业务调优中不需要调整 |
设置 net.ipv4.tcp_tw_recycle | Linux 4.12 已移除 | 只使用 tcp_tw_reuse |
I/O 调度器设为 noop,或使用启动参数 elevator= | blk-mq 下对应的是 none;elevator= 启动参数已无效 | 通过 udev 规则设置 |
vm.dirty_ratio=40 | 大内存机器上脏页堆积过多,回写时容易造成 I/O 卡顿 | 按场景设置 |
修改 /etc/pam.d/common-session 启用 pam_limits | Debian 系路径,TencentOS Server 4 的 system-auth 已启用 | systemd 服务使用 LimitNOFILE |
intel_idle.max_cstate=1 | AMD 机器上无效 | 使用 tuned latency-performance |
vm.panic_on_oom=1 | OOM 时直接重启整机 | 仅在有高可用切换机制时使用 |
磁盘类型 | 推荐调度器 | 原因 |
NVMe | none | NVMe 自带多队列,无需额外调度 |
SSD | none 或 mq-deadline | 随机访问快,调度开销无收益 |
机械盘(数据库/吞吐型) | mq-deadline | 保证请求延迟上限,开销低 |
机械盘(多用户混合负载) | bfq | 公平分配带宽,避免饿死 |
cat /sys/block/sda/queue/schedulercat > /etc/udev/rules.d/60-scheduler.rules << 'EOF'ACTION=="add|change", KERNEL=="sd[a-z]*|nvme[0-9]*n[0-9]*", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="none"ACTION=="add|change", KERNEL=="sd[a-z]*", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="mq-deadline"EOFudevadm control --reload && udevadm trigger --type=devices --action=change
vda、vdb 等)通常报告为机械盘(rotational 为 1),但后端多为云存储,保持默认的 none 即可,不要按机械盘处理。blockdev --getra /dev/sda # 单位:512 字节扇区blockdev --setra 8192 /dev/sda
discard(每次删除文件都同步执行 TRIM,会拖慢删除和写入):systemctl enable --now fstrim.timer # 默认每周执行一次
# su 为条带单元大小,sw 为数据盘个数(如 RAID5 的 4+1 盘为 4)mkfs.xfs -d su=256k,sw=4 /dev/md0
/dev/md*)上的 XFS 通常会自动识别条带参数,硬件 RAID 需要手动指定。工具 | 用途 | 安装命令 |
unixbench | 综合性能基准测试 | dnf install -y unixbench |
lmbench | 微基准测试(延迟/带宽) | dnf install -y lmbench |
sysbench | CPU/内存/IO/数据库压测 | dnf install -y sysbench |
iperf3 | 网络带宽测试 | dnf install -y iperf3 |
fio | 存储 I/O 精细化测试 | dnf install -y fio |
perf | CPU 性能分析(函数级) | dnf install -y perf |
文档反馈