github.com/containerd/Containerd@v1.4.13/platforms/cpuinfo.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package platforms 18 19 import ( 20 "bufio" 21 "os" 22 "runtime" 23 "strings" 24 25 "github.com/containerd/containerd/errdefs" 26 "github.com/containerd/containerd/log" 27 "github.com/pkg/errors" 28 ) 29 30 // Present the ARM instruction set architecture, eg: v7, v8 31 var cpuVariant string 32 33 func init() { 34 if isArmArch(runtime.GOARCH) { 35 cpuVariant = getCPUVariant() 36 } else { 37 cpuVariant = "" 38 } 39 } 40 41 // For Linux, the kernel has already detected the ABI, ISA and Features. 42 // So we don't need to access the ARM registers to detect platform information 43 // by ourselves. We can just parse these information from /proc/cpuinfo 44 func getCPUInfo(pattern string) (info string, err error) { 45 if !isLinuxOS(runtime.GOOS) { 46 return "", errors.Wrapf(errdefs.ErrNotImplemented, "getCPUInfo for OS %s", runtime.GOOS) 47 } 48 49 cpuinfo, err := os.Open("/proc/cpuinfo") 50 if err != nil { 51 return "", err 52 } 53 defer cpuinfo.Close() 54 55 // Start to Parse the Cpuinfo line by line. For SMP SoC, we parse 56 // the first core is enough. 57 scanner := bufio.NewScanner(cpuinfo) 58 for scanner.Scan() { 59 newline := scanner.Text() 60 list := strings.Split(newline, ":") 61 62 if len(list) > 1 && strings.EqualFold(strings.TrimSpace(list[0]), pattern) { 63 return strings.TrimSpace(list[1]), nil 64 } 65 } 66 67 // Check whether the scanner encountered errors 68 err = scanner.Err() 69 if err != nil { 70 return "", err 71 } 72 73 return "", errors.Wrapf(errdefs.ErrNotFound, "getCPUInfo for pattern: %s", pattern) 74 } 75 76 func getCPUVariant() string { 77 if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { 78 // Windows/Darwin only supports v7 for ARM32 and v8 for ARM64 and so we can use 79 // runtime.GOARCH to determine the variants 80 var variant string 81 switch runtime.GOARCH { 82 case "arm64": 83 variant = "v8" 84 case "arm": 85 variant = "v7" 86 default: 87 variant = "unknown" 88 } 89 90 return variant 91 } 92 93 variant, err := getCPUInfo("Cpu architecture") 94 if err != nil { 95 log.L.WithError(err).Error("failure getting variant") 96 return "" 97 } 98 99 switch strings.ToLower(variant) { 100 case "8", "aarch64": 101 // special case: if running a 32-bit userspace on aarch64, the variant should be "v7" 102 if runtime.GOARCH == "arm" { 103 variant = "v7" 104 } else { 105 variant = "v8" 106 } 107 case "7", "7m", "?(12)", "?(13)", "?(14)", "?(15)", "?(16)", "?(17)": 108 variant = "v7" 109 case "6", "6tej": 110 variant = "v6" 111 case "5", "5t", "5te", "5tej": 112 variant = "v5" 113 case "4", "4t": 114 variant = "v4" 115 case "3": 116 variant = "v3" 117 default: 118 variant = "unknown" 119 } 120 121 return variant 122 }