实际开发中经常需要存储数据, 无论是存储日志,还是二进制数据(图片,雷达数据或视频文件等), 不能一直存,是否存在一种策略:
当磁盘空间不足时,优先删除最开始写入的数据呢?
循环覆盖的策略应该有很多,这篇文章抛砖引玉,希望更多个伙伴给出更好的方案出来!
这里只提供思路,具体实现按照客户需求做多样化,最好写个抽象基类出来,后续可以多态各种需求。
磁盘分区
当前使用比例。mkfs.ext4 /dev/mmcblk1p1
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
if(argc < 5){
printf("Usage: ./a.out <创建文件数量> <创建文件大小MB> <创建文件间隔 S> <拷贝目录绝对路径>\n");
return -1;
}
std::string strcmd("");
int nFileCount = atoi(argv[1]);
int nFileSize = atoi(argv[2]);
int nTimeSpan = atoi(argv[3]);
std::string dir = argv[4];
printf("创建文件数量:%d, 创建文件大小为:%d, 创建文件间隔:%d 拷贝的目录:%s\n", nFileCount, nFileSize, nTimeSpan, dir.c_str());
for(int i = 0; i < nFileCount; ++i){
strcmd =std::string( "dd if=/dev/zero of=") + dir + "/"+std::to_string(time(0))+"_"+std::to_string(i+1)+".file bs=1048576 count=" + std::to_string(nFileSize);
printf("cmd:%s\n", strcmd.c_str());
FILE * fp = popen(strcmd.c_str(), "r");
if(nullptr == fp){
printf("pipe error .\n");
exit(127);
}
printf("create %02d , size:%d MB\n", i+1, nFileSize);
fclose(fp); fp = 0;
sleep(nTimeSpan);
}
return 0;
}
运行日志如下:
cmd:dd if=/dev/zero of=/home/ubuntu/pic/6/1703141122_91.file bs=1048576 count=100
create 91 , size:100 MB
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 3.40722 s, 30.8 MB/s
cmd:dd if=/dev/zero of=/home/ubuntu/pic/6/1703141125_92.file bs=1048576 count=100
create 92 , size:100 MB
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 3.47826 s, 30.1 MB/s
cmd:dd if=/dev/zero of=/home/ubuntu/pic/6/1703141129_93.file bs=1048576 count=100
create 93 , size:100 MB
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 3.28752 s, 31.9 MB/s
cmd:dd if=/dev/zero of=/home/ubuntu/pic/6/1703141132_94.file bs=1048576 count=100
create 94 , size:100 MB
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 3.49482 s, 30.0 MB/s
cmd:dd if=/dev/zero of=/home/ubuntu/pic/6/1703141136_95.file bs=1048576 count=100
create 95 , size:100 MB
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 3.62826 s, 28.9 MB/s
cmd:dd if=/dev/zero of=/home/ubuntu/pic/6/1703141139_96.file bs=1048576 count=100
create 96 , size:100 MB
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 3.27876 s, 32.0 MB/s
cmd:dd if=/dev/zero of=/home/ubuntu/pic/6/1703141143_97.file bs=1048576 count=100
create 97 , size:100 MB
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 3.32141 s, 31.6 MB/s
cmd:dd if=/dev/zero of=/home/ubuntu/pic/6/1703141146_98.file bs=1048576 count=100
create 98 , size:100 MB
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 3.30286 s, 31.7 MB/s
cmd:dd if=/dev/zero of=/home/ubuntu/pic/6/1703141149_99.file bs=1048576 count=100
create 99 , size:100 MB
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 3.28151 s, 32.0 MB/s
cmd:dd if=/dev/zero of=/home/ubuntu/pic/6/1703141152_100.file bs=1048576 count=100
create 100 , size:100 MB
a.out 100 512 0.5 $PWD/2
, 命令行的含义上面的代码有解释。超出阈值+一定的缓冲,一般给5%~10%足够
。