写本篇文章的目的很简单,防止采坑、防止采坑、防止采坑 我们在开发程序的过程中,往往需要预分配磁盘空间,防止因磁盘空间不够而引发程序异常问题(已踩过坑), 现网查阅资料,有些预分配磁盘空间的方法不正确,在这里特别记录一下, 除此之外,把正确的预分配的方法和大家分享一下,如果其他人有建议,欢迎拍砖狠砸
测试结果
root@xxx.xxx.xxx.xxx:/data/tmp#gcc fruncate.c -o fruncate root@xxx.xxx.xxx.xxx:/data/tmp#time ./fruncate real 0m0.001s user 0m0.000s sys 0m0.000s root@xxx.xxx.xxx.xxx:/data/tmp#ll -h test.data.fruncate.txt -rw-r--r-- 1 root root 10G Oct 25 14:58 test.data.fruncate.txt root@xxx.xxx.xxx.xxx:/data/tmp#du -sh test.data.fruncate.txt 24K test.data.fruncate.txt1 .ls显示文件的“逻辑上”的size, 这不是文件真正占用磁盘空间大小,这部分空间也会被其他进程使用。 2 .du显示文件“物理上”的size,即du显示的size是文件在硬盘上占据了多少个block计算出来的
请求tlinux内核大神后,原来还存在fallocate 和 posix_fallocate这样的函数, 还是自己太low, 其实fallocate 和 posix_fallocate两个最大的区别:
This is a nonportable, Linux-specific system call
废话也不多说, 还是直接上代码比较好一点, 一目了然
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <fcntl.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <errno.h> #include <string.h> #include <stdint.h> uint64_t file_size = 10*1024*1024*1024ULL; //int fallocate(int fd, int mode, off_t offset, off_t len); //int posix_fallocate(int fd, off_t offset, off_t len); int main() { int fd = -1; int ret = -1; fd = open("tmp.txt", O_CREAT|O_RDWR, 0666); if(fd < 0){ printf("fd < 0"); return -1; } //ret = fallocate(fd, 0, 0, file_size); ret = posix_fallocate(fd, 0, file_size); if(ret < 0 ){ printf("ret = %d, errno = %d, %s\n", ret, errno, strerror(errno)); return -1; } printf("fallocate create %.2fG file\n", file_size/1024/1024/1024.0); close(fd); return 0; }测试结果:
root@xxx.xxx.xxx.xxx:/data6#time ./posix_fallocate fallocate create 10.00G file real 0m0.014s user 0m0.000s sys 0m0.014s root@xxx.xxx.xxx.xxx:/data6#ll -h posix_fallocate.file.txt -rw-r--r-- 1 root root 10G Oct 25 15:45 posix_fallocate.file.txt root@xxx.xxx.xxx.xxx:/data6#du -sh posix_fallocate.file.txt 11G posix_fallocate.file.txt不仅可以占住空间,而且速度非常快(), 之前有过一个愚蠢的想法, 用dd命令直接创建10G文件,但是会造成IO毛刺,肯定不能这么玩,要是创造100G文件岂不是凉凉了。dd命令还是安安静静压测下磁盘性能或者创造一些小文件比较好。