Commit aaf4099a authored by Austin Clements's avatar Austin Clements

runtime: update malloc.go documentation

The big documentation comment at the top of malloc.go has gotten
woefully out of date. Update it.

Change-Id: Ibdb1bdcfdd707a6dc9db79d0633a36a28882301b
Reviewed-on: https://go-review.googlesource.com/29731Reviewed-by: default avatarHyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: default avatarRick Hudson <rlh@golang.org>
parent f67c9de6
...@@ -2,80 +2,81 @@ ...@@ -2,80 +2,81 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Memory allocator, based on tcmalloc. // Memory allocator.
//
// This was originally based on tcmalloc, but has diverged quite a bit.
// http://goog-perftools.sourceforge.net/doc/tcmalloc.html // http://goog-perftools.sourceforge.net/doc/tcmalloc.html
// The main allocator works in runs of pages. // The main allocator works in runs of pages.
// Small allocation sizes (up to and including 32 kB) are // Small allocation sizes (up to and including 32 kB) are
// rounded to one of about 100 size classes, each of which // rounded to one of about 70 size classes, each of which
// has its own free list of objects of exactly that size. // has its own free set of objects of exactly that size.
// Any free page of memory can be split into a set of objects // Any free page of memory can be split into a set of objects
// of one size class, which are then managed using free list // of one size class, which are then managed using a free bitmap.
// allocators.
// //
// The allocator's data structures are: // The allocator's data structures are:
// //
// FixAlloc: a free-list allocator for fixed-size objects, // fixalloc: a free-list allocator for fixed-size off-heap objects,
// used to manage storage used by the allocator. // used to manage storage used by the allocator.
// MHeap: the malloc heap, managed at page (4096-byte) granularity. // mheap: the malloc heap, managed at page (8192-byte) granularity.
// MSpan: a run of pages managed by the MHeap. // mspan: a run of pages managed by the mheap.
// MCentral: a shared free list for a given size class. // mcentral: collects all spans of a given size class.
// MCache: a per-thread (in Go, per-P) cache for small objects. // mcache: a per-P cache of mspans with free space.
// MStats: allocation statistics. // mstats: allocation statistics.
// //
// Allocating a small object proceeds up a hierarchy of caches: // Allocating a small object proceeds up a hierarchy of caches:
// //
// 1. Round the size up to one of the small size classes // 1. Round the size up to one of the small size classes
// and look in the corresponding MCache free list. // and look in the corresponding mspan in this P's mcache.
// If the list is not empty, allocate an object from it. // Scan the mspan's free bitmap to find a free slot.
// If there is a free slot, allocate it.
// This can all be done without acquiring a lock. // This can all be done without acquiring a lock.
// //
// 2. If the MCache free list is empty, replenish it by // 2. If the mspan has no free slots, obtain a new mspan
// taking a bunch of objects from the MCentral free list. // from the mcentral's list of mspans of the required size
// Moving a bunch amortizes the cost of acquiring the MCentral lock. // class that have free space.
// Obtaining a whole span amortizes the cost of locking
// the mcentral.
// //
// 3. If the MCentral free list is empty, replenish it by // 3. If the mcentral's mspan list is empty, obtain a run
// allocating a run of pages from the MHeap and then // of pages from the mheap to use for the mspan.
// chopping that memory into objects of the given size.
// Allocating many objects amortizes the cost of locking
// the heap.
// //
// 4. If the MHeap is empty or has no page runs large enough, // 4. If the mheap is empty or has no page runs large enough,
// allocate a new group of pages (at least 1MB) from the // allocate a new group of pages (at least 1MB) from the
// operating system. Allocating a large run of pages // operating system. Allocating a large run of pages
// amortizes the cost of talking to the operating system. // amortizes the cost of talking to the operating system.
// //
// Freeing a small object proceeds up the same hierarchy: // Sweeping an mspan and freeing objects on it proceeds up a similar
// hierarchy:
//
// 1. If the mspan is being swept in response to allocation, it
// is returned to the mcache to satisfy the allocation.
// //
// 1. Look up the size class for the object and add it to // 2. Otherwise, if the mspan still has allocated objects in it,
// the MCache free list. // it is placed on the mcentral free list for the mspan's size
// class.
// //
// 2. If the MCache free list is too long or the MCache has // 3. Otherwise, if all objects in the mspan are free, the mspan
// too much memory, return some to the MCentral free lists. // is now "idle", so it is returned to the mheap and no longer
// has a size class.
// This may coalesce it with adjacent idle mspans.
// //
// 3. If all the objects in a given span have returned to // 4. If an mspan remains idle for long enough, return its pages
// the MCentral list, return that span to the page heap. // to the operating system.
// //
// 4. If the heap has too much memory, return some to the // Allocating and freeing a large object uses the mheap
// operating system. // directly, bypassing the mcache and mcentral.
// //
// TODO(rsc): Step 4 is not implemented. // Free object slots in an mspan are zeroed only if mspan.needzero is
// false. If needzero is true, objects are zeroed as they are
// allocated. There are various benefits to delaying zeroing this way:
// //
// Allocating and freeing a large object uses the page heap // 1. Stack frame allocation can avoid zeroing altogether.
// directly, bypassing the MCache and MCentral free lists.
// //
// The small objects on the MCache and MCentral free lists // 2. It exhibits better temporal locality, since the program is
// may or may not be zeroed. They are zeroed if and only if // probably about to write to the memory.
// the second word of the object is zero. A span in the
// page heap is zeroed unless s->needzero is set. When a span
// is allocated to break into small objects, it is zeroed if needed
// and s->needzero is set. There are two main benefits to delaying the
// zeroing this way:
// //
// 1. stack frames allocated from the small object lists // 3. We don't zero pages that never get reused.
// or the page heap can avoid zeroing altogether.
// 2. the cost of zeroing when reusing a small object is
// charged to the mutator, not the garbage collector.
package runtime package runtime
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment