欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

linux磁盘顺序写、随机写的方法

程序员文章站 2022-11-18 13:29:53
一、前言 ● 随机写会导致磁头不停地换道,造成效率的极大降低;顺序写磁头几乎不用换道,或者换道的时间很短 ● 本文来讨论一下两者具体的差别以及相应的内核调用 二、环境...

一、前言

● 随机写会导致磁头不停地换道,造成效率的极大降低;顺序写磁头几乎不用换道,或者换道的时间很短
● 本文来讨论一下两者具体的差别以及相应的内核调用

二、环境准备

组件 版本
os ubuntu 16.04.4 lts
fio 2.2.10

三、fio介绍

通过fio测试,能够反映在读写中的状态,我们需要重点关注fio的输出报告中的几个关键指标:
slat :是指从 i/o 提交到实际执行 i/o 的时长(submission latency)
clat :是指从 i/o 提交到 i/o 完成的时长(completion latency)
lat :指的是从 fio 创建 i/o 到 i/o 完成的总时长
bw :吞吐量
iops :每秒 i/o 的次数

四、同步写测试

(1)同步随机写

主要采用fio作为测试工具,为了能够看到系统调用,使用strace工具,命令看起来是这样:

先来测试一个随机写

strace -f -tt -o /tmp/randwrite.log -d fio -name=randwrite -rw=randwrite \
-direct=1 -bs=4k -size=1g -numjobs=1 -group_reporting -filename=/tmp/test.db

提取关键信息

root@wilson-ubuntu:~# strace -f -tt -o /tmp/randwrite.log -d fio -name=randwrite -rw=randwrite \
> -direct=1 -bs=4k -size=1g -numjobs=1 -group_reporting -filename=/tmp/test.db
randwrite: (g=0): rw=randwrite, bs=4k-4k/4k-4k/4k-4k, ioengine=sync, iodepth=1
fio-2.2.10
starting 1 process
...
randwrite: (groupid=0, jobs=1): err= 0: pid=26882: wed aug 14 10:39:02 2019
 write: io=1024.0mb, bw=52526kb/s, iops=13131, runt= 19963msec
 clat (usec): min=42, max=18620, avg=56.15, stdev=164.79
  lat (usec): min=42, max=18620, avg=56.39, stdev=164.79
...
 bw (kb /s): min=50648, max=55208, per=99.96%, avg=52506.03, stdev=1055.83
...

run status group 0 (all jobs):
 write: io=1024.0mb, aggrb=52525kb/s, minb=52525kb/s, maxb=52525kb/s, mint=19963msec, maxt=19963msec

disk stats (read/write):
...
 sda: ios=0/262177, merge=0/25, ticks=0/7500, in_queue=7476, util=36.05%

列出了我们需要重点关注的信息:

 (1)clat ,平均时长56ms左右

 (2)lat ,平均时长56ms左右

 (3)bw ,吞吐量,大概在52m左右

再来看内核调用信息:

root@wilson-ubuntu:~# more /tmp/randwrite.log
...
26882 10:38:41.919904 lseek(3, 665198592, seek_set) = 665198592
26882 10:38:41.919920 write(3, "\220\240@\6\371\341\277>\0\200\36\31\0\0\0\0\202\2\7\320\343\6h\26p\340\277\370\330\30e\30"..., 4096) = 4096
26882 10:38:41.919969 lseek(3, 4313088, seek_set) = 4313088
26882 10:38:41.919985 write(3, "\220\240@\6\371\341\277>\0\200\36\31\0\0\0\0\202\2\7\320\343\6h\26p\340\277\370\330\30e\30"..., 4096) = 4096
26882 10:38:41.920032 lseek(3, 455880704, seek_set) = 455880704
26882 10:38:41.920048 write(3, "\220\240@\6\371\341\277>\0\200\36\31\0\0\0\0\202\2\7\320\343\6h\26p\340\277\370\330\30e\30"..., 4096) = 4096
26882 10:38:41.920096 lseek(3, 338862080, seek_set) = 338862080
26882 10:38:41.920112 write(3, "\220\240@\6\371\341\277>\0\2402\24\0\0\0\0\202\2\7\320\343\6h\26p\340\277\370\330\30e\30"..., 4096) = 4096
26882 10:38:41.920161 lseek(3, 739086336, seek_set) = 739086336
26882 10:38:41.920177 write(3, "\220\240@\6\371\341\277>\0\2402\24\0\0\0\0\202\2\7\320\343\6h\26p\340\277\370\330\30e\30"..., 4096) = 4096
26882 10:38:41.920229 lseek(3, 848175104, seek_set) = 848175104
26882 10:38:41.920245 write(3, "\220\240@\6\371\341\277>\0\2402\24\0\0\0\0\202\2\7\320\343\6h\26p\340\277\370\330\30e\30"..., 4096) = 4096
26882 10:38:41.920296 lseek(3, 1060147200, seek_set) = 1060147200
26882 10:38:41.920312 write(3, "\220\240@\6\371\341\277>\0\2402\24\0\0\0\0\202\2\7\320\343\6h\26p\340\277\370\330\30e\30"..., 4096) = 4096
26882 10:38:41.920362 lseek(3, 863690752, seek_set) = 863690752
26882 10:38:41.920377 write(3, "\220\240@\6\371\341\277>\0\2402\24\0\0\0\0\202\2\7\320\343\6h\26p\340\277\370\330\30e\30"..., 4096) = 4096
26882 10:38:41.920428 lseek(3, 279457792, seek_set) = 279457792
26882 10:38:41.920444 write(3, "\220\240@\6\371\341\277>\0\2402\24\0\0\0\0\202\2\7\320\343\6h\26p\340\277\370\330\30e\30"..., 4096) = 4096
26882 10:38:41.920492 lseek(3, 271794176, seek_set) = 271794176
26882 10:38:41.920508 write(3, "\220\240@\6\371\341\277>\0\2402\24\0\0\0\0\202\2\7\320\343\6h\26p\340\277\370\330\30e\30"..., 4096) = 4096
26882 10:38:41.920558 lseek(3, 1067864064, seek_set) = 1067864064
26882 10:38:41.920573 write(3, "\220\240@\6\371\341\277>\0\2402\24\0\0\0\0\202\2\7\320\343\6h\26p\340\277\370\330\30e\30"..., 4096) = 4096
...

随机读每一次写入之前都要通过lseek去定位当前的文件偏移量

同步顺序写

用刚才的方法来测试顺序写

root@wilson-ubuntu:~# strace -f -tt -o /tmp/write.log -d fio -name=write -rw=write \
-direct=1 -bs=4k -size=1g -numjobs=1 -group_reporting -filename=/tmp/test.db
write: (g=0): rw=write, bs=4k-4k/4k-4k/4k-4k, ioengine=sync, iodepth=1
fio-2.2.10
starting 1 process
jobs: 1 (f=1): [w(1)] [100.0% done] [0kb/70432kb/0kb /s] [0/17.7k/0 iops] [eta 00m:00s]
write: (groupid=0, jobs=1): err= 0: pid=27005: wed aug 14 10:53:02 2019
 write: io=1024.0mb, bw=70238kb/s, iops=17559, runt= 14929msec
 clat (usec): min=43, max=7464, avg=55.95, stdev=56.24
  lat (usec): min=43, max=7465, avg=56.15, stdev=56.25
...
 bw (kb /s): min=67304, max=72008, per=99.98%, avg=70225.38, stdev=1266.88
...

run status group 0 (all jobs):
 write: io=1024.0mb, aggrb=70237kb/s, minb=70237kb/s, maxb=70237kb/s, mint=14929msec, maxt=14929msec

disk stats (read/write):
...
 sda: ios=0/262162, merge=0/10, ticks=0/6948, in_queue=6932, util=46.49%

可以看到:

 吞吐量提升至70m左右

再来看一下内核调用:

root@wilson-ubuntu:~# more /tmp/write.log
...
27046 10:54:28.194508 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\360\t\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.194568 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.194627 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.194687 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.194747 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.194807 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.194868 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.194928 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.194988 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195049 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195110 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195197 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195262 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195330 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195426 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195497 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195567 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195637 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195704 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195757 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195807 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195859 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195910 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.195961 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.196012 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.196062 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0\220\24\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.196112 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0 \26\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.196162 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0 \26\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.196213 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0 \26\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.196265 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0 \26\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.196314 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0 \26\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.196363 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0 \26\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.196414 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0 \26\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.196472 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0 \26\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.196524 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0 \26\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
27046 10:54:28.196573 write(3, "\0\0\23\0\0\0\0\0\0\300\16\0\0\0\0\0\0 \26\0\0\0\0\0\0\320\17\0\0\0\0\0"..., 4096) = 4096
...

由于顺序读,不需要反复定位文件偏移量,所以能够专注于写操作

五、slat指标

从上面的测试,我们在fio的测试报告中,并没有发现slat的身影,那是由于上述都是同步操作,对同步 i/o 来说,由于 i/o 提交和 i/o 完成是一个动作,所以 slat 实际上就是 i/o 完成的时间

异步顺序写,将同步顺序写的命令添加-ioengine=libaio:

root@wilson-ubuntu:~# fio -name=write -rw=write -ioengine=libaio -direct=1 -bs=4k -size=1g -numjobs=1 -group_reporting -filename=/tmp/test.db
write: (g=0): rw=write, bs=4k-4k/4k-4k/4k-4k, ioengine=libaio, iodepth=1
fio-2.2.10
starting 1 process
jobs: 1 (f=1): [w(1)] [100.0% done] [0kb/119.3mb/0kb /s] [0/30.6k/0 iops] [eta 00m:00s]
write: (groupid=0, jobs=1): err= 0: pid=27258: wed aug 14 11:14:36 2019
 write: io=1024.0mb, bw=120443kb/s, iops=30110, runt= 8706msec
 slat (usec): min=3, max=70, avg= 4.31, stdev= 1.56
 clat (usec): min=0, max=8967, avg=28.13, stdev=55.68
  lat (usec): min=22, max=8976, avg=32.53, stdev=55.72
...
 bw (kb /s): min=118480, max=122880, per=100.00%, avg=120467.29, stdev=1525.68
...

run status group 0 (all jobs):
 write: io=1024.0mb, aggrb=120442kb/s, minb=120442kb/s, maxb=120442kb/s, mint=8706msec, maxt=8706msec

disk stats (read/write):
...
 sda: ios=0/262147, merge=0/1, ticks=0/6576, in_queue=6568, util=74.32%

可以看到,slat指标出现,lat 近似等于 slat + clat 之和(avg平均值);并且换成异步io之后,吞吐量得到了极大的提升,120m左右

六、总结

● fio应该作为磁盘的baseline工具,拿到机器(物理机或者云机器)都应该第一时间对机器的磁盘做一个基线测试,做到心中有数
● 本文所有的测试,都是绕开了缓存,在实际应用中需要将缓存的影响考虑进去

以上所述是小编给大家介绍的linux磁盘顺序写、随机写,希望对大家有所帮助