github.com/demonoid81/containerd@v1.3.4/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" { 78 // Windows 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 variant { 100 case "8", "AArch64": 101 variant = "v8" 102 case "7", "7M", "?(12)", "?(13)", "?(14)", "?(15)", "?(16)", "?(17)": 103 variant = "v7" 104 case "6", "6TEJ": 105 variant = "v6" 106 case "5", "5T", "5TE", "5TEJ": 107 variant = "v5" 108 case "4", "4T": 109 variant = "v4" 110 case "3": 111 variant = "v3" 112 default: 113 variant = "unknown" 114 } 115 116 return variant 117 }