Search code examples
c++diskfileapirateraid

C++ FileApi.h no caching, how do i get the disk activity to 100%


Hello and thanks for reviewing my problem.

I have a system configured with PCIe RAID0 controller x16 lines connected to 4 NVMe Intel drives 2Tb each through m.2 connector. Using ATTO Disk Benchmark application, File size set to 8Gb and block size set to 2Mb the max read rate is ~7Gb/S meanwhile by looking at the task manager the disk activity percentage is at the peak 100% during the process.

My problem: I developed a simple c++ application using the Qt-Creator and MinGW-64bit compiler, using FileApi.h header to open a file with system caching disabled (No Buffering) and read same byte amount (2Mb) from the same file size (8Gb) the result is not even close, the rate is so slow ~1.2Gb/S and the disk activity during the process is around 23%

here is my code:

        #include <fileapi.h>

        void main()
        {
        HANDLE dataFile;

            dataFile = CreateFileA("File.bin", GENERIC_READ, 0, nullptr,
           OPEN_EXISTING,  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, nullptr);
            FlushFileBuffers(dataFile);
            if (dataFile == INVALID_HANDLE_VALUE)
                return ;

        //Start reading 3000 times from the file
            int counter = 0;
            while(counter < 3000){
                char * buffer = new char [pktSize*sizeof(int)];
                unsigned long read;
                ReadFile(dataFile, buffer, 2097152  /*2 Megabytes */, &read, nullptr);
                counter+=1;
                delete[] buffer;
            }
        }

I appreciate any help or advice and will be super thankful.


Solution

  • On each iteration you allocate new buffer in memory. It causes large memory traffic and performance degradation. Initialize it once and reuse:

    char * buffer = new char [pktSize*sizeof(int)];
    while(counter < 3000)
    {
      unsigned long read;
      ReadFile(dataFile, buffer, 2097152  /*2 Megabytes */, &read, nullptr);
      counter+=1;
    }
    delete[] buffer;
    

    Also you should be sure that buffer size pktSize*sizeof(int) is greater than 2097152 /*2 Megabytes */.