2014-04-18 21:28:00 +09:00
|
|
|
// +build windows
|
|
|
|
|
2014-12-30 22:09:05 +09:00
|
|
|
package cpu
|
2014-04-18 21:28:00 +09:00
|
|
|
|
|
|
|
import (
|
2015-02-15 21:25:33 +09:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-04-19 01:29:34 +09:00
|
|
|
"syscall"
|
2015-03-11 23:00:06 +09:00
|
|
|
"time"
|
2014-04-19 01:29:34 +09:00
|
|
|
"unsafe"
|
2014-11-27 22:28:05 +09:00
|
|
|
|
|
|
|
common "github.com/shirou/gopsutil/common"
|
2014-04-18 21:28:00 +09:00
|
|
|
)
|
|
|
|
|
2014-11-08 22:39:06 +01:00
|
|
|
// TODO: Get percpu
|
2014-04-30 15:32:05 +09:00
|
|
|
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
|
|
|
|
var ret []CPUTimesStat
|
2014-04-18 21:28:00 +09:00
|
|
|
|
2014-11-27 22:28:05 +09:00
|
|
|
var lpIdleTime common.FILETIME
|
|
|
|
var lpKernelTime common.FILETIME
|
|
|
|
var lpUserTime common.FILETIME
|
|
|
|
r, _, _ := common.ProcGetSystemTimes.Call(
|
2014-04-19 01:29:34 +09:00
|
|
|
uintptr(unsafe.Pointer(&lpIdleTime)),
|
|
|
|
uintptr(unsafe.Pointer(&lpKernelTime)),
|
|
|
|
uintptr(unsafe.Pointer(&lpUserTime)))
|
|
|
|
if r == 0 {
|
|
|
|
return ret, syscall.GetLastError()
|
|
|
|
}
|
|
|
|
|
2014-04-30 15:32:05 +09:00
|
|
|
LOT := float64(0.0000001)
|
|
|
|
HIT := (LOT * 4294967296.0)
|
|
|
|
idle := ((HIT * float64(lpIdleTime.DwHighDateTime)) + (LOT * float64(lpIdleTime.DwLowDateTime)))
|
|
|
|
user := ((HIT * float64(lpUserTime.DwHighDateTime)) + (LOT * float64(lpUserTime.DwLowDateTime)))
|
|
|
|
kernel := ((HIT * float64(lpKernelTime.DwHighDateTime)) + (LOT * float64(lpKernelTime.DwLowDateTime)))
|
2014-04-19 01:29:34 +09:00
|
|
|
system := (kernel - idle)
|
|
|
|
|
2014-04-30 15:32:05 +09:00
|
|
|
ret = append(ret, CPUTimesStat{
|
2015-02-13 22:45:12 +09:00
|
|
|
Idle: float64(idle),
|
|
|
|
User: float64(user),
|
|
|
|
System: float64(system),
|
2014-04-19 01:29:34 +09:00
|
|
|
})
|
2014-04-18 21:28:00 +09:00
|
|
|
return ret, nil
|
|
|
|
}
|
2014-05-16 18:42:37 +09:00
|
|
|
|
|
|
|
func CPUInfo() ([]CPUInfoStat, error) {
|
|
|
|
var ret []CPUInfoStat
|
2015-02-15 21:25:33 +09:00
|
|
|
lines, err := common.GetWmic("cpu", "Family,L2CacheSize,Manufacturer,Name,NumberOfLogicalProcessors,ProcessorId,Stepping")
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
for i, l := range lines {
|
|
|
|
t := strings.Split(l, ",")
|
|
|
|
if len(t) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cache, err := strconv.Atoi(t[2])
|
|
|
|
if err != nil {
|
|
|
|
cache = 0
|
|
|
|
}
|
|
|
|
cores, err := strconv.Atoi(t[5])
|
|
|
|
if err != nil {
|
|
|
|
cores = 0
|
|
|
|
}
|
|
|
|
stepping, err := strconv.Atoi(t[7])
|
|
|
|
if err != nil {
|
|
|
|
stepping = 0
|
|
|
|
}
|
|
|
|
cpu := CPUInfoStat{
|
|
|
|
CPU: int32(i),
|
|
|
|
Family: t[1],
|
|
|
|
CacheSize: int32(cache),
|
|
|
|
VendorID: t[3],
|
|
|
|
ModelName: t[4],
|
|
|
|
Cores: int32(cores),
|
|
|
|
PhysicalID: t[6],
|
|
|
|
Stepping: int32(stepping),
|
|
|
|
Flags: []string{},
|
|
|
|
}
|
|
|
|
ret = append(ret, cpu)
|
|
|
|
}
|
2014-05-16 18:42:37 +09:00
|
|
|
return ret, nil
|
|
|
|
}
|
2015-03-11 23:00:06 +09:00
|
|
|
|
|
|
|
func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
|
|
|
|
ret := []float64{}
|
|
|
|
|
|
|
|
lines, err := common.GetWmic("cpu", "loadpercentage")
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
for _, l := range lines {
|
|
|
|
t := strings.Split(l, ",")
|
|
|
|
|
|
|
|
if len(t) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p, err := strconv.Atoi(t[1])
|
|
|
|
if err != nil {
|
|
|
|
p = 0
|
|
|
|
}
|
|
|
|
ret = append(ret, float64(p)/100.0)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|