]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: randomize compilation order when race-enabled
authorJosh Bleecher Snyder <josharian@gmail.com>
Thu, 27 Apr 2017 14:37:40 +0000 (07:37 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Thu, 27 Apr 2017 19:27:22 +0000 (19:27 +0000)
There's been one failure on the race builder so far,
before we started sorting functions by length.

The race detector can only detect actual races,
and ordering functions by length might reduce the odds
of catching some kinds of races. Give it more to chew on.

Updates #20144

Change-Id: I0206ac182cb98b70a729dea9703ecb0fef54d2d0
Reviewed-on: https://go-review.googlesource.com/41973
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/compile/internal/gc/norace.go [new file with mode: 0644]
src/cmd/compile/internal/gc/pgen.go
src/cmd/compile/internal/gc/race.go [new file with mode: 0644]

diff --git a/src/cmd/compile/internal/gc/norace.go b/src/cmd/compile/internal/gc/norace.go
new file mode 100644 (file)
index 0000000..e00f0c4
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2017 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.
+
+// +build !race
+
+package gc
+
+const raceEnabled = false
index 363d53393ad65e34a2a6edd5ef600d03b12dca33..3cf5f60331013dae02bf938a4226f987d5b16477 100644 (file)
@@ -13,6 +13,7 @@ import (
        "cmd/internal/src"
        "cmd/internal/sys"
        "fmt"
+       "math/rand"
        "sort"
        "sync"
 )
@@ -253,12 +254,22 @@ func compileSSA(fn *Node, worker int) {
 // and waits for them to complete.
 func compileFunctions() {
        if len(compilequeue) != 0 {
-               // Compile the longest functions first,
-               // since they're most likely to be the slowest.
-               // This helps avoid stragglers.
-               obj.SortSlice(compilequeue, func(i, j int) bool {
-                       return compilequeue[i].Nbody.Len() > compilequeue[j].Nbody.Len()
-               })
+               if raceEnabled {
+                       // Randomize compilation order to try to shake out races.
+                       tmp := make([]*Node, len(compilequeue))
+                       perm := rand.Perm(len(compilequeue))
+                       for i, v := range perm {
+                               tmp[v] = compilequeue[i]
+                       }
+                       copy(compilequeue, tmp)
+               } else {
+                       // Compile the longest functions first,
+                       // since they're most likely to be the slowest.
+                       // This helps avoid stragglers.
+                       obj.SortSlice(compilequeue, func(i, j int) bool {
+                               return compilequeue[i].Nbody.Len() > compilequeue[j].Nbody.Len()
+                       })
+               }
                var wg sync.WaitGroup
                c := make(chan *Node)
                for i := 0; i < nBackendWorkers; i++ {
diff --git a/src/cmd/compile/internal/gc/race.go b/src/cmd/compile/internal/gc/race.go
new file mode 100644 (file)
index 0000000..78e1997
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2017 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.
+
+// +build race
+
+package gc
+
+const raceEnabled = true