深入理解 Linux磁盘顺序写、随机写

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

二、环境准备 组件版本
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左右

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11814.html