在http://cephnotes.ksperis.com/blog/2014/12/18/use-discard-with-krbd-client-since-kernel-3-dot-18/有详细的描述,试验了一下,对于小文件的删除确实有帮助,记录如下。
1. 如果不利用discard选项挂载rbd块设备,会有如下问题
$ rbd create rbd/myrbd --size=20480
$ mkfs.xfs /dev/rbd0
$ rbd diff rbd/myrbd | awk '{ SUM += $2 } END { print SUM/1024/1024 " MB" }'
14.4062 MB
$ mount /dev/rbd0 /mnt/myrbd
$ dd if=/dev/zero of=/mnt/myrbd/testfile bs=1M count=1024
$ rbd diff rbd/myrbd | awk '{ SUM += $2 } END { print SUM/1024/1024 " MB" }'
1038.41 MB
$ rm /mnt/myrbd/testfile
$ rbd diff rbd/myrbd | awk '{ SUM += $2 } END { print SUM/1024/1024 " MB" }'
1038.41 MB
你会发现,rbd pool并没有变小。之前好像做过类似的测试,需要重启一下ceph或系统才能反映出改变,记不清了。
2. 利用discard选项挂载rbd块设备
mount -o discard /dev/rbd0 /mnt/myrbd
重复上述操作,再执行以下操作:
$ fstrim /mnt/myrbd
$ rbd diff rbd/myrbd | awk '{ SUM += $2 } END { print SUM/1024/1024 " MB" }'
10.6406 MB
这样就恢复正常了。
两种不同挂载方式的性能测试如下:
1. 未使用discard选项
$ mount /dev/rbd0 /mnt/rbd0
$ mkdir testdir; cd testdir
$ dd if=/dev/zero of=mainfile bs=1M count=200
$ split -b 4048 -a 7 mainfile; sync # 4k file / ~51k files
$ cd ..
$ time rm -rf testdir; time sync
[root@mon0 nfs]# time rm -rf testdir/; time sync
real 0m21.866s
user 0m0.155s
sys 0m5.466s
real 0m0.897s
user 0m0.001s
sys 0m0.011s
2. 使用discard选项
只给出时间
[root@mon0 nfs]# time rm -rf testdir; time sync
real 0m8.791s
user 0m0.100s
sys 0m2.858s
real 0m2.367s
user 0m0.001s
sys 0m0.001s
性能有明显提升,作者的原话是:
In the case of intensive use of the file system, with many small file, it may be more advantageous to use fstrim, for example once a day.