提交 7b61bc58 编写于 作者: S Shirou WAKAYAMA

change uint64 to float32.

上级 23b20344
......@@ -6,17 +6,17 @@ import (
type CPU_TimesStat struct {
Cpu string `json:"cpu"`
User uint64 `json:"user"`
System uint64 `json:"system"`
Idle uint64 `json:"idle"`
Nice uint64 `json:"nice"`
Iowait uint64 `json:"iowait"`
Irq uint64 `json:"irq"`
Softirq uint64 `json:"softirq"`
Steal uint64 `json:"steal"`
Guest uint64 `json:"guest"`
Guest_nice uint64 `json:"guest_nice"`
Stolen uint64 `json:"stolen"`
User float32 `json:"user"`
System float32 `json:"system"`
Idle float32 `json:"idle"`
Nice float32 `json:"nice"`
Iowait float32 `json:"iowait"`
Irq float32 `json:"irq"`
Softirq float32 `json:"softirq"`
Steal float32 `json:"steal"`
Guest float32 `json:"guest"`
Guest_nice float32 `json:"guest_nice"`
Stolen float32 `json:"stolen"`
}
func Cpu_counts() (int, error) {
......
......@@ -30,18 +30,18 @@ func Cpu_times() ([]CPU_TimesStat, error) {
return ret, err
}
user, _ := strconv.ParseInt(cpu_time[CP_USER], 10, 64)
nice, _ := strconv.ParseInt(cpu_time[CP_NICE], 10, 64)
sys, _ := strconv.ParseInt(cpu_time[CP_SYS], 10, 64)
idle, _ := strconv.ParseInt(cpu_time[CP_IDLE], 10, 64)
intr, _ := strconv.ParseInt(cpu_time[CP_INTR], 10, 64)
user, _ := strconv.ParseFloat(cpu_time[CP_USER], 32)
nice, _ := strconv.ParseFloat(cpu_time[CP_NICE], 32)
sys, _ := strconv.ParseFloat(cpu_time[CP_SYS], 32)
idle, _ := strconv.ParseFloat(cpu_time[CP_IDLE], 32)
intr, _ := strconv.ParseFloat(cpu_time[CP_INTR], 32)
c := CPU_TimesStat{
User: uint64(user / CLOCKS_PER_SEC),
Nice: uint64(nice / CLOCKS_PER_SEC),
System: uint64(sys / CLOCKS_PER_SEC),
Idle: uint64(idle / CLOCKS_PER_SEC),
Irq: uint64(intr / CLOCKS_PER_SEC), // FIXME: correct?
User: float32(user / CLOCKS_PER_SEC),
Nice: float32(nice / CLOCKS_PER_SEC),
System: float32(sys / CLOCKS_PER_SEC),
Idle: float32(idle / CLOCKS_PER_SEC),
Irq: float32(intr / CLOCKS_PER_SEC), // FIXME: correct?
}
ret = append(ret, c)
......
......@@ -3,6 +3,7 @@
package gopsutil
import (
"errors"
"strconv"
"strings"
)
......@@ -13,50 +14,59 @@ func Cpu_times() ([]CPU_TimesStat, error) {
filename := "/proc/stat"
lines, _ := ReadLines(filename)
for _, line := range lines {
fields := strings.Fields(line)
if strings.HasPrefix(fields[0], "cpu") == false {
ct, err := parseStatLine(line)
if err != nil {
continue
}
ret = append(ret, ct)
cpu := fields[0]
if cpu == "cpu" {
cpu = "cpu-total"
}
user, _ := strconv.ParseUint(fields[1], 10, 64)
nice, _ := strconv.ParseUint(fields[2], 10, 64)
system, _ := strconv.ParseUint(fields[3], 10, 64)
idle, _ := strconv.ParseUint(fields[4], 10, 64)
iowait, _ := strconv.ParseUint(fields[5], 10, 64)
irq, _ := strconv.ParseUint(fields[6], 10, 64)
softirq, _ := strconv.ParseUint(fields[7], 10, 64)
stolen, _ := strconv.ParseUint(fields[8], 10, 64)
ct := CPU_TimesStat{
Cpu: cpu,
User: user,
Nice: nice,
System: system,
Idle: idle,
Iowait: iowait,
Irq: irq,
Softirq: softirq,
Stolen: stolen,
}
if len(fields) > 9 { // Linux >= 2.6.11
steal, _ := strconv.ParseUint(fields[9], 10, 64)
ct.Steal = steal
}
if len(fields) > 10 { // Linux >= 2.6.24
guest, _ := strconv.ParseUint(fields[10], 10, 64)
ct.Guest = guest
}
if len(fields) > 11 { // Linux >= 3.2.0
guest_nice, _ := strconv.ParseUint(fields[11], 10, 64)
ct.Guest_nice = guest_nice
}
}
return ret, nil
}
ret = append(ret, ct)
func parseStatLine(line string) (CPU_TimesStat, error) {
fields := strings.Fields(line)
if strings.HasPrefix(fields[0], "cpu") == false {
// return CPU_TimesStat{}, e
return CPU_TimesStat{}, errors.New("not contain cpu")
}
return ret, nil
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 := CPU_TimesStat{
Cpu: cpu,
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
guest_nice, _ := strconv.ParseFloat(fields[11], 32)
ct.Guest_nice = float32(guest_nice)
}
return ct, nil
}
......@@ -34,9 +34,9 @@ func Cpu_times() ([]CPU_TimesStat, error) {
system := (kernel - idle)
ret = append(ret, CPU_TimesStat{
Idle: uint64(idle),
User: uint64(user),
System: uint64(system),
Idle: float32(idle),
User: float32(user),
System: float32(system),
})
return ret, nil
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册