通过IOStat命令监控IO性能-创新互联

小技巧:你知道iostat是从哪里得到IO相关信息的吗?使用strace命令能跟踪到答案:

成都创新互联长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为崇信企业提供专业的成都网站制作、成都做网站,崇信网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。
shell> strace -eopen iostat
open("/proc/diskstats", O_RDONLY)

注:Strace教程:5 simple ways to troubleshoot using Strace

注:关于diskstats的说明,参见官方文档(主要是其中的field1 ~ field11部分)。

如果你的操作系统里没有iostat命令的话,除了从源代码安装,还可以使用下面方式:

  • Centos/Fedora的安装方式是:yum install sysstat
  • Debian/Ubuntu的安装方式是:aptitude install sysstat

我最常用的iostat命令格式是:『iostat -dx 1』,意思是每隔一秒显示一次IO扩展信息。

shell> iostat -dx 1
Device:         rrqm/s  wrqm/s   r/s  w/s   rsec/s  wsec/s
sda               0.18    37.71  0.65  2.63    50.18   322.08
                avgrq-sz avgqu-sz   await  svctm  %util
                  113.46     0.35  107.49   1.67   0.55

Device:         rrqm/s  wrqm/s   r/s  w/s   rsec/s  wsec/s
sda               0.00  4208.00  0.00 165.00     0.00 163872.00
                avgrq-sz avgqu-sz   await  svctm  %util
                  993.16   119.54 1144.36   6.07 100.10

注:开头显示的是自系统启动开始的平均值,后面显示的是每段时间间隔里的平均值。

介绍一下相关参数的含义:

  • rrqm/s:队列中每秒钟合并的读请求数量
  • wrqm/s:队列中每秒钟合并的写请求数量
  • r/s:每秒钟完成的读请求数量
  • w/s:每秒钟完成的写请求数量
  • rsec/s:每秒钟读取的扇区数量
  • wsec/s:每秒钟写入的扇区数量
  • avgrq-sz:平均请求扇区的大小
  • avgqu-sz:平均请求队列的长度
  • await:平均每次请求的等待时间
  • svctm:平均每次请求的服务时间
  • util:设备的利用率

注:建议对照源代码来记忆这些参数都是如何计算出来的。

关于这些参数,相对重要的是后面几个,具体来说是:util,svctm,await,avgqu-sz:

util是设备的利用率。如果它接近100%,通常说明设备能力趋于饱和(并不绝对)。有时候会出现大于100%的情况,这是因为读取数据的时候是非原子操作。

svctm是平均每次请求的服务时间。从源代码里可以看出:(r/s+w/s)*(svctm/1000)=util。举例子:如果util达到 100%,那么此时svctm=1000/(r/s+w/s),假设IOPS是1000,那么svctm大概在1毫秒左右,如果长时间大于这个数值,说明 系统出了问题。

await是平均每次请求的等待时间。这个时间包括了队列时间和服务时间,也就是说,一般情况下,await大于svctm,它们的差值越小,则说明队列时间越短,反之差值越大,队列时间越长,说明系统出了问题。

avgqu-sz是平均请求队列的长度。毫无疑问,队列长度越短越好。

说明:svctm参数在未来某个版本的iostat会被删除,官方文档是这样描述原因的:

The average service time (svctm field) value is meaningless, as I/O statistics are calculated at block level, and we don’t know when the disk driver starts to process a request. For this reason, this field will be removed in a future sysstat version.

另外,有时候iostat会显示一些很离谱的结果,官方FAQ给出了如下的解释:

Because of a Linux kernel bug, iostat -x may display huge I/O response times (svctm) and a bandwidth utilization (%util) of 100% for some devices. Indeed these devices have a value for the field #9 (beginning after the device name) in /proc/{partitions,diskstats} which is always different from 0, and even negative sometimes. Yet this field should go to zero, since it gives the number of I/Os currently in progress (it is incremented as requests are submitted, and decremented as they finish). To (temporarily) solve the problem, you should reboot your system to reset the counters in /proc/{partitions,diskstats}.

如果大家想要更系统的了解关于IO的相关知识,可以参考如下资料:

  • Getting the hang of IOPS
  • Basic I/O Monitoring on Linux

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文名称:通过IOStat命令监控IO性能-创新互联
本文链接:http://pwwzsj.com/article/dcococ.html