Linux性能检测与调优
影响linux服务器性能因素
1、cpu
2、mem
3、磁盘I/O
4、网络
一.cpu
cat /proc/cpuinfo lscpu
uptime---查看cpu在1分钟、5分钟、15分钟的平均负载情况
root@ws ~]# uptime
10:17:57 up 5 min, 3 users, load average: 0.27, 0.56, 0.31 //平均负载
[root@ws ~]#
vmstat
[root@ws ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 2707988 2120 600292 0 0 266 5 144 192 2 4 94 0 0 1.procs(进程):
r:等待进行的进程数 b:非中断休眠状态的进程数
2.memory(内存)
swpd:虚拟内存使用情况
free:空闲都内存
buff:缓冲
cache:缓存
3.swap(交换分区)
si:从磁盘交换到内存的页数量
so:从内存交换到磁盘的页数量
4.io(输入/输出)
bi:写入到块设备的块数
bo:从块设备中读取的块数
5.system(系统)
in:每秒的中断数
cs:每秒环境切换的次数
6.cpu
us:用户进程占用cpu时间的百分比
sy:内核进程占用cpu时间的百分比
id:cpu处于空闲状态的时间百分比
wa:IO等待占用cpu时间的百分比
st:等待IO时所消耗的cpu时间
二、内存
top
free -m -m有多少兆 -g有多少g
[root@ws ~]# free -g
total used free shared buff/cache available
Mem: 3 0 2 0 0 2
Swap: 3 0 3
三、磁盘I/O
iostat命令需要安装sysstat
[root@ws ~]# iostat
Linux 3.10.0-957.el7.x86_64 (ws.example.com) 2025年07月31日 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.52 0.01 0.96 0.04 0.00 98.47
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 5.52 264.31 5.54 498837 10455
scd0 0.02 0.56 0.00 1050 0
iostat -d
[root@ws ~]# iostat -d
Linux 3.10.0-957.el7.x86_64 (ws.example.com) 2025年07月31日 _x86_64_ (4 CPU)
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 5.38 257.35 5.42 498845 10508
scd0 0.01 0.54 0.00 1050 0
tps:每秒发送到磁盘I/O的请求数
kB_read/s:每秒读取数据的块数
kB_wrtn/s:每秒写入数据的块数
kB_read:读取所有数据的块数
kB_wrtn:写入所有数据的块数
四、网络
ping
[root@ws ~]# ping www.baidu.com
PING www.a.shifen.com (183.2.172.177) 56(84) bytes of data.
64 bytes from 183.2.172.177 (183.2.172.177): icmp_seq=1 ttl=128 time=21.0 ms
64 bytes from 183.2.172.177 (183.2.172.177): icmp_seq=2 ttl=128 time=25.7 ms
64 bytes from 183.2.172.177 (183.2.172.177): icmp_seq=3 ttl=128 time=25.1 ms
64 bytes from 183.2.172.177 (183.2.172.177): icmp_seq=4 ttl=128 time=22.4 ms
ss -tnl
[root@ws ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:6000 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::6000 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
netstat -tulnp
[root@ws ~]# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 9691/X
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 9579/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 9270/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 9272/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 9488/master
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::6000 :::* LISTEN 9691/X
tcp6 0 0 :::22 :::* LISTEN 9270/sshd
tcp6 0 0 ::1:631 :::* LISTEN 9272/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 9488/master
udp 0 0 0.0.0.0:5353 0.0.0.0:* 8690/avahi-daemon:
udp 0 0 192.168.122.1:53 0.0.0.0:* 9579/dnsmasq
udp 0 0 0.0.0.0:67 0.0.0.0:* 9579/dnsmasq
udp 0 0 0.0.0.0:36957 0.0.0.0:* 8690/avahi-daemon:
udp 0 0 0.0.0.0:111 0.0.0.0:* 1/systemd
udp 0 0 0.0.0.0:746 0.0.0.0:* 8631/rpcbind
udp6 0 0 :::111 :::* 1/systemd
udp6 0 0 :::746 :::* 8631/rpcbind
调优系统性能
配置静态调优:tuned守护进程会在服务启动时或选择新的调优配置文件时应用系统设置
配置动态调优:对于动态调优而言,tuned守护进程会监视系统活动,并根据运行时行为的变化来调整设置
tuned-adm list
[root@ws ~]# tuned-adm list
Available profiles:
- balanced - General non-specialized tuned profile
- desktop - Optimize for the desktop use-case
- latency-performance - Optimize for deterministic performance at the cost of increased power consumption
- network-latency - Optimize for deterministic performance at the cost of increased power consumption, focused on low latency network performance
- network-throughput - Optimize for streaming network throughput, generally only necessary on older CPUs or 40G+ networks
- powersave - Optimize for low power consumption
- throughput-performance - Broadly applicable tuning that provides excellent performance across a variety of common server workloads
- virtual-guest - Optimize for running inside a virtual guest
- virtual-host - Optimize for running KVM guests
Current active profile: virtual-guest
balanced:非常适合需要在节能和性能之间进行折中的系统
desktop:从balanced配置文件衍生而来,加快交互式应用响应速度
throughput-performance:调优系统,以获得最大吞吐量
latency-performance:非常适合需要牺牲性能来获得低延迟的服务器系统
network-latency:从latency-performance配置文件中衍生而来,它可以启用额外的网络调优参数,以提供低网络延迟
network-throughput:从throughput-performance配置文件衍生而来,应用其他网络调优参数,以获得最大网络吞吐量。
powersave:调优系统,以最大程度实现节能
virtual-guest:当前系统在虚拟机上运行时,调优系统以获得最高性能
virtual-host:当系统充当虚拟机主机时,调优系统以获得最高性能。
[root@ws ~]# tuned-adm recommend
virtual-guest
[root@ws ~]# tuned-adm profile virtual-guest
[root@ws ~]# tuned-adm active
Current active profile: virtual-guest
[root@ws ~]#
tuned-adm active:确定当前活动的调优配置文件
tuned-adm list:列出所有可用的调优配置文件,
tuned-adm profile +调优配置文件 :将活动的配置文件切换为更符合系统当前调优要求的其他配置文件
tuned-adm recommend:可以为系统推荐调优配置文件,该机制用于在安装后确定系统的默认配置文件
tuned-adm off:关闭tuned调优
相对优先级
不同进程的重要性水平各不相同,进程调度程序可以配置为针对不同的进程采用不同的调度策略。
nice级别值在-20到19之间,默认情况下,进程将继承父进程的nice值,通常为0。nice值越高,优先级越低(也就是该进程容易将cpu使用量让给其他进程);nice越低,优先级越高(该进程更加不倾向于让出cpu使用量)
普通用户(非特权用户)的权限仅限于提高自己进程的nice级别,不能设置降低自己进程的nice级别,也不能修改其他用户进程的nice级别。但是root用户可以灵活的提高或降低进程的nice级别
使用ps命令列出所有进程的pid,comm和nice值,且按nice降序排序,升序为--sort=nice
降序:
ps axo pid,comm,nice --sort=-nice //-nice -是从大到小 木有-是从小到大
[root@ws ~]# ps axo pid,comm,nice --sort=-nice
PID COMMAND NI
47 khugepaged 19
8670 alsactl 19
10600 tracker-extract 19
10609 tracker-miner-a -
10621 tracker-miner-f 19
10645 tracker-miner-u -
46 ksmd 5
8628 rtkit-daemon 1
1 systemd 0
2 kthreadd 0
3 ksoftirqd/0 0
6 kworker/u256:0 0
sha1sum用来验证数据的完整性
sha1sum /dev/zero &
pgrep -l sha1sum
ps -o pid,comm,nice 进程ID
调整进程的优先级
renice -n 19 进程ID
[root@ws ~]# sha1sum /dev/zero &
[2] 62773
[root@ws ~]# pgrep -l sha1sum
62773 sha1sum
[root@ws ~]# ps -o pid,comm,nice 62773
PID COMMAND NI
62773 sha1sum 0
[root@ws ~]# renice 15 62773
62773 (进程 ID) 旧优先级为 0,新优先级为 15
[root@ws ~]# ps -o pid,comm,nice 62773
PID COMMAND NI
62773 sha1sum 15
linux内核优化
升级内核
rpm -ivh kernel-3.10...rpm
查看内核模块
/lib/modules
[root@ws ~]# cd /lib/modules
[root@ws modules]# ls
3.10.0-957.el7.x86_64
uname -r 查看内核版本
3.10.0-957.el7.x86_64
[root@ws modules]# uname -r
3.10.0-957.el7.x86_64
内核版本号由3部分组成
3.10.0-957
主版本号
次版本号:次版本号决定该内核是稳定版本还是开发版本 偶数稳定版本 奇数 开发版本
末版本号(修订版本号)
查询模块
lsmod |grep xfs
root@ws ~]# lsmod | grep xfs
xfs 997127 2 //这个模块调用了两次
libcrc32c 12644 3 xfs,nf_nat,nf_conntrack
[root@ws ~]#
modinfo xfs 查看模块的信息
[root@ws ~]# modinfo xfs
filename: /lib/modules/3.10.0-957.el7.x86_64/kernel/fs/xfs/xfs.ko.xz
license: GPL
description: SGI XFS with ACLs, security attributes, no debug enabled
author: Silicon Graphics, Inc.
alias: fs-xfs
retpoline: Y
rhelversion: 7.6
srcversion: 799C7EBA4C499822FD1E465
depends: libcrc32c
intree: Y
vermagic: 3.10.0-957.el7.x86_64 SMP mod_unload modversions
signer: CentOS Linux kernel signing key
sig_key: B7:0D:CF:0D:F2:D9:B7:F2:91:59:24:82:49:FD:6F:E8:7B:78:14:27
sig_hashalgo: sha256
加载模块
modprobe ext4
[root@ws ~]# modprobe ext4
[root@ws ~]# lsmod | grep ext4
ext4 579979 0
mbcache 14958 1 ext4
jbd2 107478 1 ext4
卸载模块
modprobe -r ext4
[root@ws ~]# modprobe -r ext4
[root@ws ~]# lsmod | grep ext4
开启IP转发
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p /etc/sysctl.conf
开启禁ping
vim /etc/sysctl.conf
net.ipv4.icmp_echo_ignore_all = 1
sysctl -p /etc/sysctl.conf