Skip to content

Commit

Permalink
Merge pull request #4953 from ImJasonH/cpuinfo
Browse files Browse the repository at this point in the history
Derive cpuinfo as needed, instead of at init-time
  • Loading branch information
mxpv committed Jan 19, 2021
2 parents 66fec3b + 363f2c3 commit 9c3f171
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
19 changes: 12 additions & 7 deletions platforms/cpuinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@ import (
"os"
"runtime"
"strings"
"sync"

"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log"
"github.com/pkg/errors"
)

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

func init() {
if isArmArch(runtime.GOARCH) {
cpuVariant = getCPUVariant()
} else {
cpuVariant = ""
}
var cpuVariantOnce sync.Once

func cpuVariant() string {
cpuVariantOnce.Do(func() {
if isArmArch(runtime.GOARCH) {
cpuVariantValue = getCPUVariant()
}
})
return cpuVariantValue
}

// For Linux, the kernel has already detected the ABI, ISA and Features.
Expand Down
2 changes: 1 addition & 1 deletion platforms/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func DefaultSpec() specs.Platform {
OS: runtime.GOOS,
Architecture: runtime.GOARCH,
// The Variant field will be empty if arch != ARM.
Variant: cpuVariant,
Variant: cpuVariant(),
}
}
2 changes: 1 addition & 1 deletion platforms/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestDefault(t *testing.T) {
expected := specs.Platform{
OS: runtime.GOOS,
Architecture: runtime.GOARCH,
Variant: cpuVariant,
Variant: cpuVariant(),
}
p := DefaultSpec()
if !reflect.DeepEqual(p, expected) {
Expand Down
4 changes: 2 additions & 2 deletions platforms/platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ func Parse(specifier string) (specs.Platform, error) {
if isKnownOS(p.OS) {
// picks a default architecture
p.Architecture = runtime.GOARCH
if p.Architecture == "arm" && cpuVariant != "v7" {
p.Variant = cpuVariant
if p.Architecture == "arm" && cpuVariant() != "v7" {
p.Variant = cpuVariant()
}

return p, nil
Expand Down
4 changes: 2 additions & 2 deletions platforms/platforms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestParseSelector(t *testing.T) {
defaultVariant = ""
)

if defaultArch == "arm" && cpuVariant != "v7" {
defaultVariant = cpuVariant
if defaultArch == "arm" && cpuVariant() != "v7" {
defaultVariant = cpuVariant()
}

for _, testcase := range []struct {
Expand Down

0 comments on commit 9c3f171

Please sign in to comment.