Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 8 Sep 2003 22:36:40 -0500
From:      Dan Nelson <dnelson@allantgroup.com>
To:        Sean Hamilton <sh@bel.bc.ca>
Cc:        hackers@freebsd.org
Subject:   Re: mmap(2) questions, reads not caching
Message-ID:  <20030909033640.GO48339@dan.emsphone.com>
In-Reply-To: <001c01c37659$4d38f090$0300000a@antalus>
References:  <001c01c37659$4d38f090$0300000a@antalus>

next in thread | previous in thread | raw e-mail | index | archive | help
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



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