Rate This Document
Findability
Accuracy
Completeness
Readability

Selecting a File Buffer Mechanism

Principles

The memory access speed is higher than the drive access speed. When an application reads data from or writes data to a drive, some cache is used to reduce direct access to the drive, as shown in the following figure.

clib buffer: a data buffer mechanism in user mode. When the clib buffer is enabled, data is not synchronized to the kernel immediately after being copied from the application buffer to the clib buffer. Instead, the data is synchronized to the kernel after it reaches a certain size or synchronization is proactively triggered. When data is queried, data is preferentially queried from the clib buffer. This mechanism can reduce the switching between the user mode and kernel mode (the switching occupies certain resources).

PageCache: PageCache is a file cache mechanism in kernel mode. When a user reads or writes a file, the user operates PageCache first. The kernel synchronizes data to drives based on the scheduling mechanism or when triggered by applications. The PageCache mechanism reduces drive access.

Modification Method

The application selects a proper file read/write mode based on its service characteristics.

  • The fread/fwrite function uses the clib buffer mechanism, but the read/write function does not. Therefore, the fread/fwrite function has memory copy while the read/write function does not. That is, the fread/fwrite function copies data from the application buffer to the clib buffer, however, fread/fwrite has fewer system calls than read/write. Therefore, for the operation with a large number of read/write bytes each time, memory copy occupies more resources than system calling. You can use the read/write function to reduce memory copy. For the operations with less read and write bytes each time, system calling occupies more resources than memory copy. You are advised to use the fread/ fwrite function to reduce system calls.
  • The O_DIRECT mode does not use the PageCache and does not have memory copy. However, data is read from the drive each time because there is no buffer.

    O_DIRECT is applicable to the scenario where the application has its own buffer mechanism. After data is read and written once, the data is not read from the drive anymore.