How does cat ‘know’ the optimum block size to use?

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x