cmd/bent: Change -R flag to supply ldflags for random linking, if unset.

It's quicker/better/easier to do it this way.

Change-Id: Ide76215c9ab4da02862ac171e0c4891e84a3e1e4
Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/614776
Reviewed-by: Michael Knyszek <mknyszek@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
diff --git a/cmd/bent/README.md b/cmd/bent/README.md
index e22385a..55030e8 100644
--- a/cmd/bent/README.md
+++ b/cmd/bent/README.md
@@ -56,7 +56,7 @@
 | | Less useful flags | |
 | -r string | skip get and build, just run.<br>string names Docker image if needed,<br>if not using Docker any non-empty will do. | -r f10cecc3eaac |
 | -s k | (build) shuffle flag, k = 0,1,2,3.<br>Randomize build order to reduce<br>sensitivity to other machine load  | -s 2 |
-| -G t/f | group runs by benchmark to reduce<br>time-of-day background noise (default true) | |
+| -G t/f | group runs by benchmark to reduce<br>time-of-day background noise (default false) | |
 | -X | do not reset go.mod<br>for experiments involving modifications<br>to build/\*/go.mod | |
 | -g | get benchmarks, but do not build or run | |
 | -W | print benchmark information as a markdown table | |
diff --git a/cmd/bent/bent.go b/cmd/bent/bent.go
index 5315f98..78f8277 100644
--- a/cmd/bent/bent.go
+++ b/cmd/bent/bent.go
@@ -67,7 +67,7 @@
 var container = ""
 var N = 1 // benchmark repeat count
 var R = 0 // randomized build/benchmark repeat count
-var groupRuns = true
+var groupRuns = false
 var list = false
 var initialize = false
 var test = false
@@ -192,15 +192,18 @@
 be cross-compiled.
 
 -R and -G help with timing noise studies; -R builds a binary for each
- index (builds can be parameterised by BENT_I) and -G groups benchmark
- runs together so that they experience most-similar platform noise
- (i.e., at a nearby time).
+index (builds can be parameterised by BENT_I) and -G groups benchmark
+runs together so that they experience most-similar platform noise
+(i.e., at a nearby time).  -R > 0 will set each configurations blank
+LdFlags to "-randlayout=0x${BENT_K}a${BENT_I}".  Bent supplies BENT_I,
+setting BENT_K allows runs with different sets of random link orders
 
 All the test binaries will appear in the subdirectory 'testbin', and
 test (benchmark) output will appear in the subdirectory 'bench' with
 the suffix '.stdout'.  The test output is grouped by configuration to
 allow easy benchmark comparisons with benchstat.  Other benchmarking
-results will also appear in 'bench'. `, os.Args[0], benchFile,
+results will also appear in 'bench'.
+`, os.Args[0], benchFile,
 			confFile)
 	}
 
@@ -349,28 +352,24 @@
 	// Process command-line-specified configurations.
 	// Expand environment variables mentioned there.
 	duplicates := make(map[string]bool)
-	for i, trial := range todo.Configurations {
+	for i := range todo.Configurations {
+		trial := &todo.Configurations[i]
 		trial.Name = os.ExpandEnv(trial.Name)
-		todo.Configurations[i].Name = trial.Name
 		if duplicates[trial.Name] {
-			if trial.Name == todo.Configurations[i].Name {
-				fmt.Printf("Saw duplicate configuration %s at index %d\n", trial.Name, i)
-			} else {
-				fmt.Printf("Saw duplicate configuration %s (originally %s) at index %d\n", trial.Name, todo.Configurations[i].Name, i)
-			}
+			fmt.Printf("Saw duplicate configuration %s at index %d\n", trial.Name, i)
 			os.Exit(1)
 		}
 		duplicates[trial.Name] = true
 		if configurations != nil {
 			_, present := configurations[trial.Name]
-			todo.Configurations[i].Disabled = !present
+			trial.Disabled = !present
 			if present {
 				configurations[trial.Name] = false
 			}
 		}
 		if root := trial.Root; len(root) != 0 {
 			// TODO(jfaller): I don't think we need this "/" anymore... investigate.
-			todo.Configurations[i].Root = os.ExpandEnv(root) + "/"
+			trial.Root = os.ExpandEnv(root) + "/"
 		}
 		if len(trial.RunWrapper) > 0 {
 			// Args will be expanded later with BENT_ environment variables injected.
@@ -380,6 +379,9 @@
 		// TODO would anyone ever make these depend on BENT_I etc?
 		trial.PgoGen = os.ExpandEnv(trial.PgoGen)
 		trial.PgoUse = os.ExpandEnv(trial.PgoUse)
+		if R > 0 && trial.LdFlags == "" {
+			trial.LdFlags = "-randlayout=0x${BENT_K}a${BENT_I}"
+		}
 	}
 	for b, v := range configurations {
 		if v {
diff --git a/cmd/bent/configs/configurations-random.toml b/cmd/bent/configs/configurations-random.toml
index d7eb529..6a2e49e 100644
--- a/cmd/bent/configs/configurations-random.toml
+++ b/cmd/bent/configs/configurations-random.toml
@@ -1,6 +1,10 @@
 # A sample configuration for benchmarking binaries whose function ordering has been randomized.
 # This is intended for use with the "-R n" flag and also requires Go > 1.22.
-# BENT_K can be provided externally, for example ina shell script doing iterated benchmarking
+# This is no longer necessary to use randomized linking because bent will automatically
+# insert LdFlags for randomized linking if nothing is supplied in the configuration, unless
+# a configuration needs to set additional LdFlags.
+#
+# BENT_K can be provided externally, for example in a shell script doing iterated benchmarking
 # or two create a different set of randomized link orders.
 #
 # Randomized function ordering creates differences in branch/target alignment which can then be
diff --git a/cmd/bent/configuration.go b/cmd/bent/configuration.go
index da0ebc2..89d08a1 100644
--- a/cmd/bent/configuration.go
+++ b/cmd/bent/configuration.go
@@ -197,7 +197,7 @@
 	if config.GcFlags != "" {
 		cmd.Args = append(cmd.Args, "-gcflags="+expandEnv(config.GcFlags, cmd.Env))
 	}
-	if config.LdFlags != "" {
+	if strings.TrimSpace(config.LdFlags) != "" {
 		cmd.Args = append(cmd.Args, "-ldflags="+expandEnv(config.LdFlags, cmd.Env))
 	}
 	cmd.Args = append(cmd.Args, bench.Repo)