ZT: Linux: Accessing Files With O_DIRECT
Linux: Accessing Files With O_DIRECT
By Jeremy on Linux news
A thread on the lkml began with a query about using O_DIRECT when opening a file. An early white paper written by Andrea Arcangeli [interview] to describe the O_DIRECT patch before it was merged into the 2.4 kernel explains, “with O_DIRECT the kernel will do DMA directly from/to the physical memory pointed [to] by the userspace buffer passed as [a] parameter to the read/write syscalls. So there will be no CPU and memory bandwidth spent in the copies between userspace memory and kernel cache, and there will be no CPU time spent in kernel in the management of the cache (like cache lookups, per-page locks etc..).” Linux creator Linus Torvalds was quick to reply that despite all the claims there is no good reason for mounting files with O_DIRECT, suggesting that interfaces like madvise() and posix_fadvise() should be used instead, “there really is no valid reason for EVER using O_DIRECT. You need a buffer whatever IO you do, and it might as well be the page cache. There are better ways to control the page cache than play games and think that a page cache isn’t necessary.”
Linus went on to explain, “the only reason O_DIRECT exists is because database people are too used to it, because other OS’s haven’t had enough taste to tell them to do it right, so they’ve historically hacked their OS to get out of the way. As a result, our madvise and/or posix_fadvise interfaces may not be all that strong, because people sadly don’t use them that much. It’s a sad example of a totally broken interface (O_DIRECT) resulting in better interfaces not getting used, and then not getting as much development effort put into them.” To further underscore his point, he humorously added:
“The whole notion of “direct IO” is totally brain damaged. Just say no.
This is your brain: O
This is your brain on O_DIRECT: .
Any questions?