2014-12-30 21:09:05 +08:00
|
|
|
package cpu
|
2014-04-18 15:34:47 +08:00
|
|
|
|
|
|
|
import (
|
2014-05-01 17:43:11 +08:00
|
|
|
"encoding/json"
|
2016-06-02 05:24:23 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-04-18 15:34:47 +08:00
|
|
|
"runtime"
|
2015-02-13 22:11:27 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-06-02 05:21:58 +08:00
|
|
|
"sync"
|
2014-04-18 15:34:47 +08:00
|
|
|
)
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
type TimesStat struct {
|
2014-04-30 15:16:07 +08:00
|
|
|
CPU string `json:"cpu"`
|
2015-02-13 21:45:12 +08:00
|
|
|
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"`
|
2016-03-23 09:52:46 +08:00
|
|
|
GuestNice float64 `json:"guestNice"`
|
2015-02-13 21:45:12 +08:00
|
|
|
Stolen float64 `json:"stolen"`
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
type InfoStat struct {
|
2014-05-16 17:39:17 +08:00
|
|
|
CPU int32 `json:"cpu"`
|
2016-03-23 09:52:46 +08:00
|
|
|
VendorID string `json:"vendorId"`
|
2014-05-16 17:39:17 +08:00
|
|
|
Family string `json:"family"`
|
|
|
|
Model string `json:"model"`
|
|
|
|
Stepping int32 `json:"stepping"`
|
2016-03-23 09:52:46 +08:00
|
|
|
PhysicalID string `json:"physicalId"`
|
|
|
|
CoreID string `json:"coreId"`
|
2014-05-16 17:39:17 +08:00
|
|
|
Cores int32 `json:"cores"`
|
2016-03-23 09:52:46 +08:00
|
|
|
ModelName string `json:"modelName"`
|
2014-05-16 17:39:17 +08:00
|
|
|
Mhz float64 `json:"mhz"`
|
2016-03-23 09:52:46 +08:00
|
|
|
CacheSize int32 `json:"cacheSize"`
|
2014-05-16 17:12:18 +08:00
|
|
|
Flags []string `json:"flags"`
|
|
|
|
}
|
|
|
|
|
2016-06-02 05:45:29 +08:00
|
|
|
type lastPercent struct {
|
2016-06-02 05:21:58 +08:00
|
|
|
sync.Mutex
|
|
|
|
lastCPUTimes []TimesStat
|
|
|
|
lastPerCPUTimes []TimesStat
|
|
|
|
}
|
|
|
|
|
2016-06-02 05:45:29 +08:00
|
|
|
var lastCPUPercent lastPercent
|
2016-05-20 16:59:41 +08:00
|
|
|
var invoke common.Invoker
|
2016-06-02 05:21:58 +08:00
|
|
|
|
|
|
|
func init() {
|
2016-05-20 16:59:41 +08:00
|
|
|
invoke = common.Invoke{}
|
2016-06-02 05:21:58 +08:00
|
|
|
lastCPUPercent.Lock()
|
|
|
|
lastCPUPercent.lastCPUTimes, _ = Times(false)
|
|
|
|
lastCPUPercent.lastPerCPUTimes, _ = Times(true)
|
|
|
|
lastCPUPercent.Unlock()
|
|
|
|
}
|
2014-11-02 11:05:52 +08:00
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func Counts(logical bool) (int, error) {
|
2015-03-11 22:00:06 +08:00
|
|
|
return runtime.NumCPU(), nil
|
2014-11-02 11:05:52 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func (c TimesStat) String() string {
|
2015-02-13 22:11:27 +08:00
|
|
|
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),
|
2016-03-23 09:52:46 +08:00
|
|
|
`"guestNice":` + strconv.FormatFloat(c.GuestNice, 'f', 1, 64),
|
2015-02-13 22:11:27 +08:00
|
|
|
`"stolen":` + strconv.FormatFloat(c.Stolen, 'f', 1, 64),
|
|
|
|
}
|
|
|
|
|
|
|
|
return `{` + strings.Join(v, ",") + `}`
|
2014-05-01 17:43:11 +08:00
|
|
|
}
|
2014-05-16 17:12:18 +08:00
|
|
|
|
2016-02-11 00:48:43 +08:00
|
|
|
// Total returns the total number of seconds in a CPUTimesStat
|
2016-03-22 22:09:12 +08:00
|
|
|
func (c TimesStat) Total() float64 {
|
2016-02-11 00:48:43 +08:00
|
|
|
total := c.User + c.System + c.Nice + c.Iowait + c.Irq + c.Softirq + c.Steal +
|
|
|
|
c.Guest + c.GuestNice + c.Idle + c.Stolen
|
|
|
|
return total
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func (c InfoStat) String() string {
|
2014-05-16 17:12:18 +08:00
|
|
|
s, _ := json.Marshal(c)
|
|
|
|
return string(s)
|
|
|
|
}
|