1
0
mirror of https://github.com/golang/go synced 2024-10-05 00:11:21 -06:00
go/src/pkg/runtime/malloc.h

492 lines
16 KiB
C
Raw Normal View History

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Memory allocator, based on tcmalloc.
// http://goog-perftools.sourceforge.net/doc/tcmalloc.html
// The main allocator works in runs of pages.
// Small allocation sizes (up to and including 32 kB) are
// rounded to one of about 100 size classes, each of which
// has its own free list of objects of exactly that size.
// Any free page of memory can be split into a set of objects
// of one size class, which are then managed using free list
// allocators.
//
// The allocator's data structures are:
//
// FixAlloc: a free-list allocator for fixed-size objects,
// used to manage storage used by the allocator.
// MHeap: the malloc heap, managed at page (4096-byte) granularity.
// MSpan: a run of pages managed by the MHeap.
// MCentral: a shared free list for a given size class.
// MCache: a per-thread (in Go, per-M) cache for small objects.
// MStats: allocation statistics.
//
// Allocating a small object proceeds up a hierarchy of caches:
//
// 1. Round the size up to one of the small size classes
// and look in the corresponding MCache free list.
// If the list is not empty, allocate an object from it.
// This can all be done without acquiring a lock.
//
// 2. If the MCache free list is empty, replenish it by
// taking a bunch of objects from the MCentral free list.
// Moving a bunch amortizes the cost of acquiring the MCentral lock.
//
// 3. If the MCentral free list is empty, replenish it by
// allocating a run of pages from the MHeap and then
// chopping that memory into a 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,
// allocate a new group of pages (at least 1MB) from the
// operating system. Allocating a large run of pages
// amortizes the cost of talking to the operating system.
//
// Freeing a small object proceeds up the same hierarchy:
//
// 1. Look up the size class for the object and add it to
// the MCache free list.
//
// 2. If the MCache free list is too long or the MCache has
// too much memory, return some to the MCentral free lists.
//
// 3. If all the objects in a given span have returned to
// the MCentral list, return that span to the page heap.
//
// 4. If the heap has too much memory, return some to the
// operating system.
//
// TODO(rsc): Step 4 is not implemented.
//
// Allocating and freeing a large object uses the page heap
// directly, bypassing the MCache and MCentral free lists.
//
// The small objects on the MCache and MCentral free lists
// may or may not be zeroed. They are zeroed if and only if
// the second word of the object is zero. The spans in the
// page heap are always zeroed. When a span full of objects
// is returned to the page heap, the objects that need to be
// are zeroed first. There are two main benefits to delaying the
// zeroing this way:
//
// 1. stack frames allocated from the small object lists
// can avoid zeroing altogether.
// 2. the cost of zeroing when reusing a small object is
// charged to the mutator, not the garbage collector.
//
// This C code was written with an eye toward translating to Go
// in the future. Methods have the form Type_Method(Type *t, ...).
typedef struct MCentral MCentral;
typedef struct MHeap MHeap;
typedef struct MSpan MSpan;
typedef struct MStats MStats;
typedef struct MLink MLink;
typedef struct MTypes MTypes;
enum
{
PageShift = 12,
PageSize = 1<<PageShift,
PageMask = PageSize - 1,
};
typedef uintptr PageID; // address >> PageShift
enum
{
// Computed constant. The definition of MaxSmallSize and the
// algorithm in msize.c produce some number of different allocation
// size classes. NumSizeClasses is that number. It's needed here
// because there are static arrays of this length; when msize runs its
// size choosing algorithm it double-checks that NumSizeClasses agrees.
NumSizeClasses = 61,
// Tunable constants.
MaxSmallSize = 32<<10,
FixAllocChunk = 128<<10, // Chunk size for FixAlloc
MaxMCacheListLen = 256, // Maximum objects on MCacheList
MaxMCacheSize = 2<<20, // Maximum bytes in one MCache
MaxMHeapList = 1<<(20 - PageShift), // Maximum page length for fixed-size list in MHeap.
HeapAllocChunk = 1<<20, // Chunk size for heap growth
// Number of bits in page to span calculations (4k pages).
// On 64-bit, we limit the arena to 16G, so 22 bits suffices.
// On 32-bit, we don't bother limiting anything: 20 bits for 4G.
#ifdef _64BIT
MHeapMap_Bits = 22,
#else
MHeapMap_Bits = 20,
#endif
// Max number of threads to run garbage collection.
// 2, 3, and 4 are all plausible maximums depending
// on the hardware details of the machine. The garbage
runtime: faster GC mark phase Also bump MaxGcproc to 8. benchmark old ns/op new ns/op delta Parser 3796323000 3763880000 -0.85% Parser-2 3591752500 3518560250 -2.04% Parser-4 3423825250 3334955250 -2.60% Parser-8 3304585500 3267014750 -1.14% Parser-16 3313615750 3286160500 -0.83% Tree 984128500 942501166 -4.23% Tree-2 932564444 883266222 -5.29% Tree-4 835831000 799912777 -4.30% Tree-8 819238500 789717333 -3.73% Tree-16 880837833 837840055 -5.13% Tree2 604698100 579716900 -4.13% Tree2-2 372414500 356765200 -4.20% Tree2-4 187488100 177455900 -5.56% Tree2-8 136315300 102086700 -25.11% Tree2-16 93725900 76705800 -22.18% ParserPause 157441210 166202783 +5.56% ParserPause-2 93842650 85199900 -9.21% ParserPause-4 56844404 53535684 -5.82% ParserPause-8 35739446 30767613 -16.15% ParserPause-16 32718255 27212441 -16.83% TreePause 29610557 29787725 +0.60% TreePause-2 24001659 20674421 -13.86% TreePause-4 15114887 12842781 -15.03% TreePause-8 13128725 10741747 -22.22% TreePause-16 16131360 12506901 -22.47% Tree2Pause 2673350920 2651045280 -0.83% Tree2Pause-2 1796999200 1709350040 -4.88% Tree2Pause-4 1163553320 1090706480 -6.67% Tree2Pause-8 987032520 858916360 -25.11% Tree2Pause-16 864758560 809567480 -6.81% ParserLastPause 280537000 289047000 +3.03% ParserLastPause-2 183030000 166748000 -8.90% ParserLastPause-4 105817000 91552000 -13.48% ParserLastPause-8 65127000 53288000 -18.18% ParserLastPause-16 45258000 38334000 -15.30% TreeLastPause 45072000 51449000 +12.39% TreeLastPause-2 39269000 37866000 -3.57% TreeLastPause-4 23564000 20649000 -12.37% TreeLastPause-8 20881000 15807000 -24.30% TreeLastPause-16 23297000 17309000 -25.70% Tree2LastPause 6046912000 5797120000 -4.13% Tree2LastPause-2 3724034000 3567592000 -4.20% Tree2LastPause-4 1874831000 1774524000 -5.65% Tree2LastPause-8 1363108000 1020809000 -12.79% Tree2LastPause-16 937208000 767019000 -22.18% R=rsc, 0xe2.0x9a.0x9b CC=golang-dev https://golang.org/cl/6223050
2012-05-24 00:55:50 -06:00
// collector scales well to 8 cpus.
MaxGcproc = 8,
};
// Maximum memory allocation size, a hint for callers.
// This must be a #define instead of an enum because it
// is so large.
#ifdef _64BIT
#define MaxMem (16ULL<<30) /* 16 GB */
#else
#define MaxMem ((uintptr)-1)
#endif
// A generic linked list of blocks. (Typically the block is bigger than sizeof(MLink).)
struct MLink
{
MLink *next;
};
// SysAlloc obtains a large chunk of zeroed memory from the
// operating system, typically on the order of a hundred kilobytes
// or a megabyte. If the pointer argument is non-nil, the caller
// wants a mapping there or nowhere.
//
// SysUnused notifies the operating system that the contents
// of the memory region are no longer needed and can be reused
// for other purposes. The program reserves the right to start
// accessing those pages in the future.
//
// SysFree returns it unconditionally; this is only used if
// an out-of-memory error has been detected midway through
// an allocation. It is okay if SysFree is a no-op.
//
// SysReserve reserves address space without allocating memory.
// If the pointer passed to it is non-nil, the caller wants the
// reservation there, but SysReserve can still choose another
// location if that one is unavailable.
//
// SysMap maps previously reserved address space for use.
void* runtime·SysAlloc(uintptr nbytes);
void runtime·SysFree(void *v, uintptr nbytes);
void runtime·SysUnused(void *v, uintptr nbytes);
void runtime·SysMap(void *v, uintptr nbytes);
void* runtime·SysReserve(void *v, uintptr nbytes);
// FixAlloc is a simple free-list allocator for fixed size objects.
// Malloc uses a FixAlloc wrapped around SysAlloc to manages its
// MCache and MSpan objects.
//
// Memory returned by FixAlloc_Alloc is not zeroed.
// The caller is responsible for locking around FixAlloc calls.
// Callers can keep state in the object but the first word is
// smashed by freeing and reallocating.
struct FixAlloc
{
uintptr size;
void *(*alloc)(uintptr);
void (*first)(void *arg, byte *p); // called first time p is returned
void *arg;
MLink *list;
byte *chunk;
uint32 nchunk;
uintptr inuse; // in-use bytes now
uintptr sys; // bytes obtained from system
};
void runtime·FixAlloc_Init(FixAlloc *f, uintptr size, void *(*alloc)(uintptr), void (*first)(void*, byte*), void *arg);
void* runtime·FixAlloc_Alloc(FixAlloc *f);
void runtime·FixAlloc_Free(FixAlloc *f, void *p);
// Statistics.
// Shared with Go: if you edit this structure, also edit type MemStats in mem.go.
struct MStats
{
// General statistics.
uint64 alloc; // bytes allocated and still in use
uint64 total_alloc; // bytes allocated (even if freed)
uint64 sys; // bytes obtained from system (should be sum of xxx_sys below, no locking, approximate)
uint64 nlookup; // number of pointer lookups
uint64 nmalloc; // number of mallocs
uint64 nfree; // number of frees
// Statistics about malloc heap.
// protected by mheap.Lock
uint64 heap_alloc; // bytes allocated and still in use
uint64 heap_sys; // bytes obtained from system
uint64 heap_idle; // bytes in idle spans
uint64 heap_inuse; // bytes in non-idle spans
uint64 heap_released; // bytes released to the OS
uint64 heap_objects; // total number of allocated objects
// Statistics about allocation of low-level fixed-size structures.
// Protected by FixAlloc locks.
uint64 stacks_inuse; // bootstrap stacks
uint64 stacks_sys;
uint64 mspan_inuse; // MSpan structures
uint64 mspan_sys;
uint64 mcache_inuse; // MCache structures
uint64 mcache_sys;
uint64 buckhash_sys; // profiling bucket hash table
// Statistics about garbage collector.
// Protected by stopping the world during GC.
uint64 next_gc; // next GC (in heap_alloc time)
uint64 last_gc; // last GC (in absolute time)
uint64 pause_total_ns;
uint64 pause_ns[256];
uint32 numgc;
bool enablegc;
bool debuggc;
// Statistics about allocation size classes.
struct {
uint32 size;
uint64 nmalloc;
uint64 nfree;
} by_size[NumSizeClasses];
};
#define mstats runtime·memStats /* name shared with Go */
extern MStats mstats;
// Size classes. Computed and initialized by InitSizes.
//
// SizeToClass(0 <= n <= MaxSmallSize) returns the size class,
// 1 <= sizeclass < NumSizeClasses, for n.
// Size class 0 is reserved to mean "not small".
//
// class_to_size[i] = largest size in class i
// class_to_allocnpages[i] = number of pages to allocate when
// making new objects in class i
// class_to_transfercount[i] = number of objects to move when
// taking a bunch of objects out of the central lists
// and putting them in the thread free list.
int32 runtime·SizeToClass(int32);
extern int32 runtime·class_to_size[NumSizeClasses];
extern int32 runtime·class_to_allocnpages[NumSizeClasses];
extern int32 runtime·class_to_transfercount[NumSizeClasses];
extern void runtime·InitSizes(void);
// Per-thread (in Go, per-M) cache for small objects.
// No locking needed because it is per-thread (per-M).
typedef struct MCacheList MCacheList;
struct MCacheList
{
MLink *list;
uint32 nlist;
uint32 nlistmin;
};
struct MCache
{
MCacheList list[NumSizeClasses];
runtime: use uintptr where possible in malloc stats linux/arm OMAP4 pandaboard benchmark old ns/op new ns/op delta BenchmarkBinaryTree17 68723297000 37026214000 -46.12% BenchmarkFannkuch11 34962402000 35958435000 +2.85% BenchmarkGobDecode 137298600 124182150 -9.55% BenchmarkGobEncode 60717160 60006700 -1.17% BenchmarkGzip 5647156000 5550873000 -1.70% BenchmarkGunzip 1196350000 1198670000 +0.19% BenchmarkJSONEncode 863012800 782898000 -9.28% BenchmarkJSONDecode 3312989000 2781800000 -16.03% BenchmarkMandelbrot200 45727540 45703120 -0.05% BenchmarkParse 74781800 59990840 -19.78% BenchmarkRevcomp 140043650 139462300 -0.42% BenchmarkTemplate 6467682000 5832153000 -9.83% benchmark old MB/s new MB/s speedup BenchmarkGobDecode 5.59 6.18 1.11x BenchmarkGobEncode 12.64 12.79 1.01x BenchmarkGzip 3.44 3.50 1.02x BenchmarkGunzip 16.22 16.19 1.00x BenchmarkJSONEncode 2.25 2.48 1.10x BenchmarkJSONDecode 0.59 0.70 1.19x BenchmarkParse 0.77 0.97 1.26x BenchmarkRevcomp 18.15 18.23 1.00x BenchmarkTemplate 0.30 0.33 1.10x darwin/386 core duo benchmark old ns/op new ns/op delta BenchmarkBinaryTree17 10591616577 9678245733 -8.62% BenchmarkFannkuch11 10758473315 10749303846 -0.09% BenchmarkGobDecode 34379785 34121250 -0.75% BenchmarkGobEncode 23523721 23475750 -0.20% BenchmarkGzip 2486191492 2446539568 -1.59% BenchmarkGunzip 444179328 444250293 +0.02% BenchmarkJSONEncode 221138507 219757826 -0.62% BenchmarkJSONDecode 1056034428 1048975133 -0.67% BenchmarkMandelbrot200 19862516 19868346 +0.03% BenchmarkRevcomp 3742610872 3724821662 -0.48% BenchmarkTemplate 960283112 944791517 -1.61% benchmark old MB/s new MB/s speedup BenchmarkGobDecode 22.33 22.49 1.01x BenchmarkGobEncode 32.63 32.69 1.00x BenchmarkGzip 7.80 7.93 1.02x BenchmarkGunzip 43.69 43.68 1.00x BenchmarkJSONEncode 8.77 8.83 1.01x BenchmarkJSONDecode 1.84 1.85 1.01x BenchmarkRevcomp 67.91 68.24 1.00x BenchmarkTemplate 2.02 2.05 1.01x R=rsc, 0xe2.0x9a.0x9b, mirtchovski CC=golang-dev, minux.ma https://golang.org/cl/6297047
2012-06-08 15:35:14 -06:00
uintptr size;
intptr local_cachealloc; // bytes allocated (or freed) from cache since last lock of heap
intptr local_objects; // objects allocated (or freed) from cache since last lock of heap
intptr local_alloc; // bytes allocated (or freed) since last lock of heap
uintptr local_total_alloc; // bytes allocated (even if freed) since last lock of heap
uintptr local_nmalloc; // number of mallocs since last lock of heap
uintptr local_nfree; // number of frees since last lock of heap
uintptr local_nlookup; // number of pointer lookups since last lock of heap
int32 next_sample; // trigger heap sample after allocating this many bytes
// Statistics about allocation size classes since last lock of heap
struct {
runtime: use uintptr where possible in malloc stats linux/arm OMAP4 pandaboard benchmark old ns/op new ns/op delta BenchmarkBinaryTree17 68723297000 37026214000 -46.12% BenchmarkFannkuch11 34962402000 35958435000 +2.85% BenchmarkGobDecode 137298600 124182150 -9.55% BenchmarkGobEncode 60717160 60006700 -1.17% BenchmarkGzip 5647156000 5550873000 -1.70% BenchmarkGunzip 1196350000 1198670000 +0.19% BenchmarkJSONEncode 863012800 782898000 -9.28% BenchmarkJSONDecode 3312989000 2781800000 -16.03% BenchmarkMandelbrot200 45727540 45703120 -0.05% BenchmarkParse 74781800 59990840 -19.78% BenchmarkRevcomp 140043650 139462300 -0.42% BenchmarkTemplate 6467682000 5832153000 -9.83% benchmark old MB/s new MB/s speedup BenchmarkGobDecode 5.59 6.18 1.11x BenchmarkGobEncode 12.64 12.79 1.01x BenchmarkGzip 3.44 3.50 1.02x BenchmarkGunzip 16.22 16.19 1.00x BenchmarkJSONEncode 2.25 2.48 1.10x BenchmarkJSONDecode 0.59 0.70 1.19x BenchmarkParse 0.77 0.97 1.26x BenchmarkRevcomp 18.15 18.23 1.00x BenchmarkTemplate 0.30 0.33 1.10x darwin/386 core duo benchmark old ns/op new ns/op delta BenchmarkBinaryTree17 10591616577 9678245733 -8.62% BenchmarkFannkuch11 10758473315 10749303846 -0.09% BenchmarkGobDecode 34379785 34121250 -0.75% BenchmarkGobEncode 23523721 23475750 -0.20% BenchmarkGzip 2486191492 2446539568 -1.59% BenchmarkGunzip 444179328 444250293 +0.02% BenchmarkJSONEncode 221138507 219757826 -0.62% BenchmarkJSONDecode 1056034428 1048975133 -0.67% BenchmarkMandelbrot200 19862516 19868346 +0.03% BenchmarkRevcomp 3742610872 3724821662 -0.48% BenchmarkTemplate 960283112 944791517 -1.61% benchmark old MB/s new MB/s speedup BenchmarkGobDecode 22.33 22.49 1.01x BenchmarkGobEncode 32.63 32.69 1.00x BenchmarkGzip 7.80 7.93 1.02x BenchmarkGunzip 43.69 43.68 1.00x BenchmarkJSONEncode 8.77 8.83 1.01x BenchmarkJSONDecode 1.84 1.85 1.01x BenchmarkRevcomp 67.91 68.24 1.00x BenchmarkTemplate 2.02 2.05 1.01x R=rsc, 0xe2.0x9a.0x9b, mirtchovski CC=golang-dev, minux.ma https://golang.org/cl/6297047
2012-06-08 15:35:14 -06:00
uintptr nmalloc;
uintptr nfree;
} local_by_size[NumSizeClasses];
};
void* runtime·MCache_Alloc(MCache *c, int32 sizeclass, uintptr size, int32 zeroed);
void runtime·MCache_Free(MCache *c, void *p, int32 sizeclass, uintptr size);
void runtime·MCache_ReleaseAll(MCache *c);
// MTypes describes the types of blocks allocated within a span.
// The compression field describes the layout of the data.
//
// MTypes_Empty:
// All blocks are free, or no type information is available for
// allocated blocks.
// The data field has no meaning.
// MTypes_Single:
// The span contains just one block.
// The data field holds the type information.
// The sysalloc field has no meaning.
// MTypes_Words:
// The span contains multiple blocks.
// The data field points to an array of type [NumBlocks]uintptr,
// and each element of the array holds the type of the corresponding
// block.
// MTypes_Bytes:
// The span contains at most seven different types of blocks.
// The data field points to the following structure:
// struct {
// type [8]uintptr // type[0] is always 0
// index [NumBlocks]byte
// }
// The type of the i-th block is: data.type[data.index[i]]
enum
{
MTypes_Empty = 0,
MTypes_Single = 1,
MTypes_Words = 2,
MTypes_Bytes = 3,
};
struct MTypes
{
byte compression; // one of MTypes_*
bool sysalloc; // whether (void*)data is from runtime·SysAlloc
uintptr data;
};
// An MSpan is a run of pages.
enum
{
MSpanInUse = 0,
MSpanFree,
MSpanListHead,
MSpanDead,
};
struct MSpan
{
MSpan *next; // in a span linked list
MSpan *prev; // in a span linked list
PageID start; // starting page number
uintptr npages; // number of pages in span
MLink *freelist; // list of free objects
uint32 ref; // number of allocated objects in this span
int32 sizeclass; // size class
uintptr elemsize; // computed from sizeclass or from npages
uint32 state; // MSpanInUse etc
int64 unusedsince; // First time spotted by GC in MSpanFree state
uintptr npreleased; // number of pages released to the OS
byte *limit; // end of data in span
MTypes types; // types of allocated objects in this span
};
void runtime·MSpan_Init(MSpan *span, PageID start, uintptr npages);
// Every MSpan is in one doubly-linked list,
// either one of the MHeap's free lists or one of the
// MCentral's span lists. We use empty MSpan structures as list heads.
void runtime·MSpanList_Init(MSpan *list);
bool runtime·MSpanList_IsEmpty(MSpan *list);
void runtime·MSpanList_Insert(MSpan *list, MSpan *span);
void runtime·MSpanList_Remove(MSpan *span); // from whatever list it is in
// Central list of free objects of a given size.
struct MCentral
{
Lock;
int32 sizeclass;
MSpan nonempty;
MSpan empty;
int32 nfree;
};
void runtime·MCentral_Init(MCentral *c, int32 sizeclass);
int32 runtime·MCentral_AllocList(MCentral *c, int32 n, MLink **first);
void runtime·MCentral_FreeList(MCentral *c, int32 n, MLink *first);
runtime: speedup GC sweep phase (batch free) benchmark old ns/op new ns/op delta garbage.BenchmarkParser 4370050250 3779668750 -13.51% garbage.BenchmarkParser-2 3713087000 3628771500 -2.27% garbage.BenchmarkParser-4 3519755250 3406349750 -3.22% garbage.BenchmarkParser-8 3386627750 3319144000 -1.99% garbage.BenchmarkTree 493585529 408102411 -17.32% garbage.BenchmarkTree-2 500487176 402285176 -19.62% garbage.BenchmarkTree-4 473238882 361484058 -23.61% garbage.BenchmarkTree-8 486977823 368334823 -24.36% garbage.BenchmarkTree2 31446600 31203200 -0.77% garbage.BenchmarkTree2-2 21469000 21077900 -1.82% garbage.BenchmarkTree2-4 11007600 10899100 -0.99% garbage.BenchmarkTree2-8 7692400 7032600 -8.58% garbage.BenchmarkParserPause 241863263 163249450 -32.50% garbage.BenchmarkParserPause-2 120135418 112981575 -5.95% garbage.BenchmarkParserPause-4 83411552 64580700 -22.58% garbage.BenchmarkParserPause-8 51870697 42207244 -18.63% garbage.BenchmarkTreePause 20940474 13147011 -37.22% garbage.BenchmarkTreePause-2 20115124 11146715 -44.59% garbage.BenchmarkTreePause-4 17217584 7486327 -56.52% garbage.BenchmarkTreePause-8 18258845 7400871 -59.47% garbage.BenchmarkTree2Pause 174067190 172674190 -0.80% garbage.BenchmarkTree2Pause-2 131175809 130615761 -0.43% garbage.BenchmarkTree2Pause-4 95406666 93972047 -1.50% garbage.BenchmarkTree2Pause-8 86056095 85334952 -0.84% garbage.BenchmarkParserLastPause 329932000 324790000 -1.56% garbage.BenchmarkParserLastPause-2 209383000 210456000 +0.51% garbage.BenchmarkParserLastPause-4 113981000 112921000 -0.93% garbage.BenchmarkParserLastPause-8 77967000 76625000 -1.72% garbage.BenchmarkTreeLastPause 29752000 18444000 -38.01% garbage.BenchmarkTreeLastPause-2 24274000 14766000 -39.17% garbage.BenchmarkTreeLastPause-4 19565000 8726000 -55.40% garbage.BenchmarkTreeLastPause-8 21956000 10530000 -52.04% garbage.BenchmarkTree2LastPause 314411000 311945000 -0.78% garbage.BenchmarkTree2LastPause-2 214641000 210836000 -1.77% garbage.BenchmarkTree2LastPause-4 110024000 108943000 -0.98% garbage.BenchmarkTree2LastPause-8 76873000 70263000 -8.60% R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5991049
2012-04-12 02:01:24 -06:00
void runtime·MCentral_FreeSpan(MCentral *c, MSpan *s, int32 n, MLink *start, MLink *end);
// Main malloc heap.
// The heap itself is the "free[]" and "large" arrays,
// but all the other global data is here too.
struct MHeap
{
Lock;
MSpan free[MaxMHeapList]; // free lists of given length
MSpan large; // free lists length >= MaxMHeapList
runtime: preparation for parallel GC make MHeap.allspans an array instead on a linked-list, it's required for parallel for benchmark old ns/op new ns/op delta garbage.BenchmarkTree 494435529 487962705 -1.31% garbage.BenchmarkTree-2 499652705 485358000 -2.86% garbage.BenchmarkTree-4 468482117 454093117 -3.07% garbage.BenchmarkTree-8 488533235 471872470 -3.41% garbage.BenchmarkTree-16 507835176 492558470 -3.01% garbage.BenchmarkTree2 31453900 31404300 -0.16% garbage.BenchmarkTree2-2 21440600 21477000 +0.17% garbage.BenchmarkTree2-4 10982000 11117400 +1.23% garbage.BenchmarkTree2-8 7544700 7456700 -1.17% garbage.BenchmarkTree2-16 7049500 6805700 -3.46% garbage.BenchmarkParser 4448988000 4453264000 +0.10% garbage.BenchmarkParser-2 4086045000 4057948000 -0.69% garbage.BenchmarkParser-4 3677365000 3661246000 -0.44% garbage.BenchmarkParser-8 3517253000 3540190000 +0.65% garbage.BenchmarkParser-16 3506562000 3463478000 -1.23% garbage.BenchmarkTreePause 20969784 21100238 +0.62% garbage.BenchmarkTreePause-2 20215875 20139572 -0.38% garbage.BenchmarkTreePause-4 17240709 16683624 -3.23% garbage.BenchmarkTreePause-8 18196386 17639306 -3.06% garbage.BenchmarkTreePause-16 20621158 20215056 -1.97% garbage.BenchmarkTree2Pause 173992142 173872380 -0.07% garbage.BenchmarkTree2Pause-2 131281904 131366666 +0.06% garbage.BenchmarkTree2Pause-4 93484952 95109619 +1.74% garbage.BenchmarkTree2Pause-8 88950523 86533333 -2.72% garbage.BenchmarkTree2Pause-16 86071238 84089190 -2.30% garbage.BenchmarkParserPause 135815000 135255952 -0.41% garbage.BenchmarkParserPause-2 92691523 91451428 -1.34% garbage.BenchmarkParserPause-4 53392190 51611904 -3.33% garbage.BenchmarkParserPause-8 36059523 35116666 -2.61% garbage.BenchmarkParserPause-16 30174300 27340600 -9.39% garbage.BenchmarkTreeLastPause 28420000 29142000 +2.54% garbage.BenchmarkTreeLastPause-2 23514000 26779000 +13.89% garbage.BenchmarkTreeLastPause-4 21773000 18660000 -14.30% garbage.BenchmarkTreeLastPause-8 24072000 21276000 -11.62% garbage.BenchmarkTreeLastPause-16 25149000 28541000 +13.49% garbage.BenchmarkTree2LastPause 314491000 313982000 -0.16% garbage.BenchmarkTree2LastPause-2 214363000 214715000 +0.16% garbage.BenchmarkTree2LastPause-4 109778000 111115000 +1.22% garbage.BenchmarkTree2LastPause-8 75390000 74522000 -1.15% garbage.BenchmarkTree2LastPause-16 70333000 67880000 -3.49% garbage.BenchmarkParserLastPause 327247000 326815000 -0.13% garbage.BenchmarkParserLastPause-2 217039000 212529000 -2.08% garbage.BenchmarkParserLastPause-4 119722000 111535000 -6.84% garbage.BenchmarkParserLastPause-8 70806000 69613000 -1.68% garbage.BenchmarkParserLastPause-16 62813000 48009000 -23.57% R=rsc, r CC=golang-dev https://golang.org/cl/5992055
2012-04-09 03:05:43 -06:00
MSpan **allspans;
uint32 nspan;
uint32 nspancap;
// span lookup
MSpan *map[1<<MHeapMap_Bits];
// range of addresses we might see in the heap
byte *bitmap;
uintptr bitmap_mapped;
byte *arena_start;
byte *arena_used;
byte *arena_end;
// central free lists for small size classes.
// the union makes sure that the MCentrals are
// spaced CacheLineSize bytes apart, so that each MCentral.Lock
// gets its own cache line.
union {
MCentral;
byte pad[CacheLineSize];
} central[NumSizeClasses];
FixAlloc spanalloc; // allocator for Span*
FixAlloc cachealloc; // allocator for MCache*
};
extern MHeap runtime·mheap;
void runtime·MHeap_Init(MHeap *h, void *(*allocator)(uintptr));
MSpan* runtime·MHeap_Alloc(MHeap *h, uintptr npage, int32 sizeclass, int32 acct, int32 zeroed);
void runtime·MHeap_Free(MHeap *h, MSpan *s, int32 acct);
MSpan* runtime·MHeap_Lookup(MHeap *h, void *v);
MSpan* runtime·MHeap_LookupMaybe(MHeap *h, void *v);
void runtime·MGetSizeClassInfo(int32 sizeclass, uintptr *size, int32 *npages, int32 *nobj);
void* runtime·MHeap_SysAlloc(MHeap *h, uintptr n);
void runtime·MHeap_MapBits(MHeap *h);
void runtime·MHeap_Scavenger(void);
void* runtime·mallocgc(uintptr size, uint32 flag, int32 dogc, int32 zeroed);
int32 runtime·mlookup(void *v, byte **base, uintptr *size, MSpan **s);
void runtime·gc(int32 force);
void runtime·markallocated(void *v, uintptr n, bool noptr);
void runtime·checkallocated(void *v, uintptr n);
void runtime·markfreed(void *v, uintptr n);
void runtime·checkfreed(void *v, uintptr n);
int32 runtime·checking;
void runtime·markspan(void *v, uintptr size, uintptr n, bool leftover);
void runtime·unmarkspan(void *v, uintptr size);
bool runtime·blockspecial(void*);
void runtime·setblockspecial(void*, bool);
void runtime·purgecachedstats(MCache*);
void* runtime·cnew(Type*);
void runtime·settype(void*, uintptr);
void runtime·settype_flush(M*, bool);
void runtime·settype_sysfree(MSpan*);
uintptr runtime·gettype(void*);
enum
{
// flags to malloc
FlagNoPointers = 1<<0, // no pointers here
FlagNoProfiling = 1<<1, // must not profile
FlagNoGC = 1<<2, // must not free or scan for pointers
};
void runtime·MProf_Malloc(void*, uintptr);
void runtime·MProf_Free(void*, uintptr);
void runtime·MProf_GC(void);
int32 runtime·gcprocs(void);
void runtime·helpgc(int32 nproc);
void runtime·gchelper(void);
bool runtime·getfinalizer(void *p, bool del, void (**fn)(void*), uintptr *nret);
void runtime·walkfintab(void (*fn)(void*));
enum
{
TypeInfo_SingleObject = 0,
TypeInfo_Array = 1,
TypeInfo_Map = 2,
// Enables type information at the end of blocks allocated from heap
DebugTypeAtBlockEnd = 0,
};
// defined in mgc0.go
void runtime·gc_m_ptr(Eface*);