Dynamic system memory use

John M McIntosh johnmci at smalltalkconsulting.com
Sat Feb 2 06:55:53 UTC 2002


>On Friday, February 1, 2002, at 08:37 , John M McIntosh wrote:
>
>>>It would.  It has to, since it guarantees to return a chunk of 
>>>sequential memory.  With such a large allocation, you will get 
>>>zero-filled VM-pages.
>>
>>The mmap doesn't do anything until you access the pages, even a 
>>mention of specialized zero fill logic,
>
>'Zero filled' is how the lowest-level Mach routines return the 
>'memory' (really just address space).  This is the same for mmap() 
>as for malloc().  All of that eventually comes down to the Mach 
>function vm_allocate(), at least with anonymous maps...
>
>>  (why wouldn't the VM zero fill logic be ok?, mind it might not 
>>grab zero fill pages from the VM, rather do it for you then and now)
>
>I don't know what you're saying here.

Boy I thinking I'm going to need to locate the reference to the note 
I saw. However was I thought I read is normally you have a demand 
zero page fault occur to fill a requst for another chunk of the mmap 
space. When the page fault occurs the page given to you is filled 
with zeros, I'm not sure if that happens at fault time, or if there 
is a chain of them. The note I read implied that the mmap logic would 
also do the zeroing versus having the VM do it, in retrospect however 
I'm not sure how that would be better or even doable.

The man page for freebsd systat talks about
pages zero filled on demand (`zfod'),
slow (on-the-fly) zero fills percentage (`%slo-z')
which implies the VM is zero pages somewhere/how before you actually 
ask for them.

However to be fair I'll go away and read some source code and the BSD 
internals book I've got in my todo pile before I speak from ignorance 
again.


>Hmm...so implement the grow/shrink primitives essentially as NOPs, 
>except that grow keeps track of the total size and fail at 512MB.

Right now it's very simplistic. Although we mmap 512MB as you point 
out everything is very lazy, pages (real, virtual, or otherwise) 
aren't accounted for anywhere until we actually touch the bytes.

I think the issue is here is if the mmap is better somehow, or is the 
malloc ok?


void * sqAllocateMemory(int minHeapSize, int desiredHeapSize) {
     void * debug;
     OSErr err;
     minHeapSize;

     gHeapSize = gMaxHeapSize;
     debug = mmap( NULL, gMaxHeapSize, PROT_READ | PROT_WRITE, 
MAP_ANON | MAP_SHARED,-1,0);
     if((debug == MAP_FAILED) || (((long) debug) < 0))
         return 0;
     return debug;
}

int sqGrowMemoryBy(int memoryLimit, int delta) {
     if (memoryLimit + delta - (int) memory > gMaxHeapSize)
         return memoryLimit;
   
     gHeapSize += delta;
     return memoryLimit + delta;
}

int sqShrinkMemoryBy(int memoryLimit, int delta) {
     return sqGrowMemoryBy(memoryLimit,0-delta);
}

int sqMemoryExtraBytesLeft(Boolean flag) {
     if (flag)
         return gMaxHeapSize - gHeapSize;
     else
         return 0;
}

-- 
--
===========================================================================
John M. McIntosh <johnmci at smalltalkconsulting.com> 1-800-477-2659
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
===========================================================================



More information about the Squeak-dev mailing list