views:

61

answers:

2

Hello
I'm currently writing a software to test the integrity of flash-memory devices. (using c++ iostreams)

To achieve this goal files with random content are written to the device, read back, copied, moved, compared ( I put the device under stress condition, and check if the data is valid from time to time)...
Everything looks great on the outside, but there is a problem behind the curtains.

When I analyzed the commands that are sent to the device over the ATA / SATA Cable (with a Bus Doctor), I discovered that (especially) read commands are often not executed. Nevertheless the data is availible to the program. So they have to come from a buffer, which is not acceptable in my case. Whenever I read Data it has to come straight from the disk, not from the cache. Read / Write Performance isn't important at all.

But I haven't found a way to tell Windows not to buffer anything (There is the option to deactivate write caching for a specific device in the device manager, but doesn't have the desired effect).
If anyone knows how to do this: Please tell me
( same goes for any advice concerning Linux. I think I will run into the same problems there )

Only solution I could come up with so far is Direct I/O.
What I found for Windows is this:

http://www.internals.com/utilities_main.htm

The WinIo library allows 32-bit Windows applications to directly access I/O ports and physical memory. It bypasses Windows protection mechanisms by using a combination of a kernel-mode device driver and several low-level programming techniques.

and http://www.direct-io.com/

Direct I/O is the world's first device driver for Microsoft Windows NT, Windows 2000, Windows XP and Windows Server 2003 which enables the direct hardware access for your existing software without any programming efforts on your side

Questions:
Has somebody ever had a similar problem, and can give me some advice?
Do I really have to go with Direct I/O or is there some OS option/setting I haven't found (Windows or Linux)
Do you know other libraries for Direct I/O (Windows or Linux or even better: both)

+1  A: 

For Windows you can disable buffering using the Win32 API directly.

When opening or creating a file with the CreateFile function, the FILE_FLAG_NO_BUFFERING flag can be specified to disable system caching of data being read from or written to the file. Although this gives complete and direct control over data I/O buffering, in the case of files and similar devices there are data alignment requirements that must be considered.

Not sure if this can be done in the C++ standard library. You can disable buffering at the iostream level but that may not affect the O/S buffering.

Steve Townsend
Thank you for the link and the advice! Concerning the buffering at the iostream level: I know that it can be disabled but I already flush the buffers after every read/write access to the device. (I also tried to turn them off completely but it didn't help)
zitroneneis
+1  A: 

Under Linux you can use the O_DIRECT flag when opening the device, and then all the accesses will bypass the page cache and go directly to the device.

Note that O_DIRECT reads and writes must be aligned to the device's block size (usually 512 bytes) in memory addresses, file offsets and read/write request sizes.

florin
Thank you very much, I will try to do it with the flag you proposed
zitroneneis