顺序写磁盘比随机写内存_深入理解 linux磁盘顺序写、随机写
一、前言
隨機寫會導致磁頭不停地換道,造成效率的極大降低;順序寫磁頭幾乎不用換道,或者換道的時間很短。本文來討論一下兩者具體的差別以及相應的內核調用。
二、環境準備
三、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.dbrandwrite:?(g=0):?rw=randwrite,?bs=4K-4K/4K-4K/4K-4K,?ioengine=sync,?iodepth=1fio-2.2.10Starting?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=19963msecDisk?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)?=?66519859226882?10:38:41.919920?write(3,?"220240@6371341277>020036310000202273203436H26P34027737033030e30"...,?4096)?=?409626882?10:38:41.919969?lseek(3,?4313088,?SEEK_SET)?=?431308826882?10:38:41.919985?write(3,?"220240@6371341277>020036310000202273203436H26P34027737033030e30"...,?4096)?=?409626882?10:38:41.920032?lseek(3,?455880704,?SEEK_SET)?=?45588070426882?10:38:41.920048?write(3,?"220240@6371341277>020036310000202273203436H26P34027737033030e30"...,?4096)?=?409626882?10:38:41.920096?lseek(3,?338862080,?SEEK_SET)?=?33886208026882?10:38:41.920112?write(3,?"220240@6371341277>02402240000202273203436H26P34027737033030e30"...,?4096)?=?409626882?10:38:41.920161?lseek(3,?739086336,?SEEK_SET)?=?73908633626882?10:38:41.920177?write(3,?"220240@6371341277>02402240000202273203436H26P34027737033030e30"...,?4096)?=?409626882?10:38:41.920229?lseek(3,?848175104,?SEEK_SET)?=?84817510426882?10:38:41.920245?write(3,?"220240@6371341277>02402240000202273203436H26P34027737033030e30"...,?4096)?=?409626882?10:38:41.920296?lseek(3,?1060147200,?SEEK_SET)?=?106014720026882?10:38:41.920312?write(3,?"220240@6371341277>02402240000202273203436H26P34027737033030e30"...,?4096)?=?409626882?10:38:41.920362?lseek(3,?863690752,?SEEK_SET)?=?86369075226882?10:38:41.920377?write(3,?"220240@6371341277>02402240000202273203436H26P34027737033030e30"...,?4096)?=?409626882?10:38:41.920428?lseek(3,?279457792,?SEEK_SET)?=?27945779226882?10:38:41.920444?write(3,?"220240@6371341277>02402240000202273203436H26P34027737033030e30"...,?4096)?=?409626882?10:38:41.920492?lseek(3,?271794176,?SEEK_SET)?=?27179417626882?10:38:41.920508?write(3,?"220240@6371341277>02402240000202273203436H26P34027737033030e30"...,?4096)?=?409626882?10:38:41.920558?lseek(3,?1067864064,?SEEK_SET)?=?106786406426882?10:38:41.920573?write(3,?"220240@6371341277>02402240000202273203436H26P34027737033030e30"...,?4096)?=?4096...
隨機讀每一次寫入之前都要通過lseek去定位當前的文件偏移量
2)同步順序寫
用剛才的方法來測試順序寫
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.dbwrite:?(g=0):?rw=write,?bs=4K-4K/4K-4K/4K-4K,?ioengine=sync,?iodepth=1fio-2.2.10Starting?1?processJobs:?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=14929msecDisk?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,?"0023000000300160000003600000003201700000"...,?4096)?=?409627046?10:54:28.194568?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.194627?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.194687?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.194747?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.194807?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.194868?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.194928?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.194988?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195049?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195110?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195197?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195262?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195330?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195426?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195497?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195567?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195637?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195704?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195757?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195807?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195859?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195910?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.195961?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.196012?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.196062?write(3,?"002300000030016000000220240000003201700000"...,?4096)?=?409627046?10:54:28.196112?write(3,?"002300000030016000000?260000003201700000"...,?4096)?=?409627046?10:54:28.196162?write(3,?"002300000030016000000?260000003201700000"...,?4096)?=?409627046?10:54:28.196213?write(3,?"002300000030016000000?260000003201700000"...,?4096)?=?409627046?10:54:28.196265?write(3,?"002300000030016000000?260000003201700000"...,?4096)?=?409627046?10:54:28.196314?write(3,?"002300000030016000000?260000003201700000"...,?4096)?=?409627046?10:54:28.196363?write(3,?"002300000030016000000?260000003201700000"...,?4096)?=?409627046?10:54:28.196414?write(3,?"002300000030016000000?260000003201700000"...,?4096)?=?409627046?10:54:28.196472?write(3,?"002300000030016000000?260000003201700000"...,?4096)?=?409627046?10:54:28.196524?write(3,?"002300000030016000000?260000003201700000"...,?4096)?=?409627046?10:54:28.196573?write(3,?"002300000030016000000?260000003201700000"...,?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.dbwrite:?(g=0):?rw=write,?bs=4K-4K/4K-4K/4K-4K,?ioengine=libaio,?iodepth=1fio-2.2.10Starting?1?processJobs:?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=8706msecDisk?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工具,拿到機器(物理機或者云機器)都應該第一時間對機器的磁盤做一個基線測試,做到心中有數
● 本文所有的測試,都是繞開了緩存,在實際應用中需要將緩存的影響考慮進去
老男孩IT,致力于最有用的IT干貨分享。期待與您一同進步!后臺私信1可獲取5000G珍藏大禮包(培訓視頻、精選軟件、內部資料)~讓我們一起讓IT學習更簡單!
總結
以上是生活随笔為你收集整理的顺序写磁盘比随机写内存_深入理解 linux磁盘顺序写、随机写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 交强险保险条款
- 下一篇: Service Mesh 在华为公有云的