Skip to content

Commit 9c3f171

Browse files
authored
Merge pull request #4953 from ImJasonH/cpuinfo
Derive cpuinfo as needed, instead of at init-time
2 parents 66fec3b + 363f2c3 commit 9c3f171

5 files changed

Lines changed: 18 additions & 13 deletions

File tree

platforms/cpuinfo.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,26 @@ import (
2121
"os"
2222
"runtime"
2323
"strings"
24+
"sync"
2425

2526
"github.com/containerd/containerd/errdefs"
2627
"github.com/containerd/containerd/log"
2728
"github.com/pkg/errors"
2829
)
2930

3031
// Present the ARM instruction set architecture, eg: v7, v8
31-
var cpuVariant string
32+
// Don't use this value directly; call cpuVariant() instead.
33+
var cpuVariantValue string
3234

33-
func init() {
34-
if isArmArch(runtime.GOARCH) {
35-
cpuVariant = getCPUVariant()
36-
} else {
37-
cpuVariant = ""
38-
}
35+
var cpuVariantOnce sync.Once
36+
37+
func cpuVariant() string {
38+
cpuVariantOnce.Do(func() {
39+
if isArmArch(runtime.GOARCH) {
40+
cpuVariantValue = getCPUVariant()
41+
}
42+
})
43+
return cpuVariantValue
3944
}
4045

4146
// For Linux, the kernel has already detected the ABI, ISA and Features.

platforms/defaults.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ func DefaultSpec() specs.Platform {
3333
OS: runtime.GOOS,
3434
Architecture: runtime.GOARCH,
3535
// The Variant field will be empty if arch != ARM.
36-
Variant: cpuVariant,
36+
Variant: cpuVariant(),
3737
}
3838
}

platforms/defaults_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestDefault(t *testing.T) {
2828
expected := specs.Platform{
2929
OS: runtime.GOOS,
3030
Architecture: runtime.GOARCH,
31-
Variant: cpuVariant,
31+
Variant: cpuVariant(),
3232
}
3333
p := DefaultSpec()
3434
if !reflect.DeepEqual(p, expected) {

platforms/platforms.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ func Parse(specifier string) (specs.Platform, error) {
189189
if isKnownOS(p.OS) {
190190
// picks a default architecture
191191
p.Architecture = runtime.GOARCH
192-
if p.Architecture == "arm" && cpuVariant != "v7" {
193-
p.Variant = cpuVariant
192+
if p.Architecture == "arm" && cpuVariant() != "v7" {
193+
p.Variant = cpuVariant()
194194
}
195195

196196
return p, nil

platforms/platforms_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func TestParseSelector(t *testing.T) {
3131
defaultVariant = ""
3232
)
3333

34-
if defaultArch == "arm" && cpuVariant != "v7" {
35-
defaultVariant = cpuVariant
34+
if defaultArch == "arm" && cpuVariant() != "v7" {
35+
defaultVariant = cpuVariant()
3636
}
3737

3838
for _, testcase := range []struct {

0 commit comments

Comments
 (0)