为什么fallocate -c -l1GiB
一次导致 dd 速度的第二个循环从 1.0 GB 降低到 ~200 MB/s ?
xb@dnxb:~/Downloads/test$ fallocate -l 10.2GiB lala.mp4
xb@dnxb:~/Downloads/test$ ls -larthiF --context --color
total 11G
41680908 drwxr-xr-x 121 xiaobai xiaobai ? 100K Dis 3 01:36 ../
53086804 drwxrwxr-x 2 xiaobai xiaobai ? 4.0K Dis 3 01:36 ./
53086805 -rw-rw-r-- 1 xiaobai xiaobai ? 11G Dis 3 01:37 lala.mp4
xb@dnxb:~/Downloads/test$ f='lala.mp4'; n=0; while (( "$(stat --printf="%s" $f)" > 1073741824 )); do ((n++)); echo "[dd...$n]"; dd bs=1G skip=0 count=1 if=$f of="$f.$n"; fallocate -c -l1GiB $f; done; ((n++)); mv $f $f.$n;
[dd...1]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 1.05744 s, 1.0 GB/s
[dd...2]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 3.24091 s, 331 MB/s
[dd...3]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.56858 s, 235 MB/s
[dd...4]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.02249 s, 267 MB/s
[dd...5]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.34307 s, 247 MB/s
[dd...6]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.27371 s, 251 MB/s
[dd...7]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.80721 s, 223 MB/s
[dd...8]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.17114 s, 257 MB/s
[dd...9]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.60627 s, 233 MB/s
[dd...10]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.13853 s, 259 MB/s
xb@dnxb:~/Downloads/test$
xb@dnxb:~/Downloads/test$ ls -larthiF --context --color
total 11G
41680908 drwxr-xr-x 121 xiaobai xiaobai ? 100K Dis 3 01:36 ../
53086806 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:37 lala.mp4.1
53086807 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:37 lala.mp4.2
53087049 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:37 lala.mp4.3
53087910 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:37 lala.mp4.4
53087911 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:37 lala.mp4.5
53087913 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:38 lala.mp4.6
53087914 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:38 lala.mp4.7
53087962 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:38 lala.mp4.8
53087963 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:38 lala.mp4.9
53087964 -rw-rw-r-- 1 xiaobai xiaobai ? 1.0G Dis 3 01:39 lala.mp4.10
53086805 -rw-rw-r-- 1 xiaobai xiaobai ? 200M Dis 3 01:39 lala.mp4.11
53086804 drwxrwxr-x 2 xiaobai xiaobai ? 4.0K Dis 3 01:39 ./
xb@dnxb:~/Downloads/test$
[更新]
我注意到sync
在下一个之前添加dd
将使其再次保持一致(sleep 120
也可以,但不是sleep 20
),但总时间会变慢,因为sync
花费的时间比dd
没有sync
:
xb@dnxb:~/Downloads/test$ f='lala.mp4'; n=0; while (( "$(stat --printf="%s" $f)" > 1073741824 )); do ((n++)); echo "[dd...$n]"; dd bs=1G skip=0 count=1 if=$f of="$f.$n"; fallocate -c -l1GiB $f; sync; done; ((n++)); mv $f $f.$n;
[dd...1]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.904357 s, 1.2 GB/s
[dd...2]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.902471 s, 1.2 GB/s
[dd...3]
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.896056 s, 1.2 GB/s
...
答案1
数据不直接写入磁盘。内核保留了一个缓存。 RAM 比磁盘快得多。该缓存的第一个dd
好处是它未满。不过,后续dd
必须等待缓存被刷新。
sync
请求将所有脏页刷新到磁盘。否则,它们会在几十秒后被刷新。
普通磁盘无法达到 1.2 GB/s,即使是 SSD,但也能达到 300 MB/s 左右。