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