What are 开发者_运维技巧the consequences of writing large amounts of data to disk using unbuffered file I/O (at least for everything above the operating system level)?
Details:
I'm writing a Ruby script that will execute another piece of code, capturing its stdout and stderr and writing them to a file. Apparently (in Ruby, at least), stderr is unbuffered and stdout is buffered, which in my case results in out-of-order output as the stderr lines get printed before some stdout lines.
It seems the solution is to make this portion of the code use unbuffered IO (with IO.sync = true
). However, the piece of code my script is running will also be writing large amounts of text to disk. So I'm wondering what the consequence is of not using the Ruby buffer (only the OS buffer and below), and if it is significant, how else I can get around the ordering problem?
Unbuffered I/O is slower than buffered I/O when write operations have small counts and the situation is reversed for large count write operations. In a middle range around 1,000 to 10,000 bytes per operation it doesn't make much difference.
You will also see slightly better performance when operations are aligned
What IO.sync
does is toggle automatic flushing of the buffer, but doesn't change the fact that it is still buffered.
What you might want instead is to bypass the buffering system altogether and use IO#syswrite instead:
STDERR.syswrite("Look ma, no buffers")
As the documentation says, you should pick either buffered or unbuffered and stick with it, as mixing and matching can cause issues.
精彩评论