1
0
mirror of https://github.com/golang/go synced 2024-11-13 19:10:22 -07:00
go/test/tinyfin.go
Dmitry Vyukov bf606094ee runtime: fix finalization and profiling of tiny allocations
Handling of special records for tiny allocations has two problems:
1. Once we queue a finalizer we mark the object. As the result any
   subsequent finalizers for the same object will not be queued
   during this GC cycle. If we have 16 finalizers setup (the worst case),
   finalization will take 16 GC cycles. This is what caused misbehave
   of tinyfin.go. The actual flakiness was caused by the fact that fing
   is asynchronous and don't always run before the check.
2. If a tiny block has both finalizer and profile specials,
   it is possible that we both queue finalizer, preserve the object live
   and free the profile record. As the result heap profile can be skewed.

Fix both issues by analyzing all special records for a single object at once.

Also, make tinyfin test stricter and remove reliance on real time.

Also, add a test for the problem 2. Currently heap profile missed about
a half of live memory.

Fixes #13100

Change-Id: I9ae4dc1c44893724138a4565ca5cae29f2e97544
Reviewed-on: https://go-review.googlesource.com/16591
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Dmitry Vyukov <dvyukov@google.com>
2015-11-03 18:57:18 +00:00

65 lines
1.6 KiB
Go

// run
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test finalizers work for tiny (combined) allocations.
package main
import (
"runtime"
"time"
)
func main() {
// Does not work on gccgo due to partially conservative GC.
// Try to enable when we have fully precise GC.
if runtime.Compiler == "gccgo" {
return
}
const N = 100
finalized := make(chan int32, N)
for i := 0; i < N; i++ {
x := new(int32) // subject to tiny alloc
*x = int32(i)
// the closure must be big enough to be combined
runtime.SetFinalizer(x, func(p *int32) {
finalized <- *p
})
}
runtime.GC()
count := 0
done := make([]bool, N)
timeout := time.After(5*time.Second)
for {
select {
case <-timeout:
println("timeout,", count, "finalized so far")
panic("not all finalizers are called")
case x := <-finalized:
// Check that p points to the correct subobject of the tiny allocation.
// It's a bit tricky, because we can't capture another variable
// with the expected value (it would be combined as well).
if x < 0 || x >= N {
println("got", x)
panic("corrupted")
}
if done[x] {
println("got", x)
panic("already finalized")
}
done[x] = true
count++
if count > N/10*9 {
// Some of the finalizers may not be executed,
// if the outermost allocations are combined with something persistent.
// Currently 4 int32's are combined into a 16-byte block,
// ensure that most of them are finalized.
return
}
}
}
}