diff --git a/cpu.go b/cpu.go index 708b3bad9dd842fd090624a1893042cdeb16407c..e8fb19bc3075c6ef872566436217061a7f452e69 100644 --- a/cpu.go +++ b/cpu.go @@ -21,17 +21,17 @@ type CPUTimesStat struct { } type CPUInfoStat struct { - CPU int32 `json:"cpu"` - VendorId string `json:"vendorId"` - Family string `json:"family"` - Model string `json:"model"` - Stepping int32 `json:"stepping"` - PhysicalID string `json:"physicalId"` - CoreID string `json:"coreId"` - Cores int32 `json:"cores"` - ModelName string `json:"modelName"` - Mhz float64 `json:"mhz"` - CacheSize int32 `json:"cacheSize"` + CPU int32 `json:"cpu"` + VendorID string `json:"vendorId"` + Family string `json:"family"` + Model string `json:"model"` + Stepping int32 `json:"stepping"` + PhysicalID string `json:"physicalId"` + CoreID string `json:"coreId"` + Cores int32 `json:"cores"` + ModelName string `json:"modelName"` + Mhz float64 `json:"mhz"` + CacheSize int32 `json:"cacheSize"` Flags []string `json:"flags"` } diff --git a/cpu_freebsd.go b/cpu_freebsd.go index 86a3102d50ddb602d7b56efd33783fb56a5c58f3..a4168249a4139db11ebc2595a78b0f8c74fb0960 100644 --- a/cpu_freebsd.go +++ b/cpu_freebsd.go @@ -3,7 +3,9 @@ package gopsutil import ( + "regexp" "strconv" + "strings" ) // sys/resource.h @@ -48,3 +50,38 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { return ret, nil } + +// Returns only one CPUInfoStat on FreeBSD +func CPUInfo() ([]CPUInfoStat, error) { + filename := "/var/run/dmesg.boot" + lines, _ := readLines(filename) + + var ret []CPUInfoStat + + c := CPUInfoStat{} + for _, line := range lines { + if matches := regexp.MustCompile(`CPU:\s+(.+) \(([\d.]+).+\)`).FindStringSubmatch(line); matches != nil { + c.ModelName = matches[1] + c.Mhz = parseFloat64(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 = parseInt32(matches[5]) + } else if matches := regexp.MustCompile(`Features=.+<(.+)>`).FindStringSubmatch(line); matches != nil { + for _, v := range strings.Split(matches[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], ",") { + 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 = parseInt32(matches[1]) + } + + } + + return append(ret, c), nil +} diff --git a/cpu_linux.go b/cpu_linux.go index 25f165af124d2c3f69d5a8782787db1208200297..ae17168530da4958cd533b1e40688525f2710a2f 100644 --- a/cpu_linux.go +++ b/cpu_linux.go @@ -34,7 +34,7 @@ func CPUInfo() ([]CPUInfoStat, error) { var c CPUInfoStat for _, line := range lines { fields := strings.Split(line, ":") - if len(fields) < 2{ + if len(fields) < 2 { if c.VendorId != "" { ret = append(ret, c) } @@ -48,7 +48,7 @@ func CPUInfo() ([]CPUInfoStat, error) { c = CPUInfoStat{} c.CPU = parseInt32(value) case "vendor_id": - c.VendorId = value + c.VendorID = value case "cpu family": c.Family = value case "model": diff --git a/cpu_test.go b/cpu_test.go index 0463bcf353e2ee0d4215e66d294fd08008a15ce6..5c6ca2570831ff85099c87ffffd88c02300751fa 100644 --- a/cpu_test.go +++ b/cpu_test.go @@ -50,7 +50,8 @@ func TestCpuInfo(t *testing.T) { t.Errorf("error %v", err) } for _, vv := range v { - if vv.ModelName == ""{ + fmt.Println(vv) + if vv.ModelName == "" { t.Errorf("could not get CPU Info: %v", vv) } }