Skip to content

Commit

Permalink
internal/cpu: move GODEBUGCPU options into GODEBUG
Browse files Browse the repository at this point in the history
Change internal/cpu feature configuration to use
GODEBUG=cpu.feature1=value,cpu.feature2=value...
instead of GODEBUGCPU=feature1=value,feature2=value... .

This is not a backwards compatibility breaking change
since GODEBUGCPU was introduced in go1.11 as an
undocumented compiler experiment.

Fixes #28757

Change-Id: Ib21b3fed2334baeeb061a722ab1eb513d1137e87
Reviewed-on: https://go-review.googlesource.com/c/149578
Run-TryBot: Martin Möhrmann <martisch@uos.de>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
  • Loading branch information
martisch committed Nov 14, 2018
1 parent 50941e9 commit 0a40d45
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
29 changes: 17 additions & 12 deletions src/internal/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
// used by the Go standard library.
package cpu

// DebugOptions is set to true by the runtime if the OS supports GODEBUGCPU.
// DebugOptions is set to true by the runtime if the OS supports reading
// GODEBUG early in runtime startup.
// This should not be changed after it is initialized.
var DebugOptions bool

Expand Down Expand Up @@ -130,13 +131,14 @@ type s390x struct {

// Initialize examines the processor and sets the relevant variables above.
// This is called by the runtime package early in program initialization,
// before normal init functions are run. env is set by runtime if the OS supports GODEBUGCPU.
// before normal init functions are run. env is set by runtime if the OS supports
// cpu feature options in GODEBUG.
func Initialize(env string) {
doinit()
processOptions(env)
}

// options contains the cpu debug options that can be used in GODEBUGCPU.
// options contains the cpu debug options that can be used in GODEBUG.
// Options are arch dependent and are added by the arch specific doinit functions.
// Features that are mandatory for the specific GOARCH should not be added to options
// (e.g. SSE2 on amd64).
Expand All @@ -146,16 +148,16 @@ var options []option
type option struct {
Name string
Feature *bool
Specified bool // whether feature value was specified in GODEBUGCPU
Specified bool // whether feature value was specified in GODEBUG
Enable bool // whether feature should be enabled
Required bool // whether feature is mandatory and can not be disabled
}

// processOptions enables or disables CPU feature values based on the parsed env string.
// The env string is expected to be of the form feature1=value1,feature2=value2...
// The env string is expected to be of the form cpu.feature1=value1,cpu.feature2=value2...
// where feature names is one of the architecture specifc list stored in the
// cpu packages options variable and values are either 'on' or 'off'.
// If env contains all=off then all cpu features referenced through the options
// If env contains cpu.all=off then all cpu features referenced through the options
// variable are disabled. Other feature names and values result in warning messages.
func processOptions(env string) {
field:
Expand All @@ -167,12 +169,15 @@ field:
} else {
field, env = env[:i], env[i+1:]
}
if len(field) < 4 || field[:4] != "cpu." {
continue
}
i = indexByte(field, '=')
if i < 0 {
print("GODEBUGCPU: no value specified for \"", field, "\"\n")
print("GODEBUG: no value specified for \"", field, "\"\n")
continue
}
key, value := field[:i], field[i+1:]
key, value := field[4:i], field[i+1:] // e.g. "SSE2", "on"

var enable bool
switch value {
Expand All @@ -181,7 +186,7 @@ field:
case "off":
enable = false
default:
print("GODEBUGCPU: value \"", value, "\" not supported for option ", key, "\n")
print("GODEBUG: value \"", value, "\" not supported for cpu option \"", key, "\"\n")
continue field
}

Expand All @@ -201,7 +206,7 @@ field:
}
}

print("GODEBUGCPU: unknown cpu feature \"", key, "\"\n")
print("GODEBUG: unknown cpu feature \"", key, "\"\n")
}

for _, o := range options {
Expand All @@ -210,12 +215,12 @@ field:
}

if o.Enable && !*o.Feature {
print("GODEBUGCPU: can not enable \"", o.Name, "\", missing hardware support\n")
print("GODEBUG: can not enable \"", o.Name, "\", missing CPU support\n")
continue
}

