cmd/bench: clean build cache in between bent and sweet

Currently /workdir/gocache uses up 9.3 GiB of 16 GiB of available
space in /workdir, causing Sweet to not have enough room to copy
tile38's assets over by the time we get to that.

Thing is, in these benchmarks we basically don't care about the build
cache at all since we only build everything once. Clean the cache at
least once in between bent and Sweet to let Sweet complete.

Fixes golang/go#58765.

Change-Id: I6f10a7eee0218ca2d2b0358a11cfd41af00b0667
Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/472235
Run-TryBot: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
diff --git a/cmd/bench/main.go b/cmd/bench/main.go
index 13aad3c..67668d7 100644
--- a/cmd/bench/main.go
+++ b/cmd/bench/main.go
@@ -77,6 +77,22 @@
 		pass = false
 		log.Printf("Error running bent: %v", err)
 	}
+	if os.Getenv("GO_BUILDER_NAME") != "" {
+		// On a builder, clean the Go cache in between bent and Sweet.
+		// The build cache can end up using a large portion of the builder's
+		// disk space (~60%), making Sweet run out and fail. Generally speaking
+		// we don't need the build cache because we're going to be doing every
+		// build exactly once from scratch (excluding build benchmarks, which
+		// arrange for a cacheless build their own way). However, we don't want
+		// to do this on a regular development machine because we might want to
+		// run benchmarks with the same toolchain again.
+		//
+		// Note that we only need to clean the cache with one toolchain because
+		// the build cache is shared.
+		if err := cleanGoCache(tcs[0]); err != nil {
+			return fmt.Errorf("failed to clean Go cache: %w", err)
+		}
+	}
 	if err := sweet(tcs); err != nil {
 		pass = false
 		log.Printf("Error running sweet: %v", err)
@@ -87,6 +103,17 @@
 	return nil
 }
 
+func cleanGoCache(tc *toolchain) error {
+	wd, err := os.Getwd()
+	if err != nil {
+		return err
+	}
+	if err := tc.Go.Do(wd, "clean", "-cache"); err != nil {
+		return fmt.Errorf("toolchain %s: %w", tc.Name, err)
+	}
+	return nil
+}
+
 func main() {
 	flag.Parse()