add CPUInfoStat() on Linux.

pull/4/head
Shirou WAKAYAMA 11 years ago
parent 955c9d23c0
commit e68563b7b3

@ -72,6 +72,15 @@ To becomes more useful, add more information.
- Platform family ex: debian
- Platform Version ex: Ubuntu 13.10
- CPUInfoStat()
- Processer
- Vendor ID
- Model name
- cores
- Mhz
- etc...
Some codes are ported some functions from Ohai. very thanks.

@ -20,6 +20,21 @@ type CPUTimesStat struct {
Stolen float32 `json:"stolen"`
}
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"`
Flags []string `json:"flags"`
}
func CPUCounts(logical bool) (int, error) {
return runtime.NumCPU(), nil
}
@ -28,3 +43,8 @@ func (c CPUTimesStat) String() string {
s, _ := json.Marshal(c)
return string(s)
}
func (c CPUInfoStat) String() string {
s, _ := json.Marshal(c)
return string(s)
}

@ -25,6 +25,55 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
return ret, nil
}
func CPUInfo() ([]CPUInfoStat, error) {
filename := "/proc/cpuinfo"
lines, _ := readLines(filename)
var ret []CPUInfoStat
var c CPUInfoStat
for _, line := range lines {
fields := strings.Split(line, ":")
if len(fields) < 2{
if c.VendorId != "" {
ret = append(ret, c)
}
continue
}
key := strings.TrimSpace(fields[0])
value := strings.TrimSpace(fields[1])
switch key {
case "processor":
c = CPUInfoStat{}
c.CPU = parseInt32(value)
case "vendor_id":
c.VendorId = value
case "cpu family":
c.Family = value
case "model":
c.Model = value
case "model name":
c.ModelName = value
case "stepping":
c.Stepping = parseInt32(value)
case "cpu MHz":
c.Mhz = parseFloat64(value)
case "cache size":
c.CacheSize = parseInt32(strings.Replace(value, " KB", "", 1))
case "physical id":
c.PhysicalID = value
case "core id":
c.CoreID = value
case "cpu cores":
c.Cores = parseInt32(value)
case "flags":
c.Flags = strings.Split(value, ",")
}
}
return ret, nil
}
func parseStatLine(line string) (*CPUTimesStat, error) {
fields := strings.Fields(line)

@ -43,3 +43,15 @@ func TestCPUTimeStat_String(t *testing.T) {
t.Errorf("CPUTimesStat string is invalid: %v", v)
}
}
func TestCpuInfo(t *testing.T) {
v, err := CPUInfo()
if err != nil {
t.Errorf("error %v", err)
}
for _, vv := range v {
if vv.ModelName == ""{
t.Errorf("could not get CPU Info: %v", vv)
}
}
}

Loading…
Cancel
Save