2014-04-18 15:34:47 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package cpu
|
2014-04-18 15:34:47 +08:00
|
|
|
|
|
|
|
import (
|
2014-04-24 15:15:57 +08:00
|
|
|
"errors"
|
2015-10-19 11:40:01 +08:00
|
|
|
"fmt"
|
2015-07-17 20:46:26 +08:00
|
|
|
"os/exec"
|
2014-04-18 15:34:47 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-11-27 09:25:14 +08:00
|
|
|
|
2015-10-19 11:40:01 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-04-18 15:34:47 +08:00
|
|
|
)
|
|
|
|
|
2015-07-17 20:46:26 +08:00
|
|
|
var cpu_tick = float64(100)
|
|
|
|
|
|
|
|
func init() {
|
2016-04-01 21:13:05 +08:00
|
|
|
getconf, err := exec.LookPath("/usr/bin/getconf")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-05-20 16:59:41 +08:00
|
|
|
out, err := invoke.Command(getconf, "CLK_TCK")
|
2015-07-17 20:46:26 +08:00
|
|
|
// ignore errors
|
|
|
|
if err == nil {
|
2015-07-17 20:52:43 +08:00
|
|
|
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
|
2015-07-17 20:46:26 +08:00
|
|
|
if err == nil {
|
|
|
|
cpu_tick = float64(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func Times(percpu bool) ([]TimesStat, error) {
|
2015-10-19 11:40:01 +08:00
|
|
|
filename := common.HostProc("stat")
|
2015-01-13 18:32:25 +08:00
|
|
|
var lines = []string{}
|
2014-11-02 08:14:26 +08:00
|
|
|
if percpu {
|
2015-01-13 18:32:25 +08:00
|
|
|
var startIdx uint = 1
|
|
|
|
for {
|
|
|
|
linen, _ := common.ReadLinesOffsetN(filename, startIdx, 1)
|
|
|
|
line := linen[0]
|
|
|
|
if !strings.HasPrefix(line, "cpu") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
lines = append(lines, line)
|
2016-04-01 20:34:39 +08:00
|
|
|
startIdx++
|
2015-01-13 18:32:25 +08:00
|
|
|
}
|
2014-11-02 08:14:26 +08:00
|
|
|
} else {
|
2014-11-27 09:25:14 +08:00
|
|
|
lines, _ = common.ReadLinesOffsetN(filename, 0, 1)
|
2014-11-02 08:14:26 +08:00
|
|
|
}
|
2014-05-01 11:47:43 +08:00
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
ret := make([]TimesStat, 0, len(lines))
|
2014-05-01 11:47:43 +08:00
|
|
|
|
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
|
|
|
|
}
|
2014-05-01 11:01:30 +08:00
|
|
|
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
|
|
|
|
2016-04-01 20:34:39 +08:00
|
|
|
func sysCPUPath(cpu int32, relPath string) string {
|
2015-10-19 11:40:01 +08:00
|
|
|
return common.HostSys(fmt.Sprintf("devices/system/cpu/cpu%d", cpu), relPath)
|
2015-10-19 00:46:21 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func finishCPUInfo(c *InfoStat) error {
|
2015-10-18 03:25:18 +08:00
|
|
|
if c.Mhz == 0 {
|
2016-04-01 20:34:39 +08:00
|
|
|
lines, err := common.ReadLines(sysCPUPath(c.CPU, "cpufreq/cpuinfo_max_freq"))
|
2015-10-18 03:25:18 +08:00
|
|
|
if err == nil {
|
|
|
|
value, err := strconv.ParseFloat(lines[0], 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Mhz = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(c.CoreID) == 0 {
|
2016-04-01 20:34:39 +08:00
|
|
|
lines, err := common.ReadLines(sysCPUPath(c.CPU, "topology/coreId"))
|
2015-10-19 11:40:01 +08:00
|
|
|
if err == nil {
|
2015-10-18 03:25:18 +08:00
|
|
|
c.CoreID = lines[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-23 00:43:45 +08:00
|
|
|
// 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-03-22 22:09:12 +08:00
|
|
|
func Info() ([]InfoStat, error) {
|
2015-10-19 11:40:01 +08:00
|
|
|
filename := common.HostProc("cpuinfo")
|
2014-11-27 09:25:14 +08:00
|
|
|
lines, _ := common.ReadLines(filename)
|
2014-05-16 17:12:18 +08:00
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
var ret []InfoStat
|
2014-05-16 17:12:18 +08:00
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
c := InfoStat{CPU: -1, Cores: 1}
|
2014-05-16 17:12:18 +08:00
|
|
|
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
|
|
|
continue
|
|
|
|
}
|
|
|
|
key := strings.TrimSpace(fields[0])
|
|
|
|
value := strings.TrimSpace(fields[1])
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "processor":
|
2015-10-18 03:25:18 +08:00
|
|
|
if c.CPU >= 0 {
|
|
|
|
err := finishCPUInfo(&c)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
ret = append(ret, c)
|
|
|
|
}
|
2016-03-22 22:09:12 +08:00
|
|
|
c = InfoStat{Cores: 1}
|
2015-02-13 21:45:12 +08:00
|
|
|
t, err := strconv.ParseInt(value, 10, 64)
|
2014-09-19 21:41:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
c.CPU = int32(t)
|
2016-04-12 22:08:13 +08:00
|
|
|
case "vendorId", "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":
|
2015-02-13 21:45:12 +08:00
|
|
|
t, err := strconv.ParseInt(value, 10, 64)
|
2014-09-19 21:41:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
c.Stepping = int32(t)
|
2014-05-16 17:12:18 +08:00
|
|
|
case "cpu MHz":
|
2014-09-19 21:41:29 +08:00
|
|
|
t, err := strconv.ParseFloat(value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
c.Mhz = t
|
2014-05-16 17:12:18 +08:00
|
|
|
case "cache size":
|
2015-02-13 21:45:12 +08:00
|
|
|
t, err := strconv.ParseInt(strings.Replace(value, " KB", "", 1), 10, 64)
|
2014-09-19 21:41:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
c.CacheSize = int32(t)
|
2014-05-16 17:12:18 +08:00
|
|
|
case "physical id":
|
|
|
|
c.PhysicalID = value
|
|
|
|
case "core id":
|
|
|
|
c.CoreID = value
|
2015-10-19 11:40:01 +08:00
|
|
|
case "flags", "Features":
|
2015-10-18 03:25:18 +08:00
|
|
|
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
|
2014-05-16 17:12:18 +08:00
|
|
|
}
|
2015-10-18 03:25:18 +08:00
|
|
|
ret = append(ret, c)
|
2014-05-16 17:12:18 +08:00
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func parseStatLine(line string) (*TimesStat, 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
|
2014-05-01 11:01:30 +08:00
|
|
|
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"
|
|
|
|
}
|
2015-02-13 21:45:12 +08:00
|
|
|
user, err := strconv.ParseFloat(fields[1], 64)
|
2014-09-20 09:22:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-13 21:45:12 +08:00
|
|
|
nice, err := strconv.ParseFloat(fields[2], 64)
|
2014-09-20 09:22:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-13 21:45:12 +08:00
|
|
|
system, err := strconv.ParseFloat(fields[3], 64)
|
2014-09-20 09:22:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-13 21:45:12 +08:00
|
|
|
idle, err := strconv.ParseFloat(fields[4], 64)
|
2014-09-20 09:22:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-13 21:45:12 +08:00
|
|
|
iowait, err := strconv.ParseFloat(fields[5], 64)
|
2014-09-20 09:22:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-13 21:45:12 +08:00
|
|
|
irq, err := strconv.ParseFloat(fields[6], 64)
|
2014-09-20 09:22:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-13 21:45:12 +08:00
|
|
|
softirq, err := strconv.ParseFloat(fields[7], 64)
|
2014-09-20 09:22:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-13 13:55:42 +08:00
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
ct := &TimesStat{
|
2014-04-30 15:16:07 +08:00
|
|
|
CPU: cpu,
|
2015-02-13 21:45:12 +08:00
|
|
|
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,
|
2014-04-24 15:15:57 +08:00
|
|
|
}
|
2015-07-15 16:20:12 +08:00
|
|
|
if len(fields) > 8 { // Linux >= 2.6.11
|
|
|
|
steal, err := strconv.ParseFloat(fields[8], 64)
|
2014-09-20 09:22:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-17 21:11:30 +08:00
|
|
|
ct.Steal = float64(steal) / cpu_tick
|
2014-04-24 15:15:57 +08:00
|
|
|
}
|
2015-07-15 16:20:12 +08:00
|
|
|
if len(fields) > 9 { // Linux >= 2.6.24
|
|
|
|
guest, err := strconv.ParseFloat(fields[9], 64)
|
2014-09-20 09:22:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-17 21:11:30 +08:00
|
|
|
ct.Guest = float64(guest) / cpu_tick
|
2014-04-24 15:15:57 +08:00
|
|
|
}
|
2015-07-15 16:20:12 +08:00
|
|
|
if len(fields) > 10 { // Linux >= 3.2.0
|
|
|
|
guestNice, err := strconv.ParseFloat(fields[10], 64)
|
2014-09-20 09:22:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-17 21:11:30 +08:00
|
|
|
ct.GuestNice = float64(guestNice) / cpu_tick
|
2014-04-24 15:15:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ct, nil
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|