github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/runtime/mgcsweep.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 // Garbage collector: sweeping 6 7 // The sweeper consists of two different algorithms: 8 // 9 // * The object reclaimer finds and frees unmarked slots in spans. It 10 // can free a whole span if none of the objects are marked, but that 11 // isn't its goal. This can be driven either synchronously by 12 // mcentral.cacheSpan for mcentral spans, or asynchronously by 13 // sweepone, which looks at all the mcentral lists. 14 // 15 // * The span reclaimer looks for spans that contain no marked objects 16 // and frees whole spans. This is a separate algorithm because 17 // freeing whole spans is the hardest task for the object reclaimer, 18 // but is critical when allocating new spans. The entry point for 19 // this is mheap_.reclaim and it's driven by a sequential scan of 20 // the page marks bitmap in the heap arenas. 21 // 22 // Both algorithms ultimately call mspan.sweep, which sweeps a single 23 // heap span. 24 25 package runtime 26 27 import ( 28 "runtime/internal/atomic" 29 "unsafe" 30 ) 31 32 var sweep sweepdata 33 34 // State of background sweep. 35 type sweepdata struct { 36 lock mutex 37 g *g 38 parked bool 39 started bool 40 41 nbgsweep uint32 42 npausesweep uint32 43 44 // active tracks outstanding sweepers and the sweep 45 // termination condition. 46 active activeSweep 47 48 // centralIndex is the current unswept span class. 49 // It represents an index into the mcentral span 50 // sets. Accessed and updated via its load and 51 // update methods. Not protected by a lock. 52 // 53 // Reset at mark termination. 54 // Used by mheap.nextSpanForSweep. 55 centralIndex sweepClass 56 } 57 58 // sweepClass is a spanClass and one bit to represent whether we're currently 59 // sweeping partial or full spans. 60 type sweepClass uint32 61 62 const ( 63 numSweepClasses = numSpanClasses * 2 64 sweepClassDone sweepClass = sweepClass(^uint32(0)) 65 ) 66 67 func (s *sweepClass) load() sweepClass { 68 return sweepClass(atomic.Load((*uint32)(s))) 69 } 70 71 func (s *sweepClass) update(sNew sweepClass) { 72 // Only update *s if its current value is less than sNew, 73 // since *s increases monotonically. 74 sOld := s.load() 75 for sOld < sNew && !atomic.Cas((*uint32)(s), uint32(sOld), uint32(sNew)) { 76 sOld = s.load() 77 } 78 // TODO(mknyszek): This isn't the only place we have 79 // an atomic monotonically increasing counter. It would 80 // be nice to have an "atomic max" which is just implemented 81 // as the above on most architectures. Some architectures 82 // like RISC-V however have native support for an atomic max. 83 } 84 85 func (s *sweepClass) clear() { 86 atomic.Store((*uint32)(s), 0) 87 } 88 89 // split returns the underlying span class as well as 90 // whether we're interested in the full or partial 91 // unswept lists for that class, indicated as a boolean 92 // (true means "full"). 93 func (s sweepClass) split() (spc spanClass, full bool) { 94 return spanClass(s >> 1), s&1 == 0 95 } 96 97 // nextSpanForSweep finds and pops the next span for sweeping from the 98 // central sweep buffers. It returns ownership of the span to the caller. 99 // Returns nil if no such span exists. 100 func (h *mheap) nextSpanForSweep() *mspan { 101 sg := h.sweepgen 102 for sc := sweep.centralIndex.load(); sc < numSweepClasses; sc++ { 103 spc, full := sc.split() 104 c := &h.central[spc].mcentral 105 var s *mspan 106 if full { 107 s = c.fullUnswept(sg).pop() 108 } else { 109 s = c.partialUnswept(sg).pop() 110 } 111 if s != nil { 112 // Write down that we found something so future sweepers 113 // can start from here. 114 sweep.centralIndex.update(sc) 115 return s 116 } 117 } 118 // Write down that we found nothing. 119 sweep.centralIndex.update(sweepClassDone) 120 return nil 121 } 122 123 const sweepDrainedMask = 1 << 31 124 125 // activeSweep is a type that captures whether sweeping 126 // is done, and whether there are any outstanding sweepers. 127 // 128 // Every potential sweeper must call begin() before they look 129 // for work, and end() after they've finished sweeping. 130 type activeSweep struct { 131 // state is divided into two parts. 132 // 133 // The top bit (masked by sweepDrainedMask) is a boolean 134 // value indicating whether all the sweep work has been 135 // drained from the queue. 136 // 137 // The rest of the bits are a counter, indicating the 138 // number of outstanding concurrent sweepers. 139 state atomic.Uint32 140 } 141 142 // begin registers a new sweeper. Returns a sweepLocker 143 // for acquiring spans for sweeping. Any outstanding sweeper blocks 144 // sweep termination. 145 // 146 // If the sweepLocker is invalid, the caller can be sure that all 147 // outstanding sweep work has been drained, so there is nothing left 148 // to sweep. Note that there may be sweepers currently running, so 149 // this does not indicate that all sweeping has completed. 150 // 151 // Even if the sweepLocker is invalid, its sweepGen is always valid. 152 func (a *activeSweep) begin() sweepLocker { 153 for { 154 state := a.state.Load() 155 if state&sweepDrainedMask != 0 { 156 return sweepLocker{mheap_.sweepgen, false} 157 } 158 if a.state.CompareAndSwap(state, state+1) { 159 return sweepLocker{mheap_.sweepgen, true} 160 } 161 } 162 } 163 164 // end deregisters a sweeper. Must be called once for each time 165 // begin is called if the sweepLocker is valid. 166 func (a *activeSweep) end(sl sweepLocker) { 167 if sl.sweepGen != mheap_.sweepgen { 168 throw("sweeper left outstanding across sweep generations") 169 } 170 for { 171 state := a.state.Load() 172 if (state&^sweepDrainedMask)-1 >= sweepDrainedMask { 173 throw("mismatched begin/end of activeSweep") 174 } 175 if a.state.CompareAndSwap(state, state-1) { 176 if state != sweepDrainedMask { 177 return 178 } 179 if debug.gcpacertrace > 0 { 180 live := gcController.heapLive.Load() 181 print("pacer: sweep done at heap size ", live>>20, "MB; allocated ", (live-mheap_.sweepHeapLiveBasis)>>20, "MB during sweep; swept ", mheap_.pagesSwept.Load(), " pages at ", mheap_.sweepPagesPerByte, " pages/byte\n") 182 } 183 return 184 } 185 } 186 } 187 188 // markDrained marks the active sweep cycle as having drained 189 // all remaining work. This is safe to be called concurrently 190 // with all other methods of activeSweep, though may race. 191 // 192 // Returns true if this call was the one that actually performed 193 // the mark. 194 func (a *activeSweep) markDrained() bool { 195 for { 196 state := a.state.Load() 197 if state&sweepDrainedMask != 0 { 198 return false 199 } 200 if a.state.CompareAndSwap(state, state|sweepDrainedMask) { 201 return true 202 } 203 } 204 } 205 206 // sweepers returns the current number of active sweepers. 207 func (a *activeSweep) sweepers() uint32 { 208 return a.state.Load() &^ sweepDrainedMask 209 } 210 211 // isDone returns true if all sweep work has been drained and no more 212 // outstanding sweepers exist. That is, when the sweep phase is 213 // completely done. 214 func (a *activeSweep) isDone() bool { 215 return a.state.Load() == sweepDrainedMask 216 } 217 218 // reset sets up the activeSweep for the next sweep cycle. 219 // 220 // The world must be stopped. 221 func (a *activeSweep) reset() { 222 assertWorldStopped() 223 a.state.Store(0) 224 } 225 226 // finishsweep_m ensures that all spans are swept. 227 // 228 // The world must be stopped. This ensures there are no sweeps in 229 // progress. 230 // 231 //go:nowritebarrier 232 func finishsweep_m() { 233 assertWorldStopped() 234 235 // Sweeping must be complete before marking commences, so 236 // sweep any unswept spans. If this is a concurrent GC, there 237 // shouldn't be any spans left to sweep, so this should finish 238 // instantly. If GC was forced before the concurrent sweep 239 // finished, there may be spans to sweep. 240 for sweepone() != ^uintptr(0) { 241 sweep.npausesweep++ 242 } 243 244 // Make sure there aren't any outstanding sweepers left. 245 // At this point, with the world stopped, it means one of two 246 // things. Either we were able to preempt a sweeper, or that 247 // a sweeper didn't call sweep.active.end when it should have. 248 // Both cases indicate a bug, so throw. 249 if sweep.active.sweepers() != 0 { 250 throw("active sweepers found at start of mark phase") 251 } 252 253 // Reset all the unswept buffers, which should be empty. 254 // Do this in sweep termination as opposed to mark termination 255 // so that we can catch unswept spans and reclaim blocks as 256 // soon as possible. 257 sg := mheap_.sweepgen 258 for i := range mheap_.central { 259 c := &mheap_.central[i].mcentral 260 c.partialUnswept(sg).reset() 261 c.fullUnswept(sg).reset() 262 } 263 264 // Sweeping is done, so if the scavenger isn't already awake, 265 // wake it up. There's definitely work for it to do at this 266 // point. 267 scavenger.wake() 268 269 nextMarkBitArenaEpoch() 270 } 271 272 func bgsweep(c chan int) { 273 sweep.g = getg() 274 275 lockInit(&sweep.lock, lockRankSweep) 276 lock(&sweep.lock) 277 sweep.parked = true 278 c <- 1 279 goparkunlock(&sweep.lock, waitReasonGCSweepWait, traceEvGoBlock, 1) 280 281 for { 282 // bgsweep attempts to be a "low priority" goroutine by intentionally 283 // yielding time. It's OK if it doesn't run, because goroutines allocating 284 // memory will sweep and ensure that all spans are swept before the next 285 // GC cycle. We really only want to run when we're idle. 286 // 287 // However, calling Gosched after each span swept produces a tremendous 288 // amount of tracing events, sometimes up to 50% of events in a trace. It's 289 // also inefficient to call into the scheduler so much because sweeping a 290 // single span is in general a very fast operation, taking as little as 30 ns 291 // on modern hardware. (See #54767.) 292 // 293 // As a result, bgsweep sweeps in batches, and only calls into the scheduler 294 // at the end of every batch. Furthermore, it only yields its time if there 295 // isn't spare idle time available on other cores. If there's available idle 296 // time, helping to sweep can reduce allocation latencies by getting ahead of 297 // the proportional sweeper and having spans ready to go for allocation. 298 const sweepBatchSize = 10 299 nSwept := 0 300 for sweepone() != ^uintptr(0) { 301 sweep.nbgsweep++ 302 nSwept++ 303 if nSwept%sweepBatchSize == 0 { 304 goschedIfBusy() 305 } 306 } 307 for freeSomeWbufs(true) { 308 // N.B. freeSomeWbufs is already batched internally. 309 goschedIfBusy() 310 } 311 lock(&sweep.lock) 312 if !isSweepDone() { 313 // This can happen if a GC runs between 314 // gosweepone returning ^0 above 315 // and the lock being acquired. 316 unlock(&sweep.lock) 317 continue 318 } 319 sweep.parked = true 320 goparkunlock(&sweep.lock, waitReasonGCSweepWait, traceEvGoBlock, 1) 321 } 322 } 323 324 // sweepLocker acquires sweep ownership of spans. 325 type sweepLocker struct { 326 // sweepGen is the sweep generation of the heap. 327 sweepGen uint32 328 valid bool 329 } 330 331 // sweepLocked represents sweep ownership of a span. 332 type sweepLocked struct { 333 *mspan 334 } 335 336 // tryAcquire attempts to acquire sweep ownership of span s. If it 337 // successfully acquires ownership, it blocks sweep completion. 338 func (l *sweepLocker) tryAcquire(s *mspan) (sweepLocked, bool) { 339 if !l.valid { 340 throw("use of invalid sweepLocker") 341 } 342 // Check before attempting to CAS. 343 if atomic.Load(&s.sweepgen) != l.sweepGen-2 { 344 return sweepLocked{}, false 345 } 346 // Attempt to acquire sweep ownership of s. 347 if !atomic.Cas(&s.sweepgen, l.sweepGen-2, l.sweepGen-1) { 348 return sweepLocked{}, false 349 } 350 return sweepLocked{s}, true 351 } 352 353 // sweepone sweeps some unswept heap span and returns the number of pages returned 354 // to the heap, or ^uintptr(0) if there was nothing to sweep. 355 func sweepone() uintptr { 356 gp := getg() 357 358 // Increment locks to ensure that the goroutine is not preempted 359 // in the middle of sweep thus leaving the span in an inconsistent state for next GC 360 gp.m.locks++ 361 362 // TODO(austin): sweepone is almost always called in a loop; 363 // lift the sweepLocker into its callers. 364 sl := sweep.active.begin() 365 if !sl.valid { 366 gp.m.locks-- 367 return ^uintptr(0) 368 } 369 370 // Find a span to sweep. 371 npages := ^uintptr(0) 372 var noMoreWork bool 373 for { 374 s := mheap_.nextSpanForSweep() 375 if s == nil { 376 noMoreWork = sweep.active.markDrained() 377 break 378 } 379 if state := s.state.get(); state != mSpanInUse { 380 // This can happen if direct sweeping already 381 // swept this span, but in that case the sweep 382 // generation should always be up-to-date. 383 if !(s.sweepgen == sl.sweepGen || s.sweepgen == sl.sweepGen+3) { 384 print("runtime: bad span s.state=", state, " s.sweepgen=", s.sweepgen, " sweepgen=", sl.sweepGen, "\n") 385 throw("non in-use span in unswept list") 386 } 387 continue 388 } 389 if s, ok := sl.tryAcquire(s); ok { 390 // Sweep the span we found. 391 npages = s.npages 392 if s.sweep(false) { 393 // Whole span was freed. Count it toward the 394 // page reclaimer credit since these pages can 395 // now be used for span allocation. 396 mheap_.reclaimCredit.Add(npages) 397 } else { 398 // Span is still in-use, so this returned no 399 // pages to the heap and the span needs to 400 // move to the swept in-use list. 401 npages = 0 402 } 403 break 404 } 405 } 406 sweep.active.end(sl) 407 408 if noMoreWork { 409 // The sweep list is empty. There may still be 410 // concurrent sweeps running, but we're at least very 411 // close to done sweeping. 412 413 // Move the scavenge gen forward (signaling 414 // that there's new work to do) and wake the scavenger. 415 // 416 // The scavenger is signaled by the last sweeper because once 417 // sweeping is done, we will definitely have useful work for 418 // the scavenger to do, since the scavenger only runs over the 419 // heap once per GC cycle. This update is not done during sweep 420 // termination because in some cases there may be a long delay 421 // between sweep done and sweep termination (e.g. not enough 422 // allocations to trigger a GC) which would be nice to fill in 423 // with scavenging work. 424 if debug.scavtrace > 0 { 425 systemstack(func() { 426 lock(&mheap_.lock) 427 released := atomic.Loaduintptr(&mheap_.pages.scav.released) 428 printScavTrace(released, false) 429 atomic.Storeuintptr(&mheap_.pages.scav.released, 0) 430 unlock(&mheap_.lock) 431 }) 432 } 433 scavenger.ready() 434 } 435 436 gp.m.locks-- 437 return npages 438 } 439 440 // isSweepDone reports whether all spans are swept. 441 // 442 // Note that this condition may transition from false to true at any 443 // time as the sweeper runs. It may transition from true to false if a 444 // GC runs; to prevent that the caller must be non-preemptible or must 445 // somehow block GC progress. 446 func isSweepDone() bool { 447 return sweep.active.isDone() 448 } 449 450 // Returns only when span s has been swept. 451 // 452 //go:nowritebarrier 453 func (s *mspan) ensureSwept() { 454 // Caller must disable preemption. 455 // Otherwise when this function returns the span can become unswept again 456 // (if GC is triggered on another goroutine). 457 gp := getg() 458 if gp.m.locks == 0 && gp.m.mallocing == 0 && gp != gp.m.g0 { 459 throw("mspan.ensureSwept: m is not locked") 460 } 461 462 // If this operation fails, then that means that there are 463 // no more spans to be swept. In this case, either s has already 464 // been swept, or is about to be acquired for sweeping and swept. 465 sl := sweep.active.begin() 466 if sl.valid { 467 // The caller must be sure that the span is a mSpanInUse span. 468 if s, ok := sl.tryAcquire(s); ok { 469 s.sweep(false) 470 sweep.active.end(sl) 471 return 472 } 473 sweep.active.end(sl) 474 } 475 476 // Unfortunately we can't sweep the span ourselves. Somebody else 477 // got to it first. We don't have efficient means to wait, but that's 478 // OK, it will be swept fairly soon. 479 for { 480 spangen := atomic.Load(&s.sweepgen) 481 if spangen == sl.sweepGen || spangen == sl.sweepGen+3 { 482 break 483 } 484 osyield() 485 } 486 } 487 488 // Sweep frees or collects finalizers for blocks not marked in the mark phase. 489 // It clears the mark bits in preparation for the next GC round. 490 // Returns true if the span was returned to heap. 491 // If preserve=true, don't return it to heap nor relink in mcentral lists; 492 // caller takes care of it. 493 func (sl *sweepLocked) sweep(preserve bool) bool { 494 // It's critical that we enter this function with preemption disabled, 495 // GC must not start while we are in the middle of this function. 496 gp := getg() 497 if gp.m.locks == 0 && gp.m.mallocing == 0 && gp != gp.m.g0 { 498 throw("mspan.sweep: m is not locked") 499 } 500 501 s := sl.mspan 502 if !preserve { 503 // We'll release ownership of this span. Nil it out to 504 // prevent the caller from accidentally using it. 505 sl.mspan = nil 506 } 507 508 sweepgen := mheap_.sweepgen 509 if state := s.state.get(); state != mSpanInUse || s.sweepgen != sweepgen-1 { 510 print("mspan.sweep: state=", state, " sweepgen=", s.sweepgen, " mheap.sweepgen=", sweepgen, "\n") 511 throw("mspan.sweep: bad span state") 512 } 513 514 if trace.enabled { 515 traceGCSweepSpan(s.npages * _PageSize) 516 } 517 518 mheap_.pagesSwept.Add(int64(s.npages)) 519 520 spc := s.spanclass 521 size := s.elemsize 522 523 // The allocBits indicate which unmarked objects don't need to be 524 // processed since they were free at the end of the last GC cycle 525 // and were not allocated since then. 526 // If the allocBits index is >= s.freeindex and the bit 527 // is not marked then the object remains unallocated 528 // since the last GC. 529 // This situation is analogous to being on a freelist. 530 531 // Unlink & free special records for any objects we're about to free. 532 // Two complications here: 533 // 1. An object can have both finalizer and profile special records. 534 // In such case we need to queue finalizer for execution, 535 // mark the object as live and preserve the profile special. 536 // 2. A tiny object can have several finalizers setup for different offsets. 537 // If such object is not marked, we need to queue all finalizers at once. 538 // Both 1 and 2 are possible at the same time. 539 hadSpecials := s.specials != nil 540 siter := newSpecialsIter(s) 541 for siter.valid() { 542 // A finalizer can be set for an inner byte of an object, find object beginning. 543 objIndex := uintptr(siter.s.offset) / size 544 p := s.base() + objIndex*size 545 mbits := s.markBitsForIndex(objIndex) 546 if !mbits.isMarked() { 547 // This object is not marked and has at least one special record. 548 // Pass 1: see if it has at least one finalizer. 549 hasFin := false 550 endOffset := p - s.base() + size 551 for tmp := siter.s; tmp != nil && uintptr(tmp.offset) < endOffset; tmp = tmp.next { 552 if tmp.kind == _KindSpecialFinalizer { 553 // Stop freeing of object if it has a finalizer. 554 mbits.setMarkedNonAtomic() 555 hasFin = true 556 break 557 } 558 } 559 // Pass 2: queue all finalizers _or_ handle profile record. 560 for siter.valid() && uintptr(siter.s.offset) < endOffset { 561 // Find the exact byte for which the special was setup 562 // (as opposed to object beginning). 563 special := siter.s 564 p := s.base() + uintptr(special.offset) 565 if special.kind == _KindSpecialFinalizer || !hasFin { 566 siter.unlinkAndNext() 567 freeSpecial(special, unsafe.Pointer(p), size) 568 } else { 569 // The object has finalizers, so we're keeping it alive. 570 // All other specials only apply when an object is freed, 571 // so just keep the special record. 572 siter.next() 573 } 574 } 575 } else { 576 // object is still live 577 if siter.s.kind == _KindSpecialReachable { 578 special := siter.unlinkAndNext() 579 (*specialReachable)(unsafe.Pointer(special)).reachable = true 580 freeSpecial(special, unsafe.Pointer(p), size) 581 } else { 582 // keep special record 583 siter.next() 584 } 585 } 586 } 587 if hadSpecials && s.specials == nil { 588 spanHasNoSpecials(s) 589 } 590 591 if debug.allocfreetrace != 0 || debug.clobberfree != 0 || raceenabled || msanenabled || asanenabled { 592 // Find all newly freed objects. This doesn't have to 593 // efficient; allocfreetrace has massive overhead. 594 mbits := s.markBitsForBase() 595 abits := s.allocBitsForIndex(0) 596 for i := uintptr(0); i < s.nelems; i++ { 597 if !mbits.isMarked() && (abits.index < s.freeindex || abits.isMarked()) { 598 x := s.base() + i*s.elemsize 599 if debug.allocfreetrace != 0 { 600 tracefree(unsafe.Pointer(x), size) 601 } 602 if debug.clobberfree != 0 { 603 clobberfree(unsafe.Pointer(x), size) 604 } 605 // User arenas are handled on explicit free. 606 if raceenabled && !s.isUserArenaChunk { 607 racefree(unsafe.Pointer(x), size) 608 } 609 if msanenabled && !s.isUserArenaChunk { 610 msanfree(unsafe.Pointer(x), size) 611 } 612 if asanenabled && !s.isUserArenaChunk { 613 asanpoison(unsafe.Pointer(x), size) 614 } 615 } 616 mbits.advance() 617 abits.advance() 618 } 619 } 620 621 // Check for zombie objects. 622 if s.freeindex < s.nelems { 623 // Everything < freeindex is allocated and hence 624 // cannot be zombies. 625 // 626 // Check the first bitmap byte, where we have to be 627 // careful with freeindex. 628 obj := s.freeindex 629 if (*s.gcmarkBits.bytep(obj / 8)&^*s.allocBits.bytep(obj / 8))>>(obj%8) != 0 { 630 s.reportZombies() 631 } 632 // Check remaining bytes. 633 for i := obj/8 + 1; i < divRoundUp(s.nelems, 8); i++ { 634 if *s.gcmarkBits.bytep(i)&^*s.allocBits.bytep(i) != 0 { 635 s.reportZombies() 636 } 637 } 638 } 639 640 // Count the number of free objects in this span. 641 nalloc := uint16(s.countAlloc()) 642 nfreed := s.allocCount - nalloc 643 if nalloc > s.allocCount { 644 // The zombie check above should have caught this in 645 // more detail. 646 print("runtime: nelems=", s.nelems, " nalloc=", nalloc, " previous allocCount=", s.allocCount, " nfreed=", nfreed, "\n") 647 throw("sweep increased allocation count") 648 } 649 650 s.allocCount = nalloc 651 s.freeindex = 0 // reset allocation index to start of span. 652 if trace.enabled { 653 getg().m.p.ptr().traceReclaimed += uintptr(nfreed) * s.elemsize 654 } 655 656 // gcmarkBits becomes the allocBits. 657 // get a fresh cleared gcmarkBits in preparation for next GC 658 s.allocBits = s.gcmarkBits 659 s.gcmarkBits = newMarkBits(s.nelems) 660 661 // Initialize alloc bits cache. 662 s.refillAllocCache(0) 663 664 // The span must be in our exclusive ownership until we update sweepgen, 665 // check for potential races. 666 if state := s.state.get(); state != mSpanInUse || s.sweepgen != sweepgen-1 { 667 print("mspan.sweep: state=", state, " sweepgen=", s.sweepgen, " mheap.sweepgen=", sweepgen, "\n") 668 throw("mspan.sweep: bad span state after sweep") 669 } 670 if s.sweepgen == sweepgen+1 || s.sweepgen == sweepgen+3 { 671 throw("swept cached span") 672 } 673 674 // We need to set s.sweepgen = h.sweepgen only when all blocks are swept, 675 // because of the potential for a concurrent free/SetFinalizer. 676 // 677 // But we need to set it before we make the span available for allocation 678 // (return it to heap or mcentral), because allocation code assumes that a 679 // span is already swept if available for allocation. 680 // 681 // Serialization point. 682 // At this point the mark bits are cleared and allocation ready 683 // to go so release the span. 684 atomic.Store(&s.sweepgen, sweepgen) 685 686 if s.isUserArenaChunk { 687 if preserve { 688 // This is a case that should never be handled by a sweeper that 689 // preserves the span for reuse. 690 throw("sweep: tried to preserve a user arena span") 691 } 692 if nalloc > 0 { 693 // There still exist pointers into the span or the span hasn't been 694 // freed yet. It's not ready to be reused. Put it back on the 695 // full swept list for the next cycle. 696 mheap_.central[spc].mcentral.fullSwept(sweepgen).push(s) 697 return false 698 } 699 700 // It's only at this point that the sweeper doesn't actually need to look 701 // at this arena anymore, so subtract from pagesInUse now. 702 mheap_.pagesInUse.Add(-s.npages) 703 s.state.set(mSpanDead) 704 705 // The arena is ready to be recycled. Remove it from the quarantine list 706 // and place it on the ready list. Don't add it back to any sweep lists. 707 systemstack(func() { 708 // It's the arena code's responsibility to get the chunk on the quarantine 709 // list by the time all references to the chunk are gone. 710 if s.list != &mheap_.userArena.quarantineList { 711 throw("user arena span is on the wrong list") 712 } 713 lock(&mheap_.lock) 714 mheap_.userArena.quarantineList.remove(s) 715 mheap_.userArena.readyList.insert(s) 716 unlock(&mheap_.lock) 717 }) 718 return false 719 } 720 721 if spc.sizeclass() != 0 { 722 // Handle spans for small objects. 723 if nfreed > 0 { 724 // Only mark the span as needing zeroing if we've freed any 725 // objects, because a fresh span that had been allocated into, 726 // wasn't totally filled, but then swept, still has all of its 727 // free slots zeroed. 728 s.needzero = 1 729 stats := memstats.heapStats.acquire() 730 atomic.Xadd64(&stats.smallFreeCount[spc.sizeclass()], int64(nfreed)) 731 memstats.heapStats.release() 732 733 // Count the frees in the inconsistent, internal stats. 734 gcController.totalFree.Add(int64(nfreed) * int64(s.elemsize)) 735 } 736 if !preserve { 737 // The caller may not have removed this span from whatever 738 // unswept set its on but taken ownership of the span for 739 // sweeping by updating sweepgen. If this span still is in 740 // an unswept set, then the mcentral will pop it off the 741 // set, check its sweepgen, and ignore it. 742 if nalloc == 0 { 743 // Free totally free span directly back to the heap. 744 mheap_.freeSpan(s) 745 return true 746 } 747 // Return span back to the right mcentral list. 748 if uintptr(nalloc) == s.nelems { 749 mheap_.central[spc].mcentral.fullSwept(sweepgen).push(s) 750 } else { 751 mheap_.central[spc].mcentral.partialSwept(sweepgen).push(s) 752 } 753 } 754 } else if !preserve { 755 // Handle spans for large objects. 756 if nfreed != 0 { 757 // Free large object span to heap. 758 759 // NOTE(rsc,dvyukov): The original implementation of efence 760 // in CL 22060046 used sysFree instead of sysFault, so that 761 // the operating system would eventually give the memory 762 // back to us again, so that an efence program could run 763 // longer without running out of memory. Unfortunately, 764 // calling sysFree here without any kind of adjustment of the 765 // heap data structures means that when the memory does 766 // come back to us, we have the wrong metadata for it, either in 767 // the mspan structures or in the garbage collection bitmap. 768 // Using sysFault here means that the program will run out of 769 // memory fairly quickly in efence mode, but at least it won't 770 // have mysterious crashes due to confused memory reuse. 771 // It should be possible to switch back to sysFree if we also 772 // implement and then call some kind of mheap.deleteSpan. 773 if debug.efence > 0 { 774 s.limit = 0 // prevent mlookup from finding this span 775 sysFault(unsafe.Pointer(s.base()), size) 776 } else { 777 mheap_.freeSpan(s) 778 } 779 780 // Count the free in the consistent, external stats. 781 stats := memstats.heapStats.acquire() 782 atomic.Xadd64(&stats.largeFreeCount, 1) 783 atomic.Xadd64(&stats.largeFree, int64(size)) 784 memstats.heapStats.release() 785 786 // Count the free in the inconsistent, internal stats. 787 gcController.totalFree.Add(int64(size)) 788 789 return true 790 } 791 792 // Add a large span directly onto the full+swept list. 793 mheap_.central[spc].mcentral.fullSwept(sweepgen).push(s) 794 } 795 return false 796 } 797 798 // reportZombies reports any marked but free objects in s and throws. 799 // 800 // This generally means one of the following: 801 // 802 // 1. User code converted a pointer to a uintptr and then back 803 // unsafely, and a GC ran while the uintptr was the only reference to 804 // an object. 805 // 806 // 2. User code (or a compiler bug) constructed a bad pointer that 807 // points to a free slot, often a past-the-end pointer. 808 // 809 // 3. The GC two cycles ago missed a pointer and freed a live object, 810 // but it was still live in the last cycle, so this GC cycle found a 811 // pointer to that object and marked it. 812 func (s *mspan) reportZombies() { 813 printlock() 814 print("runtime: marked free object in span ", s, ", elemsize=", s.elemsize, " freeindex=", s.freeindex, " (bad use of unsafe.Pointer? try -d=checkptr)\n") 815 mbits := s.markBitsForBase() 816 abits := s.allocBitsForIndex(0) 817 for i := uintptr(0); i < s.nelems; i++ { 818 addr := s.base() + i*s.elemsize 819 print(hex(addr)) 820 alloc := i < s.freeindex || abits.isMarked() 821 if alloc { 822 print(" alloc") 823 } else { 824 print(" free ") 825 } 826 if mbits.isMarked() { 827 print(" marked ") 828 } else { 829 print(" unmarked") 830 } 831 zombie := mbits.isMarked() && !alloc 832 if zombie { 833 print(" zombie") 834 } 835 print("\n") 836 if zombie { 837 length := s.elemsize 838 if length > 1024 { 839 length = 1024 840 } 841 hexdumpWords(addr, addr+length, nil) 842 } 843 mbits.advance() 844 abits.advance() 845 } 846 throw("found pointer to free object") 847 } 848 849 // deductSweepCredit deducts sweep credit for allocating a span of 850 // size spanBytes. This must be performed *before* the span is 851 // allocated to ensure the system has enough credit. If necessary, it 852 // performs sweeping to prevent going in to debt. If the caller will 853 // also sweep pages (e.g., for a large allocation), it can pass a 854 // non-zero callerSweepPages to leave that many pages unswept. 855 // 856 // deductSweepCredit makes a worst-case assumption that all spanBytes 857 // bytes of the ultimately allocated span will be available for object 858 // allocation. 859 // 860 // deductSweepCredit is the core of the "proportional sweep" system. 861 // It uses statistics gathered by the garbage collector to perform 862 // enough sweeping so that all pages are swept during the concurrent 863 // sweep phase between GC cycles. 864 // 865 // mheap_ must NOT be locked. 866 func deductSweepCredit(spanBytes uintptr, callerSweepPages uintptr) { 867 if mheap_.sweepPagesPerByte == 0 { 868 // Proportional sweep is done or disabled. 869 return 870 } 871 872 if trace.enabled { 873 traceGCSweepStart() 874 } 875 876 retry: 877 sweptBasis := mheap_.pagesSweptBasis.Load() 878 879 // Fix debt if necessary. 880 newHeapLive := uintptr(gcController.heapLive.Load()-mheap_.sweepHeapLiveBasis) + spanBytes 881 pagesTarget := int64(mheap_.sweepPagesPerByte*float64(newHeapLive)) - int64(callerSweepPages) 882 for pagesTarget > int64(mheap_.pagesSwept.Load()-sweptBasis) { 883 if sweepone() == ^uintptr(0) { 884 mheap_.sweepPagesPerByte = 0 885 break 886 } 887 if mheap_.pagesSweptBasis.Load() != sweptBasis { 888 // Sweep pacing changed. Recompute debt. 889 goto retry 890 } 891 } 892 893 if trace.enabled { 894 traceGCSweepDone() 895 } 896 } 897 898 // clobberfree sets the memory content at x to bad content, for debugging 899 // purposes. 900 func clobberfree(x unsafe.Pointer, size uintptr) { 901 // size (span.elemsize) is always a multiple of 4. 902 for i := uintptr(0); i < size; i += 4 { 903 *(*uint32)(add(x, i)) = 0xdeadbeef 904 } 905 } 906 907 // gcPaceSweeper updates the sweeper's pacing parameters. 908 // 909 // Must be called whenever the GC's pacing is updated. 910 // 911 // The world must be stopped, or mheap_.lock must be held. 912 func gcPaceSweeper(trigger uint64) { 913 assertWorldStoppedOrLockHeld(&mheap_.lock) 914 915 // Update sweep pacing. 916 if isSweepDone() { 917 mheap_.sweepPagesPerByte = 0 918 } else { 919 // Concurrent sweep needs to sweep all of the in-use 920 // pages by the time the allocated heap reaches the GC 921 // trigger. Compute the ratio of in-use pages to sweep 922 // per byte allocated, accounting for the fact that 923 // some might already be swept. 924 heapLiveBasis := gcController.heapLive.Load() 925 heapDistance := int64(trigger) - int64(heapLiveBasis) 926 // Add a little margin so rounding errors and 927 // concurrent sweep are less likely to leave pages 928 // unswept when GC starts. 929 heapDistance -= 1024 * 1024 930 if heapDistance < _PageSize { 931 // Avoid setting the sweep ratio extremely high 932 heapDistance = _PageSize 933 } 934 pagesSwept := mheap_.pagesSwept.Load() 935 pagesInUse := mheap_.pagesInUse.Load() 936 sweepDistancePages := int64(pagesInUse) - int64(pagesSwept) 937 if sweepDistancePages <= 0 { 938 mheap_.sweepPagesPerByte = 0 939 } else { 940 mheap_.sweepPagesPerByte = float64(sweepDistancePages) / float64(heapDistance) 941 mheap_.sweepHeapLiveBasis = heapLiveBasis 942 // Write pagesSweptBasis last, since this 943 // signals concurrent sweeps to recompute 944 // their debt. 945 mheap_.pagesSweptBasis.Store(pagesSwept) 946 } 947 } 948 }