提交 4b5bf22b 编写于 作者: S Shirou WAKAYAMA

change CPUTimes type from float32 to float64 to resolve precision issue.

上级 18ab22b6
...@@ -8,17 +8,17 @@ import ( ...@@ -8,17 +8,17 @@ import (
type CPUTimesStat struct { type CPUTimesStat struct {
CPU string `json:"cpu"` CPU string `json:"cpu"`
User float32 `json:"user"` User float64 `json:"user"`
System float32 `json:"system"` System float64 `json:"system"`
Idle float32 `json:"idle"` Idle float64 `json:"idle"`
Nice float32 `json:"nice"` Nice float64 `json:"nice"`
Iowait float32 `json:"iowait"` Iowait float64 `json:"iowait"`
Irq float32 `json:"irq"` Irq float64 `json:"irq"`
Softirq float32 `json:"softirq"` Softirq float64 `json:"softirq"`
Steal float32 `json:"steal"` Steal float64 `json:"steal"`
Guest float32 `json:"guest"` Guest float64 `json:"guest"`
GuestNice float32 `json:"guest_nice"` GuestNice float64 `json:"guest_nice"`
Stolen float32 `json:"stolen"` Stolen float64 `json:"stolen"`
} }
type CPUInfoStat struct { type CPUInfoStat struct {
...@@ -43,14 +43,14 @@ func CPUCounts(logical bool) (int, error) { ...@@ -43,14 +43,14 @@ func CPUCounts(logical bool) (int, error) {
var lastCPUTimes []CPUTimesStat var lastCPUTimes []CPUTimesStat
var lastPerCPUTimes []CPUTimesStat var lastPerCPUTimes []CPUTimesStat
func CPUPercent(interval time.Duration, percpu bool) ([]float32, error) { func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
getAllBusy := func(t CPUTimesStat) (float32, float32) { getAllBusy := func(t CPUTimesStat) (float64, float64) {
busy := t.User + t.System + t.Nice + t.Iowait + t.Irq + busy := t.User + t.System + t.Nice + t.Iowait + t.Irq +
t.Softirq + t.Steal + t.Guest + t.GuestNice + t.Stolen t.Softirq + t.Steal + t.Guest + t.GuestNice + t.Stolen
return busy + t.Idle, busy return busy + t.Idle, busy
} }
calculate := func(t1, t2 CPUTimesStat) float32 { calculate := func(t1, t2 CPUTimesStat) float64 {
t1All, t1Busy := getAllBusy(t1) t1All, t1Busy := getAllBusy(t1)
t2All, t2Busy := getAllBusy(t2) t2All, t2Busy := getAllBusy(t2)
...@@ -81,7 +81,7 @@ func CPUPercent(interval time.Duration, percpu bool) ([]float32, error) { ...@@ -81,7 +81,7 @@ func CPUPercent(interval time.Duration, percpu bool) ([]float32, error) {
} }
} }
ret := make([]float32, len(cpuTimes)) ret := make([]float64, len(cpuTimes))
if !percpu { if !percpu {
ret[0] = calculate(lastCPUTimes[0], cpuTimes[0]) ret[0] = calculate(lastCPUTimes[0], cpuTimes[0])
lastCPUTimes = cpuTimes lastCPUTimes = cpuTimes
......
...@@ -46,23 +46,23 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { ...@@ -46,23 +46,23 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
for i := 0; i < ncpu; i++ { for i := 0; i < ncpu; i++ {
offset := CPUStates * i offset := CPUStates * i
user, err := strconv.ParseFloat(cpuTimes[CPUser+offset], 32) user, err := strconv.ParseFloat(cpuTimes[CPUser+offset], 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
nice, err := strconv.ParseFloat(cpuTimes[CPNice+offset], 32) nice, err := strconv.ParseFloat(cpuTimes[CPNice+offset], 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
sys, err := strconv.ParseFloat(cpuTimes[CPSys+offset], 32) sys, err := strconv.ParseFloat(cpuTimes[CPSys+offset], 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
idle, err := strconv.ParseFloat(cpuTimes[CPIdle+offset], 32) idle, err := strconv.ParseFloat(cpuTimes[CPIdle+offset], 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
intr, err := strconv.ParseFloat(cpuTimes[CPIntr+offset], 32) intr, err := strconv.ParseFloat(cpuTimes[CPIntr+offset], 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
......
...@@ -46,23 +46,23 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { ...@@ -46,23 +46,23 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
for i := 0; i < ncpu; i++ { for i := 0; i < ncpu; i++ {
offset := CPUStates * i offset := CPUStates * i
user, err := strconv.ParseFloat(cpuTimes[CPUser+offset], 32) user, err := strconv.ParseFloat(cpuTimes[CPUser+offset], 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
nice, err := strconv.ParseFloat(cpuTimes[CPNice+offset], 32) nice, err := strconv.ParseFloat(cpuTimes[CPNice+offset], 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
sys, err := strconv.ParseFloat(cpuTimes[CPSys+offset], 32) sys, err := strconv.ParseFloat(cpuTimes[CPSys+offset], 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
idle, err := strconv.ParseFloat(cpuTimes[CPIdle+offset], 32) idle, err := strconv.ParseFloat(cpuTimes[CPIdle+offset], 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
intr, err := strconv.ParseFloat(cpuTimes[CPIntr+offset], 32) intr, err := strconv.ParseFloat(cpuTimes[CPIntr+offset], 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
......
...@@ -62,7 +62,7 @@ func CPUInfo() ([]CPUInfoStat, error) { ...@@ -62,7 +62,7 @@ func CPUInfo() ([]CPUInfoStat, error) {
switch key { switch key {
case "processor": case "processor":
c = CPUInfoStat{} c = CPUInfoStat{}
t, err := strconv.ParseInt(value, 10, 32) t, err := strconv.ParseInt(value, 10, 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
...@@ -76,7 +76,7 @@ func CPUInfo() ([]CPUInfoStat, error) { ...@@ -76,7 +76,7 @@ func CPUInfo() ([]CPUInfoStat, error) {
case "model name": case "model name":
c.ModelName = value c.ModelName = value
case "stepping": case "stepping":
t, err := strconv.ParseInt(value, 10, 32) t, err := strconv.ParseInt(value, 10, 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
...@@ -88,7 +88,7 @@ func CPUInfo() ([]CPUInfoStat, error) { ...@@ -88,7 +88,7 @@ func CPUInfo() ([]CPUInfoStat, error) {
} }
c.Mhz = t c.Mhz = t
case "cache size": case "cache size":
t, err := strconv.ParseInt(strings.Replace(value, " KB", "", 1), 10, 32) t, err := strconv.ParseInt(strings.Replace(value, " KB", "", 1), 10, 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
...@@ -98,7 +98,7 @@ func CPUInfo() ([]CPUInfoStat, error) { ...@@ -98,7 +98,7 @@ func CPUInfo() ([]CPUInfoStat, error) {
case "core id": case "core id":
c.CoreID = value c.CoreID = value
case "cpu cores": case "cpu cores":
t, err := strconv.ParseInt(value, 10, 32) t, err := strconv.ParseInt(value, 10, 64)
if err != nil { if err != nil {
return ret, err return ret, err
} }
...@@ -122,71 +122,71 @@ func parseStatLine(line string) (*CPUTimesStat, error) { ...@@ -122,71 +122,71 @@ func parseStatLine(line string) (*CPUTimesStat, error) {
if cpu == "cpu" { if cpu == "cpu" {
cpu = "cpu-total" cpu = "cpu-total"
} }
user, err := strconv.ParseFloat(fields[1], 32) user, err := strconv.ParseFloat(fields[1], 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
nice, err := strconv.ParseFloat(fields[2], 32) nice, err := strconv.ParseFloat(fields[2], 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
system, err := strconv.ParseFloat(fields[3], 32) system, err := strconv.ParseFloat(fields[3], 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
idle, err := strconv.ParseFloat(fields[4], 32) idle, err := strconv.ParseFloat(fields[4], 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
iowait, err := strconv.ParseFloat(fields[5], 32) iowait, err := strconv.ParseFloat(fields[5], 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
irq, err := strconv.ParseFloat(fields[6], 32) irq, err := strconv.ParseFloat(fields[6], 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
softirq, err := strconv.ParseFloat(fields[7], 32) softirq, err := strconv.ParseFloat(fields[7], 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
stolen, err := strconv.ParseFloat(fields[8], 32) stolen, err := strconv.ParseFloat(fields[8], 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
cpu_tick := float32(100) // TODO: how to get _SC_CLK_TCK ? cpu_tick := float64(100) // TODO: how to get _SC_CLK_TCK ?
ct := &CPUTimesStat{ ct := &CPUTimesStat{
CPU: cpu, CPU: cpu,
User: float32(user) / cpu_tick, User: float64(user) / cpu_tick,
Nice: float32(nice) / cpu_tick, Nice: float64(nice) / cpu_tick,
System: float32(system) / cpu_tick, System: float64(system) / cpu_tick,
Idle: float32(idle) / cpu_tick, Idle: float64(idle) / cpu_tick,
Iowait: float32(iowait) / cpu_tick, Iowait: float64(iowait) / cpu_tick,
Irq: float32(irq) / cpu_tick, Irq: float64(irq) / cpu_tick,
Softirq: float32(softirq) / cpu_tick, Softirq: float64(softirq) / cpu_tick,
Stolen: float32(stolen) / cpu_tick, Stolen: float64(stolen) / cpu_tick,
} }
if len(fields) > 9 { // Linux >= 2.6.11 if len(fields) > 9 { // Linux >= 2.6.11
steal, err := strconv.ParseFloat(fields[9], 32) steal, err := strconv.ParseFloat(fields[9], 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ct.Steal = float32(steal) ct.Steal = float64(steal)
} }
if len(fields) > 10 { // Linux >= 2.6.24 if len(fields) > 10 { // Linux >= 2.6.24
guest, err := strconv.ParseFloat(fields[10], 32) guest, err := strconv.ParseFloat(fields[10], 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ct.Guest = float32(guest) ct.Guest = float64(guest)
} }
if len(fields) > 11 { // Linux >= 3.2.0 if len(fields) > 11 { // Linux >= 3.2.0
guestNice, err := strconv.ParseFloat(fields[11], 32) guestNice, err := strconv.ParseFloat(fields[11], 64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ct.GuestNice = float32(guestNice) ct.GuestNice = float64(guestNice)
} }
return ct, nil return ct, nil
......
...@@ -74,7 +74,7 @@ func testCPUPercent(t *testing.T, percpu bool) { ...@@ -74,7 +74,7 @@ func testCPUPercent(t *testing.T, percpu bool) {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
for _, percent := range v { for _, percent := range v {
if percent < 0.0 || percent > 100.0*float32(numcpu) { if percent < 0.0 || percent > 100.0*float64(numcpu) {
t.Fatalf("CPUPercent value is invalid: %f", percent) t.Fatalf("CPUPercent value is invalid: %f", percent)
} }
} }
......
...@@ -32,9 +32,9 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { ...@@ -32,9 +32,9 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
system := (kernel - idle) system := (kernel - idle)
ret = append(ret, CPUTimesStat{ ret = append(ret, CPUTimesStat{
Idle: float32(idle), Idle: float64(idle),
User: float32(user), User: float64(user),
System: float32(system), System: float64(system),
}) })
return ret, nil return ret, nil
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册