编译与调试完全指南—第14章 系统配置与调优

第14章 系统配置与调优

完整 485 页的 《编译与调试完全指南》见: lengjingzju-notes发布页

系统性能调优是调试工作的重要延伸。当程序本身没有明显缺陷,但系统整体表现不佳时,往往需要从内核参数、资源限制、I/O 调度、CPU 策略等层面进行系统级调优。本章按照"资源限制→网络→内存→I/O→CPU→持久化验证"的逻辑组织,覆盖生产环境中最常用的调优手段,帮助读者建立从发现问题、调整参数到验证效果的完整调优工作流。
#mermaid-svg-58ntJNarcxM6nWh0{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-58ntJNarcxM6nWh0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-58ntJNarcxM6nWh0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-58ntJNarcxM6nWh0 .error-icon{fill:#552222;}#mermaid-svg-58ntJNarcxM6nWh0 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-58ntJNarcxM6nWh0 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-58ntJNarcxM6nWh0 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-58ntJNarcxM6nWh0 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-58ntJNarcxM6nWh0 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-58ntJNarcxM6nWh0 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-58ntJNarcxM6nWh0 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-58ntJNarcxM6nWh0 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-58ntJNarcxM6nWh0 .marker.cross{stroke:#333333;}#mermaid-svg-58ntJNarcxM6nWh0 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-58ntJNarcxM6nWh0 p{margin:0;}#mermaid-svg-58ntJNarcxM6nWh0 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-58ntJNarcxM6nWh0 .cluster-label text{fill:#333;}#mermaid-svg-58ntJNarcxM6nWh0 .cluster-label span{color:#333;}#mermaid-svg-58ntJNarcxM6nWh0 .cluster-label span p{background-color:transparent;}#mermaid-svg-58ntJNarcxM6nWh0 .label text,#mermaid-svg-58ntJNarcxM6nWh0 span{fill:#333;color:#333;}#mermaid-svg-58ntJNarcxM6nWh0 .node rect,#mermaid-svg-58ntJNarcxM6nWh0 .node circle,#mermaid-svg-58ntJNarcxM6nWh0 .node ellipse,#mermaid-svg-58ntJNarcxM6nWh0 .node polygon,#mermaid-svg-58ntJNarcxM6nWh0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-58ntJNarcxM6nWh0 .rough-node .label text,#mermaid-svg-58ntJNarcxM6nWh0 .node .label text,#mermaid-svg-58ntJNarcxM6nWh0 .image-shape .label,#mermaid-svg-58ntJNarcxM6nWh0 .icon-shape .label{text-anchor:middle;}#mermaid-svg-58ntJNarcxM6nWh0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-58ntJNarcxM6nWh0 .rough-node .label,#mermaid-svg-58ntJNarcxM6nWh0 .node .label,#mermaid-svg-58ntJNarcxM6nWh0 .image-shape .label,#mermaid-svg-58ntJNarcxM6nWh0 .icon-shape .label{text-align:center;}#mermaid-svg-58ntJNarcxM6nWh0 .node.clickable{cursor:pointer;}#mermaid-svg-58ntJNarcxM6nWh0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-58ntJNarcxM6nWh0 .arrowheadPath{fill:#333333;}#mermaid-svg-58ntJNarcxM6nWh0 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-58ntJNarcxM6nWh0 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-58ntJNarcxM6nWh0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-58ntJNarcxM6nWh0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-58ntJNarcxM6nWh0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-58ntJNarcxM6nWh0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-58ntJNarcxM6nWh0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-58ntJNarcxM6nWh0 .cluster text{fill:#333;}#mermaid-svg-58ntJNarcxM6nWh0 .cluster span{color:#333;}#mermaid-svg-58ntJNarcxM6nWh0 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-58ntJNarcxM6nWh0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-58ntJNarcxM6nWh0 rect.text{fill:none;stroke-width:0;}#mermaid-svg-58ntJNarcxM6nWh0 .icon-shape,#mermaid-svg-58ntJNarcxM6nWh0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-58ntJNarcxM6nWh0 .icon-shape p,#mermaid-svg-58ntJNarcxM6nWh0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-58ntJNarcxM6nWh0 .icon-shape .label rect,#mermaid-svg-58ntJNarcxM6nWh0 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-58ntJNarcxM6nWh0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-58ntJNarcxM6nWh0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-58ntJNarcxM6nWh0 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 系统调优全景
系统资源限制

ulimit / limits.conf / systemd
网络参数调优

sysctl net.*
内存与交换调优

sysctl vm.*
I/O调度与磁盘调优

scheduler / mount
CPU与调度调优

cpufreq / taskset / chrt
配置持久化与验证

sysctl.conf / systemctl

14.1 系统资源限制

Linux 通过 ulimit(shell 内建命令)和 /etc/security/limits.conf(PAM 配置)控制进程可使用的资源上限。这些限制分为软限制(soft,当前生效值,进程可自行调整至硬限制)和硬限制(hard,上限,仅 root 可提升)。本节覆盖文件描述符、进程线程数、堆栈大小和 systemd 服务限制四类最常见的资源限制配置。

14.1.1 文件描述符限制

文件描述符(File Descriptor)是进程访问文件、socket、管道等 I/O 资源的句柄。高并发服务器(如 Nginx、数据库)常因文件描述符不足而报 Too many open files 错误。

配置项总表:

配置项 文件路径 功能说明 默认值/建议值
用户进程限制 /etc/security/limits.conf 用户级文件描述符限制 nofile 65536
系统全局限制 /proc/sys/fs/file-max 系统级最大文件描述符数 2097152
当前使用统计 /proc/sys/fs/file-nr 已分配/空闲文件描述符数 动态
进程硬限制 ulimit -Hn 进程硬文件描述符限制 65536
进程软限制 ulimit -Sn 进程软文件描述符限制 1024
systemd 服务限制 /etc/systemd/system.conf systemd 服务文件描述符限制 DefaultLimitNOFILE=65536
systemd 用户限制 /etc/systemd/user.conf systemd 用户会话限制 DefaultLimitNOFILE=65536

配置示例:

bash 复制代码
# 查看当前限制
$ ulimit -n
1024

# 临时设置(仅当前 shell 会话有效)
$ ulimit -n 65536

# 永久设置(/etc/security/limits.conf)
# 格式:<domain> <type> <item> <value>
*       soft    nofile    65536
*       hard    nofile    65536
root    soft    nofile    65536
root    hard    nofile    65536

# 系统全局设置(临时)
$ echo 2097152 > /proc/sys/fs/file-max

# 系统全局设置(永久,/etc/sysctl.conf)
fs.file-max = 2097152

# 查看当前系统文件描述符使用情况
$ cat /proc/sys/fs/file-nr
# 输出三列:已分配数  已分配但未使用数  最大值

易错点:

易错场景 说明
ulimit -n 作用范围 仅对当前 shell 及其子进程生效,修改 limits.conf 后需重新登录
systemd 服务不受 limits.conf 控制 需在服务单元文件中单独设置 LimitNOFILE
file-maxnofile 的区别 file-max 是系统全局上限,所有进程共享;nofile 是单进程上限

14.1.2 进程与线程限制

配置项 文件路径 功能说明 默认值/建议值
用户进程数 /etc/security/limits.conf 用户最大进程数 nproc 65536
系统最大 PID /proc/sys/kernel/pid_max 系统最大进程 ID 32768(可调至 4194304)
最大线程数 /proc/sys/kernel/threads-max 系统最大线程数 总内存(KB)/8
每个进程线程数 /proc/sys/kernel/pid_max 间接限制线程数 依赖 pid_max
cgroup 线程限制 /sys/fs/cgroup/pids/ cgroup 进程数限制 无限制

配置示例:

bash 复制代码
# 查看当前进程限制
$ ulimit -u

# 设置最大进程数(临时)
$ echo 4194304 > /proc/sys/kernel/pid_max
$ echo 65536 > /proc/sys/kernel/threads-max

# 永久设置(/etc/sysctl.conf)
kernel.pid_max = 4194304
kernel.threads-max = 65536

# limits.conf 中设置用户进程数
*       soft    nproc    65536
*       hard    nproc    65536

14.1.3 堆栈大小配置

配置项 文件路径 功能说明 默认值/建议值
进程堆栈大小 ulimit -s 进程堆栈大小(KB) 8192(8MB)
内核栈大小 编译时配置 内核线程栈大小 16KB 或 32KB
pthread 栈大小 pthread_attr_setstacksize 线程栈大小 pthread 默认 2MB
递归深度限制 程序内部限制 递归函数调用深度 依赖栈大小

配置示例:

bash 复制代码
# 查看当前堆栈大小
$ ulimit -s
8192

# 临时设置堆栈大小
$ ulimit -s unlimited    # 无限制
$ ulimit -s 65536        # 64MB

# 永久设置(/etc/security/limits.conf)
*       soft    stack    65536
*       hard    stack    65536
c 复制代码
// 在程序中设置线程栈大小
#include <pthread.h>

pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 16 * 1024 * 1024); // 16MB
pthread_create(&tid, &attr, thread_func, NULL);
pthread_attr_destroy(&attr);

易错点:

