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