第二十四讲 基础篇:Linux 磁盘I/O是怎么工作的(2020.6.17)
磁盘
机械盘(HDD),数据存储在磁道上,需要磁道寻址。对连续操作有很好的性能,随机读写性能较差
固态磁盘(SSD) 由固态电子元器件组成。不需要磁道寻址,连续/随机都有很好的性能(写之前要擦除数据,对寿命有影响)
RAID架构存储
通用块层
VFS与块设备之间的一层抽象。
向上提供同一个的块设备接口,并提供统一框架来管理这些设备的驱动程序
将文件系统的应用程序发来的IO请求队列进行合并排序提高效率
IO调度算法
CFQ 完全公平调度器,为每个进程维护一个IO调度队列,并按照时间片来均匀分布每个进程的IO请求(默认调度算法)
NOOP 先入先出的方式调度,只做简单的请求合并(SSD)
NONE 什么操作都不做(虚拟机环境)
DeadLine 分别为读写请求创建不同的IO队列,并确保在deadine到达之前对请求进行处理(IO压力比较重的场景)
系统磁盘性能指标
使用率(%util)
饱和度(rareq-sz 和 wareq-sz+响应时间)
IOPS(r/s+w/s)
吞吐量(rkB/s+wkB/s)
响应时间(r_await+w_await)
一般的操作是先用fio等工具测试一下磁盘的性能,然后在用应用程序把IO打高,尽量去逼近压力测试的值
linux-perf iostat -d -x
Linux 5.3.0-53-generic (qx) 2020年06月17日 _x86_64_ (8 CPU)
Device r/s w/s rkB/s wkB/s rrqm/s wrqm/s %rrqm %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm %util
loop0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.29 0.00 0.00 10.20 0.00 0.51 0.00
loop1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.12 0.00 0.00 2.59 0.00 0.71 0.00
loop2 0.02 0.00 0.02 0.00 0.00 0.00 0.00 0.00 0.39 0.00 0.00 1.09 0.00 0.09 0.00
loop3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.37 0.00 0.00 2.63 0.00 0.61 0.00
loop4 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.15 0.00 0.00 2.38 0.00 0.32 0.00
loop5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.07 0.00 0.00 14.55 0.00 0.76 0.00
loop6 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.45 0.00 0.00 6.65 0.00 0.47 0.00
loop7 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.41 0.00 0.00 2.59 0.00 1.18 0.00
sda 0.00 0.00 0.01 0.00 0.00 0.00 0.17 43.33 0.20 1.24 0.00 11.45 4.71 2.90 0.00
sdb 0.15 10.01 4.89 83.14 0.09 3.54 36.71 26.15 0.61 0.35 0.00 31.97 8.31 0.20 0.21
loop8 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.06 0.00 0.00 1.43 0.00 0.17 0.00
loop9 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.42 0.00 0.00 13.38 0.00 0.59 0.00
loop10 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.37 0.00 0.00 2.42 0.00 0.84 0.00
loop11 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.18 0.00 0.00 5.54 0.00 0.62 0.00
loop12 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.32 0.00 0.00 2.70 0.00 0.73 0.00
loop13 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.06 0.00 0.00 1.53 0.00 0.18 0.00
loop14 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.32 0.00 0.00 10.64 0.00 0.57 0.00
loop15 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.11 0.00 0.00 2.42 0.00 1.05 0.00
loop16 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.11 0.00 0.00 2.39 0.00 0.28 0.00
loop17 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.39 0.00 0.00 2.83 0.00 0.98 0.00
loop18 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.41 0.00 0.00 5.54 0.00 0.42 0.00
loop19 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.59 0.00 0.00 2.83 0.00 1.17 0.00
loop20 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.00 0.00 1.00 0.00
主要字段的含义:
进程IO观测
依然是我们的老朋友 pidstat
$ pidstat -d 1
Linux 5.3.0-53-generic (qx) 2020年06月17日 _x86_64_ (8 CPU)
22时18分40秒 UID PID kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
22时18分41秒 UID PID kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
22时18分42秒 1000 30062 0.00 288.00 0.00 0 chrome
22时18分42秒 UID PID kB_rd/s kB_wr/s kB_ccwr/s iodelay Command
22时18分43秒 0 329 -1.00 -1.00 -1.00 1 jbd2/sdb2-8
22时18分43秒 1000 30018 0.00 304.00 0.00 0 chrome
kb_ccwr/s 这可以指标的含义是:
Number of kilobytes whose writing to disk has been cancelled by the task. This may occur when the task truncates some dirty pagecache. In this case, some IO which another task has been accounted for will not be happening.
还有一个就是iotop类似top, 可以根据各个指标进行排序