Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 20 Aug 2012 06:58:28 -0700
From:      Matthew Jacob <mj@feral.com>
To:        John Baldwin <jhb@freebsd.org>
Cc:        Poul-Henning Kamp <phk@phk.freebsd.dk>, freebsd-current@freebsd.org, Matt Jacob <mjacob@freebsd.org>
Subject:   Re: BUFSIZ = 1024, still ?
Message-ID:  <50324284.2080305@feral.com>
In-Reply-To: <201208200824.38500.jhb@freebsd.org>
References:  <6800.1345323911@critter.freebsd.dk> <201208200824.38500.jhb@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On 8/20/2012 5:24 AM, John Baldwin wrote:
> On Saturday, August 18, 2012 5:05:11 pm Poul-Henning Kamp wrote:
>> In message <5030033B.4060705@feral.com>, Matthew Jacob writes:
>>> On 8/18/2012 1:32 PM, Poul-Henning Kamp wrote:
>>>> Shouldn't we at least increase it to pagesize ?
>>>>
>>> What data suggests to you it would be better at pagesize?
>> The number of system calls to fwrite() a big file ?
> Have you looked at an actual ktrace? :)  stdio doesn't use BUFSIZ for
> regular files:
>
> head/lib/libc/stdio/makebuf.c:
> /*
>   * Internal routine to determine `proper' buffering for a file.
>   */
> int
> __swhatbuf(fp, bufsize, couldbetty)
>          FILE *fp;
>          size_t *bufsize;
>          int *couldbetty;
> {
>          struct stat st;
>
>          if (fp->_file < 0 || _fstat(fp->_file, &st) < 0) {
>                 ...
>                  *bufsize = BUFSIZ;
>                  return (__SNPT);
>          }
>
>          ...
>          if (st.st_blksize <= 0) {
>                  *bufsize = BUFSIZ;
>                  return (__SNPT);
>          }
>
>          *bufsize = st.st_blksize;
>          ...
> }
>
> For a regular file stdio will use the filesystem's block size, not BUFSIZ.
>
> Test program:
>
> #include <stdio.h>
> #include <err.h>
>
> int
> main(int ac, char **av)
> {
>          char junk;
>          FILE *fp;
>          int i;
>
>          fp = fopen("/tmp/junk", "w");
>          if (fp == NULL)
>                  err(1, "fopen");
>          for (i = 0; i < 1024 * 1024; i++)
>                  if (fwrite(&junk, sizeof(junk), 1, fp) != 1)
>                          errx(1, "fwrite failed");
>          return (0);
> }
>
> ktrace excerpt:
>
>   42599 a.out    CALL  write(0x3,0x800a04000,0x4000)
>   42599 a.out    RET   write 16384/0x4000
>   42599 a.out    CALL  write(0x3,0x800a04000,0x4000)
>   42599 a.out    RET   write 16384/0x4000
>   42599 a.out    CALL  write(0x3,0x800a04000,0x4000)
>   42599 a.out    RET   write 16384/0x4000
>   42599 a.out    CALL  write(0x3,0x800a04000,0x4000)
>   42599 a.out    RET   write 16384/0x4000
>   42599 a.out    CALL  write(0x3,0x800a04000,0x4000)
>   42599 a.out    RET   write 16384/0x4000
>   42599 a.out    CALL  write(0x3,0x800a04000,0x4000)
>   42599 a.out    RET   write 16384/0x4000
>
> This hint also works for pipes (they set st_blksize to PAGE_SIZE).  Given
> that, I think BUFSIZ should just be left alone.
>
Perfect. Data.




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?50324284.2080305>