cpu_linux.go 1.3 KB

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