cpu_linux.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. //+build !amd64,!amd64p32,!386
  5. package cpu
  6. import (
  7. "encoding/binary"
  8. "io/ioutil"
  9. )
  10. const (
  11. _AT_HWCAP = 16
  12. _AT_HWCAP2 = 26
  13. procAuxv = "/proc/self/auxv"
  14. uintSize uint = 32 << (^uint(0) >> 63)
  15. )
  16. // For those platforms don't have a 'cpuid' equivalent we use HWCAP/HWCAP2
  17. // These are initialized in cpu_$GOARCH.go
  18. // and should not be changed after they are initialized.
  19. var HWCap uint
  20. var HWCap2 uint
  21. func init() {
  22. buf, err := ioutil.ReadFile(procAuxv)
  23. if err != nil {
  24. panic("read proc auxv failed: " + err.Error())
  25. }
  26. pb := int(uintSize / 8)
  27. for i := 0; i < len(buf)-pb*2; i += pb * 2 {
  28. var tag, val uint
  29. switch uintSize {
  30. case 32:
  31. tag = uint(binary.LittleEndian.Uint32(buf[i:]))
  32. val = uint(binary.LittleEndian.Uint32(buf[i+pb:]))
  33. case 64:
  34. tag = uint(binary.LittleEndian.Uint64(buf[i:]))
  35. val = uint(binary.LittleEndian.Uint64(buf[i+pb:]))
  36. }
  37. switch tag {
  38. case _AT_HWCAP:
  39. HWCap = val
  40. case _AT_HWCAP2:
  41. HWCap2 = val
  42. }
  43. }
  44. doinit()
  45. }