236 Chapter 13
input file into the buffer cache is unavoidable. However, we already saw that write()
returns immediately after transferring data from user space to the kernel buffer
cache. Since the RAM size on the test system (4 GB) far exceeds the size of the file
being copied (100 MB), we can assume that by the time the program completes, the
output file has not actually been written to disk. Therefore, as a further experi-
ment, we ran a program that simply wrote arbitrary data to a file using different
write() buffer sizes. The results are shown in Table 13-2.
Again, the data shown in Table 13-2 was obtained from kernel 2.6.30, on an
ext2 file system with a 4096-byte block size, and each row shows the average of 20 runs.
We don’t show the test program (filebuff/write_bytes.c), but it is available in the
source code distribution for this book.
Table 13-2 shows the costs just for making write() system calls and transferring data
from user space to the kernel buffer cache using different write() buffer sizes. For
larger buffer sizes, we see significant differences from the data shown in Table 13-1.
For example, for a 65,536-byte buffer size, the elapsed time in Table 13-1 is 2.06 sec-
onds, while for Table 13-2 it is 0.09 seconds. This is because no actual disk I/O is
being performed in the latter case. In other words, the majority of the time
required for the large buffer cases in Table 13-1 is due to the disk reads.
As we’ll see in Section 13.3, when we force output operations to block until
data is transferred to the disk, the times for write() calls rise significantly.
Finally, it is worth noting that the information in Table 13-2 (and later, in
Table 13-3) represents just one form of (naive) benchmark for a file system. Fur-
thermore, the results will probably show some variation across file systems. File systems
can be measured by various other criteria, such as performance under heavy multiuser
load, speed of file creation and deletion, time required to search for a file in a large
directory, space required to store small files, or maintenance of file integrity in the
event of a system crash. Where the performance of I/O or other file-system opera-
tions is critical, there is no substitute for application-specific benchmarks on the
target platform.
Table 13-2: Time required to write a file of 100 million bytes
BUF_SIZE
Time (seconds)
Elapsed Total CPU User CPU System CPU
1 72.13 72.11 5.00 67.11
2 36.19 36.17 2.47 33.70
4 20.01 19.99 1.26 18.73
8 9.35 9.32 0.62 8.70
16 4.70 4.68 0.31 4.37
32 2.39 2.39 0.16 2.23
64 1.24 1.24 0.07 1.16
128 0.67 0.67 0.04 0.63
256 0.38 0.38 0.02 0.36
512 0.24 0.24 0.01 0.23
1024 0.17 0.17 0.01 0.16
4096 0.11 0.11 0.00 0.11
16384 0.10 0.10 0.00 0.10
65536 0.09 0.09 0.00 0.09