github.com/reiver/go@v0.0.0-20150109200633-1d0c7792f172/src/runtime/malloc2.go (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 package runtime 6 7 import "unsafe" 8 9 // Memory allocator, based on tcmalloc. 10 // http://goog-perftools.sourceforge.net/doc/tcmalloc.html 11 12 // The main allocator works in runs of pages. 13 // Small allocation sizes (up to and including 32 kB) are 14 // rounded to one of about 100 size classes, each of which 15 // has its own free list of objects of exactly that size. 16 // Any free page of memory can be split into a set of objects 17 // of one size class, which are then managed using free list 18 // allocators. 19 // 20 // The allocator's data structures are: 21 // 22 // FixAlloc: a free-list allocator for fixed-size objects, 23 // used to manage storage used by the allocator. 24 // MHeap: the malloc heap, managed at page (4096-byte) granularity. 25 // MSpan: a run of pages managed by the MHeap. 26 // MCentral: a shared free list for a given size class. 27 // MCache: a per-thread (in Go, per-P) cache for small objects. 28 // MStats: allocation statistics. 29 // 30 // Allocating a small object proceeds up a hierarchy of caches: 31 // 32 // 1. Round the size up to one of the small size classes 33 // and look in the corresponding MCache free list. 34 // If the list is not empty, allocate an object from it. 35 // This can all be done without acquiring a lock. 36 // 37 // 2. If the MCache free list is empty, replenish it by 38 // taking a bunch of objects from the MCentral free list. 39 // Moving a bunch amortizes the cost of acquiring the MCentral lock. 40 // 41 // 3. If the MCentral free list is empty, replenish it by 42 // allocating a run of pages from the MHeap and then 43 // chopping that memory into objects of the given size. 44 // Allocating many objects amortizes the cost of locking 45 // the heap. 46 // 47 // 4. If the MHeap is empty or has no page runs large enough, 48 // allocate a new group of pages (at least 1MB) from the 49 // operating system. Allocating a large run of pages 50 // amortizes the cost of talking to the operating system. 51 // 52 // Freeing a small object proceeds up the same hierarchy: 53 // 54 // 1. Look up the size class for the object and add it to 55 // the MCache free list. 56 // 57 // 2. If the MCache free list is too long or the MCache has 58 // too much memory, return some to the MCentral free lists. 59 // 60 // 3. If all the objects in a given span have returned to 61 // the MCentral list, return that span to the page heap. 62 // 63 // 4. If the heap has too much memory, return some to the 64 // operating system. 65 // 66 // TODO(rsc): Step 4 is not implemented. 67 // 68 // Allocating and freeing a large object uses the page heap 69 // directly, bypassing the MCache and MCentral free lists. 70 // 71 // The small objects on the MCache and MCentral free lists 72 // may or may not be zeroed. They are zeroed if and only if 73 // the second word of the object is zero. A span in the 74 // page heap is zeroed unless s->needzero is set. When a span 75 // is allocated to break into small objects, it is zeroed if needed 76 // and s->needzero is set. There are two main benefits to delaying the 77 // zeroing this way: 78 // 79 // 1. stack frames allocated from the small object lists 80 // or the page heap can avoid zeroing altogether. 81 // 2. the cost of zeroing when reusing a small object is 82 // charged to the mutator, not the garbage collector. 83 // 84 // This C code was written with an eye toward translating to Go 85 // in the future. Methods have the form Type_Method(Type *t, ...). 86 87 const ( 88 _PageShift = 13 89 _PageSize = 1 << _PageShift 90 _PageMask = _PageSize - 1 91 ) 92 93 const ( 94 // _64bit = 1 on 64-bit systems, 0 on 32-bit systems 95 _64bit = 1 << (^uintptr(0) >> 63) / 2 96 97 // Computed constant. The definition of MaxSmallSize and the 98 // algorithm in msize.c produce some number of different allocation 99 // size classes. NumSizeClasses is that number. It's needed here 100 // because there are static arrays of this length; when msize runs its 101 // size choosing algorithm it double-checks that NumSizeClasses agrees. 102 _NumSizeClasses = 67 103 104 // Tunable constants. 105 _MaxSmallSize = 32 << 10 106 107 // Tiny allocator parameters, see "Tiny allocator" comment in malloc.goc. 108 _TinySize = 16 109 _TinySizeClass = 2 110 111 _FixAllocChunk = 16 << 10 // Chunk size for FixAlloc 112 _MaxMHeapList = 1 << (20 - _PageShift) // Maximum page length for fixed-size list in MHeap. 113 _HeapAllocChunk = 1 << 20 // Chunk size for heap growth 114 115 // Per-P, per order stack segment cache size. 116 _StackCacheSize = 32 * 1024 117 118 // Number of orders that get caching. Order 0 is FixedStack 119 // and each successive order is twice as large. 120 // We want to cache 2KB, 4KB, 8KB, and 16KB stacks. Larger stacks 121 // will be allocated directly. 122 // Since FixedStack is different on different systems, we 123 // must vary NumStackOrders to keep the same maximum cached size. 124 // OS | FixedStack | NumStackOrders 125 // -----------------+------------+--------------- 126 // linux/darwin/bsd | 2KB | 4 127 // windows/32 | 4KB | 3 128 // windows/64 | 8KB | 2 129 // plan9 | 4KB | 3 130 _NumStackOrders = 4 - ptrSize/4*goos_windows - 1*goos_plan9 131 132 // Number of bits in page to span calculations (4k pages). 133 // On Windows 64-bit we limit the arena to 32GB or 35 bits. 134 // Windows counts memory used by page table into committed memory 135 // of the process, so we can't reserve too much memory. 136 // See http://golang.org/issue/5402 and http://golang.org/issue/5236. 137 // On other 64-bit platforms, we limit the arena to 128GB, or 37 bits. 138 // On 32-bit, we don't bother limiting anything, so we use the full 32-bit address. 139 _MHeapMap_TotalBits = (_64bit*goos_windows)*35 + (_64bit*(1-goos_windows))*37 + (1-_64bit)*32 140 _MHeapMap_Bits = _MHeapMap_TotalBits - _PageShift 141 142 _MaxMem = uintptr(1<<_MHeapMap_TotalBits - 1) 143 144 // Max number of threads to run garbage collection. 145 // 2, 3, and 4 are all plausible maximums depending 146 // on the hardware details of the machine. The garbage 147 // collector scales well to 32 cpus. 148 _MaxGcproc = 32 149 ) 150 151 // A generic linked list of blocks. (Typically the block is bigger than sizeof(MLink).) 152 // Since assignments to mlink.next will result in a write barrier being preformed 153 // this can not be used by some of the internal GC structures. For example when 154 // the sweeper is placing an unmarked object on the free list it does not want the 155 // write barrier to be called since that could result in the object being reachable. 156 type mlink struct { 157 next *mlink 158 } 159 160 // A gclink is a node in a linked list of blocks, like mlink, 161 // but it is opaque to the garbage collector. 162 // The GC does not trace the pointers during collection, 163 // and the compiler does not emit write barriers for assignments 164 // of gclinkptr values. Code should store references to gclinks 165 // as gclinkptr, not as *gclink. 166 type gclink struct { 167 next gclinkptr 168 } 169 170 // A gclinkptr is a pointer to a gclink, but it is opaque 171 // to the garbage collector. 172 type gclinkptr uintptr 173 174 // ptr returns the *gclink form of p. 175 // The result should be used for accessing fields, not stored 176 // in other data structures. 177 func (p gclinkptr) ptr() *gclink { 178 return (*gclink)(unsafe.Pointer(p)) 179 } 180 181 // sysAlloc obtains a large chunk of zeroed memory from the 182 // operating system, typically on the order of a hundred kilobytes 183 // or a megabyte. 184 // NOTE: sysAlloc returns OS-aligned memory, but the heap allocator 185 // may use larger alignment, so the caller must be careful to realign the 186 // memory obtained by sysAlloc. 187 // 188 // SysUnused notifies the operating system that the contents 189 // of the memory region are no longer needed and can be reused 190 // for other purposes. 191 // SysUsed notifies the operating system that the contents 192 // of the memory region are needed again. 193 // 194 // SysFree returns it unconditionally; this is only used if 195 // an out-of-memory error has been detected midway through 196 // an allocation. It is okay if SysFree is a no-op. 197 // 198 // SysReserve reserves address space without allocating memory. 199 // If the pointer passed to it is non-nil, the caller wants the 200 // reservation there, but SysReserve can still choose another 201 // location if that one is unavailable. On some systems and in some 202 // cases SysReserve will simply check that the address space is 203 // available and not actually reserve it. If SysReserve returns 204 // non-nil, it sets *reserved to true if the address space is 205 // reserved, false if it has merely been checked. 206 // NOTE: SysReserve returns OS-aligned memory, but the heap allocator 207 // may use larger alignment, so the caller must be careful to realign the 208 // memory obtained by sysAlloc. 209 // 210 // SysMap maps previously reserved address space for use. 211 // The reserved argument is true if the address space was really 212 // reserved, not merely checked. 213 // 214 // SysFault marks a (already sysAlloc'd) region to fault 215 // if accessed. Used only for debugging the runtime. 216 217 // FixAlloc is a simple free-list allocator for fixed size objects. 218 // Malloc uses a FixAlloc wrapped around sysAlloc to manages its 219 // MCache and MSpan objects. 220 // 221 // Memory returned by FixAlloc_Alloc is not zeroed. 222 // The caller is responsible for locking around FixAlloc calls. 223 // Callers can keep state in the object but the first word is 224 // smashed by freeing and reallocating. 225 type fixalloc struct { 226 size uintptr 227 first unsafe.Pointer // go func(unsafe.pointer, unsafe.pointer); f(arg, p) called first time p is returned 228 arg unsafe.Pointer 229 list *mlink 230 chunk *byte 231 nchunk uint32 232 inuse uintptr // in-use bytes now 233 stat *uint64 234 } 235 236 // Statistics. 237 // Shared with Go: if you edit this structure, also edit type MemStats in mem.go. 238 type mstats struct { 239 // General statistics. 240 alloc uint64 // bytes allocated and still in use 241 total_alloc uint64 // bytes allocated (even if freed) 242 sys uint64 // bytes obtained from system (should be sum of xxx_sys below, no locking, approximate) 243 nlookup uint64 // number of pointer lookups 244 nmalloc uint64 // number of mallocs 245 nfree uint64 // number of frees 246 247 // Statistics about malloc heap. 248 // protected by mheap.lock 249 heap_alloc uint64 // bytes allocated and still in use 250 heap_sys uint64 // bytes obtained from system 251 heap_idle uint64 // bytes in idle spans 252 heap_inuse uint64 // bytes in non-idle spans 253 heap_released uint64 // bytes released to the os 254 heap_objects uint64 // total number of allocated objects 255 256 // Statistics about allocation of low-level fixed-size structures. 257 // Protected by FixAlloc locks. 258 stacks_inuse uint64 // this number is included in heap_inuse above 259 stacks_sys uint64 // always 0 in mstats 260 mspan_inuse uint64 // mspan structures 261 mspan_sys uint64 262 mcache_inuse uint64 // mcache structures 263 mcache_sys uint64 264 buckhash_sys uint64 // profiling bucket hash table 265 gc_sys uint64 266 other_sys uint64 267 268 // Statistics about garbage collector. 269 // Protected by mheap or stopping the world during GC. 270 next_gc uint64 // next gc (in heap_alloc time) 271 last_gc uint64 // last gc (in absolute time) 272 pause_total_ns uint64 273 pause_ns [256]uint64 // circular buffer of recent gc pause lengths 274 pause_end [256]uint64 // circular buffer of recent gc end times (nanoseconds since 1970) 275 numgc uint32 276 enablegc bool 277 debuggc bool 278 279 // Statistics about allocation size classes. 280 281 by_size [_NumSizeClasses]struct { 282 size uint32 283 nmalloc uint64 284 nfree uint64 285 } 286 287 tinyallocs uint64 // number of tiny allocations that didn't cause actual allocation; not exported to go directly 288 } 289 290 var memstats mstats 291 292 // Size classes. Computed and initialized by InitSizes. 293 // 294 // SizeToClass(0 <= n <= MaxSmallSize) returns the size class, 295 // 1 <= sizeclass < NumSizeClasses, for n. 296 // Size class 0 is reserved to mean "not small". 297 // 298 // class_to_size[i] = largest size in class i 299 // class_to_allocnpages[i] = number of pages to allocate when 300 // making new objects in class i 301 302 var class_to_size [_NumSizeClasses]int32 303 var class_to_allocnpages [_NumSizeClasses]int32 304 var size_to_class8 [1024/8 + 1]int8 305 var size_to_class128 [(_MaxSmallSize-1024)/128 + 1]int8 306 307 type mcachelist struct { 308 list *mlink 309 nlist uint32 310 } 311 312 type stackfreelist struct { 313 list gclinkptr // linked list of free stacks 314 size uintptr // total size of stacks in list 315 } 316 317 // Per-thread (in Go, per-P) cache for small objects. 318 // No locking needed because it is per-thread (per-P). 319 type mcache struct { 320 // The following members are accessed on every malloc, 321 // so they are grouped here for better caching. 322 next_sample int32 // trigger heap sample after allocating this many bytes 323 local_cachealloc intptr // bytes allocated (or freed) from cache since last lock of heap 324 // Allocator cache for tiny objects w/o pointers. 325 // See "Tiny allocator" comment in malloc.goc. 326 tiny *byte 327 tinysize uintptr 328 local_tinyallocs uintptr // number of tiny allocs not counted in other stats 329 330 // The rest is not accessed on every malloc. 331 alloc [_NumSizeClasses]*mspan // spans to allocate from 332 333 stackcache [_NumStackOrders]stackfreelist 334 335 sudogcache *sudog 336 337 // Local allocator stats, flushed during GC. 338 local_nlookup uintptr // number of pointer lookups 339 local_largefree uintptr // bytes freed for large objects (>maxsmallsize) 340 local_nlargefree uintptr // number of frees for large objects (>maxsmallsize) 341 local_nsmallfree [_NumSizeClasses]uintptr // number of frees for small objects (<=maxsmallsize) 342 } 343 344 const ( 345 _KindSpecialFinalizer = 1 346 _KindSpecialProfile = 2 347 // Note: The finalizer special must be first because if we're freeing 348 // an object, a finalizer special will cause the freeing operation 349 // to abort, and we want to keep the other special records around 350 // if that happens. 351 ) 352 353 type special struct { 354 next *special // linked list in span 355 offset uint16 // span offset of object 356 kind byte // kind of special 357 } 358 359 // The described object has a finalizer set for it. 360 type specialfinalizer struct { 361 special special 362 fn *funcval 363 nret uintptr 364 fint *_type 365 ot *ptrtype 366 } 367 368 // The described object is being heap profiled. 369 type specialprofile struct { 370 special special 371 b *bucket 372 } 373 374 // An MSpan is a run of pages. 375 const ( 376 _MSpanInUse = iota // allocated for garbage collected heap 377 _MSpanStack // allocated for use by stack allocator 378 _MSpanFree 379 _MSpanListHead 380 _MSpanDead 381 ) 382 383 type mspan struct { 384 next *mspan // in a span linked list 385 prev *mspan // in a span linked list 386 start pageID // starting page number 387 npages uintptr // number of pages in span 388 freelist gclinkptr // list of free objects 389 // sweep generation: 390 // if sweepgen == h->sweepgen - 2, the span needs sweeping 391 // if sweepgen == h->sweepgen - 1, the span is currently being swept 392 // if sweepgen == h->sweepgen, the span is swept and ready to use 393 // h->sweepgen is incremented by 2 after every GC 394 sweepgen uint32 395 ref uint16 // capacity - number of objects in freelist 396 sizeclass uint8 // size class 397 incache bool // being used by an mcache 398 state uint8 // mspaninuse etc 399 needzero uint8 // needs to be zeroed before allocation 400 elemsize uintptr // computed from sizeclass or from npages 401 unusedsince int64 // first time spotted by gc in mspanfree state 402 npreleased uintptr // number of pages released to the os 403 limit uintptr // end of data in span 404 speciallock mutex // guards specials list 405 specials *special // linked list of special records sorted by offset. 406 } 407 408 // Every MSpan is in one doubly-linked list, 409 // either one of the MHeap's free lists or one of the 410 // MCentral's span lists. We use empty MSpan structures as list heads. 411 412 // Central list of free objects of a given size. 413 type mcentral struct { 414 lock mutex 415 sizeclass int32 416 nonempty mspan // list of spans with a free object 417 empty mspan // list of spans with no free objects (or cached in an mcache) 418 } 419 420 // Main malloc heap. 421 // The heap itself is the "free[]" and "large" arrays, 422 // but all the other global data is here too. 423 type mheap struct { 424 lock mutex 425 free [_MaxMHeapList]mspan // free lists of given length 426 freelarge mspan // free lists length >= _MaxMHeapList 427 busy [_MaxMHeapList]mspan // busy lists of large objects of given length 428 busylarge mspan // busy lists of large objects length >= _MaxMHeapList 429 allspans **mspan // all spans out there 430 gcspans **mspan // copy of allspans referenced by gc marker or sweeper 431 nspan uint32 432 sweepgen uint32 // sweep generation, see comment in mspan 433 sweepdone uint32 // all spans are swept 434 435 // span lookup 436 spans **mspan 437 spans_mapped uintptr 438 439 // range of addresses we might see in the heap 440 bitmap uintptr 441 bitmap_mapped uintptr 442 arena_start uintptr 443 arena_used uintptr 444 arena_end uintptr 445 arena_reserved bool 446 447 // write barrier shadow data+heap. 448 // 64-bit systems only, enabled by GODEBUG=wbshadow=1. 449 shadow_enabled bool // shadow should be updated and checked 450 shadow_reserved bool // shadow memory is reserved 451 shadow_heap uintptr // heap-addr + shadow_heap = shadow heap addr 452 shadow_data uintptr // data-addr + shadow_data = shadow data addr 453 data_start uintptr // start of shadowed data addresses 454 data_end uintptr // end of shadowed data addresses 455 456 // central free lists for small size classes. 457 // the padding makes sure that the MCentrals are 458 // spaced CacheLineSize bytes apart, so that each MCentral.lock 459 // gets its own cache line. 460 central [_NumSizeClasses]struct { 461 mcentral mcentral 462 pad [_CacheLineSize]byte 463 } 464 465 spanalloc fixalloc // allocator for span* 466 cachealloc fixalloc // allocator for mcache* 467 specialfinalizeralloc fixalloc // allocator for specialfinalizer* 468 specialprofilealloc fixalloc // allocator for specialprofile* 469 speciallock mutex // lock for sepcial record allocators. 470 471 // Malloc stats. 472 largefree uint64 // bytes freed for large objects (>maxsmallsize) 473 nlargefree uint64 // number of frees for large objects (>maxsmallsize) 474 nsmallfree [_NumSizeClasses]uint64 // number of frees for small objects (<=maxsmallsize) 475 } 476 477 var mheap_ mheap 478 479 const ( 480 // flags to malloc 481 _FlagNoScan = 1 << 0 // GC doesn't have to scan object 482 _FlagNoZero = 1 << 1 // don't zero memory 483 ) 484 485 // NOTE: Layout known to queuefinalizer. 486 type finalizer struct { 487 fn *funcval // function to call 488 arg unsafe.Pointer // ptr to object 489 nret uintptr // bytes of return values from fn 490 fint *_type // type of first argument of fn 491 ot *ptrtype // type of ptr to object 492 } 493 494 type finblock struct { 495 alllink *finblock 496 next *finblock 497 cnt int32 498 _ int32 499 fin [(_FinBlockSize - 2*ptrSize - 2*4) / unsafe.Sizeof(finalizer{})]finalizer 500 } 501 502 // Information from the compiler about the layout of stack frames. 503 type bitvector struct { 504 n int32 // # of bits 505 bytedata *uint8 506 } 507 508 type stackmap struct { 509 n int32 // number of bitmaps 510 nbit int32 // number of bits in each bitmap 511 bytedata [1]byte // bitmaps, each starting on a 32-bit boundary 512 } 513 514 // Returns pointer map data for the given stackmap index 515 // (the index is encoded in PCDATA_StackMapIndex). 516 517 // defined in mgc0.go