github.com/Andyfoo/golang/x/sys@v0.0.0-20190901054642-57c1bf301704/cpu/cpu_linux.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //+build !amd64,!amd64p32,!386
     6  
     7  package cpu
     8  
     9  import (
    10  	"io/ioutil"
    11  )
    12  
    13  const (
    14  	_AT_HWCAP  = 16
    15  	_AT_HWCAP2 = 26
    16  
    17  	procAuxv = "/proc/self/auxv"
    18  
    19  	uintSize = int(32 << (^uint(0) >> 63))
    20  )
    21  
    22  // For those platforms don't have a 'cpuid' equivalent we use HWCAP/HWCAP2
    23  // These are initialized in cpu_$GOARCH.go
    24  // and should not be changed after they are initialized.
    25  var hwCap uint
    26  var hwCap2 uint
    27  
    28  func init() {
    29  	buf, err := ioutil.ReadFile(procAuxv)
    30  	if err != nil {
    31  		// e.g. on android /proc/self/auxv is not accessible, so silently
    32  		// ignore the error and leave Initialized = false
    33  		return
    34  	}
    35  
    36  	bo := hostByteOrder()
    37  	for len(buf) >= 2*(uintSize/8) {
    38  		var tag, val uint
    39  		switch uintSize {
    40  		case 32:
    41  			tag = uint(bo.Uint32(buf[0:]))
    42  			val = uint(bo.Uint32(buf[4:]))
    43  			buf = buf[8:]
    44  		case 64:
    45  			tag = uint(bo.Uint64(buf[0:]))
    46  			val = uint(bo.Uint64(buf[8:]))
    47  			buf = buf[16:]
    48  		}
    49  		switch tag {
    50  		case _AT_HWCAP:
    51  			hwCap = val
    52  		case _AT_HWCAP2:
    53  			hwCap2 = val
    54  		}
    55  	}
    56  	doinit()
    57  
    58  	Initialized = true
    59  }