Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fastest way to write sequential data to disk under Mac OS X?

I need a way to store large data chunks(~1-2MB) at a high rate (~200-300Mbit/s).
After some research I found several options:

  • aio_write
  • Direct_IO
  • Carbon File Manager's PBWriteForkAsync()
  • default fwrite(), wrapped in a block and dispatched via GCD
  • NSData's appendData in an NSOperation
  • ...

This wiki page describes the state of aio_write under Linux. What I didn't find was a similar page about the state of aio_write for Mac OS X.

NSOperation or Blocks+GCD seems to be a technique to achieve non-blocking IO. It is used in several open source IO libraries (e.g. https://github.com/mikeash/MAAsyncIO)

Has someone with a similar problem found a suitable solution?
Currently I tend towards PBWriteForkAsync as it takes some 'tuning'parameters. It also should be 64-bit safe.

like image 585
Thomas Zoechling Avatar asked Jun 16 '11 17:06

Thomas Zoechling


People also ask

What is sequential write speed of a hard drive?

Drives that are used primarily for large multimedia files or backups should have relatively high sequential write speeds. When manufacturers quote speeds for devices such as flash drives, hard drives and SSD's, unless otherwise specified, they are referring to the sequential speed.

How does a file get written to disk?

The file is opened... Data is 'streamed' to disk. The data in memory is in a large contiguous buffer. It is written to disk in its raw form directly from that buffer. The size of the buffer is configurable, but fixed for the duration of the stream. Buffers are written to the file, one after another.

How can I achieve the fastest possible sequential file I/O in C++?

Are there generally accepted guidelines for achieving the fastest possible sequential file I/O in C++? Rule 0: Measure. Use all available profiling tools and get to know them. It's almost a commandment in programming that if you didn't measure it you don't know how fast it is, and for I/O this is even more true.

What is the fastest way to read a binary file?

BinaryReader and BinaryWriter with a suitable buffer size are pretty fast. If you are reading into structures, the unsafe approach described in this article will get you reading fast, and writing is similar. I also agree with the suggestion to double-check that I/O is really the bottleneck.


2 Answers

I don't know MacOS very well, but I'd also try open and write syscalls from unistd.h with the non-blocking option O_NONBLOCK. reference

like image 67
Giovanni Funchal Avatar answered Sep 21 '22 15:09

Giovanni Funchal


You should use unbuffered I/O for the writes, in Carbon this is FSWriteFork() with kFSNoCacheBit, in BSD use fcntl() with F_NOCACHE.

Rather than use the system's non-blocking IO, you may want to consider a worker thread to write the blocks sequentially using a queue. This will give you more control and may end up being simpler, particularly if you want to monitor the queue to see if you are keeping up or not.

See here for more information.

like image 24
Bids Avatar answered Sep 19 '22 15:09

Bids