cmd/bench: wait for load average to drop before starting

The coordinator runs tests on a freshly booted VM which may still be
running background boot tasks when bench starts.

For minimal noise, wait for the system load average to drop (indicating
background tasks have completed) before continuing with benchmarking.

For golang/go#49207.

Change-Id: I8df01592fea31d49eae54074213e202b21d5728a
Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/362375
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Trust: Michael Pratt <mpratt@google.com>
Run-TryBot: Michael Pratt <mpratt@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/cmd/bench/idle_linux.go b/cmd/bench/idle_linux.go
new file mode 100644
index 0000000..7e8a7c1
--- /dev/null
+++ b/cmd/bench/idle_linux.go
@@ -0,0 +1,64 @@
+// Copyright 2021 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.
+
+package main
+
+import (
+	"fmt"
+	"log"
+	"os"
+	"strconv"
+	"strings"
+	"time"
+)
+
+const idleMaxLoad = 0.2
+
+// loadAvg returns the 1-minute load average.
+func loadAvg() (float64, error) {
+	b, err := os.ReadFile("/proc/loadavg")
+	if err != nil {
+		return 0, fmt.Errorf("error reading /proc/loadavg: %w", err)
+	}
+
+	s := strings.TrimSpace(string(b))
+	log.Printf("Load average: %s", s)
+
+	parts := strings.Split(s, " ")
+
+	avg, err := strconv.ParseFloat(parts[0], 64)
+	if err != nil {
+		return 0, fmt.Errorf("malformed load average %q: %v", parts[0], err)
+	}
+	return avg, nil
+}
+
+func waitForIdle() error {
+	avg, err := loadAvg()
+	if err != nil {
+		return fmt.Errorf("error reading load average: %w", err)
+	}
+	if avg < idleMaxLoad {
+		return nil
+	}
+
+	log.Printf("Waiting for load average to drop below %.2f...", idleMaxLoad)
+
+	tick := time.NewTicker(30 * time.Second)
+	defer tick.Stop()
+
+	for _ = range tick.C {
+		avg, err := loadAvg()
+		if err != nil {
+			return fmt.Errorf("error reading load average: %w", err)
+		}
+		if avg < idleMaxLoad {
+			break
+		}
+
+		log.Printf("Waiting for load average to drop below %.2f...", idleMaxLoad)
+	}
+
+	return nil
+}
diff --git a/cmd/bench/idle_other.go b/cmd/bench/idle_other.go
new file mode 100644
index 0000000..10798b2
--- /dev/null
+++ b/cmd/bench/idle_other.go
@@ -0,0 +1,13 @@
+// Copyright 2021 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.
+
+//go:build !linux
+// +build !linux
+
+package main
+
+// waitForIdle is only implemented for Linux.
+func waitForIdle() error {
+	return nil
+}
diff --git a/cmd/bench/main.go b/cmd/bench/main.go
index 67d0d8b..7a3863b 100644
--- a/cmd/bench/main.go
+++ b/cmd/bench/main.go
@@ -10,6 +10,7 @@
 package main
 
 import (
+	"flag"
 	"fmt"
 	"log"
 	"os"
@@ -18,6 +19,8 @@
 	"strings"
 )
 
+var wait = flag.Bool("wait", true, "wait for system idle before starting benchmarking")
+
 func determineGOROOT() (string, error) {
 	g, ok := os.LookupEnv("GOROOT")
 	if ok {
@@ -60,6 +63,16 @@
 }
 
 func main() {
+	flag.Parse()
+
+	if *wait {
+		// We may be on a freshly booted VM. Wait for boot tasks to
+		// complete before continuing.
+		if err := waitForIdle(); err != nil {
+			log.Fatalf("Failed to wait for idle: %v", err)
+		}
+	}
+
 	goroot, err := determineGOROOT()
 	if err != nil {
 		log.Fatalf("Unable to determine GOROOT: %v", err)