github.com/haraldrudell/parl@v0.4.176/punix/processor-linux.go (about)

     1  //go:build linux
     2  
     3  /*
     4  © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     5  ISC License
     6  */
     7  
     8  package punix
     9  
    10  import (
    11  	"os"
    12  	"strings"
    13  
    14  	"github.com/haraldrudell/parl/perrors"
    15  )
    16  
    17  const (
    18  	procCpuInfo     = "/proc/cpuinfo"
    19  	prefixModelName = "model name\t: "
    20  )
    21  
    22  // Processor returns human readable string like "Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz"
    23  func Processor() (model string, err error) {
    24  
    25  	// get byte array of cpu information
    26  	var byts []byte
    27  	if byts, err = os.ReadFile(procCpuInfo); perrors.Is(&err, "ReadFile %s %w", procCpuInfo, err) {
    28  		return // read error return
    29  	}
    30  
    31  	// linear-search lines to find first cpu model line
    32  	//	- there is one per core, but they are typically the same
    33  	for _, line := range strings.Split(string(byts), "\n") {
    34  		if m := strings.TrimPrefix(line, prefixModelName); m != line {
    35  			model = m
    36  			return // model found return
    37  		}
    38  	}
    39  
    40  	return // model not available return
    41  }