Merge branch 'master' of github.com:shirou/gopsutil

pull/4/head
Shirou WAKAYAMA 11 years ago
commit d612aa5931

@ -21,17 +21,17 @@ type CPUTimesStat struct {
} }
type CPUInfoStat struct { type CPUInfoStat struct {
CPU int32 `json:"cpu"` CPU int32 `json:"cpu"`
VendorId string `json:"vendorId"` VendorID string `json:"vendorId"`
Family string `json:"family"` Family string `json:"family"`
Model string `json:"model"` Model string `json:"model"`
Stepping int32 `json:"stepping"` Stepping int32 `json:"stepping"`
PhysicalID string `json:"physicalId"` PhysicalID string `json:"physicalId"`
CoreID string `json:"coreId"` CoreID string `json:"coreId"`
Cores int32 `json:"cores"` Cores int32 `json:"cores"`
ModelName string `json:"modelName"` ModelName string `json:"modelName"`
Mhz float64 `json:"mhz"` Mhz float64 `json:"mhz"`
CacheSize int32 `json:"cacheSize"` CacheSize int32 `json:"cacheSize"`
Flags []string `json:"flags"` Flags []string `json:"flags"`
} }

@ -3,7 +3,9 @@
package gopsutil package gopsutil
import ( import (
"regexp"
"strconv" "strconv"
"strings"
) )
// sys/resource.h // sys/resource.h
@ -48,3 +50,38 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
return ret, nil 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
}

@ -34,7 +34,7 @@ func CPUInfo() ([]CPUInfoStat, error) {
var c CPUInfoStat var c CPUInfoStat
for _, line := range lines { for _, line := range lines {
fields := strings.Split(line, ":") fields := strings.Split(line, ":")
if len(fields) < 2{ if len(fields) < 2 {
if c.VendorId != "" { if c.VendorId != "" {
ret = append(ret, c) ret = append(ret, c)
} }
@ -48,7 +48,7 @@ func CPUInfo() ([]CPUInfoStat, error) {
c = CPUInfoStat{} c = CPUInfoStat{}
c.CPU = parseInt32(value) c.CPU = parseInt32(value)
case "vendor_id": case "vendor_id":
c.VendorId = value c.VendorID = value
case "cpu family": case "cpu family":
c.Family = value c.Family = value
case "model": case "model":

@ -50,7 +50,8 @@ func TestCpuInfo(t *testing.T) {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
for _, vv := range v { for _, vv := range v {
if vv.ModelName == ""{ fmt.Println(vv)
if vv.ModelName == "" {
t.Errorf("could not get CPU Info: %v", vv) t.Errorf("could not get CPU Info: %v", vv)
} }
} }

@ -35,3 +35,8 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
}) })
return ret, nil return ret, nil
} }
func CPUInfo() ([]CPUInfoStat, error) {
var ret []CPUInfoStat
return ret, nil
}

Loading…
Cancel
Save