From owner-freebsd-questions@FreeBSD.ORG Wed Jun 23 03:27:40 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id EFC9E16A4CF; Wed, 23 Jun 2004 03:27:39 +0000 (GMT) Received: from canning.wemm.org (canning.wemm.org [192.203.228.65]) by mx1.FreeBSD.org (Postfix) with ESMTP id CC0C143D55; Wed, 23 Jun 2004 03:27:39 +0000 (GMT) (envelope-from peter@evilpete.dyndns.org) Received: from fw.wemm.org (canning.wemm.org [192.203.228.65]) by canning.wemm.org (Postfix) with ESMTP id 3E5DB2A8EF; Tue, 22 Jun 2004 20:27:34 -0700 (PDT) (envelope-from peter@overcee.wemm.org) Received: from overcee.wemm.org (overcee.wemm.org [10.0.0.3]) by fw.wemm.org (Postfix) with ESMTP id 9BFA5E285; Tue, 22 Jun 2004 20:27:33 -0700 (PDT) (envelope-from peter@overcee.wemm.org) Received: from overcee.wemm.org (localhost [127.0.0.1]) by overcee.wemm.org (8.12.11/8.12.11) with ESMTP id i5N3RWci018266; Tue, 22 Jun 2004 20:27:32 -0700 (PDT) (envelope-from peter@overcee.wemm.org) Received: from localhost (localhost [[UNIX: localhost]]) by overcee.wemm.org (8.12.11/8.12.11/Submit) id i5N3RUTt018265; Tue, 22 Jun 2004 20:27:30 -0700 (PDT) (envelope-from peter) From: Peter Wemm To: freebsd-current@freebsd.org Date: Tue, 22 Jun 2004 20:27:30 -0700 User-Agent: KMail/1.6.1 References: <200406220015.i5M0F9br036789@apollo.backplane.com> <200406220108.31366@aldan> In-Reply-To: <200406220108.31366@aldan> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="koi8-u" Content-Transfer-Encoding: 7bit Message-Id: <200406222027.30702.peter@wemm.org> cc: Matthew Dillon cc: current@freebsd.org cc: questions@freebsd.org cc: Mikhail Teterin cc: Julian Elischer cc: Mikhail Teterin Subject: Re: read vs. mmap (or io vs. page faults) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Jun 2004 03:27:40 -0000 On Monday 21 June 2004 10:08 pm, Mikhail Teterin wrote: > On Monday 21 June 2004 08:15 pm, Matthew Dillon wrote: > > = :The mmap interface is supposed to be more efficient -- theoreticly > = :-- because it requires one less buffer-copying, and because it > = :(together with the possible madvise()) provides the kernel with > more = :information thus enabling it to make better (at least -- no > worse) = :decisions. > > = Well, I think you forgot my earlier explanation regarding > buffer = copying. = Buffer copying is a very cheap operation if > it occurs = within the L1 or L2 cache, and that is precisely what > is happening = when you read() int > > This could explain, why using mmap is not faster than read, but it > does not explain, why it is slower. > > I'm afraid, your vast knowledge of the internals of the kernel > workings obscure your vision. I, on the other hand, "enjoy" an almost > total ignorance of it, and can see, that mmap interface _allows_ for > a more (certainly, no _less_) efficient handling of the IO, than > read. That the kernel is not using all the information passed to it, > I can only explain by deficiencies/simplicity the implementation. At the risk of propagating the thread, take a step back for a minute. 10-15 years ago, when mmap was first on the drawing boards as a concept for unix, the cost of a kernel trap and entering the vm system for fault recovery versus memory bandwidth is very very different compared to today. Back then, getting into the kernel was relatively painless and memory was proportionally very slow and expensive to use. However, these days, the memory subsystem is proportionally much much faster relative to the cost of kernel traps and vm processing and recovery. The amount of "work" for the kernel to do a read() and a high-speed memory copy is much less than the cost of taking a page fault, running a whole bunch of really really nasty code in the vm system, repairing the damage from the page fault, updating the process paging state and restarting the instruction. The numbers you're posting are a simple reflection of the fact that the read syscall path has fewer (and less expensive) instructions to execute compared to the mmap fault paths. Some operating systems implemented read(2) as an internal in-kernel mmap/fault/copy/unmap. Naturally, that made mmap look fast compared to read, at the time. But that isn't how it is implemented in FreeBSD. mmap is more valuable as a programmer convenience these days. Don't make the mistake of assuming its faster, especially since the cost of a copy has gone way down. Also, dont assume that read() is faster for cases where you're reading a file that has been mmapped (and possibly even dirtied) in another process. -- Peter Wemm - peter@wemm.org; peter@FreeBSD.org; peter@yahoo-inc.com "All of this is for nothing if we don't go to the stars" - JMS/B5