views:

642

answers:

4

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?

+1  A: 

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?

Dietrich Epp
Thanks. hdparm is useful ("hdparm --direct -t" and "hdparm --direct -T" give the drive's sequential and burst transfer rates respectively), but I would also like to test random I/O patterns.
cheduardo
+1  A: 

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.

anon
+1  A: 

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...

imaspy
A: 

For future Google searches, this question is answered by this other StackOverflow question: http://stackoverflow.com/questions/2299402/how-does-one-do-raw-io-on-mac-os-x-ie-equivalent-to-linuxs-o-direct-flag

Evan Jones