github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/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  	// handle edge case for Raspberry Pi ARMv6 devices (which due to a kernel quirk, report "CPU architecture: 7")
   100  	// https://www.raspberrypi.org/forums/viewtopic.php?t=12614
   101  	if runtime.GOARCH == "arm" && variant == "7" {
   102  		model, err := getCPUInfo("model name")
   103  		if err == nil && strings.HasPrefix(strings.ToLower(model), "armv6-compatible") {
   104  			variant = "6"
   105  		}
   106  	}
   107  
   108  	switch strings.ToLower(variant) {
   109  	case "8", "aarch64":
   110  		// special case: if running a 32-bit userspace on aarch64, the variant should be "v7"
   111  		if runtime.GOARCH == "arm" {
   112  			variant = "v7"
   113  		} else {
   114  			variant = "v8"
   115  		}
   116  	case "7", "7m", "?(12)", "?(13)", "?(14)", "?(15)", "?(16)", "?(17)":
   117  		variant = "v7"
   118  	case "6", "6tej":
   119  		variant = "v6"
   120  	case "5", "5t", "5te", "5tej":
   121  		variant = "v5"
   122  	case "4", "4t":
   123  		variant = "v4"
   124  	case "3":
   125  		variant = "v3"
   126  	default:
   127  		variant = "unknown"
   128  	}
   129  
   130  	return variant
   131  }