shirou_gopsutil/cpu/cpu.go

75 lines
2.0 KiB
Go
Raw Normal View History

2014-12-30 21:09:05 +08:00
package cpu
2014-04-18 15:34:47 +08:00
import (
"encoding/json"
2014-04-18 15:34:47 +08:00
"runtime"
"strconv"
"strings"
2014-04-18 15:34:47 +08:00
)
2014-04-30 14:32:05 +08:00
type CPUTimesStat struct {
2014-04-30 15:16:07 +08:00
CPU string `json:"cpu"`
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"`
2014-04-18 15:34:47 +08:00
}
2014-05-16 17:12:18 +08:00
type CPUInfoStat struct {
2014-05-16 17:39:17 +08:00
CPU int32 `json:"cpu"`
VendorID string `json:"vendor_id"`
2014-05-16 17:39:17 +08:00
Family string `json:"family"`
Model string `json:"model"`
Stepping int32 `json:"stepping"`
PhysicalID string `json:"physical_id"`
CoreID string `json:"core_id"`
2014-05-16 17:39:17 +08:00
Cores int32 `json:"cores"`
ModelName string `json:"model_name"`
2014-05-16 17:39:17 +08:00
Mhz float64 `json:"mhz"`
CacheSize int32 `json:"cache_size"`
2014-05-16 17:12:18 +08:00
Flags []string `json:"flags"`
}
2014-11-02 11:05:52 +08:00
var lastCPUTimes []CPUTimesStat
var lastPerCPUTimes []CPUTimesStat
func CPUCounts(logical bool) (int, error) {
return runtime.NumCPU(), nil
2014-11-02 11:05:52 +08:00
}
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, ",") + `}`
}
2014-05-16 17:12:18 +08:00
func (c CPUInfoStat) String() string {
s, _ := json.Marshal(c)
return string(s)
}
2014-11-02 11:05:52 +08:00
func init() {
lastCPUTimes, _ = CPUTimes(false)
lastPerCPUTimes, _ = CPUTimes(true)
}