shirou_gopsutil/cpu_linux.go

123 lines
2.7 KiB
Go
Raw Normal View History

2014-04-18 15:34:47 +08:00
// +build linux
2014-04-22 08:44:22 +08:00
package gopsutil
2014-04-18 15:34:47 +08:00
import (
2014-04-24 15:15:57 +08:00
"errors"
2014-04-18 15:34:47 +08:00
"strconv"
"strings"
)
2014-04-30 15:16:07 +08:00
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
2014-04-18 15:34:47 +08:00
filename := "/proc/stat"
2014-04-30 15:22:54 +08:00
lines, _ := readLines(filename)
2014-05-01 11:47:43 +08:00
ret := make([]CPUTimesStat, 0, len(lines))
2014-04-18 15:34:47 +08:00
for _, line := range lines {
2014-04-24 15:15:57 +08:00
ct, err := parseStatLine(line)
if err != nil {
2014-04-18 15:34:47 +08:00
continue
}
ret = append(ret, *ct)
2014-04-18 15:34:47 +08:00
2014-04-24 15:15:57 +08:00
}
return ret, nil
}
2014-04-18 15:34:47 +08:00
2014-05-16 17:12:18 +08:00
func CPUInfo() ([]CPUInfoStat, error) {
filename := "/proc/cpuinfo"
lines, _ := readLines(filename)
var ret []CPUInfoStat
var c CPUInfoStat
for _, line := range lines {
fields := strings.Split(line, ":")
2014-05-16 17:39:17 +08:00
if len(fields) < 2 {
2014-05-16 17:12:18 +08:00
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":
2014-05-16 17:39:17 +08:00
c.VendorID = value
2014-05-16 17:12:18 +08:00
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) {
2014-04-24 15:15:57 +08:00
fields := strings.Fields(line)
if strings.HasPrefix(fields[0], "cpu") == false {
2014-04-30 15:16:07 +08:00
// return CPUTimesStat{}, e
return nil, errors.New("not contain cpu")
2014-04-18 15:34:47 +08:00
}
2014-04-24 15:15:57 +08:00
cpu := fields[0]
if cpu == "cpu" {
cpu = "cpu-total"
}
user, _ := strconv.ParseFloat(fields[1], 32)
nice, _ := strconv.ParseFloat(fields[2], 32)
system, _ := strconv.ParseFloat(fields[3], 32)
idle, _ := strconv.ParseFloat(fields[4], 32)
iowait, _ := strconv.ParseFloat(fields[5], 32)
irq, _ := strconv.ParseFloat(fields[6], 32)
softirq, _ := strconv.ParseFloat(fields[7], 32)
stolen, _ := strconv.ParseFloat(fields[8], 32)
ct := &CPUTimesStat{
2014-04-30 15:16:07 +08:00
CPU: cpu,
2014-04-24 15:15:57 +08:00
User: float32(user),
Nice: float32(nice),
System: float32(system),
Idle: float32(idle),
Iowait: float32(iowait),
Irq: float32(irq),
Softirq: float32(softirq),
Stolen: float32(stolen),
}
if len(fields) > 9 { // Linux >= 2.6.11
steal, _ := strconv.ParseFloat(fields[9], 32)
ct.Steal = float32(steal)
}
if len(fields) > 10 { // Linux >= 2.6.24
guest, _ := strconv.ParseFloat(fields[10], 32)
ct.Guest = float32(guest)
}
if len(fields) > 11 { // Linux >= 3.2.0
2014-04-30 15:16:07 +08:00
guestNice, _ := strconv.ParseFloat(fields[11], 32)
ct.GuestNice = float32(guestNice)
2014-04-24 15:15:57 +08:00
}
return ct, nil
2014-04-18 15:34:47 +08:00
}