proc_cpuinfo_linux.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2022 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. //go:build linux && arm64
  5. // +build linux,arm64
  6. package cpu
  7. import (
  8. "errors"
  9. "io"
  10. "os"
  11. "strings"
  12. )
  13. func readLinuxProcCPUInfo() error {
  14. f, err := os.Open("/proc/cpuinfo")
  15. if err != nil {
  16. return err
  17. }
  18. defer f.Close()
  19. var buf [1 << 10]byte // enough for first CPU
  20. n, err := io.ReadFull(f, buf[:])
  21. if err != nil && err != io.ErrUnexpectedEOF {
  22. return err
  23. }
  24. in := string(buf[:n])
  25. const features = "\nFeatures : "
  26. i := strings.Index(in, features)
  27. if i == -1 {
  28. return errors.New("no CPU features found")
  29. }
  30. in = in[i+len(features):]
  31. if i := strings.Index(in, "\n"); i != -1 {
  32. in = in[:i]
  33. }
  34. m := map[string]*bool{}
  35. initOptions() // need it early here; it's harmless to call twice
  36. for _, o := range options {
  37. m[o.Name] = o.Feature
  38. }
  39. // The EVTSTRM field has alias "evstrm" in Go, but Linux calls it "evtstrm".
  40. m["evtstrm"] = &ARM64.HasEVTSTRM
  41. for _, f := range strings.Fields(in) {
  42. if p, ok := m[f]; ok {
  43. *p = true
  44. }
  45. }
  46. return nil
  47. }