2016-02-12 18:02:16 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package cpu
|
|
|
|
|
|
|
|
import (
|
2018-03-06 00:52:43 +00:00
|
|
|
"context"
|
2016-02-12 18:02:16 +00:00
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/StackExchange/wmi"
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2017-07-28 16:46:44 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Win32_Processor struct {
|
|
|
|
LoadPercentage *uint16
|
|
|
|
Family uint16
|
|
|
|
Manufacturer string
|
|
|
|
Name string
|
|
|
|
NumberOfLogicalProcessors uint32
|
2016-05-09 15:19:04 +00:00
|
|
|
ProcessorID *string
|
2016-02-12 18:02:16 +00:00
|
|
|
Stepping *string
|
|
|
|
MaxClockSpeed uint32
|
|
|
|
}
|
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
// Win32_PerfFormattedData_Counters_ProcessorInformation stores instance value of the perf counters
|
|
|
|
type Win32_PerfFormattedData_Counters_ProcessorInformation struct {
|
|
|
|
Name string
|
|
|
|
PercentDPCTime uint64
|
|
|
|
PercentIdleTime uint64
|
|
|
|
PercentUserTime uint64
|
|
|
|
PercentProcessorTime uint64
|
|
|
|
PercentInterruptTime uint64
|
|
|
|
PercentPriorityTime uint64
|
|
|
|
PercentPrivilegedTime uint64
|
|
|
|
InterruptsPerSec uint32
|
|
|
|
ProcessorFrequency uint32
|
|
|
|
DPCRate uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// Win32_PerfFormattedData_PerfOS_System struct to have count of processes and processor queue length
|
|
|
|
type Win32_PerfFormattedData_PerfOS_System struct {
|
|
|
|
Processes uint32
|
|
|
|
ProcessorQueueLength uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// Times returns times stat per cpu and combined for all CPUs
|
2016-05-09 15:19:04 +00:00
|
|
|
func Times(percpu bool) ([]TimesStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return TimesWithContext(context.Background(), percpu)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
2017-07-28 16:46:44 +00:00
|
|
|
if percpu {
|
|
|
|
return perCPUTimes()
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
var ret []TimesStat
|
2016-02-12 18:02:16 +00:00
|
|
|
var lpIdleTime common.FILETIME
|
|
|
|
var lpKernelTime common.FILETIME
|
|
|
|
var lpUserTime common.FILETIME
|
|
|
|
r, _, _ := common.ProcGetSystemTimes.Call(
|
|
|
|
uintptr(unsafe.Pointer(&lpIdleTime)),
|
|
|
|
uintptr(unsafe.Pointer(&lpKernelTime)),
|
|
|
|
uintptr(unsafe.Pointer(&lpUserTime)))
|
|
|
|
if r == 0 {
|
2017-07-28 16:46:44 +00:00
|
|
|
return ret, windows.GetLastError()
|
2016-02-12 18:02:16 +00: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)))
|
|
|
|
system := (kernel - idle)
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
ret = append(ret, TimesStat{
|
2017-07-28 16:46:44 +00:00
|
|
|
CPU: "cpu-total",
|
2016-02-12 18:02:16 +00:00
|
|
|
Idle: float64(idle),
|
|
|
|
User: float64(user),
|
|
|
|
System: float64(system),
|
|
|
|
})
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
func Info() ([]InfoStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return InfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
var ret []InfoStat
|
2016-02-12 18:02:16 +00:00
|
|
|
var dst []Win32_Processor
|
|
|
|
q := wmi.CreateQuery(&dst, "")
|
2018-03-06 00:52:43 +00:00
|
|
|
if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil {
|
2016-02-12 18:02:16 +00:00
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var procID string
|
|
|
|
for i, l := range dst {
|
|
|
|
procID = ""
|
2016-05-09 15:19:04 +00:00
|
|
|
if l.ProcessorID != nil {
|
|
|
|
procID = *l.ProcessorID
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
cpu := InfoStat{
|
2016-02-12 18:02:16 +00:00
|
|
|
CPU: int32(i),
|
|
|
|
Family: fmt.Sprintf("%d", l.Family),
|
|
|
|
VendorID: l.Manufacturer,
|
|
|
|
ModelName: l.Name,
|
|
|
|
Cores: int32(l.NumberOfLogicalProcessors),
|
|
|
|
PhysicalID: procID,
|
|
|
|
Mhz: float64(l.MaxClockSpeed),
|
|
|
|
Flags: []string{},
|
|
|
|
}
|
|
|
|
ret = append(ret, cpu)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
|
|
|
|
// PerfInfo returns the performance counter's instance value for ProcessorInformation.
|
|
|
|
// Name property is the key by which overall, per cpu and per core metric is known.
|
|
|
|
func PerfInfo() ([]Win32_PerfFormattedData_Counters_ProcessorInformation, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return PerfInfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func PerfInfoWithContext(ctx context.Context) ([]Win32_PerfFormattedData_Counters_ProcessorInformation, error) {
|
2017-07-28 16:46:44 +00:00
|
|
|
var ret []Win32_PerfFormattedData_Counters_ProcessorInformation
|
2018-03-06 00:52:43 +00:00
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
q := wmi.CreateQuery(&ret, "")
|
2018-03-06 00:52:43 +00:00
|
|
|
err := common.WMIQueryWithContext(ctx, q, &ret)
|
|
|
|
if err != nil {
|
|
|
|
return []Win32_PerfFormattedData_Counters_ProcessorInformation{}, err
|
|
|
|
}
|
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcInfo returns processes count and processor queue length in the system.
|
|
|
|
// There is a single queue for processor even on multiprocessors systems.
|
|
|
|
func ProcInfo() ([]Win32_PerfFormattedData_PerfOS_System, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return ProcInfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProcInfoWithContext(ctx context.Context) ([]Win32_PerfFormattedData_PerfOS_System, error) {
|
2017-07-28 16:46:44 +00:00
|
|
|
var ret []Win32_PerfFormattedData_PerfOS_System
|
|
|
|
q := wmi.CreateQuery(&ret, "")
|
2018-03-06 00:52:43 +00:00
|
|
|
err := common.WMIQueryWithContext(ctx, q, &ret)
|
|
|
|
if err != nil {
|
|
|
|
return []Win32_PerfFormattedData_PerfOS_System{}, err
|
|
|
|
}
|
2017-07-28 16:46:44 +00:00
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// perCPUTimes returns times stat per cpu, per core and overall for all CPUs
|
|
|
|
func perCPUTimes() ([]TimesStat, error) {
|
|
|
|
var ret []TimesStat
|
|
|
|
stats, err := PerfInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, v := range stats {
|
|
|
|
c := TimesStat{
|
|
|
|
CPU: v.Name,
|
|
|
|
User: float64(v.PercentUserTime),
|
|
|
|
System: float64(v.PercentPrivilegedTime),
|
|
|
|
Idle: float64(v.PercentIdleTime),
|
|
|
|
Irq: float64(v.PercentInterruptTime),
|
|
|
|
}
|
|
|
|
ret = append(ret, c)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|