From owner-freebsd-hackers@FreeBSD.ORG Fri Apr 4 17:10:46 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 37FFA37B401 for ; Fri, 4 Apr 2003 17:10:46 -0800 (PST) Received: from stork.mail.pas.earthlink.net (stork.mail.pas.earthlink.net [207.217.120.188]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9D6DB43F85 for ; Fri, 4 Apr 2003 17:10:45 -0800 (PST) (envelope-from tlambert2@mindspring.com) Received: from pool0021.cvx22-bradley.dialup.earthlink.net ([209.179.198.21] helo=mindspring.com) by stork.mail.pas.earthlink.net with asmtp (SSLv3:RC4-MD5:128) (Exim 3.33 #1) id 191cCg-0002ee-00; Fri, 04 Apr 2003 17:10:43 -0800 Message-ID: <3E8E2CA8.3B6086E6@mindspring.com> Date: Fri, 04 Apr 2003 17:08:56 -0800 From: Terry Lambert X-Mailer: Mozilla 4.79 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 To: Sreekanth References: <000001c2fa20$2ee5ba20$ae28a8c0@SREELAPTOP> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-ELNK-Trace: b1a02af9316fbb217a47c185c03b154d40683398e744b8a4db152b73e3f84429e7038bf54a82224193caf27dac41a8fd350badd9bab72f9c350badd9bab72f9c cc: freebsd-hackers@freebsd.org Subject: Re: Mmap and malloc 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: Sat, 05 Apr 2003 01:10:46 -0000 Note: For really basic questions, you should probably post questions like these to mailto:questions@freebsd.org . - This is the third time in 24 hours you have asked this question. The first two times, you asked it one hour apart. The last time you asked it on -arch, which is definitely the wrong list to ask on; it is a architectural discussion list. I will answer your question here on -hackers, but realize that asking the same thing repeatedly in short intervals is likely to get you ignored in the future. - Sreekanth wrote: > I have a situation i am not sure i understand.I am running a 4.6.2 > FreeBSD system with a Dual Xeon board with 2GB of Physical memory. Not relevent. Both mmap() and malloc() operate on vitural address spaces, and are user space, and so are limited by your UVA size. Worst case, mmap() or malloc() will give you more memory than you actually have physical memory for, and then start swapping. That's what "virtual memory" is supposed to do. > I have a test program which continuoulsy calls mmap(with larger size > each time) till it receives MAP_FAILED return value. I then call > malloc(with bigger memory size each time) continuosly till it > fails.Here are my results. > > Max mem mapped = 714 MB > Max mem alloc = 308 MB > > My MAXDMSIZ is 1200*1024*1024 and KVA_PAGE is 512 (2GB) > > If i swap the order in wich mmap and malloc are called, then i get the > following result. > Max mem alloc = 1200MB > Max mem mapped = 711MB > > My questions are as follows > 1) Why is the difference between two approaches. ? Malloced memory comes from growing the BSS, and mapping anonymous pages into them. Mmap'ed memory comes from selecting an address and mapping pages into it. There is a specific limit on the number of pages you can mmap(), which is administratively enforced. It is both a kernel tuning option, and an administrative "soft" limit. Which you are hitting depends on which is smaller. The "soft" limit is set via your login class in /etc/login.conf. The reason the two approaches give different answers is that you are probably not specifying a mapping address for the mmap()'ed memory that you are using. As a result, malloc() stops giving you anonymous pages as soon as it hits its head on the first mapping that you did with mmap(). Specify a location for the mappings, and the problem will "go away". Specifically, add a "debug" option to your test program, and then print out as "0x%08x" each of the start and end addresses of the memory regions returned by mmap() and malloc() in both cases. The overlap collision should become very obvious to you very quickly. > 2) What exactly is the relationship between mmap malloc and KVA_PAGES. 4G - KVA_PAGES = available user process address space. Or to put it another way: UVA + KVA = 4G That's because 2^32 = 4G, and you are running on a 32 bit architecture. Both mmap() and malloc() allocate portions of the available UVA. > 3) I found that the Maximum mmapped memory = 4GB - KVA_PAGES*PAGE_SIZE > - MAXDSIZ - 134 > What is the 134 value..? It is very consistent for me for various > values of KVA_PAGES and MAXDSIZ, so i am ruling it being just a > coincidence. Overhead for the kernel. If you want to understand where every byte is going, you are going to have to understand everything in locore.s, machdep.c, and the boot loader. Frankly, it's not worth the amount of effort required to do this (I say this having done it once), because the information changes from release to release. -- Terry