Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unbuffered I/O in Linux

I'm writing lots and lots of data that will not be read again for weeks - as my program runs the amount of free memory on the machine (displayed with 'free' or 'top') drops very quickly, the amount of memory my app uses does not increase - neither does the amount of memory used by other processes.

This leads me to believe the memory is being consumed by the filesystems cache - since I do not intend to read this data for a long time I'm hoping to bypass the systems buffers, such that my data is written directly to disk. I dont have dreams of improving perf or being a super ninja, my hope is to give a hint to the filesystem that I'm not going to be coming back for this memory any time soon, so dont spend time optimizing for those cases.

On Windows I've faced similar problems and fixed the problem using FILE_FLAG_NO_BUFFERING|FILE_FLAG_WRITE_THROUGH - the machines memory was not consumed by my app and the machine was more usable in general. I'm hoping to duplicate the improvements I've seen but on Linux. On Windows there is the restriction of writing in sector sized pieces, I'm happy with this restriction for the amount of gain I've measured.

is there a similar way to do this in Linux?

like image 405
stuck Avatar asked Jan 16 '11 05:01

stuck


People also ask

What does unbuffered I o mean?

Unbuffered I/O bypasses the file system cache and avoids double-caching of data within the server process and the file system cache. The c-treeACE configuration option UNBUFFERED_IO enables unbuffered I/O for the specified file.

What is buffered and unbuffered IO?

With buffered I/O, there is a lot of data copying happening: program structures –> FILE buffer –> kernel buffer –> disk. With unbuffered I/O, the copy to the FILE buffer is avoided, and with scatter/gather I/O, the kernel might be able to avoid a copy into its own buffers.

What is unbuffered file?

Defaults: BUFFERED is the default for SEQUENTIAL files. UNBUFFERED is the default for DIRECT files. BUFFERED. Specifies that during transmission to and from a data set, each record of a RECORD file must pass through intermediate storage buffers.

What is buffered and unbuffered in c?

Unixy C libraries generally default all FILE s to fully buffered, with two exceptions. stderr defaults to unbuffered. For any other FILE , if setvbuf has not been used on it at the time of the first actual read or write, and isatty is true for the underlying file descriptor, then the FILE becomes line-buffered.


3 Answers

The closest equivalent to the Windows flags you mention I can think of is to open your file with the open(2) flags O_DIRECT | O_SYNC:

   O_DIRECT (Since Linux 2.4.10)
          Try to minimize cache effects of the I/O to and from this file.  In
          general this will degrade performance, but it is useful in special
          situations, such as when applications do their own caching.  File I/O
          is done directly to/from user space buffers.  The O_DIRECT flag on its
          own makes at an effort to transfer data synchronously, but does not
          give the guarantees of the O_SYNC that data and necessary metadata are
          transferred.  To guarantee synchronous I/O the O_SYNC must be used in
          addition to O_DIRECT.  See NOTES below for further discussion.

          A semantically similar (but deprecated) interface for block devices is
          described in raw(8).

Granted, trying to do research on this flag to confirm it's what you want I found this interesting piece telling you that unbuffered I/O is a bad idea, Linus describing it as "brain damaged". According to that you should be using madvise() instead to tell the kernel how to cache pages. YMMV.

like image 187
asveikau Avatar answered Nov 15 '22 17:11

asveikau


You can use O_DIRECT, but in that case you need to do the block IO yourself; you must write in multiples of the FS block size and on block boundaries (it is possible that it is not mandatory but if you do not its performance will suck x1000 because every unaligned write will need a read first).

Another much less impacting way of stopping your blocks using up the OS cache without using O_DIRECT, is to use posix_fadvise(fd, offset,len, POSIX_FADV_DONTNEED). Under Linux 2.6 kernels which support it, this immediately discards (clean) blocks from the cache. Of course you need to use fdatasync() or such like first, otherwise the blocks may still be dirty and hence won't be cleared from the cache.

It is probably a bad idea of fdatasync() and posix_fadvise( ... POSIX_FADV_DONTNEED) after every write, but instead wait until you've done a reasonable amount (50M, 100M maybe).

So in short

  • after every (significant chunk) of writes,
  • Call fdatasync followed by posix_fadvise( ... POSIX_FADV_DONTNEED)
  • This will flush the data to disc and immediately remove them from the OS cache, leaving space for more important things.

Some users have found that things like fast-growing log files can easily blow "more useful" stuff out of the disc cache, which reduces cache hits a lot on a box which needs to have a lot of read cache, but also writes logs quickly. This is the main motivation for this feature.

However, like any optimisation

a) You're not going to need it so

b) Do not do it (yet)

like image 44
MarkR Avatar answered Nov 15 '22 17:11

MarkR


as my program runs the amount of free memory on the machine drops very quickly

Why is this a problem? Free memory is memory that isn't serving any useful purpose. When it's used to cache data, at least there is a chance it will be useful.

If one of your programs requests more memory, file caches will be the first thing to go. Linux knows that it can re-read that data from disk whenever it wants, so it will just reap the memory and give it a new use.

It's true that Linux by default waits around 30 seconds (this is what the value used to be anyhow) before flushing writes to disk. You can speed this up with a call to fsync(). But once the data has been written to disk, there's practically zero cost to keeping a cache of the data in memory.

Seeing as you write to the file and don't read from it, Linux will probably guess that this data is the best to throw out, in preference to other cached data. So don't waste effort trying to optimise unless you've confirmed that it's a performance problem.

like image 20
Artelius Avatar answered Nov 15 '22 17:11

Artelius