aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/mgcpacer_test.go
diff options
context:
space:
mode:
authorMichael Anthony Knyszek <mknyszek@google.com>2022-02-08 00:52:11 +0000
committerMichael Knyszek <mknyszek@google.com>2022-02-10 18:55:42 +0000
commite4a173adf6ffbd5f46b2bcb3f9eedf661bf2e4d1 (patch)
treeadec11403ef1174cf46b3d700dcb22af8feb8f05 /src/runtime/mgcpacer_test.go
parent18c2033ba587ce63fc9f2d6f52b8bb2e395c561f (diff)
downloadgo-e4a173adf6ffbd5f46b2bcb3f9eedf661bf2e4d1.tar.gz
go-e4a173adf6ffbd5f46b2bcb3f9eedf661bf2e4d1.zip
runtime: make piController much more defensive about overflow
If something goes horribly wrong with the assumptions surrounding a piController, its internal error state might accumulate in an unbounded manner. In practice this means unexpected Inf and NaN values. Avoid this by identifying cases where the error overflows and resetting controller state. In the scavenger, this case is much more likely. All that has to happen is the proportional relationship between sleep time and estimated CPU usage has to break down. Unfortunately because we're just measuring monotonic time for all this, there are lots of ways it could happen, especially in an oversubscribed system. In these cases, just fall back on a conservative pace for scavenging and try to wait out the issue. In the pacer I'm pretty sure this is impossible. Because we wire the output of the controller to the input, the response is very directly correlated, so it's impossible for the controller's core assumption to break down. While we're in the pacer, add more detail about why that controller is even there, as well as its purpose. Finally, let's be proactive about other sources of overflow, namely overflow from a very large input value. This change adds a check after the first few operations to detect overflow issues from the input, specifically the multiplication. No tests for the pacer because I was unable to actually break the pacer's controller under a fuzzer, and no tests for the scavenger because it is not really in a testable state. However: * This change includes a fuzz test for the piController. * I broke out the scavenger code locally and fuzz tested it, confirming that the patch eliminates the original failure mode. * I tested that on a local heap-spike test, the scavenger continues operating as expected under normal conditions. Fixes #51061. Change-Id: I02a01d2dbf0eb9d2a8a8e7274d4165c2b6a3415a Reviewed-on: https://go-review.googlesource.com/c/go/+/383954 Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> Trust: Michael Knyszek <mknyszek@google.com> Run-TryBot: Michael Knyszek <mknyszek@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/runtime/mgcpacer_test.go')
-rw-r--r--src/runtime/mgcpacer_test.go45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/runtime/mgcpacer_test.go b/src/runtime/mgcpacer_test.go
index 9ec0e5172b..10a8ca2520 100644
--- a/src/runtime/mgcpacer_test.go
+++ b/src/runtime/mgcpacer_test.go
@@ -715,3 +715,48 @@ func (f float64Stream) limit(min, max float64) float64Stream {
return v
}
}
+
+func FuzzPIController(f *testing.F) {
+ isNormal := func(x float64) bool {
+ return !math.IsInf(x, 0) && !math.IsNaN(x)
+ }
+ isPositive := func(x float64) bool {
+ return isNormal(x) && x > 0
+ }
+ // Seed with constants from controllers in the runtime.
+ // It's not critical that we keep these in sync, they're just
+ // reasonable seed inputs.
+ f.Add(0.3375, 3.2e6, 1e9, 0.001, 1000.0, 0.01)
+ f.Add(0.9, 4.0, 1000.0, -1000.0, 1000.0, 0.84)
+ f.Fuzz(func(t *testing.T, kp, ti, tt, min, max, setPoint float64) {
+ // Ignore uninteresting invalid parameters. These parameters
+ // are constant, so in practice surprising values will be documented
+ // or will be other otherwise immediately visible.
+ //
+ // We just want to make sure that given a non-Inf, non-NaN input,
+ // we always get a non-Inf, non-NaN output.
+ if !isPositive(kp) || !isPositive(ti) || !isPositive(tt) {
+ return
+ }
+ if !isNormal(min) || !isNormal(max) || min > max {
+ return
+ }
+ // Use a random source, but make it deterministic.
+ rs := rand.New(rand.NewSource(800))
+ randFloat64 := func() float64 {
+ return math.Float64frombits(rs.Uint64())
+ }
+ p := NewPIController(kp, ti, tt, min, max)
+ state := float64(0)
+ for i := 0; i < 100; i++ {
+ input := randFloat64()
+ // Ignore the "ok" parameter. We're just trying to break it.
+ // state is intentionally completely uncorrelated with the input.
+ var ok bool
+ state, ok = p.Next(input, setPoint, 1.0)
+ if !isNormal(state) {
+ t.Fatalf("got NaN or Inf result from controller: %f %v", state, ok)
+ }
+ }
+ })
+}