`

Linux System and Performance Monitoring(I/O篇)

阅读更多

Linux System and Performance Monitoring(I/O篇) 
Date: 2009.07.21 
Author: Darren Hoch 
译: Tonnyom[AT]hotmail.com
6.0 I/O 监控介绍
磁盘I/O 子系统是Linux 系统中最慢的部分.这个主要是归于CPU到物理操作磁盘之间距离(译注:盘片旋转以及寻道).如果拿读取磁盘和内存的时间作比较就是分钟级到秒级,这就像7天和7分钟的区别.因此本质上,Linux 内核就是要最低程度的降低I/O 数.本章将诉述内核在磁盘和内存之间处理数据的这个过程中,哪些地方会产生I/O.
6.1 读和写数据 - 内存页
Linux 内核将硬盘I/O 进行分页,多数Linux 系统的默认页大小为4K.读和写磁盘块进出到内存都为4K 页大小.你可以使用time 这个命令加-v 参数,来检查你系统中设置的页大小:
# /usr/bin/time -v date 
<snip> 
Page size (bytes): 4096 
<snip>
6.2 Major and Minor Page Faults(译注:主要页错误和次要页错误)
Linux,类似多数的UNIX 系统,使用一个虚拟内存层来映射硬件地址空间.当一个进程被启动,内核先扫描CPU caches和物理内存.如果进程需要的数据在这2个地方都没找到,就需要从磁盘上读取,此时内核过程就是major page fault(MPF).MPF 要求磁盘子系统检索页并缓存进RAM.
一旦内存页被映射进内存的buffer cache(buff)中,内核将尝试从内存中读取或写入,此时内核过程就是minor page fault(MnPF).与在磁盘上操作相比,MnPF 通过反复使用内存中的内存页就大大的缩短了内核时间.
以下的例子,使用time 命令验证了,当进程启动后,MPF 和 MnPF 的变化情况.第一次运行进程,MPF 会更多:
# /usr/bin/time -v evolution 
<snip> 
Major (requiring I/O) page faults: 163 
Minor (reclaiming a frame) page faults: 5918 
<snip>
第二次再运行时,内核已经不需要进行MPF了,因为进程所需的数据已经在内存中:
# /usr/bin/time -v evolution 
<snip> 
Major (requiring I/O) page faults: 0 
Minor (reclaiming a frame) page faults: 5581 
<snip>
6.3 The File Buffer Cache(译注:文件缓存区)
文件缓存区就是指,内核将MPF 过程最小化,MnPF 过程最大化.随着系统不断的产生I/O,buffer cache也将不断的增加.直到内存不够,以及系统需要释放老的内存页去给其他用户进程使用时,系统就会丢弃这些内存页.结果是,很多sa(译注:系统管理员)对系统中过少的free memory(译注:空闲内存)表示担心,实际上这是系统更高效的在使用caches.
以下例子,是查看/proc/meminfo 文件:
# cat /proc/meminfo 
MemTotal: 2075672 kB 
MemFree: 52528 kB 
Buffers: 24596 kB 
Cached: 1766844 kB 
<snip>
可以看出,这个系统总计有2GB (Memtotal)的可用内存.当前的空闲内存为52MB (MemFree),有24 MB内存被分配磁盘写操作(Buffers),还有1.7 GB页用于读磁盘(Cached).
内核这样是通过MnPF机制,而不代表所有的页都是来自磁盘.通过以上部分,我们不可能确认系统是否处于瓶颈中.
6.4 Type of Memory Pages
在Linux 内核中,memory pages有3种,分别是:
1,Read Pages - 这些页通过MPF 从磁盘中读入,而且是只读.这些页存在于Buffer Cache中以及包括不能够修改的静态文件,二进制文件,还有库文件.当内核需要它们时,将读取到内存中.如果内存不足,内核将释放它们回空闲列表中.程序再次请求时,则通过MPF 再次读回内存.
2,Dirty Pages - 这些页是内核在内存中已经被修改过的数据页.当这些页需要同步回磁盘上,由pdflush 负责写回磁盘.如果内存不足,kswapd (与pdflush 一起)将这些页写回到磁盘上并释放更多的内存.
3,Anonymous Pages - 这些页属于某个进程,但是没有任何磁盘文件和它们有关.他们不能和同步回磁盘.如果内存不足,kswapd 将他们写入swap 分区上并释放更多的内存("swapping" pages).
6.5 Writing Data Pages Back to Disk
应用程序有很多选择可以写脏页回磁盘上,可通过I/O 调度器使用 fsync() 或 sync() 这样的系统函数来实现立即写回.如果应用程序没有调用以上函数,pdflush 进程会定期与磁盘进行同步.
# ps -ef | grep pdflush 
root 186 6 0 18:04 ? 00:00:00 [pdflush]
7.0 监控 I/O
当觉得系统中出现了I/O 瓶颈时,可以使用标准的监控软件来查找原因.这些工具包括了top,vmstat,iostat,sar.它们的输出结果一小部分是很相似,不过每个也都提供了各自对于性能不同方面的解释.以下章节就将讨论哪些情况会导致I/O 瓶颈的出现.
7.1 Calculating IO's Per Second(译注:IOPS 的计算)
每个I/O 请求到磁盘都需要若干时间.主要是因为磁盘的盘边必须旋转,机头必须寻道.磁盘的旋转常常被称为"rotational delay"(RD),机头的移动称为"disk seek"(DS).一个I/O 请求所需的时间计算就是DS加上RD.磁盘的RD 基于设备自身RPM 单位值(译注:RPM 是Revolutions Perminute的缩写,是转/每分钟,代表了硬盘的转速).一个RD 就是一个盘片旋转的半圆.如何计算一个10K RPM设备的RD 值呢:
1, 10000 RPM / 60 seconds (10000/60 = 166 RPS) 
2, 转换为 166分之1 的值(1/166 = 0.006 seconds/Rotation) 
3, 单位转换为毫秒(6 MS/Rotation) 
4, 旋转半圆的时间(6/2 = 3MS) 也就是 RD 
5, 加上平均3 MS 的寻道时间 (3MS + 3MS = 6MS) 
6, 加上2MS 的延迟(6MS + 2MS = 8MS) 
7, 1000 MS / 8 MS (1000/8 = 125 IOPS)
每次应用程序产生一个I/O,在10K RPM磁盘上都要花费平均 8MS.在这个固定时间里,磁盘将尽可能且有效率在进行读写磁盘.IOPS 可以计算出大致的I/O 请求数,10K RPM 磁盘有能力提供120-150 次IOPS.评估IOPS 的效能,可用每秒读写I/O 字节数除以每秒读写IOPS 数得出.
7.2 Random vs Sequential I/O(译注:随机/顺序 I/O)
per I/O产生的KB 字节数是与系统本身workload相关的,有2种不同workload的类型,它们是sequential和random.
7.2.1 Sequential I/O(译注:顺序IO)
iostat 命令提供信息包括IOPS 和每个I/O 数据处理的总额.可使用iostat -x 查看.顺序的workload是同时读顺序请求大量的数据.这包括的应用,比如有商业数据库(database)在执行大量的查询和流媒体服务.在这个workload 中,KB per I/O 的比率应该是很高的.Sequential workload 是可以同时很快的移动大量数据.如果每个I/O 都节省了时间,那就意味了能带来更多的数据处理.
# iostat -x 1
avg-cpu: %user %nice %sys %idle 
0.00 0.00 57.1 4 42.86
Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util 
/dev/sda 0.00 12891.43 0.00 105.71 0.00 1 06080.00 0.00 53040.00 1003.46 1099.43 3442.43 26.49 280.00 
/dev/sda1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
/dev/sda2 0.00 12857.14 0.00 5.71 0.00 105782.86 0.00 52891.43 18512.00 559.14 780.00 490.00 280.00 
/dev/sda3 0.00 34.29 0.00 100.00 0.00 297.14 0.00 148.57 2.97 540.29 594.57 24.00 240.00
avg-cpu: %user %nice %sys %idle 
0.00 0.00 23.53 76.47
Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util 
/dev/sda 0.00 17320.59 0.00 102.94 0.00 142305.88 0.00 71152.94 1382.40 6975.29 952.29 28.57 294.12 
/dev/sda1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
/dev/sda2 0.00 16844.12 0.00 102.94 0.00 138352.94 0.00 69176.47 1344.00 6809.71 952.29 28.57 294.12 
/dev/sda3 0.00 476.47 0.00 0.00 0.00 952.94 0.00 1976.47 0.00 165.59 0.00 0.00 276.47
评估IOPS 的效能,可用每秒读写I/O 字节数除以每秒读写IOPS 数得出,比如 
rkB/s 除以 r/s 
wkB/s 除以 w/s
53040/105 = 505KB per I/O 
71152/102 = 697KB per I/O 
在上面例子可看出,每次循环下,/dev/sda 的per I/O 都在增加.
7.2.2 Random I/O(译注:随机IO)
Random的worklaod环境下,不依赖于数据大小的多少,更多依赖的是磁盘的IOPS 数.Web和Mail 服务就是典型的Random workload.I/O 请求内容都很小.Random workload是同时每秒会有更多的请求数产生.所以,磁盘的IOPS 数是关键.
# iostat -x 1
avg-cpu: %user %nice %sys %idle 
2.04 0.00 97.96 0.00
Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util 
/dev/sda 0.00 633.67 3.06 102.31 24.49 5281.63 12.24 2640.82 288.89 73.67 113.89 27.22 50.00 
/dev/sda1 0.00 5.10 0.00 2.04 0.00 57.14 0.00 28.57 28.00 1.12 55.00 55.00 11.22 
/dev/sda2 0.00 628.57 3.06 100.27 24.49 5224.49 12.24 2612.24 321.50 72.55 121.25 30.63 50.00 
/dev/sda3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
avg-cpu: %user %nice %sys %idle 
2.15 0.00 97.85 0.00
Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util 
/dev/sda 0.00 41.94 6.45 130.98 51.61 352.69 25.81 3176.34 19.79 2.90 286.32 7.37 15.05 
/dev/sda1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 
/dev/sda2 0.00 41.94 4.30 130.98 34.41 352.69 17.20 3176.34 21.18 2.90 320.00 8.24 15.05 
/dev/sda3 0.00 0.00 2.15 0.00 17.20 0.00 8.60 0.00 8.00 0.00 0.00 0.00 0.00
计算方式和之前的公式一致:
2640/102 = 23KB per I/O 
3176/130 = 24KB per I/O
(译注:对于顺序I/O来说,主要是考虑读取大量数据的能力即KB per request.对于随机I/O系统,更需要考虑的是IOPS值)
7.3 When Virtual Memory Kills I/O
如果系统没有足够的RAM 响应所有的请求,就会使用到SWAP device.就像使用文件系统I/O,使用SWAP device 代价也很大.如果系统已经没有物理内存可用,那就都在SWAP disk上创建很多很多的内存分页,如果同一文件系统的数据都在尝试访问SWAP device,那系统将遇到I/O 瓶颈.最终导致系统性能的全面崩溃.如果内存页不能够及时读或写磁盘,它们就一直保留在RAM中.如果保留时间太久,内核又必须释放内存空间.问题来了,I/O 操作都被阻塞住了,什么都没做就被结束了,不可避免地就出现kernel panic和system crash.
下面的vmstat 示范了一个内存不足情况下的系统:
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu---- 
r b swpd free buff cache si so bi bo in cs us sy id wa 
17 0 1250 3248 45820 1488472 30 132 992 0 2437 7657 23 50 0 23 
11 0 1376 3256 45820 1488888 57 245 416 0 2391 7173 10 90 0 0 
12 0 1582 1688 45828 1490228 63 131 1348 76 2432 7315 10 90 0 10 
12 2 3981 1848 45468 1489824 185 56 2300 68 2478 9149 15 12 0 73 
14 2 10385 2400 44484 1489732 0 87 1112 20 2515 11620 0 12 0 88 
14 2 12671 2280 43644 1488816 76 51 1812 204 2546 11407 20 45 0 35
这个结果可看出,大量的读请求回内存(bi),导致了空闲内存在不断的减少(free).这就使得系统写入swap device的块数目(so)和swap 空间(swpd)在不断增加.同时看到CPU WIO time(wa)百分比很大.这表明I/O 请求已经导致CPU 开始效率低下.
要看swaping 对磁盘的影响,可使用iostat 检查swap 分区
# iostat -x 1
avg-cpu: %user %nice %sys %idle 
0.00 0.00 100.00 0.00
Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util 
/dev/sda 0.00 1766.67 4866.67 1700.00 38933.33 31200.00 19466.67 15600.00 10.68 6526.67 100.56 5.08 3333.33 
/dev/sda1 0.00 933.33 0.00 0.00 0.00 7733.33 0.00 3866.67 0.00 20.00 2145.07 7.37 200.00 
/dev/sda2 0.00 0.00 4833.33 0.00 38666.67 533.33 19333.33 266.67 8.11 373.33 8.07 6.90 87.00 
/dev/sda3 0.00 833.33 33.33 1700.00 266.67 22933.33 133.33 11466.67 13.38 6133.33 358.46 11.35 1966.67
在这个例子中,swap device(/dev/sda1) 和 file system device(/dev/sda3)在互相作用于I/O. 其中任一个会有很高写请求(w/s),也会有很高wait time(await),或者较低的服务时间比率(svctm).这表明2个分区之间互有联系,互有影响.
7.4 结论
I/O 性能监控包含了以下几点:
1,当CPU 有等待I/O 情况时,那说明磁盘处于超负荷状态. 
2,计算你的磁盘能够承受多大的IOPS 数. 
3,确定你的应用是属于随机或者顺序读取磁盘. 
4,监控磁盘慢需要比较wait time(await) 和 service time(svctm). 
5,监控swap 和系统分区,要确保virtual memory不是文件系统I/O 的瓶颈.
分享到:
评论

相关推荐

    Linux System and Performance Monitoring

    Linux System and Performance Monitoring

    Linux System and Performance Monitoring 英文教程

    一个简短的44页的英文教程,简单实用,针对性强

    Linux System and Performance Monitoring

    分CPU篇,memory篇,i/o篇,network篇 讲述如何对系统性能进行监测。 讲得很透彻 , 而且还很全面。 理论结合实际 , 其中案例分析都很好。不花哨 , 采用的工具及命令都是最基本的 , 有助于实际操作 。

    Prentice.Hall.Performance.Tuning.for.Linux.Servers

    and benchmarking Interpret performance data to analyze your Linux server's real-world behavior Optimize Linux system schedulers, memory, I/O, file systems, and networking Tune web, file, database, ...

    UNIX and Linux System Administration Handbook 5th Ed

    UNIX® and Linux® System Administration Handbook, Fifth Edition, is today’s definitive guide to installing, configuring, and maintaining any UNIX or Linux system, including systems that supply core ...

    UNIX And Linux System Administration Handbook, 5th Edition

    UNIX® and Linux® System Administration Handbook, Fifth Edition, is today’s definitive guide to installing, configuring, and maintaining any UNIX or Linux system, including systems that supply core ...

    Extreme Linux Performance Monitoring Part II

    Disk IO subsystems are the slowest part of any Linux system. This is due mainly to their distance from the CPU and the fact that disks require the physics to work (rotation and seek). If the time ...

    Monitoring_and_managing_system_status_and_performance-en-US.pdf

    Redhat 监控管理系统状态及性能 Red_Hat_Enterprise_Linux-8-Monitoring_and_managing_system_status_and_performance-en-US.pdf

    .Linux.Hacker

    Title: Linux Hacker Author: Mr Ajay Kumar Tiwari Length: 136 pages Edition: 1 Language: English Publisher: CreateSpace Independent Publishing Platform ...Chapter 12: System Monitoring and Performance

    Linux: Powerful Server Administration

    This Learning Path is intended for system administrators with a basic understanding of Linux operating systems and written with the novice-to-intermediate Linux user in mind. To get the most of this ...

    Linux Shell Scripting Cookbook - Third Edition

    From there, you'll learn text processing, web interactions, network and system monitoring, and system tuning. Software engineers will learn how to examine system applications, how to use modern ...

    java7帮助文档

    The java.nio.file package and its related package, java.nio.file.attribute, provide comprehensive support for file I/O and for accessing the file system; see File I/O (featuring NIO.2). NIO stands for...

    CentOS.7.Linux.Server.Cookbook.2nd.Ed.pdf

    Install and configure CentOS 7 Linux server system from scratch using normal and advanced methods Maintain a performance-based and secure server solution by deploying expert configuration advice and ...

    SystemTap_Beginners_Guide

    For system administrators, SystemTap can be used as a performance monitoring tool for Fedora. It is most useful when other similar tools cannot precisely pinpoint a bottleneck in the system, requiring...

    Python Network Programming Cookbook, 2nd Edition - 2017

    Chapter 2, Multiplexing Socket I/O for Better Performance, discusses various useful techniques for scaling your client/server applications with default and third-party libraries. OneKing.Soul Preface ...

    Sles performance tuning

    2 System Monitoring Utilities 9 2.1 Multi-Purpose Tools. . . . . . . . . . . . . . . . . . . . . . . . 9 2.2 System Information. . . . . . . . . . . . . . . . . . . . . . . . 17 2.3 Processes. . . . ....

    Apache Accumulo for Developers

    Shows you how to build Accumulo, Hadoop, and ZooKeeper clusters from scratch on both Windows and Linux Allows you to get hands-on knowledge about how to run Accumulo on Amazon EC2, Google Cloud ...

    Oracle WebLogic Server 10gR3: Troubleshooting Methodologies

    JVM Management: Java SE 6.0 Monitoring and Management Architecture Identifying Processes and Threads Obtaining a Thread Dump Using WLS Memory: Define Java Heap Garbage Collection Review Configuring ...

    8-07-14_MegaCLI for linux_windows

    SCGCQ00355536 (DFCT) - MegaCli and StorCli can not manage both controllers of different types n the same system under FreeBSD SCGCQ00362808 (DFCT) - MegaCli 32 Crashes in Windows in specific system ...

Global site tag (gtag.js) - Google Analytics