Tutorial :Portability of open(…O_DIRECT) in C?



Question:

In C file I/O, the O_DIRECT flag can be used to minimize cache effects for a file being open()ed. I understand that this is not a POSIX feature, has been present in the Linux kernel since version 2.4.10, and that Linus is opposed to the interface in general. Under NetBSD, it seems to work as advertised. Example call:

  int fd = open(filename, O_DIRECT);  

I am attempting to write some low-level disk benchmarking utilities, and using O_DIRECT looks to be a potentially good way of measuring the disk and drive performance without the effects of the OS filesystem/block cache. Ideally, I would like to be able to run the benchmark on Linux, Windows (Cygwin is OK), Mac OS X, and BSD systems. Is O_DIRECT the best way to bypass the OS disk caches, in terms of portability and reliability for benchmarking? Are there alternatives?


Solution:1

For windows, you should look at CreateFile function, with flags FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH. ( http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx ). But then you'll have to use windows functions for reading and writing : SetFilePointer, WriteFile, ReadFile...


Solution:2

O_DIRECT doesn't even exist on Mac OS X (I just grep'd for it in /usr/include and it wasn't there). As for Linux, what will your tool do that hdparm doesn't?


Solution:3

I don't think it is supported by Windows - at least I can find no mention in the MSDN. This means that it presumably can;'t be supported by Cygwin either, as Cygwin needs to use the underlying Windows OS.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »