open-nomad/vendor/github.com/shirou/gopsutil/cpu/cpu_linux.go

276 lines
6.1 KiB
Go
Raw Normal View History

2016-02-12 18:02:16 +00:00
// +build linux
package cpu
import (
"errors"
"fmt"
"os/exec"
"strconv"
"strings"
"github.com/shirou/gopsutil/internal/common"
)
var cpu_tick = float64(100)
func init() {
2016-05-09 15:19:04 +00:00
getconf, err := exec.LookPath("/usr/bin/getconf")
if err != nil {
return
}
2016-06-17 14:31:00 +00:00
out, err := invoke.Command(getconf, "CLK_TCK")
2016-02-12 18:02:16 +00:00
// ignore errors
if err == nil {
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
if err == nil {
cpu_tick = float64(i)
}
}
}
2016-05-09 15:19:04 +00:00
func Times(percpu bool) ([]TimesStat, error) {
2016-02-12 18:02:16 +00:00
filename := common.HostProc("stat")
var lines = []string{}
if percpu {
var startIdx uint = 1
for {
linen, _ := common.ReadLinesOffsetN(filename, startIdx, 1)
if len(linen) == 0 {
break
}
2016-02-12 18:02:16 +00:00
line := linen[0]
if !strings.HasPrefix(line, "cpu") {
break
}
lines = append(lines, line)
2016-05-09 15:19:04 +00:00
startIdx++
2016-02-12 18:02:16 +00:00
}
} else {
lines, _ = common.ReadLinesOffsetN(filename, 0, 1)
}
2016-05-09 15:19:04 +00:00
ret := make([]TimesStat, 0, len(lines))
2016-02-12 18:02:16 +00:00
for _, line := range lines {
ct, err := parseStatLine(line)
if err != nil {
continue
}
ret = append(ret, *ct)
}
return ret, nil
}
2016-05-09 15:19:04 +00:00
func sysCPUPath(cpu int32, relPath string) string {
2016-02-12 18:02:16 +00:00
return common.HostSys(fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath)
}
2016-05-09 15:19:04 +00:00
func finishCPUInfo(c *InfoStat) error {
var lines []string
var err error
var value float64
2016-02-12 18:02:16 +00:00
if len(c.CoreID) == 0 {
lines, err = common.ReadLines(sysCPUPath(c.CPU, "topology/core_id"))
2016-02-12 18:02:16 +00:00
if err == nil {
c.CoreID = lines[0]
}
}
// override the value of c.Mhz with cpufreq/cpuinfo_max_freq regardless
// of the value from /proc/cpuinfo because we want to report the maximum
// clock-speed of the CPU for c.Mhz, matching the behaviour of Windows
lines, err = common.ReadLines(sysCPUPath(c.CPU, "cpufreq/cpuinfo_max_freq"))
// if we encounter errors below but has a value from parsing /proc/cpuinfo
// then we ignore the error
if err != nil {
if c.Mhz == 0 {
return err
} else {
return nil
}
}
value, err = strconv.ParseFloat(lines[0], 64)
if err != nil {
if c.Mhz == 0 {
return err
} else {
return nil
}
}
c.Mhz = value / 1000.0 // value is in kHz
2016-02-12 18:02:16 +00:00
return nil
}
// CPUInfo on linux will return 1 item per physical thread.
//
// CPUs have three levels of counting: sockets, cores, threads.
// Cores with HyperThreading count as having 2 threads per core.
// Sockets often come with many physical CPU cores.
// For example a single socket board with two cores each with HT will
// return 4 CPUInfoStat structs on Linux and the "Cores" field set to 1.
2016-05-09 15:19:04 +00:00
func Info() ([]InfoStat, error) {
2016-02-12 18:02:16 +00:00
filename := common.HostProc("cpuinfo")
lines, _ := common.ReadLines(filename)
2016-05-09 15:19:04 +00:00
var ret []InfoStat
2016-02-12 18:02:16 +00:00
2016-05-09 15:19:04 +00:00
c := InfoStat{CPU: -1, Cores: 1}
2016-02-12 18:02:16 +00:00
for _, line := range lines {
fields := strings.Split(line, ":")
if len(fields) < 2 {
continue
}
key := strings.TrimSpace(fields[0])
value := strings.TrimSpace(fields[1])
switch key {
case "processor":
if c.CPU >= 0 {
err := finishCPUInfo(&c)
if err != nil {
return ret, err
}
ret = append(ret, c)
}
2016-05-09 15:19:04 +00:00
c = InfoStat{Cores: 1}
2016-02-12 18:02:16 +00:00
t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return ret, err
}
c.CPU = int32(t)
2016-05-09 15:19:04 +00:00
case "vendorId", "vendor_id":
2016-02-12 18:02:16 +00:00
c.VendorID = value
case "cpu family":
c.Family = value
case "model":
c.Model = value
case "model name", "cpu":
2016-02-12 18:02:16 +00:00
c.ModelName = value
if strings.Contains(value, "POWER8") ||
strings.Contains(value, "POWER7") {
c.Model = strings.Split(value, " ")[0]
c.Family = "POWER"
c.VendorID = "IBM"
}
case "stepping", "revision":
val := value
if key == "revision" {
val = strings.Split(value, ".")[0]
}
t, err := strconv.ParseInt(val, 10, 64)
2016-02-12 18:02:16 +00:00
if err != nil {
return ret, err
}
c.Stepping = int32(t)
case "cpu MHz", "clock":
// treat this as the fallback value, thus we ignore error
if t, err := strconv.ParseFloat(strings.Replace(value, "MHz", "", 1), 64); err == nil {
c.Mhz = t
2016-02-12 18:02:16 +00:00
}
case "cache size":
t, err := strconv.ParseInt(strings.Replace(value, " KB", "", 1), 10, 64)
if err != nil {
return ret, err
}
c.CacheSize = int32(t)
case "physical id":
c.PhysicalID = value
case "core id":
c.CoreID = value
case "flags", "Features":
c.Flags = strings.FieldsFunc(value, func(r rune) bool {
return r == ',' || r == ' '
})
}
}
if c.CPU >= 0 {
err := finishCPUInfo(&c)
if err != nil {
return ret, err
}
ret = append(ret, c)
}
return ret, nil
}
2016-05-09 15:19:04 +00:00
func parseStatLine(line string) (*TimesStat, error) {
2016-02-12 18:02:16 +00:00
fields := strings.Fields(line)
if strings.HasPrefix(fields[0], "cpu") == false {
// return CPUTimesStat{}, e
return nil, errors.New("not contain cpu")
}
cpu := fields[0]
if cpu == "cpu" {
cpu = "cpu-total"
}
user, err := strconv.ParseFloat(fields[1], 64)
if err != nil {
return nil, err
}
nice, err := strconv.ParseFloat(fields[2], 64)
if err != nil {
return nil, err
}
system, err := strconv.ParseFloat(fields[3], 64)
if err != nil {
return nil, err
}
idle, err := strconv.ParseFloat(fields[4], 64)
if err != nil {
return nil, err
}
iowait, err := strconv.ParseFloat(fields[5], 64)
if err != nil {
return nil, err
}
irq, err := strconv.ParseFloat(fields[6], 64)
if err != nil {
return nil, err
}
softirq, err := strconv.ParseFloat(fields[7], 64)
if err != nil {
return nil, err
}
2016-05-09 15:19:04 +00:00
ct := &TimesStat{
2016-02-12 18:02:16 +00:00
CPU: cpu,
User: float64(user) / cpu_tick,
Nice: float64(nice) / cpu_tick,
System: float64(system) / cpu_tick,
Idle: float64(idle) / cpu_tick,
Iowait: float64(iowait) / cpu_tick,
Irq: float64(irq) / cpu_tick,
Softirq: float64(softirq) / cpu_tick,
}
if len(fields) > 8 { // Linux >= 2.6.11
steal, err := strconv.ParseFloat(fields[8], 64)
if err != nil {
return nil, err
}
ct.Steal = float64(steal) / cpu_tick
}
if len(fields) > 9 { // Linux >= 2.6.24
guest, err := strconv.ParseFloat(fields[9], 64)
if err != nil {
return nil, err
}
ct.Guest = float64(guest) / cpu_tick
}
if len(fields) > 10 { // Linux >= 3.2.0
guestNice, err := strconv.ParseFloat(fields[10], 64)
if err != nil {
return nil, err
}
ct.GuestNice = float64(guestNice) / cpu_tick
}
return ct, nil
}