易错场景 说明
堆栈过小 递归或大局部变量引发段错误(SIGSEGV
堆栈过大 浪费虚拟地址空间,对多线程程序影响尤为明显
ulimit -s unlimited 并不意味着无限,实际受限于可用虚拟地址空间

14.1.4 systemd 服务限制

systemd 管理的服务不受 /etc/security/limits.conf 控制,必须在服务单元文件(.service)中单独配置资源限制。这是因为 systemd 服务由 PID 1 直接启动,不经过 PAM 登录流程。

资源限制指令总表:

配置项 功能说明 示例值 对应 ulimit 选项
LimitNOFILE 文件描述符限制 65536 ulimit -n
LimitNPROC 进程数限制 65536 ulimit -u
LimitSTACK 堆栈大小限制 8M ulimit -s
LimitCORE 核心转储大小 infinity ulimit -c
LimitMEMLOCK 可锁定内存上限 64M ulimit -l
LimitAS 地址空间限制 infinity ulimit -v
MemoryMax cgroup 内存使用上限 2G 无对应
MemoryHigh cgroup 内存使用高水位 1.5G 无对应
CPUQuota CPU 使用限制 50% 无对应
TasksMax 最大任务数(进程+线程) 4096 无对应

完整服务单元文件示例:

ini 复制代码
# /etc/systemd/system/myapp.service
[Unit]
Description=My Application Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/myapp
User=myapp
Group=myapp

# === 资源限制配置 ===
LimitNOFILE=65536
LimitNPROC=65536
LimitSTACK=8M
LimitCORE=infinity
LimitMEMLOCK=64M

# === cgroup 资源控制(需要 cgroup v2)===
MemoryMax=2G
MemoryHigh=1.5G
CPUQuota=50%
TasksMax=4096

[Install]
WantedBy=multi-user.target

使用 override 片段修改已有服务(推荐方式):

直接编辑发行版提供的服务文件会在升级时被覆盖。推荐使用 systemctl edit 创建 override 片段:

bash 复制代码
# 创建 override 片段(自动创建 /etc/systemd/system/nginx.service.d/override.conf)
$ sudo systemctl edit nginx

# 在打开的编辑器中写入:
[Service]
LimitNOFILE=131072
LimitNPROC=65536
MemoryMax=4G
CPUQuota=200%

等效于创建了以下文件:

ini 复制代码
# /etc/systemd/system/nginx.service.d/override.conf
[Service]
LimitNOFILE=131072
LimitNPROC=65536
MemoryMax=4G
CPUQuota=200%

全局默认限制(影响所有 systemd 服务):

ini 复制代码
# /etc/systemd/system.conf
[Manager]
DefaultLimitNOFILE=65536
DefaultLimitNPROC=65536
DefaultLimitSTACK=8M
DefaultLimitCORE=infinity
ini 复制代码
# /etc/systemd/user.conf(用户会话)
[Manager]
DefaultLimitNOFILE=65536

配置生效与验证:

bash 复制代码
# 修改后重新加载 systemd 配置
$ sudo systemctl daemon-reload

# 重启服务使配置生效
$ sudo systemctl restart myapp

# 查看服务的实际资源限制
$ systemctl show myapp | grep -E "Limit|Memory|CPU"
LimitNOFILE=65536
LimitNPROC=65536
LimitSTACK=8388608
LimitCORE=infinity
MemoryMax=2147483648
CPUQuotaPerSecUSec=500ms

# 查看服务进程的 /proc 限制(更直观)
$ cat /proc/$(systemctl show -p MainPID --value myapp)/limits
Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              8388608              bytes
Max core file size        unlimited            unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             65536                65536                processes
Max open files            65536                65536                files
Max locked memory         67108864             67108864             bytes
Max address space         unlimited            unlimited            bytes

MemoryMaxCPUQuota 详解:

参数 说明 注意事项
MemoryMax 服务可使用的最大内存量,超出后触发 OOM kill 需要 cgroup v2(stat -fc %T /sys/fs/cgroup/ 输出 cgroup2fs
MemoryHigh 内存使用高水位,超出后内核积极回收 不会直接 kill 进程,比 MemoryMax 更温和
CPUQuota CPU 时间配额百分比,50% 表示最多使用半个 CPU 核心 多核场景可设为 200% 表示 2 个核心
TasksMax 服务允许创建的最大进程/线程总数 防止 fork 炸弹

易错点:

易错场景 说明
修改 limits.conf 对 systemd 服务无效 systemd 服务不经过 PAM,必须在 .service 文件中配置
修改后忘记 daemon-reload 必须执行 systemctl daemon-reloadrestart
MemoryMax 设置过小 服务可能频繁被 OOM kill,查看 journalctl -u <service> 确认
CPUQuota 与 CPUShares 混淆 CPUQuota 是硬限制,CPUShares 是相对权重(仅竞争时生效)

14.2 网络参数调优

网络参数通过 sysctl 命令或 /proc/sys/net/ 下的文件进行调整。所有 net.* 参数均可通过 /etc/sysctl.conf/etc/sysctl.d/*.conf 持久化。本节从 TCP/IP 协议栈、网络缓冲区和拥塞控制三个维度介绍网络调优。

14.2.1 TCP/IP 协议栈调优

参数路径 功能说明 默认值 建议值
net.core.somaxconn 监听队列最大长度(完成三次握手) 128 1024~4096
net.ipv4.tcp_max_syn_backlog SYN 队列长度(半连接) 1024 2048~8192
net.core.netdev_max_backlog 网卡接收队列长度 1000 5000
net.ipv4.tcp_rmem TCP 接收缓冲区(min, default, max) 4096 87380 6291456 4096 87380 16777216
net.ipv4.tcp_wmem TCP 发送缓冲区(min, default, max) 4096 16384 4194304 4096 65536 16777216
net.ipv4.tcp_mem TCP 内存压力控制(pages) 根据内存自动计算 根据内存调整
net.ipv4.tcp_keepalive_time TCP 保活时间(秒) 7200 1800
net.ipv4.tcp_keepalive_intvl 保活探测间隔(秒) 75 30
net.ipv4.tcp_keepalive_probes 保活探测次数 9 3
net.ipv4.tcp_fin_timeout FIN_WAIT_2 状态超时(秒) 60 30
net.ipv4.tcp_max_tw_buckets TIME_WAIT 套接字最大数量 262144 524288
net.ipv4.tcp_tw_reuse 允许重用 TIME-WAIT 套接字 0 1
net.ipv4.tcp_tw_recycle 快速回收 TIME-WAIT(NAT 环境禁用) 0 0(不推荐开启)
net.ipv4.tcp_syncookies SYN Cookies 防御 SYN Flood 1 1
net.ipv4.ip_local_port_range 本地端口范围 32768 60999 1024 65000
net.ipv4.tcp_max_orphans 孤儿套接字最大数量 65536 65536~262144

tcp_max_orphans 参数详解:

孤儿套接字(Orphan Socket)是指不再被任何用户空间文件描述符引用的 TCP 套接字。典型产生场景包括:

  • 进程调用 close() 后,TCP 连接仍处于 FIN_WAIT 或 LAST_ACK 状态
  • 进程异常退出,未正常关闭连接
  • 进程调用了 shutdown() 但未 close()
要点 说明
作用 限制系统中孤儿 TCP 套接字的最大数量,防止 DoS 攻击
超出行为 超出限制时,内核直接 RST 该连接并打印警告信息
内存开销 每个孤儿套接字占用约 64KB 不可交换的内核内存
默认值 等于内核参数 NR_FILE,根据系统内存自动调整
调优建议 不建议降低此值;高并发短连接场景可适当增大
监控方法 cat /proc/net/sockstat 查看 orphan 字段
bash 复制代码
# 查看当前孤儿套接字数量
$ cat /proc/net/sockstat
sockets: used 1234
TCP: inuse 567 orphan 89 tw 456 alloc 678 mem 123
# orphan 89 表示当前有 89 个孤儿套接字

# 查看当前 tcp_max_orphans 设置
$ sysctl net.ipv4.tcp_max_orphans
net.ipv4.tcp_max_orphans = 65536

# 增大孤儿套接字限制(高并发短连接场景)
$ sysctl -w net.ipv4.tcp_max_orphans=262144

参数关系图:
#mermaid-svg-Bhe4UZgRvWcCfhu6{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Bhe4UZgRvWcCfhu6 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .error-icon{fill:#552222;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .marker.cross{stroke:#333333;}#mermaid-svg-Bhe4UZgRvWcCfhu6 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Bhe4UZgRvWcCfhu6 p{margin:0;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .cluster-label text{fill:#333;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .cluster-label span{color:#333;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .cluster-label span p{background-color:transparent;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .label text,#mermaid-svg-Bhe4UZgRvWcCfhu6 span{fill:#333;color:#333;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .node rect,#mermaid-svg-Bhe4UZgRvWcCfhu6 .node circle,#mermaid-svg-Bhe4UZgRvWcCfhu6 .node ellipse,#mermaid-svg-Bhe4UZgRvWcCfhu6 .node polygon,#mermaid-svg-Bhe4UZgRvWcCfhu6 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .rough-node .label text,#mermaid-svg-Bhe4UZgRvWcCfhu6 .node .label text,#mermaid-svg-Bhe4UZgRvWcCfhu6 .image-shape .label,#mermaid-svg-Bhe4UZgRvWcCfhu6 .icon-shape .label{text-anchor:middle;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .rough-node .label,#mermaid-svg-Bhe4UZgRvWcCfhu6 .node .label,#mermaid-svg-Bhe4UZgRvWcCfhu6 .image-shape .label,#mermaid-svg-Bhe4UZgRvWcCfhu6 .icon-shape .label{text-align:center;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .node.clickable{cursor:pointer;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .arrowheadPath{fill:#333333;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Bhe4UZgRvWcCfhu6 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Bhe4UZgRvWcCfhu6 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Bhe4UZgRvWcCfhu6 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .cluster text{fill:#333;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .cluster span{color:#333;}#mermaid-svg-Bhe4UZgRvWcCfhu6 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Bhe4UZgRvWcCfhu6 rect.text{fill:none;stroke-width:0;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .icon-shape,#mermaid-svg-Bhe4UZgRvWcCfhu6 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .icon-shape p,#mermaid-svg-Bhe4UZgRvWcCfhu6 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .icon-shape .label rect,#mermaid-svg-Bhe4UZgRvWcCfhu6 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Bhe4UZgRvWcCfhu6 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Bhe4UZgRvWcCfhu6 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Bhe4UZgRvWcCfhu6 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 孤儿套接字


进程 close/异常退出
孤儿套接字

tcp_max_orphans
超出限制?
内核发送 RST
等待超时关闭
TCP连接关闭


FIN 到达
FIN_WAIT_2

tcp_fin_timeout
TIME_WAIT

tcp_max_tw_buckets
tcp_tw_reuse?
重用端口
等待 2MSL
TCP连接建立
SYN 到达
SYN 队列

tcp_max_syn_backlog
连接建立
Accept 队列

net.core.somaxconn

14.2.2 网络缓冲区调优

参数路径 功能说明 默认值 建议值
net.core.rmem_max 接收缓冲区最大值 212992 16777216
net.core.wmem_max 发送缓冲区最大值 212992 16777216
net.core.rmem_default 接收缓冲区默认值 212992 873800
net.core.wmem_default 发送缓冲区默认值 212992 655360
net.core.optmem_max 辅助数据最大长度 20480 65536
net.core.netdev_budget NAPI 处理包数量预算 300 600

14.2.3 拥塞控制算法

参数路径 功能说明 可用选项
net.ipv4.tcp_congestion_control TCP 拥塞控制算法 cubicrenobbrhtcp
net.ipv4.tcp_available_congestion_control 可用拥塞控制算法 查看系统支持
net.ipv4.tcp_allowed_congestion_control 允许使用的算法 默认所有可用

算法对比:

算法 特点 适用场景
cubic Linux 默认,基于窗口增长 通用场景
bbr Google 开发,基于带宽和延迟估计 高延迟/高带宽网络
reno 经典算法,基于丢包 低延迟局域网
htcp Hamilton TCP,适合高带宽延迟积 广域网

配置示例:

bash 复制代码
# 查看当前算法
$ sysctl net.ipv4.tcp_congestion_control

# 查看可用算法
$ sysctl net.ipv4.tcp_available_congestion_control

# 加载 bbr 模块(如果未加载)
$ modprobe tcp_bbr

# 设置算法(临时)
$ sysctl -w net.ipv4.tcp_congestion_control=bbr

# 永久设置(/etc/sysctl.conf)
net.ipv4.tcp_congestion_control = bbr

14.2.4 配置示例

bash 复制代码
# 临时设置网络参数
$ echo 1024 > /proc/sys/net/core/somaxconn
$ echo 2048 > /proc/sys/net/ipv4/tcp_max_syn_backlog

# 永久设置(/etc/sysctl.conf 或 /etc/sysctl.d/99-network.conf)
net.core.somaxconn = 1024
net.ipv4.tcp_max_syn_backlog = 2048
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_mem = 8388608 12582912 16777216
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1800
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_orphans = 262144
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.optmem_max = 65536
net.core.netdev_budget = 600

14.3 内存与交换调优

内存调优的核心目标是:在物理内存充足时减少不必要的交换,在内存紧张时合理控制脏页写回和缓存回收策略,避免 OOM 或过度交换导致的性能抖动。

14.3.1 虚拟内存管理

参数路径 功能说明 默认值 建议值
vm.swappiness 交换倾向(0-100),值越大越倾向交换 60 10(服务器)/ 1(数据库)
vm.vfs_cache_pressure 目录和 inode 缓存回收压力 100 50
vm.dirty_ratio 系统脏页百分比阈值(触发同步写) 20 10
vm.dirty_background_ratio 后台写回脏页百分比阈值 10 5
vm.dirty_expire_centisecs 脏页过期时间(1/100 秒) 3000 500
vm.dirty_writeback_centisecs 后台写回间隔(1/100 秒) 500 100
vm.min_free_kbytes 系统保留的最小空闲内存(KB) 自动计算 内存的 1-3%
vm.overcommit_memory 内存过量使用策略(0/1/2) 0 0(通用)/ 2(严格)
vm.overcommit_ratio 过量使用比率(%),仅 overcommit_memory=2 时有效 50 50~90
vm.max_map_count 进程最大内存映射区域数 65530 262144
vm.nr_hugepages 大页数量 0 根据应用需要

vm.overcommit_memory 三种策略对比:

策略 说明 适用场景
0 启发式过量使用 内核估算可用内存,拒绝明显不合理的请求 通用场景(默认)
1 总是过量使用 内核假装总有足够内存,直到真正耗尽 某些科学计算应用
2 不过量使用 承诺地址空间不超过 swap + overcommit_ratio% 的物理内存 对稳定性要求极高的场景

vm.swappiness 工作原理:
#mermaid-svg-jikVbLMb60RjqsGU{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-jikVbLMb60RjqsGU .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-jikVbLMb60RjqsGU .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-jikVbLMb60RjqsGU .error-icon{fill:#552222;}#mermaid-svg-jikVbLMb60RjqsGU .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-jikVbLMb60RjqsGU .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-jikVbLMb60RjqsGU .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-jikVbLMb60RjqsGU .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-jikVbLMb60RjqsGU .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-jikVbLMb60RjqsGU .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-jikVbLMb60RjqsGU .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-jikVbLMb60RjqsGU .marker{fill:#333333;stroke:#333333;}#mermaid-svg-jikVbLMb60RjqsGU .marker.cross{stroke:#333333;}#mermaid-svg-jikVbLMb60RjqsGU svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-jikVbLMb60RjqsGU p{margin:0;}#mermaid-svg-jikVbLMb60RjqsGU .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-jikVbLMb60RjqsGU .cluster-label text{fill:#333;}#mermaid-svg-jikVbLMb60RjqsGU .cluster-label span{color:#333;}#mermaid-svg-jikVbLMb60RjqsGU .cluster-label span p{background-color:transparent;}#mermaid-svg-jikVbLMb60RjqsGU .label text,#mermaid-svg-jikVbLMb60RjqsGU span{fill:#333;color:#333;}#mermaid-svg-jikVbLMb60RjqsGU .node rect,#mermaid-svg-jikVbLMb60RjqsGU .node circle,#mermaid-svg-jikVbLMb60RjqsGU .node ellipse,#mermaid-svg-jikVbLMb60RjqsGU .node polygon,#mermaid-svg-jikVbLMb60RjqsGU .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-jikVbLMb60RjqsGU .rough-node .label text,#mermaid-svg-jikVbLMb60RjqsGU .node .label text,#mermaid-svg-jikVbLMb60RjqsGU .image-shape .label,#mermaid-svg-jikVbLMb60RjqsGU .icon-shape .label{text-anchor:middle;}#mermaid-svg-jikVbLMb60RjqsGU .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-jikVbLMb60RjqsGU .rough-node .label,#mermaid-svg-jikVbLMb60RjqsGU .node .label,#mermaid-svg-jikVbLMb60RjqsGU .image-shape .label,#mermaid-svg-jikVbLMb60RjqsGU .icon-shape .label{text-align:center;}#mermaid-svg-jikVbLMb60RjqsGU .node.clickable{cursor:pointer;}#mermaid-svg-jikVbLMb60RjqsGU .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-jikVbLMb60RjqsGU .arrowheadPath{fill:#333333;}#mermaid-svg-jikVbLMb60RjqsGU .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-jikVbLMb60RjqsGU .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-jikVbLMb60RjqsGU .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-jikVbLMb60RjqsGU .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-jikVbLMb60RjqsGU .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-jikVbLMb60RjqsGU .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-jikVbLMb60RjqsGU .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-jikVbLMb60RjqsGU .cluster text{fill:#333;}#mermaid-svg-jikVbLMb60RjqsGU .cluster span{color:#333;}#mermaid-svg-jikVbLMb60RjqsGU div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-jikVbLMb60RjqsGU .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-jikVbLMb60RjqsGU rect.text{fill:none;stroke-width:0;}#mermaid-svg-jikVbLMb60RjqsGU .icon-shape,#mermaid-svg-jikVbLMb60RjqsGU .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-jikVbLMb60RjqsGU .icon-shape p,#mermaid-svg-jikVbLMb60RjqsGU .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-jikVbLMb60RjqsGU .icon-shape .label rect,#mermaid-svg-jikVbLMb60RjqsGU .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-jikVbLMb60RjqsGU .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-jikVbLMb60RjqsGU .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-jikVbLMb60RjqsGU :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 0
60 默认
100
内存压力增大
swappiness 值
不主动交换

仅当空闲内存极低时交换
平衡策略

同时回收文件缓存和交换匿名页
积极交换

优先交换匿名页

14.3.2 透明大页配置

透明大页(Transparent HugePages, THP)将多个 4KB 页合并为 2MB 大页,减少 TLB miss,但可能导致内存碎片和延迟抖动。

参数路径 功能说明 可用选项
/sys/kernel/mm/transparent_hugepage/enabled 透明大页启用状态 alwaysmadvisenever
/sys/kernel/mm/transparent_hugepage/defrag 透明大页碎片整理 alwaysdeferdefer+madvisemadvisenever
/sys/kernel/mm/transparent_hugepage/khugepaged/defrag khugepaged 碎片整理 0/1

选项说明:

选项 说明
always 对所有进程启用透明大页
madvise 仅对调用 madvise(MADV_HUGEPAGE) 的区域启用
never 完全禁用透明大页

配置示例:

bash 复制代码
# 查看当前状态(方括号表示当前选项)
$ cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]

# 禁用透明大页(某些数据库如 Redis、MongoDB 建议)
$ echo never > /sys/kernel/mm/transparent_hugepage/enabled
$ echo never > /sys/kernel/mm/transparent_hugepage/defrag

# 永久禁用(通过 systemd 或 rc.local)
# /etc/rc.local 中添加:
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag

14.3.3 配置示例

bash 复制代码
# 减少交换倾向
$ echo 10 > /proc/sys/vm/swappiness

# 优化脏页写回
$ echo 5 > /proc/sys/vm/dirty_background_ratio
$ echo 10 > /proc/sys/vm/dirty_ratio
$ echo 500 > /proc/sys/vm/dirty_expire_centisecs
$ echo 100 > /proc/sys/vm/dirty_writeback_centisecs

# 增加内存映射数量(适合 Java、Elasticsearch 等应用)
$ echo 262144 > /proc/sys/vm/max_map_count

# 设置最小空闲内存(如 8GB 内存的机器,设置约 1-3%)
$ echo 262144 > /proc/sys/vm/min_free_kbytes

# 永久设置(/etc/sysctl.conf)
vm.swappiness = 10
vm.vfs_cache_pressure = 50
vm.dirty_ratio = 10
vm.dirty_background_ratio = 5
vm.dirty_expire_centisecs = 500
vm.dirty_writeback_centisecs = 100
vm.max_map_count = 262144
vm.overcommit_memory = 0

14.4 I/O 调度与磁盘调优

I/O 子系统是系统性能的关键瓶颈之一。本节从 I/O 调度器选择、请求队列与预读配置、文件系统参数和 ext4 挂载优化四个方面介绍磁盘调优方法。

14.4.1 I/O 调度器选择

I/O 调度器决定了磁盘请求的处理顺序。不同存储介质适合不同的调度器。

参数路径 功能说明 可用选项
/sys/block/<device>/queue/scheduler I/O 调度器选择 noopdeadlinecfqbfqkybermq-deadline
/sys/block/<device>/queue/nr_requests 请求队列深度 128 → 256~512
/sys/block/<device>/queue/read_ahead_kb 预读大小(KB) 128 → 256~1024
/sys/block/<device>/queue/nomerges 合并请求开关 0/1/2
/sys/block/<device>/queue/iosched/ 调度器具体参数 调度器相关

调度器对比与选择建议:

调度器 算法特点 适用场景 内核版本
noop 简单 FIFO,不做排序 SSD/NVMe(无寻道时间) 所有版本
mq-deadline 多队列 deadline,保证延迟 SSD/NVMe、数据库 4.11+
deadline 保证每个请求的最大等待时间 传统 HDD(服务器) 所有版本
cfq 完全公平排队,按进程公平分配 传统 HDD(桌面) 已废弃
bfq 预算公平排队,交互性好 传统 HDD(桌面) 4.12+
kyber 轻量级,自适应 高速 NVMe 4.12+

调度器选择决策图:
#mermaid-svg-gp2KCmrYyt6nmeVL{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-gp2KCmrYyt6nmeVL .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-gp2KCmrYyt6nmeVL .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-gp2KCmrYyt6nmeVL .error-icon{fill:#552222;}#mermaid-svg-gp2KCmrYyt6nmeVL .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-gp2KCmrYyt6nmeVL .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-gp2KCmrYyt6nmeVL .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-gp2KCmrYyt6nmeVL .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-gp2KCmrYyt6nmeVL .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-gp2KCmrYyt6nmeVL .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-gp2KCmrYyt6nmeVL .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-gp2KCmrYyt6nmeVL .marker{fill:#333333;stroke:#333333;}#mermaid-svg-gp2KCmrYyt6nmeVL .marker.cross{stroke:#333333;}#mermaid-svg-gp2KCmrYyt6nmeVL svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-gp2KCmrYyt6nmeVL p{margin:0;}#mermaid-svg-gp2KCmrYyt6nmeVL .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-gp2KCmrYyt6nmeVL .cluster-label text{fill:#333;}#mermaid-svg-gp2KCmrYyt6nmeVL .cluster-label span{color:#333;}#mermaid-svg-gp2KCmrYyt6nmeVL .cluster-label span p{background-color:transparent;}#mermaid-svg-gp2KCmrYyt6nmeVL .label text,#mermaid-svg-gp2KCmrYyt6nmeVL span{fill:#333;color:#333;}#mermaid-svg-gp2KCmrYyt6nmeVL .node rect,#mermaid-svg-gp2KCmrYyt6nmeVL .node circle,#mermaid-svg-gp2KCmrYyt6nmeVL .node ellipse,#mermaid-svg-gp2KCmrYyt6nmeVL .node polygon,#mermaid-svg-gp2KCmrYyt6nmeVL .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-gp2KCmrYyt6nmeVL .rough-node .label text,#mermaid-svg-gp2KCmrYyt6nmeVL .node .label text,#mermaid-svg-gp2KCmrYyt6nmeVL .image-shape .label,#mermaid-svg-gp2KCmrYyt6nmeVL .icon-shape .label{text-anchor:middle;}#mermaid-svg-gp2KCmrYyt6nmeVL .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-gp2KCmrYyt6nmeVL .rough-node .label,#mermaid-svg-gp2KCmrYyt6nmeVL .node .label,#mermaid-svg-gp2KCmrYyt6nmeVL .image-shape .label,#mermaid-svg-gp2KCmrYyt6nmeVL .icon-shape .label{text-align:center;}#mermaid-svg-gp2KCmrYyt6nmeVL .node.clickable{cursor:pointer;}#mermaid-svg-gp2KCmrYyt6nmeVL .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-gp2KCmrYyt6nmeVL .arrowheadPath{fill:#333333;}#mermaid-svg-gp2KCmrYyt6nmeVL .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-gp2KCmrYyt6nmeVL .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-gp2KCmrYyt6nmeVL .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-gp2KCmrYyt6nmeVL .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-gp2KCmrYyt6nmeVL .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-gp2KCmrYyt6nmeVL .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-gp2KCmrYyt6nmeVL .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-gp2KCmrYyt6nmeVL .cluster text{fill:#333;}#mermaid-svg-gp2KCmrYyt6nmeVL .cluster span{color:#333;}#mermaid-svg-gp2KCmrYyt6nmeVL div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-gp2KCmrYyt6nmeVL .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-gp2KCmrYyt6nmeVL rect.text{fill:none;stroke-width:0;}#mermaid-svg-gp2KCmrYyt6nmeVL .icon-shape,#mermaid-svg-gp2KCmrYyt6nmeVL .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-gp2KCmrYyt6nmeVL .icon-shape p,#mermaid-svg-gp2KCmrYyt6nmeVL .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-gp2KCmrYyt6nmeVL .icon-shape .label rect,#mermaid-svg-gp2KCmrYyt6nmeVL .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-gp2KCmrYyt6nmeVL .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-gp2KCmrYyt6nmeVL .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-gp2KCmrYyt6nmeVL :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} SSD/NVMe
传统 HDD
服务器/数据库
桌面/交互
存储介质类型?
推荐: noop 或 mq-deadline
使用场景?
推荐: deadline
推荐: bfq

14.4.2 请求队列与预读

bash 复制代码
# 查看当前 I/O 调度器(方括号为当前选项)
$ cat /sys/block/sda/queue/scheduler
noop [deadline] cfq

# 设置 I/O 调度器
$ echo deadline > /sys/block/sda/queue/scheduler

# 增加请求队列深度
$ echo 512 > /sys/block/sda/queue/nr_requests

# 调整预读大小(顺序读密集型工作负载)
$ echo 1024 > /sys/block/sda/queue/read_ahead_kb

# 禁用请求合并(某些基准测试场景)
$ echo 1 > /sys/block/sda/queue/nomerges

14.4.3 文件系统参数

参数路径 功能说明 默认值 建议值
fs.aio-max-nr 异步 I/O 最大数量 65536 1048576
fs.inotify.max_user_watches inotify 监视最大数量 8192 524288
fs.inotify.max_user_instances inotify 实例最大数量 128 1024
fs.inotify.max_queued_events inotify 事件队列大小 16384 65536
fs.file-max 文件描述符系统限制 见 14.1.1 2097152
fs.suid_dumpable SUID 程序 coredump 权限 0 0(安全)/ 1(调试)

fs.suid_dumpable 参数详解:

该参数控制设置了 SUID/SGID 位的程序在崩溃时是否生成 coredump 文件。SUID 程序以文件所有者(通常是 root)的权限运行,如果允许其生成 coredump,可能泄露敏感信息。

说明 安全性 适用场景
0 不生成 SUID 程序的 coredump 最安全 生产环境(默认)
1 生成 coredump,权限为文件所有者 中等 开发调试环境
2 生成 coredump,权限为 root 最低 仅限安全隔离的调试环境
bash 复制代码
# 查看当前设置
$ sysctl fs.suid_dumpable
fs.suid_dumpable = 0

# 调试 SUID 程序时临时启用(调试完毕后恢复)
$ sysctl -w fs.suid_dumpable=1

# 永久设置(/etc/sysctl.conf)
fs.suid_dumpable = 0

安全注意事项:

  • 生产环境必须 保持 fs.suid_dumpable = 0,防止通过 coredump 泄露特权进程的内存内容
  • 调试完成后应立即恢复为 0
  • 设置为 2 时,coredump 文件属主为 root,普通用户无法读取,但 root 可以分析
  • 该参数还影响 /proc/<pid>/maps 等接口的可读性(值为 0 时,其他用户无法读取 SUID 进程的 proc 信息)

14.4.4 ext4 挂载优化

挂载选项 功能说明 推荐设置
noatime 不更新文件访问时间 推荐(减少 I/O)
nodiratime 不更新目录访问时间 推荐
relatime 相对时间更新(平衡方案) 默认
data=ordered 数据先于元数据写入(安全) 默认
data=writeback 元数据先于数据写入(快但不安全) 性能优先场景
data=journal 数据和元数据都写入日志(最安全) 极端安全需求
barrier=1 写屏障(数据安全) 1(启用)
discard SSD TRIM 支持 SSD 推荐
stripe_width 条带宽度(RAID 优化) 根据 RAID 设置

noatime vs relatime 对比:

特性 noatime relatime
atime 更新 从不更新 仅当 atime < mtime/ctime 或超过 24h 时更新
I/O 减少 最大 较大
兼容性 某些程序依赖 atime 兼容绝大多数程序
推荐场景 高性能服务器 通用场景

14.4.5 配置示例

bash 复制代码
# 优化 ext4 挂载选项(临时)
$ mount -o remount,noatime,nodiratime,data=ordered,barrier=1 /dev/sda1 /mnt

# 永久设置(/etc/fstab)
/dev/sda1  /mnt  ext4  defaults,noatime,nodiratime,data=ordered,barrier=1  0  2

# SSD 启用 TRIM
/dev/nvme0n1p1  /  ext4  defaults,noatime,discard  0  1

# 文件系统参数永久设置(/etc/sysctl.conf)
fs.aio-max-nr = 1048576
fs.inotify.max_user_watches = 524288
fs.inotify.max_user_instances = 1024
fs.inotify.max_queued_events = 65536

14.5 CPU 与调度调优

CPU 调优涵盖频率管理、进程与 CPU 核心的绑定关系以及调度策略选择。合理的 CPU 调优能够显著降低延迟、提升吞吐量,对实时性要求高的应用尤为关键。

14.5.1 CPU 频率调节

配置项 文件路径 功能说明 可用选项
调节器 /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor CPU 频率调节策略 powersaveperformanceondemandconservativeuserspaceschedutil
最小频率 /sys/devices/system/cpu/cpu*/cpufreq/scaling_min_freq 最小 CPU 频率 硬件相关
最大频率 /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq 最大 CPU 频率 硬件相关

调节器对比:

调节器 行为 适用场景
performance 固定最高频率 服务器、性能测试
powersave 固定最低频率 笔记本、节能
ondemand 根据负载动态调整(响应快) 桌面、通用
conservative 根据负载动态调整(响应慢) 对频率变化敏感的场景
userspace 由用户空间程序控制 自定义控制
schedutil 由调度器驱动(推荐) 现代内核通用

配置示例:

bash 复制代码
# 查看当前调节器
$ cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

# 查看可用调节器
$ cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors

# 设置所有 CPU 为性能模式(临时)
$ echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

# 使用 cpupower 工具(推荐)
$ cpupower frequency-set -g performance

# 查看 CPU 频率信息
$ cpupower frequency-info

14.5.2 CPU 亲和性与隔离

命令/配置 功能说明 示例
taskset 设置进程 CPU 亲和性 taskset -c 0-3 ./program
numactl NUMA 控制与亲和性 numactl --cpunodebind=0 --membind=0 ./program
isolcpus 内核启动参数隔离 CPU isolcpus=2,3(隔离 CPU2,3)
irqbalance IRQ 中断平衡服务 自动平衡中断

配置示例:

bash 复制代码
# 设置进程 CPU 亲和性(启动时)
$ taskset -c 0-3 ./program

# 设置运行中进程的 CPU 亲和性
$ taskset -cp 0-3 <pid>

# NUMA 控制(绑定到 NUMA 节点 0)
$ numactl --cpunodebind=0 --membind=0 ./program

# NUMA 内存交织(跨节点分配)
$ numactl --interleave=all ./program

# 查看 NUMA 拓扑
$ numactl --hardware

# 内核启动参数隔离 CPU(GRUB 配置)
# /etc/default/grub 中:
GRUB_CMDLINE_LINUX="isolcpus=2,3"
# 更新 GRUB
$ update-grub

taskset 常用选项:

选项 说明
-c <cpulist> 使用 CPU 编号列表(如 0-3
-p <pid> 操作已运行的进程
-a 显示所有 CPU 的亲和性掩码

14.5.3 进程调度策略

调度策略 功能说明 适用场景 优先级范围
SCHED_OTHER 普通分时调度(CFS) 普通进程 nice -20~19
SCHED_BATCH 批处理调度 非交互批处理 nice -20~19
SCHED_IDLE 空闲调度 最低优先级后台任务 0
SCHED_FIFO 实时先进先出 硬实时任务 1~99
SCHED_RR 实时轮转 软实时任务 1~99
SCHED_DEADLINE 截止时间调度 严格时序要求 运行时/截止时间/周期

配置示例:

bash 复制代码
# 查看进程调度策略
$ chrt -p <PID>

# 设置实时 FIFO 调度,优先级 99
$ chrt -f -p 99 <PID>

# 设置实时 RR 调度,优先级 50
$ chrt -r -p 50 <PID>

# 启动时设置调度策略
$ chrt -f 99 ./realtime_program

# 查看系统支持的调度策略
$ chrt -m

易错点:

易错场景 说明
实时优先级 99 死循环 会导致系统无响应
权限要求 设置实时调度需要 CAP_SYS_NICE 权限(通常需要 root)
SCHED_DEADLINE 需要通过 sched_setattr() 系统调用设置,chrt 不支持

14.6 调优配置持久化与验证

调优参数分为临时设置(重启失效)和永久设置(写入配置文件)。本节介绍配置的持久化方法、验证手段、按应用类型的调优建议以及监控与调优的循环流程。

14.6.1 /etc/sysctl.conf 统一配置模板

sysctl 配置文件加载顺序:

sysctl --system 命令会按以下顺序加载所有配置文件,后加载的文件覆盖先加载的同名参数

复制代码
/usr/lib/sysctl.d/*.conf      ← 发行版/软件包提供的默认配置(优先级最低)
    ↓
/run/sysctl.d/*.conf           ← 运行时生成的配置(重启后丢失)
    ↓
/etc/sysctl.d/*.conf           ← 管理员自定义配置(按文件名字母序)
    ↓
/etc/sysctl.conf               ← 最终加载,优先级最高
目录/文件 用途 修改建议
/usr/lib/sysctl.d/ 发行版和软件包默认值 不要修改,升级时会被覆盖
/run/sysctl.d/ 运行时临时配置 重启后丢失,一般不手动创建
/etc/sysctl.d/ 管理员自定义配置(推荐) 按功能分文件,如 99-network.conf99-vm.conf
/etc/sysctl.conf 传统统一配置文件 优先级最高,适合少量全局配置

最佳实践: 推荐在 /etc/sysctl.d/ 下按功能分类创建配置文件,而非全部写入 /etc/sysctl.conf

bash 复制代码
/etc/sysctl.d/
├── 10-network.conf      # 网络调优
├── 20-memory.conf       # 内存调优
├── 30-filesystem.conf   # 文件系统调优
└── 99-tuning.conf       # 综合调优(最后加载,可覆盖前面的配置)

综合配置模板(/etc/sysctl.conf/etc/sysctl.d/99-tuning.conf):

ini 复制代码
# ==================== 网络调优 ====================
net.core.somaxconn = 1024
net.ipv4.tcp_max_syn_backlog = 2048
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_mem = 8388608 12582912 16777216
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1800
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_orphans = 262144
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.optmem_max = 65536
net.core.netdev_budget = 600

# ==================== 内存调优 ====================
vm.swappiness = 10
vm.vfs_cache_pressure = 50
vm.dirty_ratio = 10
vm.dirty_background_ratio = 5
vm.dirty_expire_centisecs = 500
vm.dirty_writeback_centisecs = 100
vm.max_map_count = 262144
vm.overcommit_memory = 0
vm.min_free_kbytes = 262144

# ==================== 文件系统调优 ====================
fs.file-max = 2097152
fs.aio-max-nr = 1048576
fs.inotify.max_user_watches = 524288
fs.inotify.max_user_instances = 1024
fs.inotify.max_queued_events = 65536

14.6.2 配置验证命令

bash 复制代码
# 应用 sysctl 配置
$ sysctl -p /etc/sysctl.conf
$ sysctl -p /etc/sysctl.d/99-tuning.conf

# 加载所有系统配置文件(按加载顺序)
$ sysctl --system

# 查看当前所有 sysctl 配置
$ sysctl -a

# 查看特定配置
$ sysctl net.core.somaxconn
$ sysctl vm.swappiness

# 查看进程资源限制
$ cat /proc/<pid>/limits

# 查看系统资源使用
$ cat /proc/meminfo
$ cat /proc/sys/fs/file-nr

# 查看网络连接状态
$ ss -tunap
$ netstat -s

# 查看中断分布
$ cat /proc/interrupts | sort -nr

# 查看 CPU 频率
$ cpupower frequency-info

# 使 ulimit 配置生效(需要重新登录)
# 修改 /etc/security/limits.conf 后重新登录

# 使 systemd 配置生效
$ systemctl daemon-reload
$ systemctl restart <service>

14.6.3 按应用类型调优建议

Web 服务器(Nginx/Apache):

bash 复制代码
# 增加文件描述符
# nginx.conf
worker_connections 10240;
worker_rlimit_nofile 20480;

# 网络参数优化
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65000

数据库(MySQL/PostgreSQL):

bash 复制代码
# 减少交换
vm.swappiness = 1

# 增加内存映射
vm.max_map_count = 262144

# I/O 调度器
echo deadline > /sys/block/sda/queue/scheduler

# 禁用透明大页(MySQL/PostgreSQL 建议)
echo never > /sys/kernel/mm/transparent_hugepage/enabled

# 增加文件描述符
fs.file-max = 2097152

Java 应用:

bash 复制代码
# 增加内存映射(JVM 需要大量 mmap)
vm.max_map_count = 262144

# 增加文件描述符
ulimit -n 65536

# 堆栈大小
ulimit -s unlimited

# JVM 参数配合
# -XX:+UseLargePages
# -XX:MaxHeapFreeRatio=70

高性能计算(HPC):

bash 复制代码
# CPU 性能模式
cpupower frequency-set -g performance

# 进程绑定
taskset -c 0-7 ./program

# NUMA 优化
numactl --interleave=all ./program

# 禁用透明大页碎片整理
echo never > /sys/kernel/mm/transparent_hugepage/defrag

调优建议汇总表:

应用类型 关键参数 建议值
Web 服务器 somaxconn、tcp_max_syn_backlog、nofile 4096、8192、65536
数据库 vm.swappiness、transparent_hugepage、I/O 调度器 1、never、deadline
Java 应用 vm.max_map_count、nofile、ulimit -s 262144、65536、unlimited
HPC CPU 调节器、taskset、numactl performance、绑核、交织

14.6.4 监控与调优循环流程

#mermaid-svg-Qemb0IEns9qNQVO0{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Qemb0IEns9qNQVO0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Qemb0IEns9qNQVO0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Qemb0IEns9qNQVO0 .error-icon{fill:#552222;}#mermaid-svg-Qemb0IEns9qNQVO0 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Qemb0IEns9qNQVO0 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Qemb0IEns9qNQVO0 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Qemb0IEns9qNQVO0 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Qemb0IEns9qNQVO0 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Qemb0IEns9qNQVO0 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Qemb0IEns9qNQVO0 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Qemb0IEns9qNQVO0 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Qemb0IEns9qNQVO0 .marker.cross{stroke:#333333;}#mermaid-svg-Qemb0IEns9qNQVO0 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Qemb0IEns9qNQVO0 p{margin:0;}#mermaid-svg-Qemb0IEns9qNQVO0 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Qemb0IEns9qNQVO0 .cluster-label text{fill:#333;}#mermaid-svg-Qemb0IEns9qNQVO0 .cluster-label span{color:#333;}#mermaid-svg-Qemb0IEns9qNQVO0 .cluster-label span p{background-color:transparent;}#mermaid-svg-Qemb0IEns9qNQVO0 .label text,#mermaid-svg-Qemb0IEns9qNQVO0 span{fill:#333;color:#333;}#mermaid-svg-Qemb0IEns9qNQVO0 .node rect,#mermaid-svg-Qemb0IEns9qNQVO0 .node circle,#mermaid-svg-Qemb0IEns9qNQVO0 .node ellipse,#mermaid-svg-Qemb0IEns9qNQVO0 .node polygon,#mermaid-svg-Qemb0IEns9qNQVO0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Qemb0IEns9qNQVO0 .rough-node .label text,#mermaid-svg-Qemb0IEns9qNQVO0 .node .label text,#mermaid-svg-Qemb0IEns9qNQVO0 .image-shape .label,#mermaid-svg-Qemb0IEns9qNQVO0 .icon-shape .label{text-anchor:middle;}#mermaid-svg-Qemb0IEns9qNQVO0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Qemb0IEns9qNQVO0 .rough-node .label,#mermaid-svg-Qemb0IEns9qNQVO0 .node .label,#mermaid-svg-Qemb0IEns9qNQVO0 .image-shape .label,#mermaid-svg-Qemb0IEns9qNQVO0 .icon-shape .label{text-align:center;}#mermaid-svg-Qemb0IEns9qNQVO0 .node.clickable{cursor:pointer;}#mermaid-svg-Qemb0IEns9qNQVO0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Qemb0IEns9qNQVO0 .arrowheadPath{fill:#333333;}#mermaid-svg-Qemb0IEns9qNQVO0 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Qemb0IEns9qNQVO0 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Qemb0IEns9qNQVO0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Qemb0IEns9qNQVO0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Qemb0IEns9qNQVO0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Qemb0IEns9qNQVO0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Qemb0IEns9qNQVO0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Qemb0IEns9qNQVO0 .cluster text{fill:#333;}#mermaid-svg-Qemb0IEns9qNQVO0 .cluster span{color:#333;}#mermaid-svg-Qemb0IEns9qNQVO0 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Qemb0IEns9qNQVO0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Qemb0IEns9qNQVO0 rect.text{fill:none;stroke-width:0;}#mermaid-svg-Qemb0IEns9qNQVO0 .icon-shape,#mermaid-svg-Qemb0IEns9qNQVO0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Qemb0IEns9qNQVO0 .icon-shape p,#mermaid-svg-Qemb0IEns9qNQVO0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Qemb0IEns9qNQVO0 .icon-shape .label rect,#mermaid-svg-Qemb0IEns9qNQVO0 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Qemb0IEns9qNQVO0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Qemb0IEns9qNQVO0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Qemb0IEns9qNQVO0 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 否

  1. 基准测试

建立性能基线
2. 监控分析

使用工具监控系统
3. 瓶颈识别

确定性能瓶颈
4. 参数调整

修改相关配置
5. 验证测试

验证调整效果
是否满足目标?
6. 持续监控

建立长期监控

各阶段推荐工具:

阶段 推荐工具
基准测试 sysbench、fio、iperf3、perf bench
监控分析 top、vmstat、iostat、sar、dstat、nmon
瓶颈识别 perf、flamegraph、strace、iotop、iftop
参数调整 sysctl、/etc/sysctl.conf、limits.conf、/etc/fstab
验证测试 重复基准测试,对比前后数据
持续监控 Prometheus、Grafana、sar 定时采集

14.7 本章小结

本章系统介绍了 Linux 系统配置与调优的完整知识体系,从资源限制到网络、内存、I/O、CPU 各子系统的参数调优,再到配置持久化和验证方法。以下从多个维度进行总结。

调优参数分类速查总表:

调优领域 核心配置文件/接口 关键参数 典型工具
资源限制 /etc/security/limits.confulimit、systemd .service nofile、nproc、stack、MemoryMax ulimit、systemctl show
网络调优 /proc/sys/net/、sysctl somaxconn、tcp_rmem、tcp_congestion_control、tcp_max_orphans sysctl、ss
内存调优 /proc/sys/vm/、sysctl swappiness、dirty_ratio、overcommit_memory sysctl、free
I/O 调优 /sys/block/*/queue//etc/fstab scheduler、nr_requests、noatime、fs.suid_dumpable iostat、mount
CPU 调优 /sys/devices/system/cpu/、chrt scaling_governor、isolcpus、调度策略 cpupower、taskset、chrt
持久化 /etc/sysctl.conf/etc/sysctl.d//etc/fstab、GRUB 所有上述参数 sysctl -p、systemctl

调优工具功能速查表:

工具/命令 功能 典型用法
ulimit 查看/设置当前 shell 资源限制 ulimit -n 65536
sysctl 查看/设置内核参数 sysctl -w vm.swappiness=10
cpupower CPU 频率管理 cpupower frequency-set -g performance
taskset 设置 CPU 亲和性 taskset -c 0-3 ./program
numactl NUMA 内存/CPU 绑定 numactl --cpunodebind=0 ./program
chrt 设置进程调度策略 chrt -f 99 ./program
mount -o remount 重新挂载文件系统(修改选项) mount -o remount,noatime /
systemctl show 查看服务资源限制 `systemctl show nginx
systemctl edit 创建服务 override 片段 systemctl edit nginx

调优注意事项总结:

注意事项 说明
临时 vs 永久 echo / sysctl -w 为临时设置,重启失效;需写入配置文件持久化
systemd 服务 不受 limits.conf 控制,需在 .service 文件中配置
调优需验证 每次调整后必须通过基准测试验证效果,避免盲目调优
生产环境谨慎 某些参数(如 tcp_tw_recycle)在特定环境下有副作用
一次只改一个参数 便于定位哪个参数产生了效果
sysctl 加载顺序 /usr/lib/sysctl.d//run/sysctl.d//etc/sysctl.d//etc/sysctl.conf
SUID coredump 生产环境保持 fs.suid_dumpable=0,防止信息泄露
孤儿套接字 每个孤儿套接字占约 64KB 不可交换内存,高并发场景需关注
相关推荐
大鹏的NLP博客2 小时前
通用 Linux 嵌入式板端 C/C++ ABI 与 Glibc 依赖治理规范
linux·c++·交叉编译
tedcloud1233 小时前
book-to-skill 怎么部署?把技术书和项目文档变成 AI Agent 可复用知识
linux·运维·服务器·人工智能·开源
瀚高PG实验室3 小时前
SQL优化案例:使用分区表优化SQL查询性能
linux·服务器·数据库·sql·microsoft·postgresql
青禾8373 小时前
Linux 系统信息、权限管理与 Python 并发编程(协程与线程)完全指南
linux·python
2301_800954994 小时前
Linux 常用系统信息与进程管理命令速查
linux·运维·服务器
T型码农要学习4 小时前
eMMC_命令整理
linux·驱动开发·emmc
刚入门的大一新生6 小时前
Linux-进程4
linux·运维·服务器
-今昭-6 小时前
MooseFS分布式文件系统
linux·服务器·网络
byte轻骑兵6 小时前
【BlueZ 】蓝牙 HCI 协议基础:与 BlueZ 源码的层面对应关系
linux·hci·电脑蓝牙·嵌入式蓝牙·buez