1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-05-01 13:48:52 +08:00

change to use sysctl machdep.cpu. for CpuInfo on darwin.

This commit is contained in:
Shirou WAKAYAMA 2014-08-08 23:50:15 +09:00
parent a199f2d854
commit 2d08e158d6

View File

@ -3,7 +3,7 @@
package gopsutil package gopsutil
import ( import (
"regexp" "os/exec"
"strconv" "strconv"
"strings" "strings"
) )
@ -53,34 +53,45 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
// Returns only one CPUInfoStat on FreeBSD // Returns only one CPUInfoStat on FreeBSD
func CPUInfo() ([]CPUInfoStat, error) { func CPUInfo() ([]CPUInfoStat, error) {
filename := "/var/run/dmesg.boot"
lines, _ := readLines(filename)
var ret []CPUInfoStat var ret []CPUInfoStat
out, err := exec.Command("/usr/sbin/sysctl", "machdep.cpu").Output()
if err != nil {
return ret, err
}
c := CPUInfoStat{} c := CPUInfoStat{}
for _, line := range lines { for _, line := range strings.Split(string(out), "\n") {
if matches := regexp.MustCompile(`CPU:\s+(.+) \(([\d.]+).+\)`).FindStringSubmatch(line); matches != nil { values := strings.Fields(line)
c.ModelName = matches[1]
c.Mhz = mustParseFloat64(matches[2]) if strings.HasPrefix(line, "machdep.cpu.brand_string") {
} else if matches := regexp.MustCompile(`Origin = "(.+)" Id = (.+) Family = (.+) Model = (.+) Stepping = (.+)`).FindStringSubmatch(line); matches != nil { c.ModelName = strings.Join(values[1:], " ")
c.VendorID = matches[1] } else if strings.HasPrefix(line, "machdep.cpu.family") {
c.Family = matches[3] c.Family = values[1]
c.Model = matches[4] } else if strings.HasPrefix(line, "machdep.cpu.model") {
c.Stepping = mustParseInt32(matches[5]) c.Model = values[1]
} else if matches := regexp.MustCompile(`Features=.+<(.+)>`).FindStringSubmatch(line); matches != nil { } else if strings.HasPrefix(line, "machdep.cpu.stepping") {
for _, v := range strings.Split(matches[1], ",") { c.Stepping = mustParseInt32(values[1])
} else if strings.HasPrefix(line, "machdep.cpu.features") {
for _, v := range values[1:] {
c.Flags = append(c.Flags, strings.ToLower(v)) c.Flags = append(c.Flags, strings.ToLower(v))
} }
} else if matches := regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`).FindStringSubmatch(line); matches != nil { } else if strings.HasPrefix(line, "machdep.cpu.leaf7_features") {
for _, v := range strings.Split(matches[1], ",") { for _, v := range values[1:] {
c.Flags = append(c.Flags, strings.ToLower(v)) c.Flags = append(c.Flags, strings.ToLower(v))
} }
} else if matches := regexp.MustCompile(`Logical CPUs per core: (\d+)`).FindStringSubmatch(line); matches != nil { } else if strings.HasPrefix(line, "machdep.cpu.extfeatures") {
// FIXME: no this line? for _, v := range values[1:] {
c.Cores = mustParseInt32(matches[1]) c.Flags = append(c.Flags, strings.ToLower(v))
}
} else if strings.HasPrefix(line, "machdep.cpu.core_count") {
c.Cores = mustParseInt32(values[1])
} }
// TODO:
// c.Mhz = mustParseFloat64(values[1])
// c.VendorID = matches[1]
} }
return append(ret, c), nil return append(ret, c), nil