性能监控与优化
在服务器或开发环境中,性能监控与优化是保持系统稳定运行的关键。
本文介绍在 Rocky Linux 上使用常见工具监控 CPU、内存、磁盘与网络性能,并提供实用的优化建议。
一、CPU 使用情况监控
1. 查看实时 CPU 使用率
top
常用快捷键:
| 快捷键 | 作用 |
|---|---|
1 |
显示每个 CPU 核心使用情况 |
P |
按 CPU 使用率排序 |
q |
退出 top |
2. 使用 htop 提供更友好的界面
sudo dnf install -y htop
htop
htop 支持彩色显示、交互式终止进程和 CPU 核心可视化。
二、内存与交换空间监控
1. 查看内存使用情况
free -h
输出示例:
total used free shared buff/cache available
Mem: 7.6G 2.3G 3.8G 112M 1.5G 4.9G
Swap: 2.0G 0B 2.0G
2. 使用 vmstat 观察内存与进程状态
vmstat 2
每 2 秒刷新一次,可持续监控内存与 CPU。
三、磁盘性能与空间分析
1. 查看磁盘使用情况
df -h
2. 查看目录空间占用
du -sh /*
3. 分析磁盘 I/O 性能
安装 iotop:
sudo dnf install -y iotop
sudo iotop
显示每个进程的读写速度,有助于发现磁盘瓶颈。
4. 测试磁盘读写性能
sudo dnf install -y hdparm
sudo hdparm -Tt /dev/sda
四、网络性能与连接状态
1. 查看实时网络流量
sudo dnf install -y iftop
sudo iftop -i eth0
2. 检查端口监听与连接状态
sudo ss -tulnp
3. Ping 延迟与丢包检测
ping -c 5 8.8.8.8
4. 使用 nload 可视化网络流量
sudo dnf install -y nload
sudo nload
五、系统总体资源概览
使用 dstat 工具
sudo dnf install -y dstat
dstat -tcmnd
输出包括时间、CPU、内存、磁盘与网络的综合统计信息。
六、性能日志与分析工具
| 工具 | 作用 | 命令 |
|---|---|---|
sar |
系统活动报告,可生成 CPU/内存/IO 历史数据 | sar -u 1 5 |
pidstat |
按进程统计 CPU 使用率 | pidstat 1 |
perf |
内核性能分析工具 | perf top |
systemd-analyze |
分析系统启动时间 | systemd-analyze blame |
安装命令:
sudo dnf install -y sysstat perf
七、系统优化建议
1. 调整 Swappiness(内存换页倾向)
默认值为 60,可改为 10 以减少磁盘交换:
cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
永久修改:
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
2. 优化文件描述符限制
编辑 /etc/security/limits.conf:
* soft nofile 65535
* hard nofile 65535
3. 优化网络连接参数
编辑 /etc/sysctl.conf:
net.core.somaxconn = 1024
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
应用配置:
sudo sysctl -p
八、CPU 与 IO 调度优化
1. 查看当前调度策略
cat /sys/block/sda/queue/scheduler
2. 修改为性能优先模式(例如 mq-deadline)
echo mq-deadline | sudo tee /sys/block/sda/queue/scheduler
九、服务与守护进程优化
1. 禁用不必要的服务
列出所有开机启动项:
sudo systemctl list-unit-files --type=service
禁用不需要的服务:
sudo systemctl disable bluetooth.service
2. 使用 tuned 进行系统自动优化
sudo dnf install -y tuned
sudo systemctl enable --now tuned
sudo tuned-adm list
sudo tuned-adm profile throughput-performance
💡 tuned 会根据服务器类型(虚拟机、数据库、网络负载)自动优化 CPU、磁盘与内存参数。
十、总结
- 使用
htop、iotop、iftop、nload实时监控系统性能 - 合理调整内核参数与服务数量可显著提升性能
tuned提供一键自动优化方案- 对于关键业务系统,建议部署 Prometheus + Grafana 做长期性能可视化监控
✅ 提示:性能优化应遵循“监控 → 分析 → 调整 → 验证”的闭环原则,避免盲目调参造成系统不稳定。
相关内容
- 备份与恢复 本文介绍如何在 Rocky Linux 系统中执行数据与系统的备份与恢复,包括 rsync、tar、Timeshift、快照与自动化定时任务等多种方法。
- Rocky Linux 与 RHEL 的区别 详细分析 Rocky Linux 与 Red Hat Enterprise Linux (RHEL) 的区别,包括开发背景、兼容性、更新策略、社区支持和企业使用...
- 定时任务(cron) 本文详细介绍 Rocky Linux 中使用 cron 定时任务的配置方法,包括 crontab 基础语法、系统任务调度、日志管理与常见问题排查。
- 数据库服务(MySQL / PostgreSQL) 在 Rocky Linux 中安装和配置 MySQL 与 PostgreSQL 数据库服务,涵盖安装命令、防火墙配置、开机自启和基础安全设置。
- Python 与 Node.js 环境配置 在 Rocky Linux 中安装与管理 Python、pip、Node.js 与 npm 环境,涵盖多版本管理、全局模块安装、国内源配置等实用技巧。
- 磁盘与分区管理(lsblk、fdisk) 详细介绍 Rocky Linux 中使用 lsblk 和 fdisk 工具进行磁盘和分区管理的方法,包括查看磁盘信息、创建分区、删除分区等操作