]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.garbage] runtime: fix nfree accounting
authorAustin Clements <austin@google.com>
Fri, 29 Apr 2016 13:44:53 +0000 (09:44 -0400)
committerAustin Clements <austin@google.com>
Fri, 29 Apr 2016 15:25:26 +0000 (15:25 +0000)
Commit 8dda1c4 changed the meaning of "nfree" in sweep from the number
of newly freed objects to the total number of free objects in the
span, but didn't update where sweep added nfree to c.local_nsmallfree.
Hence, we're over-accounting the number of frees. This is causing
TestArrayHash to fail with "too many allocs NNN - hash not balanced".

Fix this by computing the number of newly freed objects and adding
that to c.local_nsmallfree, so it behaves like it used to. Computing
this requires a small tweak to mallocgc: apparently we've never set
s.allocCount when allocating a large object; fix this by setting it to
1 so sweep doesn't get confused.

Change-Id: I31902ffd310110da4ffd807c5c06f1117b872dc8
Reviewed-on: https://go-review.googlesource.com/22595
Reviewed-by: Rick Hudson <rlh@golang.org>
Run-TryBot: Austin Clements <austin@google.com>

src/runtime/malloc.go
src/runtime/mgcsweep.go

index 2ac504f9dc77a774868383ab26a0ff0012289228..438cd0616172121489de0b23fb241876e654e6cb 100644 (file)
@@ -696,6 +696,7 @@ func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
                        s = largeAlloc(size, needzero)
                })
                s.freeindex = 1
+               s.allocCount = 1
                x = unsafe.Pointer(s.base())
                size = s.elemsize
        }
index c9ef63547a3b0795876ece60f62adb1317a76446..82537edaaa6bdfbdf2e5dc2fedd023aaaef7cf40 100644 (file)
@@ -272,8 +272,14 @@ func (s *mspan) sweep(preserve bool) bool {
                s.needzero = 1
                freeToHeap = true
        }
+       nalloc := uint16(s.nelems) - uint16(nfree)
+       nfreed := s.allocCount - nalloc
+       if nalloc > s.allocCount {
+               print("runtime: nelems=", s.nelems, " nfree=", nfree, " nalloc=", nalloc, " previous allocCount=", s.allocCount, " nfreed=", nfreed, "\n")
+               throw("sweep increased allocation count")
+       }
 
-       s.allocCount = uint16(s.nelems) - uint16(nfree)
+       s.allocCount = nalloc
        wasempty := s.nextFreeIndex() == s.nelems
        s.freeindex = 0 // reset allocation index to start of span.
 
@@ -304,7 +310,7 @@ func (s *mspan) sweep(preserve bool) bool {
        }
 
        if nfree > 0 && cl != 0 {
-               c.local_nsmallfree[cl] += uintptr(nfree)
+               c.local_nsmallfree[cl] += uintptr(nfreed)
                res = mheap_.central[cl].mcentral.freeSpan(s, preserve, wasempty)
                // MCentral_FreeSpan updates sweepgen
        } else if freeToHeap {