From owner-freebsd-current@FreeBSD.ORG Thu Aug 4 19:39:01 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 A5E9316A41F; Thu, 4 Aug 2005 19:39:01 +0000 (GMT) (envelope-from PeterJeremy@optushome.com.au) Received: from mail23.syd.optusnet.com.au (mail23.syd.optusnet.com.au [211.29.133.164]) by mx1.FreeBSD.org (Postfix) with ESMTP id E8E9043D46; Thu, 4 Aug 2005 19:38:56 +0000 (GMT) (envelope-from PeterJeremy@optushome.com.au) Received: from cirb503493.alcatel.com.au (c220-239-19-236.belrs4.nsw.optusnet.com.au [220.239.19.236]) by mail23.syd.optusnet.com.au (8.12.11/8.12.11) with ESMTP id j74Jcmci016495 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO); Fri, 5 Aug 2005 05:38:49 +1000 Received: from cirb503493.alcatel.com.au (localhost.alcatel.com.au [127.0.0.1]) by cirb503493.alcatel.com.au (8.12.10/8.12.10) with ESMTP id j74JcmSR002907; Fri, 5 Aug 2005 05:38:48 +1000 (EST) (envelope-from pjeremy@cirb503493.alcatel.com.au) Received: (from pjeremy@localhost) by cirb503493.alcatel.com.au (8.12.10/8.12.9/Submit) id j74Jcmpt002906; Fri, 5 Aug 2005 05:38:48 +1000 (EST) (envelope-from pjeremy) Date: Fri, 5 Aug 2005 05:38:48 +1000 From: Peter Jeremy To: Andrey Chernov , Maxim.Sobolev@portaone.com, Dan Nelson , "current@freebsd.org" Message-ID: <20050804193848.GC2104@cirb503493.alcatel.com.au> References: <42F0CCD5.9090200@portaone.com> <20050803150117.GD93405@dan.emsphone.com> <42F0E9B2.9080208@portaone.com> <20050804060251.GA21228@nagual.pp.ru> <20050804063908.GA21871@nagual.pp.ru> <20050804075711.GB271@cirb503493.alcatel.com.au> <20050804082527.GA22992@nagual.pp.ru> <20050804104236.GC271@cirb503493.alcatel.com.au> <20050804105208.GA26150@nagual.pp.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050804105208.GA26150@nagual.pp.ru> User-Agent: Mutt/1.4.2i Cc: 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: Thu, 04 Aug 2005 19:39:01 -0000 On Thu, 2005-Aug-04 14:52:08 +0400, Andrey Chernov wrote: >But there is no similar user-visible knob to turn on/off fseek's in-buffer >seeking, so it is always off for chardev for more safety. ... >> In both cases above, seek really needs to be intelligent - more so >> than for regular files. It needs to lseek() in multiples of the >> device block size and then adjust the buffer offset to handle any >> remainder. > >I don't understand this statement well enough. Currently fseek always >sense in-buffer data for regular files for both SEEK_SET and SEEK_CUR. Consider buffered stdio to a disk device. The underlying device requires I/O to be in multiples of 512 bytes with an offset at multiples of 512 bytes. IMHO, these alignment requirements should be hidden from the user - I should be able to write code like: c = getc(disk); fseek(disk, 3, SEEK_CUR); w = getw(disk); fseek(disk, 1, SEEK_CUR); c1 = getc(disk); fseek(disk, c1, SEEK_CUR); to work my way through data on the disk. Currently, I can't do that because the fseek() is transparent and the underlying lseek() will fail. Instead, I need to write code like: fread(buf, sizef(buf), 1, disk); char *bp = buf; ... /* getc */ c = *bp++; if (bp == buf + sizeof(buf)) { fread(buf, sizef(buf), 1, disk); bp = buf; } /* fseek(+3) */ bp += 3; if (bp >= buf + sizeof(buf)) { fread(buf, sizef(buf), 1, disk); bp = buf; } /* getw */ if (buf + sizeof(buf) - bp > sizeof(int)) { w = *(int *)bp; bp += sizeof(int); } else { int i; for (w = i = 0; i < sizeof(int); i++) w |= *bp++ << (i * 8); if (bp == buf + sizeof(buf)) { fread(buf, sizef(buf), 1, disk); bp = buf; } } } /* fseek(+1) */ bp++; if (bp >= buf + sizeof(buf)) { fread(buf, sizef(buf), 1, disk); bp = buf; } /* getc */ c1 = *bp++; if (bp == buf + sizeof(buf)) { fread(buf, sizef(buf), 1, disk); bp = buf; } /* fseek(+c1) */ bp += c1; if (bp >= buf + sizeof(buf)) { fread(buf, sizef(buf), 1, disk); bp = buf; } ie, I've had to implement my own buffering which basically negates the whole purpose of stdio. (Note that both sets of code examples need error checking added - which significantly increases the size of each). -- Peter Jeremy