| 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 ( |
| 8 |
"runtime/internal/atomic" |
| 9 |
"unsafe" |
| 10 |
) |
| 11 |
|
| 12 |
// Per-thread (in Go, per-P) cache for small objects. |
| 13 |
// This includes a small object cache and local allocation stats. |
| 14 |
// No locking needed because it is per-thread (per-P). |
| 15 |
// |
| 16 |
// mcaches are allocated from non-GC'd memory, so any heap pointers |
| 17 |
// must be specially handled. |
| 18 |
// |
| 19 |
//go:notinheap |
| 20 |
type mcache struct { |
| 21 |
// The following members are accessed on every malloc, |
| 22 |
// so they are grouped here for better caching. |
| 23 |
nextSample uintptr // trigger heap sample after allocating this many bytes |
| 24 |
scanAlloc uintptr // bytes of scannable heap allocated |
| 25 |
|
| 26 |
// Allocator cache for tiny objects w/o pointers. |
| 27 |
// See "Tiny allocator" comment in malloc.go. |
| 28 |
|
| 29 |
// tiny points to the beginning of the current tiny block, or |
| 30 |
// nil if there is no current tiny block. |
| 31 |
// |
| 32 |
// tiny is a heap pointer. Since mcache is in non-GC'd memory, |
| 33 |
// we handle it by clearing it in releaseAll during mark |
| 34 |
// termination. |
| 35 |
// |
| 36 |
// tinyAllocs is the number of tiny allocations performed |
| 37 |
// by the P that owns this mcache. |
| 38 |
tiny uintptr |
| 39 |
tinyoffset uintptr |
| 40 |
tinyAllocs uintptr |
| 41 |
|
| 42 |
// The rest is not accessed on every malloc. |
| 43 |
|
| 44 |
alloc [numSpanClasses]*mspan // spans to allocate from, indexed by spanClass |
| 45 |
|
| 46 |
// flushGen indicates the sweepgen during which this mcache |
| 47 |
// was last flushed. If flushGen != mheap_.sweepgen, the spans |
| 48 |
// in this mcache are stale and need to the flushed so they |
| 49 |
// can be swept. This is done in acquirep. |
| 50 |
flushGen uint32 |
| 51 |
} |
| 52 |
|
| 53 |
// A gclink is a node in a linked list of blocks, like mlink, |
| 54 |
// but it is opaque to the garbage collector. |
| 55 |
// The GC does not trace the pointers during collection, |
| 56 |
// and the compiler does not emit write barriers for assignments |
| 57 |
// of gclinkptr values. Code should store references to gclinks |
| 58 |
// as gclinkptr, not as *gclink. |
| 59 |
type gclink struct { |
| 60 |
next gclinkptr |
| 61 |
} |
| 62 |
|
| 63 |
// A gclinkptr is a pointer to a gclink, but it is opaque |
| 64 |
// to the garbage collector. |
| 65 |
type gclinkptr uintptr |
| 66 |
|
| 67 |
// ptr returns the *gclink form of p. |
| 68 |
// The result should be used for accessing fields, not stored |
| 69 |
// in other data structures. |
| 70 |
func (p gclinkptr) ptr() *gclink { |
| 71 |
return (*gclink)(unsafe.Pointer(p)) |
| 72 |
} |
| 73 |
|
| 74 |
// dummy mspan that contains no free objects. |
| 75 |
var emptymspan mspan |
| 76 |
|
| 77 |
func allocmcache() *mcache { |
| 78 |
var c *mcache |
| 79 |
systemstack(func() { |
| 80 |
lock(&mheap_.lock) |
| 81 |
c = (*mcache)(mheap_.cachealloc.alloc()) |
| 82 |
c.flushGen = mheap_.sweepgen |
| 83 |
unlock(&mheap_.lock) |
| 84 |
}) |
| 85 |
for i := range c.alloc { |
| 86 |
c.alloc[i] = &emptymspan |
| 87 |
} |
| 88 |
c.nextSample = nextSample() |
| 89 |
return c |
| 90 |
} |
| 91 |
|
| 92 |
// freemcache releases resources associated with this |
| 93 |
// mcache and puts the object onto a free list. |
| 94 |
// |
| 95 |
// In some cases there is no way to simply release |
| 96 |
// resources, such as statistics, so donate them to |
| 97 |
// a different mcache (the recipient). |
| 98 |
func freemcache(c *mcache) { |
| 99 |
systemstack(func() { |
| 100 |
c.releaseAll() |
| 101 |
|
| 102 |
// NOTE(rsc,rlh): If gcworkbuffree comes back, we need to coordinate |
| 103 |
// with the stealing of gcworkbufs during garbage collection to avoid |
| 104 |
// a race where the workbuf is double-freed. |
| 105 |
// gcworkbuffree(c.gcworkbuf) |
| 106 |
|
| 107 |
lock(&mheap_.lock) |
| 108 |
mheap_.cachealloc.free(unsafe.Pointer(c)) |
| 109 |
unlock(&mheap_.lock) |
| 110 |
}) |
| 111 |
} |
| 112 |
|
| 113 |
// getMCache is a convenience function which tries to obtain an mcache. |
| 114 |
// |
| 115 |
// Returns nil if we're not bootstrapping or we don't have a P. The caller's |
| 116 |
// P must not change, so we must be in a non-preemptible state. |
| 117 |
func getMCache(mp *m) *mcache { |
| 118 |
// Grab the mcache, since that's where stats live. |
| 119 |
pp := mp.p.ptr() |
| 120 |
var c *mcache |
| 121 |
if pp == nil { |
| 122 |
// We will be called without a P while bootstrapping, |
| 123 |
// in which case we use mcache0, which is set in mallocinit. |
| 124 |
// mcache0 is cleared when bootstrapping is complete, |
| 125 |
// by procresize. |
| 126 |
c = mcache0 |
| 127 |
} else { |
| 128 |
c = pp.mcache |
| 129 |
} |
| 130 |
return c |
| 131 |
} |
| 132 |
|
| 133 |
// refill acquires a new span of span class spc for c. This span will |
| 134 |
// have at least one free object. The current span in c must be full. |
| 135 |
// |
| 136 |
// Must run in a non-preemptible context since otherwise the owner of |
| 137 |
// c could change. |
| 138 |
func (c *mcache) refill(spc spanClass) { |
| 139 |
// Return the current cached span to the central lists. |
| 140 |
s := c.alloc[spc] |
| 141 |
|
| 142 |
if uintptr(s.allocCount) != s.nelems { |
| 143 |
throw("refill of span with free space remaining") |
| 144 |
} |
| 145 |
if s != &emptymspan { |
| 146 |
// Mark this span as no longer cached. |
| 147 |
if s.sweepgen != mheap_.sweepgen+3 { |
| 148 |
throw("bad sweepgen in refill") |
| 149 |
} |
| 150 |
mheap_.central[spc].mcentral.uncacheSpan(s) |
| 151 |
} |
| 152 |
|
| 153 |
// Get a new cached span from the central lists. |
| 154 |
s = mheap_.central[spc].mcentral.cacheSpan() |
| 155 |
if s == nil { |
| 156 |
throw("out of memory") |
| 157 |
} |
| 158 |
|
| 159 |
if uintptr(s.allocCount) == s.nelems { |
| 160 |
throw("span has no free space") |
| 161 |
} |
| 162 |
|
| 163 |
// Indicate that this span is cached and prevent asynchronous |
| 164 |
// sweeping in the next sweep phase. |
| 165 |
s.sweepgen = mheap_.sweepgen + 3 |
| 166 |
|
| 167 |
// Assume all objects from this span will be allocated in the |
| 168 |
// mcache. If it gets uncached, we'll adjust this. |
| 169 |
stats := memstats.heapStats.acquire() |
| 170 |
atomic.Xadduintptr(&stats.smallAllocCount[spc.sizeclass()], uintptr(s.nelems)-uintptr(s.allocCount)) |
| 171 |
|
| 172 |
// Flush tinyAllocs. |
| 173 |
if spc == tinySpanClass { |
| 174 |
atomic.Xadduintptr(&stats.tinyAllocCount, c.tinyAllocs) |
| 175 |
c.tinyAllocs = 0 |
| 176 |
} |
| 177 |
memstats.heapStats.release() |
| 178 |
|
| 179 |
// Update heapLive with the same assumption. |
| 180 |
// While we're here, flush scanAlloc, since we have to call |
| 181 |
// revise anyway. |
| 182 |
usedBytes := uintptr(s.allocCount) * s.elemsize |
| 183 |
gcController.update(int64(s.npages*pageSize)-int64(usedBytes), int64(c.scanAlloc)) |
| 184 |
c.scanAlloc = 0 |
| 185 |
|
| 186 |
c.alloc[spc] = s |
| 187 |
} |
| 188 |
|
| 189 |
// allocLarge allocates a span for a large object. |
| 190 |
func (c *mcache) allocLarge(size uintptr, noscan bool) *mspan { |
| 191 |
if size+_PageSize < size { |
| 192 |
throw("out of memory") |
| 193 |
} |
| 194 |
npages := size >> _PageShift |
| 195 |
if size&_PageMask != 0 { |
| 196 |
npages++ |
| 197 |
} |
| 198 |
|
| 199 |
// Deduct credit for this span allocation and sweep if |
| 200 |
// necessary. mHeap_Alloc will also sweep npages, so this only |
| 201 |
// pays the debt down to npage pages. |
| 202 |
deductSweepCredit(npages*_PageSize, npages) |
| 203 |
|
| 204 |
spc := makeSpanClass(0, noscan) |
| 205 |
s := mheap_.alloc(npages, spc) |
| 206 |
if s == nil { |
| 207 |
throw("out of memory") |
| 208 |
} |
| 209 |
stats := memstats.heapStats.acquire() |
| 210 |
atomic.Xadduintptr(&stats.largeAlloc, npages*pageSize) |
| 211 |
atomic.Xadduintptr(&stats.largeAllocCount, 1) |
| 212 |
memstats.heapStats.release() |
| 213 |
|
| 214 |
// Update heapLive. |
| 215 |
gcController.update(int64(s.npages*pageSize), 0) |
| 216 |
|
| 217 |
// Put the large span in the mcentral swept list so that it's |
| 218 |
// visible to the background sweeper. |
| 219 |
mheap_.central[spc].mcentral.fullSwept(mheap_.sweepgen).push(s) |
| 220 |
s.limit = s.base() + size |
| 221 |
heapBitsForAddr(s.base()).initSpan(s) |
| 222 |
return s |
| 223 |
} |
| 224 |
|
| 225 |
func (c *mcache) releaseAll() { |
| 226 |
// Take this opportunity to flush scanAlloc. |
| 227 |
scanAlloc := int64(c.scanAlloc) |
| 228 |
c.scanAlloc = 0 |
| 229 |
|
| 230 |
sg := mheap_.sweepgen |
| 231 |
dHeapLive := int64(0) |
| 232 |
for i := range c.alloc { |
| 233 |
s := c.alloc[i] |
| 234 |
if s != &emptymspan { |
| 235 |
// Adjust nsmallalloc in case the span wasn't fully allocated. |
| 236 |
n := uintptr(s.nelems) - uintptr(s.allocCount) |
| 237 |
stats := memstats.heapStats.acquire() |
| 238 |
atomic.Xadduintptr(&stats.smallAllocCount[spanClass(i).sizeclass()], -n) |
| 239 |
memstats.heapStats.release() |
| 240 |
if s.sweepgen != sg+1 { |
| 241 |
// refill conservatively counted unallocated slots in gcController.heapLive. |
| 242 |
// Undo this. |
| 243 |
// |
| 244 |
// If this span was cached before sweep, then |
| 245 |
// gcController.heapLive was totally recomputed since |
| 246 |
// caching this span, so we don't do this for |
| 247 |
// stale spans. |
| 248 |
dHeapLive -= int64(n) * int64(s.elemsize) |
| 249 |
} |
| 250 |
// Release the span to the mcentral. |
| 251 |
mheap_.central[i].mcentral.uncacheSpan(s) |
| 252 |
c.alloc[i] = &emptymspan |
| 253 |
} |
| 254 |
} |
| 255 |
// Clear tinyalloc pool. |
| 256 |
c.tiny = 0 |
| 257 |
c.tinyoffset = 0 |
| 258 |
|
| 259 |
// Flush tinyAllocs. |
| 260 |
stats := memstats.heapStats.acquire() |
| 261 |
atomic.Xadduintptr(&stats.tinyAllocCount, c.tinyAllocs) |
| 262 |
c.tinyAllocs = 0 |
| 263 |
memstats.heapStats.release() |
| 264 |
|
| 265 |
// Updated heapScan and heapLive. |
| 266 |
gcController.update(dHeapLive, scanAlloc) |
| 267 |
} |
| 268 |
|
| 269 |
// prepareForSweep flushes c if the system has entered a new sweep phase |
| 270 |
// since c was populated. This must happen between the sweep phase |
| 271 |
// starting and the first allocation from c. |
| 272 |
func (c *mcache) prepareForSweep() { |
| 273 |
// Alternatively, instead of making sure we do this on every P |
| 274 |
// between starting the world and allocating on that P, we |
| 275 |
// could leave allocate-black on, allow allocation to continue |
| 276 |
// as usual, use a ragged barrier at the beginning of sweep to |
| 277 |
// ensure all cached spans are swept, and then disable |
| 278 |
// allocate-black. However, with this approach it's difficult |
| 279 |
// to avoid spilling mark bits into the *next* GC cycle. |
| 280 |
sg := mheap_.sweepgen |
| 281 |
if c.flushGen == sg { |
| 282 |
return |
| 283 |
} else if c.flushGen != sg-2 { |
| 284 |
println("bad flushGen", c.flushGen, "in prepareForSweep; sweepgen", sg) |
| 285 |
throw("bad flushGen") |
| 286 |
} |
| 287 |
c.releaseAll() |
| 288 |
atomic.Store(&c.flushGen, mheap_.sweepgen) // Synchronizes with gcStart |
| 289 |
} |