Testing for linux cpuinfo
This commit is contained in:
1 parent
cbc9e4a0b4
commit
ea5e79a46a
8 files changed
+172
-20
No files matched your search
@@ -0,0 +1,50 @@
|
||||
//go:build linux
|
||||
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var numberRegex *regexp.Regexp = regexp.MustCompile(`\d+`)
|
||||
|
||||
func getLinuxCpuInfo() (ProcessorInformation, error) {
|
||||
procFile, err := os.Open("/proc/meminfo")
|
||||
if err != nil {
|
||||
return ProcessorInformation{}, fmt.Errorf("Could not find /proc/meminfo. Are you sure you are on linux?")
|
||||
}
|
||||
defer procFile.Close()
|
||||
|
||||
fileReader := bufio.NewScanner(procFile)
|
||||
|
||||
//var idleTime uint64
|
||||
//var kernelTime uint64
|
||||
//var userTime uint64
|
||||
|
||||
for fileReader.Scan() {
|
||||
line := fileReader.Text()
|
||||
|
||||
if strings.HasPrefix(line, "cpu ") {
|
||||
fields := strings.Split(line, " ")
|
||||
for _, field := range fields {
|
||||
fmt.Print(field)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ProcessorInformation{}, nil
|
||||
}
|
||||
|
||||
func GetCPUInformation() ProcessorInformation {
|
||||
info, err := getLinuxCpuInfo()
|
||||
if err != nil {
|
||||
log.Printf("Could not read processor info. Error: %v", err)
|
||||
return ProcessorInformation{}
|
||||
}
|
||||
return info
|
||||
}
|
||||
Reference in new issue
Block a user