github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/runtime/malloc.h (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Memory allocator, based on tcmalloc.
     6  // http://goog-perftools.sourceforge.net/doc/tcmalloc.html
     7  
     8  // The main allocator works in runs of pages.
     9  // Small allocation sizes (up to and including 32 kB) are
    10  // rounded to one of about 100 size classes, each of which
    11  // has its own free list of objects of exactly that size.
    12  // Any free page of memory can be split into a set of objects
    13  // of one size class, which are then managed using free list
    14  // allocators.
    15  //
    16  // The allocator's data structures are:
    17  //
    18  //	FixAlloc: a free-list allocator for fixed-size objects,
    19  //		used to manage storage used by the allocator.
    20  //	MHeap: the malloc heap, managed at page (4096-byte) granularity.
    21  //	MSpan: a run of pages managed by the MHeap.
    22  //	MCentral: a shared free list for a given size class.
    23  //	MCache: a per-thread (in Go, per-M) cache for small objects.
    24  //	MStats: allocation statistics.
    25  //
    26  // Allocating a small object proceeds up a hierarchy of caches:
    27  //
    28  //	1. Round the size up to one of the small size classes
    29  //	   and look in the corresponding MCache free list.
    30  //	   If the list is not empty, allocate an object from it.
    31  //	   This can all be done without acquiring a lock.
    32  //
    33  //	2. If the MCache free list is empty, replenish it by
    34  //	   taking a bunch of objects from the MCentral free list.
    35  //	   Moving a bunch amortizes the cost of acquiring the MCentral lock.
    36  //
    37  //	3. If the MCentral free list is empty, replenish it by
    38  //	   allocating a run of pages from the MHeap and then
    39  //	   chopping that memory into a objects of the given size.
    40  //	   Allocating many objects amortizes the cost of locking
    41  //	   the heap.
    42  //
    43  //	4. If the MHeap is empty or has no page runs large enough,
    44  //	   allocate a new group of pages (at least 1MB) from the
    45  //	   operating system.  Allocating a large run of pages
    46  //	   amortizes the cost of talking to the operating system.
    47  //
    48  // Freeing a small object proceeds up the same hierarchy:
    49  //
    50  //	1. Look up the size class for the object and add it to
    51  //	   the MCache free list.
    52  //
    53  //	2. If the MCache free list is too long or the MCache has
    54  //	   too much memory, return some to the MCentral free lists.
    55  //
    56  //	3. If all the objects in a given span have returned to
    57  //	   the MCentral list, return that span to the page heap.
    58  //
    59  //	4. If the heap has too much memory, return some to the
    60  //	   operating system.
    61  //
    62  //	TODO(rsc): Step 4 is not implemented.
    63  //
    64  // Allocating and freeing a large object uses the page heap
    65  // directly, bypassing the MCache and MCentral free lists.
    66  //
    67  // The small objects on the MCache and MCentral free lists
    68  // may or may not be zeroed.  They are zeroed if and only if
    69  // the second word of the object is zero.  The spans in the
    70  // page heap are always zeroed.  When a span full of objects
    71  // is returned to the page heap, the objects that need to be
    72  // are zeroed first.  There are two main benefits to delaying the
    73  // zeroing this way:
    74  //
    75  //	1. stack frames allocated from the small object lists
    76  //	   can avoid zeroing altogether.
    77  //	2. the cost of zeroing when reusing a small object is
    78  //	   charged to the mutator, not the garbage collector.
    79  //
    80  // This C code was written with an eye toward translating to Go
    81  // in the future.  Methods have the form Type_Method(Type *t, ...).
    82  
    83  typedef struct MCentral	MCentral;
    84  typedef struct MHeap	MHeap;
    85  typedef struct MSpan	MSpan;
    86  typedef struct MStats	MStats;
    87  typedef struct MLink	MLink;
    88  typedef struct MTypes	MTypes;
    89  typedef struct GCStats	GCStats;
    90  
    91  enum
    92  {
    93  	PageShift	= 12,
    94  	PageSize	= 1<<PageShift,
    95  	PageMask	= PageSize - 1,
    96  };
    97  typedef	uintptr	PageID;		// address >> PageShift
    98  
    99  enum
   100  {
   101  	// Computed constant.  The definition of MaxSmallSize and the
   102  	// algorithm in msize.c produce some number of different allocation
   103  	// size classes.  NumSizeClasses is that number.  It's needed here
   104  	// because there are static arrays of this length; when msize runs its
   105  	// size choosing algorithm it double-checks that NumSizeClasses agrees.
   106  	NumSizeClasses = 61,
   107  
   108  	// Tunable constants.
   109  	MaxSmallSize = 32<<10,
   110  
   111  	FixAllocChunk = 128<<10,	// Chunk size for FixAlloc
   112  	MaxMCacheListLen = 256,		// Maximum objects on MCacheList
   113  	MaxMCacheSize = 2<<20,		// Maximum bytes in one MCache
   114  	MaxMHeapList = 1<<(20 - PageShift),	// Maximum page length for fixed-size list in MHeap.
   115  	HeapAllocChunk = 1<<20,		// Chunk size for heap growth
   116  
   117  	// Number of bits in page to span calculations (4k pages).
   118  	// On Windows 64-bit we limit the arena to 32GB or 35 bits (see below for reason).
   119  	// On other 64-bit platforms, we limit the arena to 128GB, or 37 bits.
   120  	// On 32-bit, we don't bother limiting anything, so we use the full 32-bit address.
   121  #ifdef _64BIT
   122  #ifdef GOOS_windows
   123  	// Windows counts memory used by page table into committed memory
   124  	// of the process, so we can't reserve too much memory.
   125  	// See http://golang.org/issue/5402 and http://golang.org/issue/5236.
   126  	MHeapMap_Bits = 35 - PageShift,
   127  #else
   128  	MHeapMap_Bits = 37 - PageShift,
   129  #endif
   130  #else
   131  	MHeapMap_Bits = 32 - PageShift,
   132  #endif
   133  
   134  	// Max number of threads to run garbage collection.
   135  	// 2, 3, and 4 are all plausible maximums depending
   136  	// on the hardware details of the machine.  The garbage
   137  	// collector scales well to 8 cpus.
   138  	MaxGcproc = 8,
   139  };
   140  
   141  // Maximum memory allocation size, a hint for callers.
   142  // This must be a #define instead of an enum because it
   143  // is so large.
   144  #ifdef _64BIT
   145  #define	MaxMem	(1ULL<<(MHeapMap_Bits+PageShift))	/* 128 GB or 32 GB */
   146  #else
   147  #define	MaxMem	((uintptr)-1)
   148  #endif
   149  
   150  // A generic linked list of blocks.  (Typically the block is bigger than sizeof(MLink).)
   151  struct MLink
   152  {
   153  	MLink *next;
   154  };
   155  
   156  // SysAlloc obtains a large chunk of zeroed memory from the
   157  // operating system, typically on the order of a hundred kilobytes
   158  // or a megabyte.  If the pointer argument is non-nil, the caller
   159  // wants a mapping there or nowhere.
   160  //
   161  // SysUnused notifies the operating system that the contents
   162  // of the memory region are no longer needed and can be reused
   163  // for other purposes.  The program reserves the right to start
   164  // accessing those pages in the future.
   165  //
   166  // SysFree returns it unconditionally; this is only used if
   167  // an out-of-memory error has been detected midway through
   168  // an allocation.  It is okay if SysFree is a no-op.
   169  //
   170  // SysReserve reserves address space without allocating memory.
   171  // If the pointer passed to it is non-nil, the caller wants the
   172  // reservation there, but SysReserve can still choose another
   173  // location if that one is unavailable.
   174  //
   175  // SysMap maps previously reserved address space for use.
   176  
   177  void*	runtime·SysAlloc(uintptr nbytes);
   178  void	runtime·SysFree(void *v, uintptr nbytes);
   179  void	runtime·SysUnused(void *v, uintptr nbytes);
   180  void	runtime·SysMap(void *v, uintptr nbytes);
   181  void*	runtime·SysReserve(void *v, uintptr nbytes);
   182  
   183  // FixAlloc is a simple free-list allocator for fixed size objects.
   184  // Malloc uses a FixAlloc wrapped around SysAlloc to manages its
   185  // MCache and MSpan objects.
   186  //
   187  // Memory returned by FixAlloc_Alloc is not zeroed.
   188  // The caller is responsible for locking around FixAlloc calls.
   189  // Callers can keep state in the object but the first word is
   190  // smashed by freeing and reallocating.
   191  struct FixAlloc
   192  {
   193  	uintptr size;
   194  	void *(*alloc)(uintptr);
   195  	void (*first)(void *arg, byte *p);	// called first time p is returned
   196  	void *arg;
   197  	MLink *list;
   198  	byte *chunk;
   199  	uint32 nchunk;
   200  	uintptr inuse;	// in-use bytes now
   201  	uintptr sys;	// bytes obtained from system
   202  };
   203  
   204  void	runtime·FixAlloc_Init(FixAlloc *f, uintptr size, void *(*alloc)(uintptr), void (*first)(void*, byte*), void *arg);
   205  void*	runtime·FixAlloc_Alloc(FixAlloc *f);
   206  void	runtime·FixAlloc_Free(FixAlloc *f, void *p);
   207  
   208  
   209  // Statistics.
   210  // Shared with Go: if you edit this structure, also edit type MemStats in mem.go.
   211  struct MStats
   212  {
   213  	// General statistics.
   214  	uint64	alloc;		// bytes allocated and still in use
   215  	uint64	total_alloc;	// bytes allocated (even if freed)
   216  	uint64	sys;		// bytes obtained from system (should be sum of xxx_sys below, no locking, approximate)
   217  	uint64	nlookup;	// number of pointer lookups
   218  	uint64	nmalloc;	// number of mallocs
   219  	uint64	nfree;  // number of frees
   220  
   221  	// Statistics about malloc heap.
   222  	// protected by mheap.Lock
   223  	uint64	heap_alloc;	// bytes allocated and still in use
   224  	uint64	heap_sys;	// bytes obtained from system
   225  	uint64	heap_idle;	// bytes in idle spans
   226  	uint64	heap_inuse;	// bytes in non-idle spans
   227  	uint64	heap_released;	// bytes released to the OS
   228  	uint64	heap_objects;	// total number of allocated objects
   229  
   230  	// Statistics about allocation of low-level fixed-size structures.
   231  	// Protected by FixAlloc locks.
   232  	uint64	stacks_inuse;	// bootstrap stacks
   233  	uint64	stacks_sys;
   234  	uint64	mspan_inuse;	// MSpan structures
   235  	uint64	mspan_sys;
   236  	uint64	mcache_inuse;	// MCache structures
   237  	uint64	mcache_sys;
   238  	uint64	buckhash_sys;	// profiling bucket hash table
   239  
   240  	// Statistics about garbage collector.
   241  	// Protected by mheap or stopping the world during GC.
   242  	uint64	next_gc;	// next GC (in heap_alloc time)
   243  	uint64  last_gc;	// last GC (in absolute time)
   244  	uint64	pause_total_ns;
   245  	uint64	pause_ns[256];
   246  	uint32	numgc;
   247  	bool	enablegc;
   248  	bool	debuggc;
   249  
   250  	// Statistics about allocation size classes.
   251  	struct {
   252  		uint32 size;
   253  		uint64 nmalloc;
   254  		uint64 nfree;
   255  	} by_size[NumSizeClasses];
   256  };
   257  
   258  #define mstats runtime·memStats	/* name shared with Go */
   259  extern MStats mstats;
   260  
   261  // Size classes.  Computed and initialized by InitSizes.
   262  //
   263  // SizeToClass(0 <= n <= MaxSmallSize) returns the size class,
   264  //	1 <= sizeclass < NumSizeClasses, for n.
   265  //	Size class 0 is reserved to mean "not small".
   266  //
   267  // class_to_size[i] = largest size in class i
   268  // class_to_allocnpages[i] = number of pages to allocate when
   269  //	making new objects in class i
   270  // class_to_transfercount[i] = number of objects to move when
   271  //	taking a bunch of objects out of the central lists
   272  //	and putting them in the thread free list.
   273  
   274  int32	runtime·SizeToClass(int32);
   275  extern	int32	runtime·class_to_size[NumSizeClasses];
   276  extern	int32	runtime·class_to_allocnpages[NumSizeClasses];
   277  extern	int32	runtime·class_to_transfercount[NumSizeClasses];
   278  extern	void	runtime·InitSizes(void);
   279  
   280  
   281  // Per-thread (in Go, per-M) cache for small objects.
   282  // No locking needed because it is per-thread (per-M).
   283  typedef struct MCacheList MCacheList;
   284  struct MCacheList
   285  {
   286  	MLink *list;
   287  	uint32 nlist;
   288  	uint32 nlistmin;
   289  };
   290  
   291  struct MCache
   292  {
   293  	MCacheList list[NumSizeClasses];
   294  	uintptr size;
   295  	intptr local_cachealloc;	// bytes allocated (or freed) from cache since last lock of heap
   296  	intptr local_objects;	// objects allocated (or freed) from cache since last lock of heap
   297  	intptr local_alloc;	// bytes allocated (or freed) since last lock of heap
   298  	uintptr local_total_alloc;	// bytes allocated (even if freed) since last lock of heap
   299  	uintptr local_nmalloc;	// number of mallocs since last lock of heap
   300  	uintptr local_nfree;	// number of frees since last lock of heap
   301  	uintptr local_nlookup;	// number of pointer lookups since last lock of heap
   302  	int32 next_sample;	// trigger heap sample after allocating this many bytes
   303  	// Statistics about allocation size classes since last lock of heap
   304  	struct {
   305  		uintptr nmalloc;
   306  		uintptr nfree;
   307  	} local_by_size[NumSizeClasses];
   308  
   309  };
   310  
   311  void*	runtime·MCache_Alloc(MCache *c, int32 sizeclass, uintptr size, int32 zeroed);
   312  void	runtime·MCache_Free(MCache *c, void *p, int32 sizeclass, uintptr size);
   313  void	runtime·MCache_ReleaseAll(MCache *c);
   314  
   315  // MTypes describes the types of blocks allocated within a span.
   316  // The compression field describes the layout of the data.
   317  //
   318  // MTypes_Empty:
   319  //     All blocks are free, or no type information is available for
   320  //     allocated blocks.
   321  //     The data field has no meaning.
   322  // MTypes_Single:
   323  //     The span contains just one block.
   324  //     The data field holds the type information.
   325  //     The sysalloc field has no meaning.
   326  // MTypes_Words:
   327  //     The span contains multiple blocks.
   328  //     The data field points to an array of type [NumBlocks]uintptr,
   329  //     and each element of the array holds the type of the corresponding
   330  //     block.
   331  // MTypes_Bytes:
   332  //     The span contains at most seven different types of blocks.
   333  //     The data field points to the following structure:
   334  //         struct {
   335  //             type  [8]uintptr       // type[0] is always 0
   336  //             index [NumBlocks]byte
   337  //         }
   338  //     The type of the i-th block is: data.type[data.index[i]]
   339  enum
   340  {
   341  	MTypes_Empty = 0,
   342  	MTypes_Single = 1,
   343  	MTypes_Words = 2,
   344  	MTypes_Bytes = 3,
   345  };
   346  struct MTypes
   347  {
   348  	byte	compression;	// one of MTypes_*
   349  	bool	sysalloc;	// whether (void*)data is from runtime·SysAlloc
   350  	uintptr	data;
   351  };
   352  
   353  // An MSpan is a run of pages.
   354  enum
   355  {
   356  	MSpanInUse = 0,
   357  	MSpanFree,
   358  	MSpanListHead,
   359  	MSpanDead,
   360  };
   361  struct MSpan
   362  {
   363  	MSpan	*next;		// in a span linked list
   364  	MSpan	*prev;		// in a span linked list
   365  	PageID	start;		// starting page number
   366  	uintptr	npages;		// number of pages in span
   367  	MLink	*freelist;	// list of free objects
   368  	uint32	ref;		// number of allocated objects in this span
   369  	int32	sizeclass;	// size class
   370  	uintptr	elemsize;	// computed from sizeclass or from npages
   371  	uint32	state;		// MSpanInUse etc
   372  	int64   unusedsince;	// First time spotted by GC in MSpanFree state
   373  	uintptr npreleased;	// number of pages released to the OS
   374  	byte	*limit;		// end of data in span
   375  	MTypes	types;		// types of allocated objects in this span
   376  };
   377  
   378  void	runtime·MSpan_Init(MSpan *span, PageID start, uintptr npages);
   379  
   380  // Every MSpan is in one doubly-linked list,
   381  // either one of the MHeap's free lists or one of the
   382  // MCentral's span lists.  We use empty MSpan structures as list heads.
   383  void	runtime·MSpanList_Init(MSpan *list);
   384  bool	runtime·MSpanList_IsEmpty(MSpan *list);
   385  void	runtime·MSpanList_Insert(MSpan *list, MSpan *span);
   386  void	runtime·MSpanList_Remove(MSpan *span);	// from whatever list it is in
   387  
   388  
   389  // Central list of free objects of a given size.
   390  struct MCentral
   391  {
   392  	Lock;
   393  	int32 sizeclass;
   394  	MSpan nonempty;
   395  	MSpan empty;
   396  	int32 nfree;
   397  };
   398  
   399  void	runtime·MCentral_Init(MCentral *c, int32 sizeclass);
   400  int32	runtime·MCentral_AllocList(MCentral *c, int32 n, MLink **first);
   401  void	runtime·MCentral_FreeList(MCentral *c, int32 n, MLink *first);
   402  void	runtime·MCentral_FreeSpan(MCentral *c, MSpan *s, int32 n, MLink *start, MLink *end);
   403  
   404  // Main malloc heap.
   405  // The heap itself is the "free[]" and "large" arrays,
   406  // but all the other global data is here too.
   407  struct MHeap
   408  {
   409  	Lock;
   410  	MSpan free[MaxMHeapList];	// free lists of given length
   411  	MSpan large;			// free lists length >= MaxMHeapList
   412  	MSpan **allspans;
   413  	uint32	nspan;
   414  	uint32	nspancap;
   415  
   416  	// span lookup
   417  	MSpan *map[1<<MHeapMap_Bits];
   418  
   419  	// range of addresses we might see in the heap
   420  	byte *bitmap;
   421  	uintptr bitmap_mapped;
   422  	byte *arena_start;
   423  	byte *arena_used;
   424  	byte *arena_end;
   425  
   426  	// central free lists for small size classes.
   427  	// the padding makes sure that the MCentrals are
   428  	// spaced CacheLineSize bytes apart, so that each MCentral.Lock
   429  	// gets its own cache line.
   430  	struct {
   431  		MCentral;
   432  		byte pad[CacheLineSize];
   433  	} central[NumSizeClasses];
   434  
   435  	FixAlloc spanalloc;	// allocator for Span*
   436  	FixAlloc cachealloc;	// allocator for MCache*
   437  };
   438  extern MHeap *runtime·mheap;
   439  
   440  void	runtime·MHeap_Init(MHeap *h, void *(*allocator)(uintptr));
   441  MSpan*	runtime·MHeap_Alloc(MHeap *h, uintptr npage, int32 sizeclass, int32 acct, int32 zeroed);
   442  void	runtime·MHeap_Free(MHeap *h, MSpan *s, int32 acct);
   443  MSpan*	runtime·MHeap_Lookup(MHeap *h, void *v);
   444  MSpan*	runtime·MHeap_LookupMaybe(MHeap *h, void *v);
   445  void	runtime·MGetSizeClassInfo(int32 sizeclass, uintptr *size, int32 *npages, int32 *nobj);
   446  void*	runtime·MHeap_SysAlloc(MHeap *h, uintptr n);
   447  void	runtime·MHeap_MapBits(MHeap *h);
   448  void	runtime·MHeap_Scavenger(void);
   449  
   450  void*	runtime·mallocgc(uintptr size, uint32 flag, int32 dogc, int32 zeroed);
   451  int32	runtime·mlookup(void *v, byte **base, uintptr *size, MSpan **s);
   452  void	runtime·gc(int32 force);
   453  void	runtime·markallocated(void *v, uintptr n, bool noptr);
   454  void	runtime·checkallocated(void *v, uintptr n);
   455  void	runtime·markfreed(void *v, uintptr n);
   456  void	runtime·checkfreed(void *v, uintptr n);
   457  extern	int32	runtime·checking;
   458  void	runtime·markspan(void *v, uintptr size, uintptr n, bool leftover);
   459  void	runtime·unmarkspan(void *v, uintptr size);
   460  bool	runtime·blockspecial(void*);
   461  void	runtime·setblockspecial(void*, bool);
   462  void	runtime·purgecachedstats(MCache*);
   463  void*	runtime·cnew(Type*);
   464  
   465  void	runtime·settype(void*, uintptr);
   466  void	runtime·settype_flush(M*, bool);
   467  void	runtime·settype_sysfree(MSpan*);
   468  uintptr	runtime·gettype(void*);
   469  
   470  enum
   471  {
   472  	// flags to malloc
   473  	FlagNoPointers = 1<<0,	// no pointers here
   474  	FlagNoProfiling = 1<<1,	// must not profile
   475  	FlagNoGC = 1<<2,	// must not free or scan for pointers
   476  };
   477  
   478  void	runtime·MProf_Malloc(void*, uintptr);
   479  void	runtime·MProf_Free(void*, uintptr);
   480  void	runtime·MProf_GC(void);
   481  int32	runtime·gcprocs(void);
   482  void	runtime·helpgc(int32 nproc);
   483  void	runtime·gchelper(void);
   484  
   485  bool	runtime·getfinalizer(void *p, bool del, FuncVal **fn, uintptr *nret);
   486  void	runtime·walkfintab(void (*fn)(void*));
   487  
   488  enum
   489  {
   490  	TypeInfo_SingleObject = 0,
   491  	TypeInfo_Array = 1,
   492  	TypeInfo_Map = 2,
   493  	TypeInfo_Chan = 3,
   494  
   495  	// Enables type information at the end of blocks allocated from heap	
   496  	DebugTypeAtBlockEnd = 0,
   497  };
   498  
   499  // defined in mgc0.go
   500  void	runtime·gc_m_ptr(Eface*);
   501  void	runtime·gc_itab_ptr(Eface*);
   502  
   503  void	runtime·memorydump(void);