How Much to Buffer?

Let's do some maths to determine what should be the buffer size to ensure the best performance.

Now arises the following question: how many updates should LFS buffer before writing to disk? The answer, of course, depends on the disk itself, specifically how high the positioning overhead is in comparison to the transfer rate; see the FFS chapter for a similar analysis.

For example, assume that positioning (i.e., rotation and seek overheads) before each write takes roughly TpositionT_{position} seconds. Assume further that the disk transfer rate is Rpeak MB/sR_{peak}\ MB/s. How much should LFS buffer before writing when running on such a disk?

The way to think about this is that every time you write, you pay a fixed overhead of the positioning cost. Thus, how much do you have to write in order to amortize that cost? The more you write, the better (obviously), and the closer you get to achieving peak bandwidth.

To obtain a concrete answer, let’s assume we are writing out DD MB. The time to write out this chunk of data (TwriteT_{write}) is the positioning time TpositionT_{position} plus the time to transfer DD(DRpeak\frac {D} {R_{peak}}), or:

Twrite=Tposition+DRpeakT_{write} = T_{position} + \frac {D} {R_{peak}}

And thus the effective rate of writing (ReffectiveR_{effective}), which is just the amount of data written divided by the total time to write it, is:

Reffective=DTwrite=DTposition+DRpeakR_{effective} = \frac {D} {T_{write}} = \frac {D} {T_{position} + \frac {D} {R_{peak}} }

What we’re interested in is getting the effective rate (ReffectiveR_{effective}) close to the peak rate. Specifically, we want the effective rate to be some fraction F of the peak rate, where 0<F<10<F <1 (a typical FF might be 0.9, or 90% of the peak rate). In mathematical form, this means we want Reffective=F×RpeakR_{effective}=F \times R_{peak}

At this point, we can solve for DD:

Reffective=DTposition+DRpeak=F×RpeakR_{effective} = \frac {D} {T_{position} + \frac {D} {R_{peak}} } = F \times R_{peak}

D=F×Rpeak×(Tposition+DRpeak)D = F \times R_{peak} \times (T_{position} + \frac {D} {R_{peak}})

D=(F×Rpeak×Tposition)+(F×Rpeak×DRpeak)D = (F \times R_{peak} \times T_{position} ) + (F \times R_{peak} \times \frac {D} {R_{peak}})

D=F1F×Rpeak×TpositionD = \frac {F} {1-F} \times R_{peak} \times T_{position}

Let’s do an example, with a disk with a positioning time of 10 milliseconds and peak transfer rate of 100 MB/s; assume we want an effective bandwidth of 90% of peak (FF = 0.9). In this case, DD would be:

D=0.90.1×100MB/s×0.01 seconds=9 MBD=\frac {0.9} {0.1} \times 100 MB/s \times 0.01 \ seconds = 9 \ MB

Try some different values to see how much we need to buffer in order to approach peak bandwidth:

Get hands-on with 1200+ tech skills courses.