if !o.Enable && o.Required {
print("GODEBUGCPU: can not disable \"", o.Name, "\", required feature\n")
print("GODEBUG: can not disable \"", o.Name, "\", required CPU feature\n")
continue
}

Expand Down
8 changes: 4 additions & 4 deletions src/internal/cpu/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func runDebugOptionsTest(t *testing.T, test string, options string) {

testenv.MustHaveExec(t)

env := "GODEBUGCPU=" + options
env := "GODEBUG=" + options

cmd := exec.Command(os.Args[0], "-test.run="+test)
cmd.Env = append(cmd.Env, env)
Expand All @@ -58,14 +58,14 @@ func runDebugOptionsTest(t *testing.T, test string, options string) {
}

func TestDisableAllCapabilities(t *testing.T) {
runDebugOptionsTest(t, "TestAllCapabilitiesDisabled", "all=off")
runDebugOptionsTest(t, "TestAllCapabilitiesDisabled", "cpu.all=off")
}

func TestAllCapabilitiesDisabled(t *testing.T) {
MustHaveDebugOptionsSupport(t)

if os.Getenv("GODEBUGCPU") != "all=off" {
t.Skipf("skipping test: GODEBUGCPU=all=off not set")
if os.Getenv("GODEBUG") != "cpu.all=off" {
t.Skipf("skipping test: GODEBUG=cpu.all=off not set")
}

for _, o := range Options {
Expand Down
12 changes: 6 additions & 6 deletions src/internal/cpu/cpu_x86_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ func TestX86ifAVX2hasAVX(t *testing.T) {
}

func TestDisableSSE2(t *testing.T) {
runDebugOptionsTest(t, "TestSSE2DebugOption", "sse2=off")
runDebugOptionsTest(t, "TestSSE2DebugOption", "cpu.sse2=off")
}

func TestSSE2DebugOption(t *testing.T) {
MustHaveDebugOptionsSupport(t)

if os.Getenv("GODEBUGCPU") != "sse2=off" {
t.Skipf("skipping test: GODEBUGCPU=sse2=off not set")
if os.Getenv("GODEBUG") != "cpu.sse2=off" {
t.Skipf("skipping test: GODEBUG=cpu.sse2=off not set")
}

want := runtime.GOARCH != "386" // SSE2 can only be disabled on 386.
Expand All @@ -37,14 +37,14 @@ func TestSSE2DebugOption(t *testing.T) {
}

func TestDisableSSE3(t *testing.T) {
runDebugOptionsTest(t, "TestSSE3DebugOption", "sse3=off")
runDebugOptionsTest(t, "TestSSE3DebugOption", "cpu.sse3=off")
}

func TestSSE3DebugOption(t *testing.T) {
MustHaveDebugOptionsSupport(t)

if os.Getenv("GODEBUGCPU") != "sse3=off" {
t.Skipf("skipping test: GODEBUGCPU=sse3=off not set")
if os.Getenv("GODEBUG") != "cpu.sse3=off" {
t.Skipf("skipping test: GODEBUG=cpu.sse3=off not set")
}

want := false
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,18 +476,18 @@ const (
_GoidCacheBatch = 16
)

// cpuinit extracts the environment variable GODEBUGCPU from the environment on
// Linux and Darwin and calls internal/cpu.Initialize.
// cpuinit extracts the environment variable GODEBUG from the environment on
// Unix-like operating systems and calls internal/cpu.Initialize.
func cpuinit() {
const prefix = "GODEBUGCPU="
const prefix = "GODEBUG="
var env string

switch GOOS {
case "aix", "darwin", "dragonfly", "freebsd", "netbsd", "openbsd", "solaris", "linux":
cpu.DebugOptions = true

// Similar to goenv_unix but extracts the environment value for
// GODEBUGCPU directly.
// GODEBUG directly.
// TODO(moehrmann): remove when general goenvs() can be called before cpuinit()
n := int32(0)
for argv_index(argv, argc+1+n) != nil {
Expand Down

0 comments on commit 0a40d45

Please sign in to comment.