Skip to content

Commit

Permalink
cpu: fix build option on unix.
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed Mar 11, 2015
1 parent ead8393 commit 170390c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 36 deletions.
43 changes: 7 additions & 36 deletions cpu/cpu_unix.go
@@ -1,13 +1,13 @@
// +build linux,freebsd,darwin
// +build linux freebsd darwin

package cpu

import (
"encoding/json"
"strconv"
"strings"
"time"
)
import "time"

func init() {
lastCPUTimes, _ = CPUTimes(false)
lastPerCPUTimes, _ = CPUTimes(true)
}

func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
getAllBusy := func(t CPUTimesStat) (float64, float64) {
Expand Down Expand Up @@ -59,32 +59,3 @@ func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
}
return ret, nil
}

func (c CPUTimesStat) String() string {
v := []string{
`"cpu":"` + c.CPU + `"`,
`"user":` + strconv.FormatFloat(c.User, 'f', 1, 64),
`"system":` + strconv.FormatFloat(c.System, 'f', 1, 64),
`"idle":` + strconv.FormatFloat(c.Idle, 'f', 1, 64),
`"nice":` + strconv.FormatFloat(c.Nice, 'f', 1, 64),
`"iowait":` + strconv.FormatFloat(c.Iowait, 'f', 1, 64),
`"irq":` + strconv.FormatFloat(c.Irq, 'f', 1, 64),
`"softirq":` + strconv.FormatFloat(c.Softirq, 'f', 1, 64),
`"steal":` + strconv.FormatFloat(c.Steal, 'f', 1, 64),
`"guest":` + strconv.FormatFloat(c.Guest, 'f', 1, 64),
`"guest_nice":` + strconv.FormatFloat(c.GuestNice, 'f', 1, 64),
`"stolen":` + strconv.FormatFloat(c.Stolen, 'f', 1, 64),
}

return `{` + strings.Join(v, ",") + `}`
}

func (c CPUInfoStat) String() string {
s, _ := json.Marshal(c)
return string(s)
}

func init() {
lastCPUTimes, _ = CPUTimes(false)
lastPerCPUTimes, _ = CPUTimes(true)
}
1 change: 1 addition & 0 deletions cpu/cpu_windows.go
Expand Up @@ -98,6 +98,7 @@ func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
if err != nil {
p = 0
}
// but windows can only get one percent.
ret = append(ret, float64(p)/100.0)
}
return ret, nil
Expand Down

0 comments on commit 170390c

Please sign in to comment.