From owner-freebsd-current@FreeBSD.ORG Wed Aug 3 15:01:18 2005 Return-Path: X-Original-To: current@freebsd.org Delivered-To: freebsd-current@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 95A5916A41F for ; Wed, 3 Aug 2005 15:01:18 +0000 (GMT) (envelope-from dan@dan.emsphone.com) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id 29D3743D45 for ; Wed, 3 Aug 2005 15:01:18 +0000 (GMT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.13.1/8.13.3) id j73F1HgV048066; Wed, 3 Aug 2005 10:01:17 -0500 (CDT) (envelope-from dan) Date: Wed, 3 Aug 2005 10:01:17 -0500 From: Dan Nelson To: Maxim.Sobolev@portaone.com Message-ID: <20050803150117.GD93405@dan.emsphone.com> References: <42F0CCD5.9090200@portaone.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <42F0CCD5.9090200@portaone.com> X-OS: FreeBSD 5.4-STABLE X-message-flag: Outlook Error User-Agent: Mutt/1.5.9i Cc: "current@freebsd.org" Subject: Re: Sub-optimal libc's read-ahead buffering behaviour X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 03 Aug 2005 15:01:18 -0000 In the last episode (Aug 03), Maxim Sobolev said: > I have found the scenario in which our libc behaves utterly > suboptimally. Consider the following piece of code reads and > processes every other 512-bytes block in a file (error handling > intentionally omitted): > > What I have discovered in this case is that libc reads 4096 bytes > from the file for *each* fread(3) call, despite the fact that it can > only do one actual read(2) for every fourth fread(3) and satisfy the > rest from the internal buffer (4096 bytes). However, if I replace > fseek(3) with just another dummy fread(3) everything works as > expected - libc does only one read for every 8 fread(3) calls (4 > dummy and 4 real). > > Is it something which should be fixed or are there some subtle > reasons for the current behaviour? I don't think stdio buffers character devices. If you try it again, opening a regular file (I tried with /boot/kernel/kernel), you'll see much better behaviour. There are still some unnecessary seeks though: truss ./a.out open("/boot/kernel/kernel",O_RDONLY,0666) = 3 (0x3) fstat(3,{mode=-r-xr-xr-x ,inode=12295,size=4372853,blksize=8192}) = 0 (0x0) readlink("/etc/malloc.conf","AJ",63) = 2 (0x2) issetugid() = 0 (0x0) mmap(0x0,4096,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 672555008 (0x28166000) break(0x804b000) = 0 (0x0) break(0x804d000) = 0 (0x0) read(3,"\^?ELF\^A\^A\^A\t\0\0\0\0\0\0\0\0\^B\0"...,8192) = 8192 (0x2000) read(3,"\0\0\0\0\0\0\0\0\M-u\^E\0\0\M-+\^V\0\0q"...,8192) = 8192 (0x2000) exit(0x0) truss ./a.out 1 open("/boot/kernel/kernel",O_RDONLY,0666) = 3 (0x3) fstat(3,{mode=-r-xr-xr-x ,inode=12295,size=4372853,blksize=8192}) = 0 (0x0) readlink("/etc/malloc.conf","AJ",63) = 2 (0x2) issetugid() = 0 (0x0) mmap(0x0,4096,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0x0) = 672555008 (0x28166000) break(0x804b000) = 0 (0x0) break(0x804d000) = 0 (0x0) read(3,"\^?ELF\^A\^A\^A\t\0\0\0\0\0\0\0\0\^B\0"...,8192) = 8192 (0x2000) lseek(3,0x0,SEEK_CUR) = 8192 (0x2000) lseek(3,0x2000,SEEK_SET) = 8192 (0x2000) read(3,"\0\0\0\0\0\0\0\0\M-u\^E\0\0\M-+\^V\0\0q"...,8192) = 8192 (0x2000) exit(0x0) -- Dan Nelson dnelson@allantgroup.com