Succesive write result in accessing the disk only once

时间:2016-03-02 11:02:01

标签: c++ performance

In a given school excerice , i was requested to measure proccessor operation time. Among other things, I was asked measure the time it takes to access the disk, and was strongly suggested to use functions on files.

Therefore, i came up with this simple "solution":

/* Time measurement function for accessing the disk.
   returns time in nano-seconds upon success,
   and -1 upon failure.
   */
double osm_disk_time(unsigned int iterations)
{
    // Check for illegal input
    iterations = (iterations == ILLEGAL_ITER_NUM) ? DEFAULT_ITER_NUM:iterations;

    // Start measuring.
    startTimeSuccess =  gettimeofday(&timeStart, NULL);
    for (unsigned int i = 0; i < iterations/ LOOP_FACTOR; ++i) {
        //operation. (loop unrolling): Write a char and flush memory

        if (fputs(LETTER_TO_WRITE, pFile) < SUCCESS || fflush(pFile) < SUCCESS)
        {
            return FAILURE;
        }

        if (fputs(LETTER_TO_WRITE, pFile) < SUCCESS || fflush(pFile) < SUCCESS)
        {
            return FAILURE;
        }

        if (fputs(LETTER_TO_WRITE, pFile) < SUCCESS || fflush(pFile) < SUCCESS)
        {
            return FAILURE;
        }
    }
    // Stop measuring.
    endTimeSuccess = gettimeofday(&timeEnd, NULL);

    return returnValueChecker(startTimeSuccess, endTimeSuccess, iterations);
}

The problem is that my measuring time was significantly below the avrege. According to some research I did, I understand that multiple "writes" may access the disk only once, and this may be the reason why my measures arent accurate. I dont understand why it happens and what can i do to fix this problem.

(PS: returnValueChecker() just check returning values).

2 个答案:

答案 0 :(得分:3)

涉及多个级别的缓存。第一个是图书馆本身。您可以使用fflush缓解它。第二个是在操作系统中,称为页面缓存。还有第三个 - 磁盘驱动器本身的硬件缓存。

为避免缓存效应,可以采用以下几种策略:

  • 使用比可用RAM大得多的文件。这样整个文件就无法缓存在RAM中。

  • 使用特殊调用进行直接磁盘访问(在操作系统文档中查找),或者像使用库缓存一样刷新操作系统缓存。例如,使用POSIX的fsync电话。

  • 您很可能对随机访问时间感兴趣。因此,创建一个大文件(&gt; RAM),然后在随机位置读/写。

答案 1 :(得分:0)

您可以在写入文件后调用fsync(file_descriptor)。 该调用将刷新缓冲区中的数据到磁盘。 要么 您可以传递O_SYNC标志以打开打开文件的呼叫。 这将关闭优化,即每次进行写入调用时它都会将数据刷新到磁盘。