I am trying to open a file for recording using std :: ofstream and I want to set it in write-through mode (for example, use the "FILE_FLAG_WRITE_THROUGH" provided by the CreateFile Win API).
Is there an STL way to achieve this? I do not want to write WinAPI based code. My goal is to disable OS caching and write using different block sizes to get data related to storage performance. I can’t use the standard comparison tool because the goal is to understand how to optimize the recording level settings for the specific repository I have to rely on.
@Update This is MWE where I would like to see different save times when changing blk_size value:
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vector>
#include <ctime>
std::vector<unsigned char>
GenerateRandomData(long numBytes)
{
std::vector<unsigned char> res(numBytes);
std::srand(std::time(0));
for (int i = 0; i < numBytes; ++i)
res[i] = static_cast<unsigned char>(std::rand() % 256);
return res;
}
int main(int, char)
{
const long dataLength = 1 * 1024 * 1024 * 1024;
std::vector<unsigned char> outbuf = GenerateRandomData(dataLength);
const auto blk_size = 128 * 1024;
char blk[blk_size];
std::ofstream ofs;
ofs.rdbuf()->pubsetbuf(blk, blk_size);
ofs.setf(std::ios_base::unitbuf);
ofs.open("output.dat", std::ofstream::binary | std::ofstream::trunc);
auto ptr_idx = 0;
auto ptr = reinterpret_cast<char*>(outbuf.data());
const auto outbuf_size = outbuf.size();
std::clock_t sw = clock();
ofs.write((const char *)&ptr[ptr_idx], outbuf_size);
ofs.flush();
ofs.close();
sw = ( clock() - sw );
double writtenBytes = static_cast<double>(outbuf.size());
double writtenMBytes = writtenBytes / (1024 * 1024);
double testSeconds = static_cast<double>(sw) / static_cast<double>(CLOCKS_PER_SEC);
double avgSpeed = writtenMBytes / testSeconds;
std::cout << "Benchmark: written " << writtenMBytes << " MB in " << testSeconds << " sec. => " << avgSpeed << "MB/s" << std::endl;
std::getchar();
return 0;
}
Thank you in advance