cpu_linux.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // e.g. on android /proc/self/auxv is not accessible, so silently
  24. // ignore the error and leave Initialized = false
  25. return
  26. }
  27. bo := hostByteOrder()
  28. for len(buf) >= 2*(uintSize/8) {
  29. var tag, val uint
  30. switch uintSize {
  31. case 32:
  32. tag = uint(bo.Uint32(buf[0:]))
  33. val = uint(bo.Uint32(buf[4:]))
  34. buf = buf[8:]
  35. case 64:
  36. tag = uint(bo.Uint64(buf[0:]))
  37. val = uint(bo.Uint64(buf[8:]))
  38. buf = buf[16:]
  39. }
  40. switch tag {
  41. case _AT_HWCAP:
  42. hwCap = val
  43. case _AT_HWCAP2:
  44. hwCap2 = val
  45. }
  46. }
  47. doinit()
  48. Initialized = true
  49. }