proc_cpuinfo_linux.mx raw

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