2020-12-02 00:03:20 +00:00
|
|
|
package watchdog
|
|
|
|
|
|
|
|
import (
|
2020-12-04 13:08:00 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2020-12-02 00:03:20 +00:00
|
|
|
"runtime"
|
2020-12-04 13:08:00 +00:00
|
|
|
"runtime/debug"
|
2021-01-18 17:59:51 +00:00
|
|
|
"strconv"
|
2020-12-02 00:03:20 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
"github.com/elastic/gosigar"
|
2020-12-02 00:03:20 +00:00
|
|
|
"github.com/raulk/clock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-01-18 17:59:51 +00:00
|
|
|
const (
|
|
|
|
// EnvTestIsolated is a marker property for the runner to confirm that this
|
|
|
|
// test is running in isolation (i.e. a dedicated process).
|
|
|
|
EnvTestIsolated = "TEST_ISOLATED"
|
2020-12-04 13:08:00 +00:00
|
|
|
|
2021-01-18 17:59:51 +00:00
|
|
|
// EnvTestDockerMemLimit is the memory limit applied in a docker container.
|
|
|
|
EnvTestDockerMemLimit = "TEST_DOCKER_MEMLIMIT"
|
2020-12-04 13:08:00 +00:00
|
|
|
)
|
|
|
|
|
2021-01-18 17:59:51 +00:00
|
|
|
// DockerMemLimit is initialized in the init() function from the
|
|
|
|
// EnvTestDockerMemLimit env variable.
|
|
|
|
var DockerMemLimit int // bytes
|
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
func init() {
|
|
|
|
Logger = &stdlog{log: log.New(os.Stdout, "[watchdog test] ", log.LstdFlags|log.Lmsgprefix), debug: true}
|
2021-01-18 17:59:51 +00:00
|
|
|
|
|
|
|
if l := os.Getenv(EnvTestDockerMemLimit); l != "" {
|
|
|
|
l, err := strconv.Atoi(l)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
DockerMemLimit = l
|
|
|
|
}
|
2020-12-02 00:03:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:59:51 +00:00
|
|
|
func skipIfNotIsolated(t *testing.T) {
|
|
|
|
if os.Getenv(EnvTestIsolated) != "1" {
|
|
|
|
t.Skipf("skipping test in non-isolated mode")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
limit uint64 = 64 << 20 // 64MiB.
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestControl_Isolated(t *testing.T) {
|
|
|
|
skipIfNotIsolated(t)
|
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
debug.SetGCPercent(100)
|
|
|
|
|
2021-01-18 17:59:51 +00:00
|
|
|
rounds := 100
|
|
|
|
if DockerMemLimit != 0 {
|
|
|
|
rounds /= int(float64(DockerMemLimit)*0.8) / 1024 / 1024
|
|
|
|
}
|
|
|
|
|
|
|
|
// retain 1MiB every iteration.
|
2020-12-04 13:08:00 +00:00
|
|
|
var retained [][]byte
|
2021-01-18 17:59:51 +00:00
|
|
|
for i := 0; i < rounds; i++ {
|
2020-12-04 13:08:00 +00:00
|
|
|
b := make([]byte, 1*1024*1024)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = byte(i)
|
|
|
|
}
|
|
|
|
retained = append(retained, b)
|
|
|
|
}
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
for _, b := range retained {
|
|
|
|
for i := range b {
|
|
|
|
b[i] = byte(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ms runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&ms)
|
2021-01-18 17:59:51 +00:00
|
|
|
require.NotZero(t, ms.NumGC) // GCs have taken place, but...
|
|
|
|
require.Zero(t, ms.NumForcedGC) // ... no forced GCs beyond our initial one.
|
2020-12-02 00:03:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 17:59:51 +00:00
|
|
|
func TestHeapDriven_Isolated(t *testing.T) {
|
|
|
|
skipIfNotIsolated(t)
|
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
// we can't mock ReadMemStats, because we're relying on the go runtime to
|
|
|
|
// enforce the GC run, and the go runtime won't use our mock. Therefore, we
|
|
|
|
// need to do the actual thing.
|
|
|
|
debug.SetGCPercent(100)
|
|
|
|
|
2020-12-02 00:03:20 +00:00
|
|
|
clk := clock.NewMock()
|
|
|
|
Clock = clk
|
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
observations := make([]*runtime.MemStats, 0, 100)
|
2021-01-18 17:59:51 +00:00
|
|
|
NotifyGC = func() {
|
2020-12-04 13:08:00 +00:00
|
|
|
var ms runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&ms)
|
|
|
|
observations = append(observations, &ms)
|
|
|
|
}
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
// limit is 64MiB.
|
|
|
|
err, stopFn := HeapDriven(limit, NewAdaptivePolicy(0.5))
|
2020-12-02 00:03:20 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-04 13:08:00 +00:00
|
|
|
defer stopFn()
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
time.Sleep(500 * time.Millisecond) // give time for the watchdog to init.
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
// retain 1MiB every iteration, up to 100MiB (beyond heap limit!).
|
|
|
|
var retained [][]byte
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
retained = append(retained, make([]byte, 1*1024*1024))
|
|
|
|
}
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
for _, o := range observations {
|
|
|
|
fmt.Println("heap alloc:", o.HeapAlloc, "next gc:", o.NextGC, "gc count:", o.NumGC, "forced gc:", o.NumForcedGC)
|
|
|
|
}
|
2020-12-02 00:03:20 +00:00
|
|
|
|
|
|
|
var ms runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&ms)
|
2020-12-09 16:13:27 +00:00
|
|
|
require.GreaterOrEqual(t, ms.NumGC, uint32(9)) // over 9 GCs should've taken place.
|
2020-12-04 13:08:00 +00:00
|
|
|
}
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2021-01-18 17:59:51 +00:00
|
|
|
func TestSystemDriven_Isolated(t *testing.T) {
|
|
|
|
skipIfNotIsolated(t)
|
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
debug.SetGCPercent(100)
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
clk := clock.NewMock()
|
|
|
|
Clock = clk
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
// mock the system reporting.
|
|
|
|
var actualUsed uint64
|
|
|
|
sysmemFn = func(g *gosigar.Mem) error {
|
|
|
|
g.ActualUsed = actualUsed
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
// limit is 64MiB.
|
|
|
|
err, stopFn := SystemDriven(limit, 5*time.Second, NewAdaptivePolicy(0.5))
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer stopFn()
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
time.Sleep(200 * time.Millisecond) // give time for the watchdog to init.
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
notifyCh := make(chan struct{}, 1)
|
2021-01-18 17:59:51 +00:00
|
|
|
NotifyGC = func() {
|
2020-12-04 13:08:00 +00:00
|
|
|
notifyCh <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// first tick; used = 0.
|
|
|
|
clk.Add(5 * time.Second)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
require.Len(t, notifyCh, 0) // no GC has taken place.
|
|
|
|
|
|
|
|
// second tick; used = just over 50%; will trigger GC.
|
|
|
|
actualUsed = (limit / 2) + 1
|
|
|
|
clk.Add(5 * time.Second)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
require.Len(t, notifyCh, 1)
|
|
|
|
<-notifyCh
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
// third tick; just below 75%; no GC.
|
|
|
|
actualUsed = uint64(float64(limit)*0.75) - 1
|
|
|
|
clk.Add(5 * time.Second)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
require.Len(t, notifyCh, 0)
|
2020-12-02 00:03:20 +00:00
|
|
|
|
2020-12-04 13:08:00 +00:00
|
|
|
// fourth tick; 75% exactly; will trigger GC.
|
|
|
|
actualUsed = uint64(float64(limit)*0.75) + 1
|
|
|
|
clk.Add(5 * time.Second)
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
require.Len(t, notifyCh, 1)
|
|
|
|
<-notifyCh
|
|
|
|
|
|
|
|
var ms runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&ms)
|
|
|
|
require.GreaterOrEqual(t, ms.NumForcedGC, uint32(2))
|
2020-12-02 00:03:20 +00:00
|
|
|
}
|