diff --git a/cpu/cpu.go b/cpu/cpu.go index 67b4fad..2aaf5a0 100644 --- a/cpu/cpu.go +++ b/cpu/cpu.go @@ -8,17 +8,17 @@ import ( type CPUTimesStat struct { CPU string `json:"cpu"` - 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"` - GuestNice float32 `json:"guest_nice"` - Stolen float32 `json:"stolen"` + User float64 `json:"user"` + System float64 `json:"system"` + Idle float64 `json:"idle"` + Nice float64 `json:"nice"` + Iowait float64 `json:"iowait"` + Irq float64 `json:"irq"` + Softirq float64 `json:"softirq"` + Steal float64 `json:"steal"` + Guest float64 `json:"guest"` + GuestNice float64 `json:"guest_nice"` + Stolen float64 `json:"stolen"` } type CPUInfoStat struct { @@ -43,14 +43,14 @@ func CPUCounts(logical bool) (int, error) { var lastCPUTimes []CPUTimesStat var lastPerCPUTimes []CPUTimesStat -func CPUPercent(interval time.Duration, percpu bool) ([]float32, error) { - getAllBusy := func(t CPUTimesStat) (float32, float32) { +func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) { + getAllBusy := func(t CPUTimesStat) (float64, float64) { busy := t.User + t.System + t.Nice + t.Iowait + t.Irq + t.Softirq + t.Steal + t.Guest + t.GuestNice + t.Stolen return busy + t.Idle, busy } - calculate := func(t1, t2 CPUTimesStat) float32 { + calculate := func(t1, t2 CPUTimesStat) float64 { t1All, t1Busy := getAllBusy(t1) t2All, t2Busy := getAllBusy(t2) @@ -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 { ret[0] = calculate(lastCPUTimes[0], cpuTimes[0]) lastCPUTimes = cpuTimes diff --git a/cpu/cpu_darwin.go b/cpu/cpu_darwin.go index f40012e..b33ec98 100644 --- a/cpu/cpu_darwin.go +++ b/cpu/cpu_darwin.go @@ -46,23 +46,23 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { for i := 0; i < ncpu; i++ { offset := CPUStates * i - user, err := strconv.ParseFloat(cpuTimes[CPUser+offset], 32) + user, err := strconv.ParseFloat(cpuTimes[CPUser+offset], 64) if err != nil { return ret, err } - nice, err := strconv.ParseFloat(cpuTimes[CPNice+offset], 32) + nice, err := strconv.ParseFloat(cpuTimes[CPNice+offset], 64) if err != nil { return ret, err } - sys, err := strconv.ParseFloat(cpuTimes[CPSys+offset], 32) + sys, err := strconv.ParseFloat(cpuTimes[CPSys+offset], 64) if err != nil { return ret, err } - idle, err := strconv.ParseFloat(cpuTimes[CPIdle+offset], 32) + idle, err := strconv.ParseFloat(cpuTimes[CPIdle+offset], 64) if err != nil { return ret, err } - intr, err := strconv.ParseFloat(cpuTimes[CPIntr+offset], 32) + intr, err := strconv.ParseFloat(cpuTimes[CPIntr+offset], 64) if err != nil { return ret, err } diff --git a/cpu/cpu_freebsd.go b/cpu/cpu_freebsd.go index 5ac4fc2..cd76723 100644 --- a/cpu/cpu_freebsd.go +++ b/cpu/cpu_freebsd.go @@ -46,23 +46,23 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { for i := 0; i < ncpu; i++ { offset := CPUStates * i - user, err := strconv.ParseFloat(cpuTimes[CPUser+offset], 32) + user, err := strconv.ParseFloat(cpuTimes[CPUser+offset], 64) if err != nil { return ret, err } - nice, err := strconv.ParseFloat(cpuTimes[CPNice+offset], 32) + nice, err := strconv.ParseFloat(cpuTimes[CPNice+offset], 64) if err != nil { return ret, err } - sys, err := strconv.ParseFloat(cpuTimes[CPSys+offset], 32) + sys, err := strconv.ParseFloat(cpuTimes[CPSys+offset], 64) if err != nil { return ret, err } - idle, err := strconv.ParseFloat(cpuTimes[CPIdle+offset], 32) + idle, err := strconv.ParseFloat(cpuTimes[CPIdle+offset], 64) if err != nil { return ret, err } - intr, err := strconv.ParseFloat(cpuTimes[CPIntr+offset], 32) + intr, err := strconv.ParseFloat(cpuTimes[CPIntr+offset], 64) if err != nil { return ret, err } diff --git a/cpu/cpu_linux.go b/cpu/cpu_linux.go index aa1dd90..20a82a8 100644 --- a/cpu/cpu_linux.go +++ b/cpu/cpu_linux.go @@ -62,7 +62,7 @@ func CPUInfo() ([]CPUInfoStat, error) { switch key { case "processor": c = CPUInfoStat{} - t, err := strconv.ParseInt(value, 10, 32) + t, err := strconv.ParseInt(value, 10, 64) if err != nil { return ret, err } @@ -76,7 +76,7 @@ func CPUInfo() ([]CPUInfoStat, error) { case "model name": c.ModelName = value case "stepping": - t, err := strconv.ParseInt(value, 10, 32) + t, err := strconv.ParseInt(value, 10, 64) if err != nil { return ret, err } @@ -88,7 +88,7 @@ func CPUInfo() ([]CPUInfoStat, error) { } c.Mhz = t 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 { return ret, err } @@ -98,7 +98,7 @@ func CPUInfo() ([]CPUInfoStat, error) { case "core id": c.CoreID = value case "cpu cores": - t, err := strconv.ParseInt(value, 10, 32) + t, err := strconv.ParseInt(value, 10, 64) if err != nil { return ret, err } @@ -122,71 +122,71 @@ func parseStatLine(line string) (*CPUTimesStat, error) { if cpu == "cpu" { cpu = "cpu-total" } - user, err := strconv.ParseFloat(fields[1], 32) + user, err := strconv.ParseFloat(fields[1], 64) if err != nil { return nil, err } - nice, err := strconv.ParseFloat(fields[2], 32) + nice, err := strconv.ParseFloat(fields[2], 64) if err != nil { return nil, err } - system, err := strconv.ParseFloat(fields[3], 32) + system, err := strconv.ParseFloat(fields[3], 64) if err != nil { return nil, err } - idle, err := strconv.ParseFloat(fields[4], 32) + idle, err := strconv.ParseFloat(fields[4], 64) if err != nil { return nil, err } - iowait, err := strconv.ParseFloat(fields[5], 32) + iowait, err := strconv.ParseFloat(fields[5], 64) if err != nil { return nil, err } - irq, err := strconv.ParseFloat(fields[6], 32) + irq, err := strconv.ParseFloat(fields[6], 64) if err != nil { return nil, err } - softirq, err := strconv.ParseFloat(fields[7], 32) + softirq, err := strconv.ParseFloat(fields[7], 64) if err != nil { return nil, err } - stolen, err := strconv.ParseFloat(fields[8], 32) + stolen, err := strconv.ParseFloat(fields[8], 64) if err != nil { 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{ CPU: cpu, - User: float32(user) / cpu_tick, - Nice: float32(nice) / cpu_tick, - System: float32(system) / cpu_tick, - Idle: float32(idle) / cpu_tick, - Iowait: float32(iowait) / cpu_tick, - Irq: float32(irq) / cpu_tick, - Softirq: float32(softirq) / cpu_tick, - Stolen: float32(stolen) / cpu_tick, + 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, + Stolen: float64(stolen) / cpu_tick, } 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 { return nil, err } - ct.Steal = float32(steal) + ct.Steal = float64(steal) } 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 { return nil, err } - ct.Guest = float32(guest) + ct.Guest = float64(guest) } 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 { return nil, err } - ct.GuestNice = float32(guestNice) + ct.GuestNice = float64(guestNice) } return ct, nil diff --git a/cpu/cpu_test.go b/cpu/cpu_test.go index 00345cf..c4b02cb 100644 --- a/cpu/cpu_test.go +++ b/cpu/cpu_test.go @@ -74,7 +74,7 @@ func testCPUPercent(t *testing.T, percpu bool) { t.Errorf("error %v", err) } 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) } } diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index 3a14943..b2fa3a0 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -32,9 +32,9 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { system := (kernel - idle) ret = append(ret, CPUTimesStat{ - Idle: float32(idle), - User: float32(user), - System: float32(system), + Idle: float64(idle), + User: float64(user), + System: float64(system), }) return ret, nil }