我正在对 SSD 进行一些实验,以检查它们在电源故障下是否会丢失数据。第一步,我在 SSD 上写入随机数据,然后在下一步中,我将电源故障注入它。最后,我从 SSD 读取数据并检查数据是否与我在电源故障前写入的数据匹配。不幸的是,所有测试都通过了,没有任何数据丢失。
这是我的实验的示例代码。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
using namespace std;
int main()
{
FILE *fp = fopen("/dev/sdb", "rb+");
char* writeBuffer = (char*) malloc(4096 * 16 * sizeof(char));
char* readBuffer = (char*) malloc(4096 * 16 * sizeof(char));
for(int i = 0; i < 4096 * 16; i++)
readBuffer[i] = rand() % 256;
fseeko64(fp, (unsigned long long int) 6107552 * 4096, SEEK_SET);
int writeBytes = fwrite((void*) writeBuffer, sizeof(char), 4096 * 16, fp);
sleep(2); // power fault injection
fseeko64(fp, (unsigned long long int) 6107552 * 4096, SEEK_SET);
int readBytes = fread((void*) readBuffer, sizeof(char), 4096 * 16, fp);
cout << "Write Bytes : " << writeBytes
<< " Read Bytes : " << readBytes << endl;
fclose(fp);
return 0;
}
blktrace
我在运行时执行了上面的代码/dev/sdb
。我得到了以下结果。fread()
输出中没有任何痕迹blktrace
。
blktrace -d /dev/sdb -o - | blkparse -i -
8,16 3 1 0.000000000 74 Q W 48860416 + 8 [kworker/u16:1]
8,16 3 2 0.000005829 74 G W 48860416 + 8 [kworker/u16:1]
8,16 3 3 0.000007357 74 P N [kworker/u16:1]
8,16 3 4 0.000010943 74 Q W 48860424 + 8 [kworker/u16:1]
8,16 3 5 0.000012601 74 M W 48860424 + 8 [kworker/u16:1]
8,16 3 6 0.000014427 74 Q W 48860432 + 8 [kworker/u16:1]
8,16 3 7 0.000014899 74 M W 48860432 + 8 [kworker/u16:1]
8,16 3 8 0.000016525 74 Q W 48860440 + 8 [kworker/u16:1]
8,16 3 9 0.000016780 74 M W 48860440 + 8 [kworker/u16:1]
8,16 3 10 0.000018818 74 Q W 48860448 + 8 [kworker/u16:1]
8,16 3 11 0.000019168 74 M W 48860448 + 8 [kworker/u16:1]
8,16 3 12 0.000022649 74 Q W 48860456 + 8 [kworker/u16:1]
8,16 3 13 0.000022929 74 M W 48860456 + 8 [kworker/u16:1]
8,16 3 14 0.000024345 74 Q W 48860464 + 8 [kworker/u16:1]
8,16 3 15 0.000024682 74 M W 48860464 + 8 [kworker/u16:1]
8,16 3 16 0.000025933 74 Q W 48860472 + 8 [kworker/u16:1]
8,16 3 17 0.000026205 74 M W 48860472 + 8 [kworker/u16:1]
8,16 3 18 0.000027480 74 Q W 48860480 + 8 [kworker/u16:1]
8,16 3 19 0.000027803 74 M W 48860480 + 8 [kworker/u16:1]
8,16 3 20 0.000029141 74 Q W 48860488 + 8 [kworker/u16:1]
8,16 3 21 0.000029416 74 M W 48860488 + 8 [kworker/u16:1]
8,16 3 22 0.000030519 74 Q W 48860496 + 8 [kworker/u16:1]
8,16 3 23 0.000030856 74 M W 48860496 + 8 [kworker/u16:1]
8,16 3 24 0.000032865 74 Q W 48860504 + 8 [kworker/u16:1]
8,16 3 25 0.000033172 74 M W 48860504 + 8 [kworker/u16:1]
8,16 3 26 0.000034933 74 Q W 48860512 + 8 [kworker/u16:1]
8,16 3 27 0.000035258 74 M W 48860512 + 8 [kworker/u16:1]
8,16 3 28 0.000036723 74 Q W 48860520 + 8 [kworker/u16:1]
8,16 3 29 0.000037081 74 M W 48860520 + 8 [kworker/u16:1]
8,16 3 30 0.000044758 74 Q W 48860528 + 8 [kworker/u16:1]
8,16 3 31 0.000045026 74 M W 48860528 + 8 [kworker/u16:1]
8,16 3 32 0.000046789 74 Q W 48860536 + 8 [kworker/u16:1]
8,16 3 33 0.000047079 74 M W 48860536 + 8 [kworker/u16:1]
8,16 3 34 0.000048702 74 I W 48860416 + 128 [kworker/u16:1]
8,16 3 35 0.000050392 74 U N [kworker/u16:1] 1
8,16 3 36 0.000052170 74 D W 48860416 + 128 [kworker/u16:1]
8,16 2 1 0.000885952 0 C W 48860416 + 128 [0]
如何禁用从缓冲区读取?