sweet: limit build parallelism for cockroachdb

This is an attempt to get the PGO performance builders working.

Change-Id: Id4d2e5f59da323554a98aca45528efbcee9618a2
Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/631678
Reviewed-by: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/sweet/harnesses/cockroachdb.go b/sweet/harnesses/cockroachdb.go
index 2782bcd..4ffc2bc 100644
--- a/sweet/harnesses/cockroachdb.go
+++ b/sweet/harnesses/cockroachdb.go
@@ -94,6 +94,15 @@
 		return fmt.Errorf("failed to run %q: %v: output:\n%s", cmd, err, out)
 	}
 
+	// Limit build parallelism. Letting build parallelism go arbitrarily high causes
+	// OOM crashes on high core-count machines for PGO builds.
+	//
+	// TODO(mknyszek): Consider only doing this for PGO builds. Currently we have no way
+	// to tell if we're doing a PGO build from this context without somewhat fragile checks.
+	if _, ok := env.Lookup("GOMAXPROCS"); !ok && runtime.NumCPU() > 32 {
+		env = env.MustSet("GOMAXPROCS=32")
+	}
+
 	// Get the Go version. Then, finall build the cockroach binary with `go build`.
 	//
 	// Build the cockroach-short binary as it is functionally the same, but without