1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00
shirou_gopsutil/cpu/cpu_linux.go

202 lines
4.0 KiB
Go
Raw Normal View History

2014-04-18 16:34:47 +09:00
// +build linux
2014-12-30 22:09:05 +09:00
package cpu
2014-04-18 16:34:47 +09:00
import (
2014-04-24 16:15:57 +09:00
"errors"
2015-07-17 21:46:26 +09:00
"os/exec"
2014-04-18 16:34:47 +09:00
"strconv"
"strings"
2014-11-27 10:25:14 +09:00
common "github.com/shirou/gopsutil/common"
2014-04-18 16:34:47 +09:00
)
2015-07-17 21:46:26 +09:00
var cpu_tick = float64(100)
func init() {
out, err := exec.Command("/usr/bin/getconf", "CLK_TCK").Output()
// ignore errors
if err == nil {
2015-07-17 21:52:43 +09:00
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
2015-07-17 21:46:26 +09:00
if err == nil {
cpu_tick = float64(i)
}
}
}
2014-04-30 16:16:07 +09:00
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
2014-04-18 16:34:47 +09:00
filename := "/proc/stat"
var lines = []string{}
2014-11-02 01:14:26 +01:00
if percpu {
var startIdx uint = 1
for {
linen, _ := common.ReadLinesOffsetN(filename, startIdx, 1)
line := linen[0]
if !strings.HasPrefix(line, "cpu") {
break
}
lines = append(lines, line)
startIdx += 1
}
2014-11-02 01:14:26 +01:00
} else {
2014-11-27 10:25:14 +09:00
lines, _ = common.ReadLinesOffsetN(filename, 0, 1)
2014-11-02 01:14:26 +01:00
}
2014-05-01 12:47:43 +09:00
ret := make([]CPUTimesStat, 0, len(lines))
2014-04-18 16:34:47 +09:00
for _, line := range lines {
2014-04-24 16:15:57 +09:00
ct, err := parseStatLine(line)
if err != nil {
2014-04-18 16:34:47 +09:00
continue
}
ret = append(ret, *ct)
2014-04-18 16:34:47 +09:00
2014-04-24 16:15:57 +09:00
}
return ret, nil
}
2014-04-18 16:34:47 +09:00
2014-05-16 18:12:18 +09:00
func CPUInfo() ([]CPUInfoStat, error) {
filename := "/proc/cpuinfo"
2014-11-27 10:25:14 +09:00
lines, _ := common.ReadLines(filename)
2014-05-16 18:12:18 +09:00
var ret []CPUInfoStat
var c CPUInfoStat
for _, line := range lines {
fields := strings.Split(line, ":")
2014-05-16 18:39:17 +09:00
if len(fields) < 2 {
2014-05-16 18:49:50 +09:00
if c.VendorID != "" {
2014-05-16 18:12:18 +09:00
ret = append(ret, c)
}
continue
}
key := strings.TrimSpace(fields[0])
value := strings.TrimSpace(fields[1])
switch key {
case "processor":
c = CPUInfoStat{}
t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return ret, err
}
c.CPU = int32(t)
2014-05-16 18:12:18 +09:00
case "vendor_id":
2014-05-16 18:39:17 +09:00
c.VendorID = value
2014-05-16 18:12:18 +09:00
case "cpu family":
c.Family = value
case "model":
c.Model = value
case "model name":
c.ModelName = value
case "stepping":
t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return ret, err
}
c.Stepping = int32(t)
2014-05-16 18:12:18 +09:00
case "cpu MHz":
t, err := strconv.ParseFloat(value, 64)
if err != nil {
return ret, err
}
c.Mhz = t
2014-05-16 18:12:18 +09: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)
2014-05-16 18:12:18 +09:00
case "physical id":
c.PhysicalID = value
case "core id":
c.CoreID = value
case "cpu cores":
t, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return ret, err
}
c.Cores = int32(t)
2014-05-16 18:12:18 +09:00
case "flags":
c.Flags = strings.Split(value, ",")
}
}
return ret, nil
}
func parseStatLine(line string) (*CPUTimesStat, error) {
2014-04-24 16:15:57 +09:00
fields := strings.Fields(line)
if strings.HasPrefix(fields[0], "cpu") == false {
2014-04-30 16:16:07 +09:00
// return CPUTimesStat{}, e
return nil, errors.New("not contain cpu")
2014-04-18 16:34:47 +09:00
}
2014-04-24 16:15:57 +09:00
cpu := fields[0]
if cpu == "cpu" {
cpu = "cpu-total"
}
user, err := strconv.ParseFloat(fields[1], 64)
2014-09-20 10:22:41 +09:00
if err != nil {
return nil, err
}
nice, err := strconv.ParseFloat(fields[2], 64)
2014-09-20 10:22:41 +09:00
if err != nil {
return nil, err
}
system, err := strconv.ParseFloat(fields[3], 64)
2014-09-20 10:22:41 +09:00
if err != nil {
return nil, err
}
idle, err := strconv.ParseFloat(fields[4], 64)
2014-09-20 10:22:41 +09:00
if err != nil {
return nil, err
}
iowait, err := strconv.ParseFloat(fields[5], 64)
2014-09-20 10:22:41 +09:00
if err != nil {
return nil, err
}
irq, err := strconv.ParseFloat(fields[6], 64)
2014-09-20 10:22:41 +09:00
if err != nil {
return nil, err
}
softirq, err := strconv.ParseFloat(fields[7], 64)
2014-09-20 10:22:41 +09:00
if err != nil {
return nil, err
}
2015-02-13 14:55:42 +09:00
ct := &CPUTimesStat{
2014-04-30 16:16:07 +09: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,
2014-04-24 16:15:57 +09:00
}
if len(fields) > 8 { // Linux >= 2.6.11
steal, err := strconv.ParseFloat(fields[8], 64)
2014-09-20 10:22:41 +09:00
if err != nil {
return nil, err
}
ct.Steal = float64(steal)
2014-04-24 16:15:57 +09:00
}
if len(fields) > 9 { // Linux >= 2.6.24
guest, err := strconv.ParseFloat(fields[9], 64)
2014-09-20 10:22:41 +09:00
if err != nil {
return nil, err
}
ct.Guest = float64(guest)
2014-04-24 16:15:57 +09:00
}
if len(fields) > 10 { // Linux >= 3.2.0
guestNice, err := strconv.ParseFloat(fields[10], 64)
2014-09-20 10:22:41 +09:00
if err != nil {
return nil, err
}
ct.GuestNice = float64(guestNice)
2014-04-24 16:15:57 +09:00
}
return ct, nil
2014-04-18 16:34:47 +09:00
}