Cat – How Does Cat Determine Optimum Block Size?

block-devicecatdd

From reading this, it seems that when copying data to a different hard drive, cat automatically uses the optimum block size (or very near it).

I wonder how it determines the optimum block size, and whether the method cat uses can be applied to dd somehow.

Best Answer

The main loop of GNU cat, in the simplest case is (function simple_cat from cat.c):

while (true)
    {
        /* Read a block of input. */
        n_read = safe_read (input_desc, buf, bufsize);

        /* ... */
    }

Then the question becomes "how is bufsize set?" The answer is it's using io_blksize (insize = io_blksize (stat_buf)), which is defined as follows:

io_blksize (struct stat sb)
{
  return MAX (IO_BUFSIZE, ST_BLKSIZE (sb));
}

where ST_BLKSIZE gives the operating system's idea of the file system's preferred I/O block size (as accessed using stat), and IO_BUFSIZE is defined as 128*1024 (128KB). Here is an excerpt of the Linux stat syscall documentation:

blksize_t st_blksize; /* blocksize for file system I/O */ (...)

The st_blksize field gives the "preferred" blocksize for efficient
file system I/O.   (Writing to a file in smaller  chunks may cause
an inefficient read-modify-rewrite.)

So it seems that GNU cat will read in blocks of 128KB or the file system's recommended I/O block size, whichever is larger.

Related Question