A handful of common application workloads can readily benefit from a little well-intentioned advice to the kernel. Such advice can go a long way toward mitigating the burden of I/O. With hard disks being so slow, and modern processors being so fast, every little bit helps, and good advice can go a long way.
Before reading a chunk of a file, a process can provide thePOSIX_FADV_WILLNEEDhint to instruct the kernel to read the file into the page cache. The I/O will occur asynchronously, in the background. When the application ultimately accesses the file, the operation can complete without generating blocking I/O.
Conversely, after reading or writing a lot of data—say, while continuously streaming video to disk—a process can provide thePOSIX_FADV_DONTNEEDhint to instruct the kernel to evict the given chunk of the file from the page cache. A large streaming operation can continually fill the page cache. If the application never intends to access the data again, this means the page cache will be filled with superfluous data, at the expense of potentially more useful data. Thus, it makes sense for a streaming video application to periodically request that streamed data be evicted from the cache.
A process that intends to read in an entire file can provide thePOSIX_FADV_SEQUENTIALhint, instructing the kernel to perform aggressive readahead. Conversely, a process that knows it is going to access a file randomly, seeking to and fro, can provide thePOSIX_FADV_RANDOMhint, instructing the kernel that readahead will be nothing but worthless overhead.