Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 16 Dec 1995 16:13:38 -0800
From:      David Greenman <davidg@Root.COM>
To:        David Hovemeyer <daveho@infocom.com>
Cc:        hackers@freebsd.org
Subject:   Re: mmap and memory utilization 
Message-ID:  <199512170013.QAA00305@corbin.Root.COM>
In-Reply-To: Your message of "Sat, 16 Dec 95 13:16:39 EST." <Pine.BSF.3.91.951216123927.19249G-100000@infocom.com> 

next in thread | previous in thread | raw e-mail | index | archive | help
>I am writing a program that needs to do a lot of character at
>a time reading through regular files, both forwards and backwards.
>I decided to use mmap(2) rather than read(2) to implement this.
>My reasoning is that it is easier to do random access in memory
>than in a file.  Also, I imagine that calling read(2) for reading
>single characters is inefficient in terms of system call overhead.
>I could use iostreams (it's a C++ program) to get buffering of
>reads, but iostreams are big and hairy, and still more awkward
>to use than memory for what I want to do.
>
>What I am wondering is
>
>	What is the impact of mmap'ing a large file and then
>	scanning linearly through it?

   Depends on what you mean by "large".

>Currently I am thinking of mmap'ing the entire file; if the file is
>approximately as large as physical memory, will this cause excessive
>paging?  (I am assuming an infinite amount of virtual memory,
>but a limited amount of physical memory.)  Will the program degrade
>the performance of other programs which are running?  Would the mmap
>be likely to fail?

   Depends on what you consider "excessive". :-)

>In most cases I expect the mmap'ed file to be small (< 16K), but
>I want the program to be a "good citizen" under extreme conditions.
>I'm pretty clueless about the details of virtual memory and how
>it is implemented in FreeBSD.

   In those cases it will work extremely well and is much faster than doing
small reads. As the file involved gets larger than available memory, the
performance will degrade. This may or may not be an issue depending on what
other processes are running on the machine. For most 'normal' sized files on
a machine with a 'reasonable' amount of memory, mmap() is faster than read().

>Hmm, I just noticed the madvise(2) man page: is this what I need
>to use?  Maybe I could say MADV_DONTNEED to pages scanned past,
>and MADV_WILLNEED to pages about to be accessed?

   madvise(2) isn't yet implemented in FreeBSD.

-DG



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