From owner-freebsd-hackers@FreeBSD.ORG Mon Sep 8 20:36:47 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 39D7C16A4BF for ; Mon, 8 Sep 2003 20:36:47 -0700 (PDT) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8688C4400B for ; Mon, 8 Sep 2003 20:36:46 -0700 (PDT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.12.9/8.12.9) id h893aetc004539; Mon, 8 Sep 2003 22:36:40 -0500 (CDT) (envelope-from dan) Date: Mon, 8 Sep 2003 22:36:40 -0500 From: Dan Nelson To: Sean Hamilton Message-ID: <20030909033640.GO48339@dan.emsphone.com> References: <001c01c37659$4d38f090$0300000a@antalus> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <001c01c37659$4d38f090$0300000a@antalus> X-OS: FreeBSD 5.1-CURRENT X-message-flag: Outlook Error User-Agent: Mutt/1.5.4i cc: hackers@freebsd.org Subject: Re: mmap(2) questions, reads not caching X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Sep 2003 03:36:47 -0000 In the last episode (Sep 08), Sean Hamilton said: > I have some code resembling: > > FILE * f = fopen (filename, "rb"); > mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fileno (f), 0); > > I've found that reads are not brought into disk cache. Successive > reads on the same file once again read from disk. If I cat the file > to /dev/null, then the mmap(2) does indeed read the data from cache. > What's going on here? mmap just maps a file to memory. It doesn't force the OS to cache the pages any more than it already does. You can try calling madvsize(M_WILLNEED) and see if that helps. > Also, the man page states that the mapped region may be longer than the > specified size. Does this have any implications for the size which is passed > to munmap(2)? If I pass the same size to munmap(2), then will there still be > leftover, or will the entire region be unmapped? It simply rounds up to the nearest pagesize. If you map 1 byte, you get a page. munmap does the same thing. > And, should I be passing MAP_PRIVATE or MAP_SHARED to read-only mmaps? Does > it make any difference at all? I don't think it matters. -- Dan Nelson dnelson@allantgroup.com