Linux服务器随着业务的增加,会带来服务器的各种问题,比如CPU过高、load值过高、网卡流量过高、内存使用量过高等等。
Linux查看进程的方法有很多,但是想要找准哪个进程占的CPU、内存、以及I/O就要使用多种工具进行组合、最终找到问题所在。这篇文章讲述找出服务器I/O占用的原因及解决方法。
查看当前系统是否是由于I/O过高造成的原因,可以使用Linux最简单的也是最常用的工具top
12345 top - 09:24:57 up 360 days, 17:02, 2 users, load average: 5.69, 6.78, 7.04
Tasks: 200 total, 3 running, 197 sleeping, 0 stopped, 0 zombie
Cpu(s): 22.3%us, 5.9%sy, 0.0%ni, 51.3%id, 20.3%wa, 0.0%hi, 0.2%si, 0.0%st
Mem: 24592712k total, 24399804k used, 192908k free, 8852k buffers
Swap: 8388600k total, 4956692k used, 3431908k free, 17608380k cached
20.3%wa正是磁盘I/O占用CPU百分比的时间片。
查看哪个进程占用的I/O过高,使用iotop,ps,lsof
iotop
Total DISK READ: 57.94 M/s | Total DISK WRITE: 3.85 K/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
4036 be/4 search 1076.67 K/s 26.92 K/s 0.00 % 99.99 % bs_r -f bin/conf/bs.conf -d
4040 be/4 search 1245.86 K/s 0.00 B/s 0.00 % 99.36 % bs_r -f bin/conf/bs.conf -d
20678 be/4 search 745.98 K/s 0.00 B/s 0.00 % 96.99 % mcreate -daemon
4039 be/4 search 2045.67 K/s 0.00 B/s 0.00 % 94.30 % bs_r -f bin/conf/bs.conf -d
4050 be/4 search 2.09 M/s 0.00 B/s 0.00 % 94.27 % bs_r -f bin/conf/bs.conf -d
4045 be/4 search 2.01 M/s 0.00 B/s 0.00 % 94.13 % bs_r -f bin/conf/bs.conf -d
4049 be/4 search 2.25 M/s 0.00 B/s 0.00 % 93.79 % bs_r -f bin/conf/bs.conf -d
4048 be/4 search 1641.92 K/s 0.00 B/s 0.00 % 93.62 % bs_r -f bin/conf/bs.conf -d
4029 be/4 search 1080.51 K/s 0.00 B/s 0.00 % 92.49 % bs_r -f bin/conf/bs.conf -d
4044 be/4 search 1538.10 K/s 0.00 B/s 0.00 % 91.64 % bs_r -f bin/conf/bs.conf -d
4032 be/4 search 1211.25 K/s 0.00 B/s 0.00 % 91.21 % bs_r -f bin/conf/bs.conf -d
4025 be/4 search 1899.55 K/s 0.00 B/s 0.00 % 90.32 % bs_r -f bin/conf/bs.conf -d
可以看出由于“bs_r -f bin/conf/bs.conf -d”这个进程占用总体I/O过高导致。
默认没有安装iotop,可以进行yum install iotop -y进行安装。
ps -eo state,pid,cmd
[root@localhost ~]# for x in `seq 1 1 10`;do ps -eo state,pid,cmd | grep "^D" ;echo "-------";sleep 1;done
D 4327 rsync -av ./data/idx3 172.16.175.193::MINI_SEARCH
D 8462 /data1/minisearch/intraexpress/bin/server/SearchServer_field -P 21751 -p 21750 -F /data1/minisearch/intraexpress/bin/conf/fields.ini -L /data1/minisearch/intraexpress/log -d
D 9804 date +updateattr %F %T
D 9854 /bin/sh /data1/minisearch/intranews3/3/bin/shell/idxserver.sh
D 9913 /bin/sh /data1/minisearch/intranews3/6/bin/shell/idxserver.sh
D 10510 /bin/sh /data1/minisearch/intranews3/6/bin/shell/absserver.sh
D 10615 /bin/sh /data1/minisearch/intranews3/3/bin/shell/absserver.sh
-------
D 4327 rsync -av ./data/idx3 172.16.175.193::MINI_SEARCH
D 8462 /data1/minisearch/intraexpress/bin/server/SearchServer_field -P 21751 -p 21750 -F /data1/minisearch/intraexpress/bin/conf/fields.ini -L /data1/minisearch/intraexpress/log -d
D 9804 date +updateattr %F %T
D 9854 /bin/sh /data1/minisearch/intranews3/3/bin/shell/idxserver.sh
D 9913 /bin/sh /data1/minisearch/intranews3/6/bin/shell/idxserver.sh
D 10510 /bin/sh /data1/minisearch/intranews3/6/bin/shell/absserver.sh
D 10615 /bin/sh /data1/minisearch/intranews3/3/bin/shell/absserver.sh
D 31670 [jbd2/sda6-8]
-------
D 4327 rsync -av ./data/idx3 172.16.175.193::MINI_SEARCH
D 8462 /data1/minisearch/intraexpress/bin/server/SearchServer_field -P 21751 -p 21750 -F /data1/minisearch/intraexpress/bin/conf/fields.ini -L /data1/minisearch/intraexpress/log -d
D 9804 date +updateattr %F %T
D 9854 /bin/sh /data1/minisearch/intranews3/3/bin/shell/idxserver.sh
D 9913 /bin/sh /data1/minisearch/intranews3/6/bin/shell/idxserver.sh
D 10510 /bin/sh /data1/minisearch/intranews3/6/bin/shell/absserver.sh
D 10615 /bin/sh /data1/minisearch/intranews3/3/bin/shell/absserver.sh
D 11182 awk -F {print $1;}
D 31670 [jbd2/sda6-8]
通过man ps可以查到:
1234567 D Uninterruptible sleep (usually IO)
R Running or runnable (on run queue)
S Interruptible sleep (waiting for an event to complete)
T Stopped, either by a job control signal or because it is being traced.
W paging (not valid since the 2.6.xx kernel)
X dead (should never be seen)
Z Defunct ("zombie") process, terminated but not reaped by its parent.
D的状态一般是I/O出现了问题,说明进程在等待I/O,可以是磁盘I/O,网络I/O或者其他,可以看出
/bin/sh /data1/minisearch/intranews3/3/bin/shell/idxserver.sh
这个进程已经有I/O限制
lsof -p 进程号
为了查看此进程在读哪个文件,或者在写入哪个文件
[root@localhost ~]# lsof -p 27746
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
idxserver 27746 search cwd DIR 8,6 4096 79691789 /data1/minisearch/intranews3/6/bin
idxserver 27746 search rtd DIR 8,3 4096 2 /
idxserver 27746 search txt REG 8,3 939344 4213 /bin/bash
idxserver 27746 search mem REG 8,3 156928 7113 /lib64/ld-2.12.so
idxserver 27746 search mem REG 8,3 1926800 7114 /lib64/libc-2.12.so
idxserver 27746 search mem REG 8,3 22536 7119 /lib64/libdl-2.12.so
idxserver 27746 search mem REG 8,3 138280 1415 /lib64/libtinfo.so.5.7
idxserver 27746 search mem REG 8,1 99158576 524298 /usr/lib/locale/locale-archive
idxserver 27746 search mem REG 8,1 26060 655626 /usr/lib64/gconv/gconv-modules.cache
idxserver 27746 search 0r CHR 1,3 0t0 3968 /dev/null
idxserver 27746 search 1w REG 8,6 229869234162 79692293 /data1/minisearch/intranews3/6/log/idxserver.out
idxserver 27746 search 2w REG 8,6 229869234162 79692293 /data1/minisearch/intranews3/6/log/idxserver.out
查看哪一块磁盘占用的磁盘I/O比较高iostat
[root@localhost ~]# iostat -xk 1
Linux 2.6.32-431.11.2.el6.toa.2.x86_64 (localhost.localdomain) 09/01/2016 _x86_64_ (8 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
10.87 0.00 4.32 3.70 0.00 81.11
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util
sda 1.15 239.29 119.35 9.46 3057.00 995.47 62.92 0.04 0.29 0.62 7.99
avg-cpu: %user %nice %system %iowait %steal %idle
2.52 0.00 2.14 76.98 0.00 18.36
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util
sda 25.00 0.00 1670.00 0.00 49600.00 0.00 59.40 135.50 40.75 0.60 100.00
avg-cpu: %user %nice %system %iowait %steal %idle
5.42 0.00 0.88 73.30 0.00 20.40
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util
sda 26.00 0.00 1538.00 0.00 50996.00 0.00 66.31 133.85 42.16 0.65 100.10
iostat -xk 1每一秒打印磁盘相关情况。
几个比较重要的参数做解释:
r/s:每秒读次数
w/s:每秒写操作
rkB/s:每秒读kB大小
wkB/s:每秒写kB大小
wait:平均每次设备I/O操作的时间
%util:一秒中有百分之多少的时间用于 I/O 操作,即被io消耗的cpu百分比
如果util达到100%说明已经达到磁盘的I/O瓶颈,需要优化,或者换成SSD