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