Filesystems

寫入 EXT4 比直接寫入磁碟快?

  • May 6, 2020

我試圖測試一些 SSD 的寫入速度,當直接寫入磁碟時,在某種程度上比格式化為 ext4 時寫入磁碟要慢。這是如何運作的?這是正確的還是我測量錯誤?

for i in {1..5}; do dd if=/dev/zero of=/dev/sda1 bs=1G count=1 oflag=dsync; done
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 7.18148 s, 150 MB/s
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 7.18312 s, 149 MB/s
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 7.1938 s, 149 MB/s
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 7.15976 s, 150 MB/s
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 7.2125 s, 149 MB/s

如果我現在將磁碟格式化為 ext4

mkfs.ext4 /dev/sda1
mount /dev/sda1 /tmp/test
mount -ls
/dev/sda1 on /tmp/test type ext4 (rw,relatime,data=ordered)

for i in {1..5}; do dd if=/dev/zero of=/tmp/test/test.txt bs=1G count=1 oflag=dsync; done
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.66437 s, 230 MB/s
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.60112 s, 233 MB/s
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.58899 s, 234 MB/s
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.61334 s, 233 MB/s
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.60241 s, 233 MB/s

謝謝

約翰內斯

編輯:當像frostschutz 建議的那樣啟動 /proc/sys/vm/block_dump 然後復製到 ext4 驅動器時,很明顯核心對數據的分割方式不同。

for i in {1..5}; do dd if=/dev/zero of=/tmp/test/test.txt bs=1G count=1 oflag=dsync; done
[  922.895200] dd(2571): READ block 74112 on unknown-block(8,0) (8 sectors)
[  922.903712] dd(2571): READ block 8448 on unknown-block(8,0) (8 sectors)
[  923.724470] dd(2571): dirtied inode 12 (test.txt) on sda
[  923.729762] dd(2571): dirtied inode 12 (test.txt) on sda
[  923.735005] dd(2571): dirtied inode 12 (test.txt) on sda
[  924.543323] kworker/u8:0(2560): READ block 8320 on unknown-block(8,0) (8 sectors)
[  924.553112] kworker/u8:0(2560): WRITE block 278528 on unknown-block(8,0) (2048 sectors)
[  924.561496] kworker/u8:0(2560): WRITE block 280576 on unknown-block(8,0) (2048 sectors)
[  924.570013] kworker/u8:0(2560): WRITE block 282624 on unknown-block(8,0) (2048 sectors)
[  924.578534] kworker/u8:0(2560): WRITE block 284672 on unknown-block(8,0) (2048 sectors)

for i in {1..5}; do dd if=/dev/zero of=/dev/sda bs=1G count=1 oflag=dsync; done
[ 1504.428021] kworker/u8:0(2560): WRITE block 0 on unknown-block(8,0) (8 sectors)
[ 1504.435320] kworker/u8:0(2560): WRITE block 8 on unknown-block(8,0) (8 sectors)
[ 1504.442589] kworker/u8:0(2560): WRITE block 16 on unknown-block(8,0) (8 sectors)
[ 1504.449955] kworker/u8:0(2560): WRITE block 24 on unknown-block(8,0) (8 sectors)
[ 1504.457342] kworker/u8:0(2560): WRITE block 32 on unknown-block(8,0) (8 sectors)
[ 1504.464720] kworker/u8:0(2560): WRITE block 40 on unknown-block(8,0) (8 sectors)

mkfsTRIM/丟棄整個設備,從而提供最佳基準條件。

同樣/proc/sys/vm/block_dump啟用(警告 - 大量輸出),我看到 8 個扇區的寫入(原始塊設備上的 dd)與 16384 個扇區的寫入(ext4 上的 dd),所以這可能是由於核心決定拆分事物的方式起來,因為你不能從字面上發送 1G 塊寫入?

dd 在 ext4 上:

dd(12080): dirtied inode 12 (test.txt) on loop0
dd(12080): dirtied inode 12 (test.txt) on loop0
dd(12080): dirtied inode 12 (test.txt) on loop0
kworker/u8:4(10318): READ block 2056 on loop0 (8 sectors)
kworker/u8:4(10318): WRITE block 278528 on loop0 (16384 sectors)
kworker/u8:4(10318): WRITE block 294912 on loop0 (16384 sectors)
kworker/u8:4(10318): WRITE block 311296 on loop0 (16384 sectors)
kworker/u8:4(10318): WRITE block 327680 on loop0 (16384 sectors)
kworker/u8:4(10318): WRITE block 344064 on loop0 (16384 sectors)
kworker/u8:4(10318): WRITE block 360448 on loop0 (16384 sectors)
...

直接dd:

dd(12116): WRITE block 0 on loop0 (8 sectors)
dd(12116): WRITE block 8 on loop0 (8 sectors)
dd(12116): WRITE block 16 on loop0 (8 sectors)
dd(12116): WRITE block 24 on loop0 (8 sectors)
dd(12116): WRITE block 32 on loop0 (8 sectors)
dd(12116): WRITE block 40 on loop0 (8 sectors)
dd(12116): WRITE block 48 on loop0 (8 sectors)
dd(12116): WRITE block 56 on loop0 (8 sectors)
dd(12116): WRITE block 64 on loop0 (8 sectors)
dd(12116): WRITE block 72 on loop0 (8 sectors)
dd(12116): WRITE block 80 on loop0 (8 sectors)
dd(12116): WRITE block 88 on loop0 (8 sectors)
dd(12116): WRITE block 96 on loop0 (8 sectors)
dd(12116): WRITE block 104 on loop0 (8 sectors)
dd(12116): WRITE block 112 on loop0 (8 sectors)
dd(12116): WRITE block 120 on loop0 (8 sectors)
dd(12116): WRITE block 128 on loop0 (8 sectors)
...

現在我只測試了一個循環設備,而不是真正的 SSD,所以……它可能不准確。

引用自:https://unix.stackexchange.com/questions/584684