2014-04-18 20:28:00 +08:00
|
|
|
// +build windows
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package cpu
|
2014-04-18 20:28:00 +08:00
|
|
|
|
|
|
|
import (
|
2015-04-19 23:05:31 +08:00
|
|
|
"fmt"
|
2014-04-19 00:29:34 +08:00
|
|
|
"syscall"
|
2015-03-11 22:00:06 +08:00
|
|
|
"time"
|
2014-04-19 00:29:34 +08:00
|
|
|
"unsafe"
|
2014-11-27 21:28:05 +08:00
|
|
|
|
2015-04-19 23:05:31 +08:00
|
|
|
"github.com/StackExchange/wmi"
|
|
|
|
|
2014-11-27 21:28:05 +08:00
|
|
|
common "github.com/shirou/gopsutil/common"
|
2014-04-18 20:28:00 +08:00
|
|
|
)
|
|
|
|
|
2015-04-19 23:05:31 +08:00
|
|
|
type Win32_Processor struct {
|
|
|
|
LoadPercentage uint16
|
|
|
|
Family uint16
|
|
|
|
Manufacturer string
|
|
|
|
Name string
|
|
|
|
NumberOfLogicalProcessors uint32
|
2015-08-26 13:21:10 +08:00
|
|
|
ProcessorId *string
|
2015-04-19 23:05:31 +08:00
|
|
|
Stepping *string
|
|
|
|
MaxClockSpeed uint32
|
|
|
|
}
|
|
|
|
|
2014-11-09 05:39:06 +08:00
|
|
|
// TODO: Get percpu
|
2014-04-30 14:32:05 +08:00
|
|
|
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
|
|
|
|
var ret []CPUTimesStat
|
2014-04-18 20:28:00 +08:00
|
|
|
|
2014-11-27 21:28:05 +08:00
|
|
|
var lpIdleTime common.FILETIME
|
|
|
|
var lpKernelTime common.FILETIME
|
|
|
|
var lpUserTime common.FILETIME
|
|
|
|
r, _, _ := common.ProcGetSystemTimes.Call(
|
2014-04-19 00:29:34 +08:00
|
|
|
uintptr(unsafe.Pointer(&lpIdleTime)),
|
|
|
|
uintptr(unsafe.Pointer(&lpKernelTime)),
|
|
|
|
uintptr(unsafe.Pointer(&lpUserTime)))
|
|
|
|
if r == 0 {
|
|
|
|
return ret, syscall.GetLastError()
|
|
|
|
}
|
|
|
|
|
2014-04-30 14:32:05 +08: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 00:29:34 +08:00
|
|
|
system := (kernel - idle)
|
|
|
|
|
2014-04-30 14:32:05 +08:00
|
|
|
ret = append(ret, CPUTimesStat{
|
2015-02-13 21:45:12 +08:00
|
|
|
Idle: float64(idle),
|
|
|
|
User: float64(user),
|
|
|
|
System: float64(system),
|
2014-04-19 00:29:34 +08:00
|
|
|
})
|
2014-04-18 20:28:00 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|
2014-05-16 17:42:37 +08:00
|
|
|
|
|
|
|
func CPUInfo() ([]CPUInfoStat, error) {
|
|
|
|
var ret []CPUInfoStat
|
2015-04-19 23:05:31 +08:00
|
|
|
var dst []Win32_Processor
|
|
|
|
q := wmi.CreateQuery(&dst, "")
|
|
|
|
err := wmi.Query(q, &dst)
|
2015-02-15 20:25:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2015-08-26 13:21:10 +08:00
|
|
|
|
2015-08-28 13:56:05 +08:00
|
|
|
var procID string
|
2015-08-26 13:21:10 +08:00
|
|
|
for i, l := range dst {
|
2015-08-28 13:56:05 +08:00
|
|
|
procID = ""
|
|
|
|
if l.ProcessorId != nil {
|
|
|
|
procID = *l.ProcessorId
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-15 20:25:33 +08:00
|
|
|
cpu := CPUInfoStat{
|
|
|
|
CPU: int32(i),
|
2015-04-19 23:05:31 +08:00
|
|
|
Family: fmt.Sprintf("%d", l.Family),
|
|
|
|
VendorID: l.Manufacturer,
|
|
|
|
ModelName: l.Name,
|
|
|
|
Cores: int32(l.NumberOfLogicalProcessors),
|
2015-08-26 13:21:10 +08:00
|
|
|
PhysicalID: procID,
|
2015-04-19 23:05:31 +08:00
|
|
|
Mhz: float64(l.MaxClockSpeed),
|
2015-02-15 20:25:33 +08:00
|
|
|
Flags: []string{},
|
|
|
|
}
|
|
|
|
ret = append(ret, cpu)
|
|
|
|
}
|
2015-04-19 23:05:31 +08:00
|
|
|
|
2014-05-16 17:42:37 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|
2015-03-11 22:00:06 +08:00
|
|
|
|
|
|
|
func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
|
2015-04-19 23:05:31 +08:00
|
|
|
var ret []float64
|
|
|
|
var dst []Win32_Processor
|
|
|
|
q := wmi.CreateQuery(&dst, "")
|
|
|
|
err := wmi.Query(q, &dst)
|
2015-03-11 22:00:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2015-04-19 23:05:31 +08:00
|
|
|
for _, l := range dst {
|
|
|
|
// use range but windows can only get one percent.
|
|
|
|
ret = append(ret, float64(l.LoadPercentage)/100.0)
|
2015-03-11 22:00:06 +08:00
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|