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 (
|
2017-11-09 15:44:26 -08:00
|
|
|
"context"
|
2015-04-20 00:05:31 +09:00
|
|
|
"fmt"
|
2014-04-19 01:29:34 +09:00
|
|
|
"unsafe"
|
2014-11-27 22:28:05 +09:00
|
|
|
|
2015-04-20 00:05:31 +09:00
|
|
|
"github.com/StackExchange/wmi"
|
2015-10-20 00:04:57 +09:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2017-06-02 13:51:00 -07:00
|
|
|
"golang.org/x/sys/windows"
|
2014-04-18 21:28:00 +09:00
|
|
|
)
|
|
|
|
|
2015-04-20 00:05:31 +09:00
|
|
|
type Win32_Processor struct {
|
2015-09-03 21:48:18 +09:00
|
|
|
LoadPercentage *uint16
|
2015-04-20 00:05:31 +09:00
|
|
|
Family uint16
|
|
|
|
Manufacturer string
|
|
|
|
Name string
|
|
|
|
NumberOfLogicalProcessors uint32
|
2016-04-01 21:34:39 +09:00
|
|
|
ProcessorID *string
|
2015-04-20 00:05:31 +09:00
|
|
|
Stepping *string
|
|
|
|
MaxClockSpeed uint32
|
|
|
|
}
|
|
|
|
|
2017-05-25 17:54:43 +05:30
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-06-05 15:46:35 +05:30
|
|
|
// Win32_PerfFormattedData_PerfOS_System struct to have count of processes and processor queue length
|
2017-05-25 17:54:43 +05:30
|
|
|
type Win32_PerfFormattedData_PerfOS_System struct {
|
|
|
|
Processes uint32
|
|
|
|
ProcessorQueueLength uint32
|
|
|
|
}
|
|
|
|
|
2017-06-05 15:46:35 +05:30
|
|
|
// Times returns times stat per cpu and combined for all CPUs
|
2016-03-22 23:09:12 +09:00
|
|
|
func Times(percpu bool) ([]TimesStat, error) {
|
2017-12-31 15:25:49 +09:00
|
|
|
return TimesWithContext(context.Background(), percpu)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
2017-06-05 14:18:20 +05:30
|
|
|
if percpu {
|
|
|
|
return perCPUTimes()
|
|
|
|
}
|
2014-04-18 21:28:00 +09:00
|
|
|
|
2017-06-05 14:18:20 +05:30
|
|
|
var ret []TimesStat
|
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 {
|
2017-06-02 13:51:00 -07:00
|
|
|
return ret, windows.GetLastError()
|
2014-04-19 01:29:34 +09:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2016-03-22 23:09:12 +09:00
|
|
|
ret = append(ret, TimesStat{
|
2017-07-20 11:47:15 +08:00
|
|
|
CPU: "cpu-total",
|
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
|
|
|
|
2016-03-22 23:09:12 +09:00
|
|
|
func Info() ([]InfoStat, error) {
|
2017-12-31 15:25:49 +09:00
|
|
|
return InfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
2016-03-22 23:09:12 +09:00
|
|
|
var ret []InfoStat
|
2015-04-20 00:05:31 +09:00
|
|
|
var dst []Win32_Processor
|
|
|
|
q := wmi.CreateQuery(&dst, "")
|
2017-11-09 15:44:26 -08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
if err := common.WMIQueryWithContext(ctx, q, &dst); err != nil {
|
2015-02-15 21:25:33 +09:00
|
|
|
return ret, err
|
|
|
|
}
|
2015-09-03 21:48:18 +09:00
|
|
|
|
2015-08-28 06:56:05 +01:00
|
|
|
var procID string
|
2015-09-03 21:48:18 +09:00
|
|
|
for i, l := range dst {
|
2015-08-28 06:56:05 +01:00
|
|
|
procID = ""
|
2016-04-01 21:34:39 +09:00
|
|
|
if l.ProcessorID != nil {
|
|
|
|
procID = *l.ProcessorID
|
2015-08-28 06:56:05 +01:00
|
|
|
}
|
2015-09-03 21:48:18 +09:00
|
|
|
|
2016-03-22 23:09:12 +09:00
|
|
|
cpu := InfoStat{
|
2015-02-15 21:25:33 +09:00
|
|
|
CPU: int32(i),
|
2015-04-20 00:05:31 +09:00
|
|
|
Family: fmt.Sprintf("%d", l.Family),
|
|
|
|
VendorID: l.Manufacturer,
|
|
|
|
ModelName: l.Name,
|
|
|
|
Cores: int32(l.NumberOfLogicalProcessors),
|
2015-08-25 22:21:10 -07:00
|
|
|
PhysicalID: procID,
|
2015-04-20 00:05:31 +09:00
|
|
|
Mhz: float64(l.MaxClockSpeed),
|
2015-02-15 21:25:33 +09:00
|
|
|
Flags: []string{},
|
|
|
|
}
|
|
|
|
ret = append(ret, cpu)
|
|
|
|
}
|
2015-04-20 00:05:31 +09:00
|
|
|
|
2014-05-16 18:42:37 +09:00
|
|
|
return ret, nil
|
|
|
|
}
|
2017-05-25 17:54:43 +05:30
|
|
|
|
|
|
|
// 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) {
|
2017-12-31 15:25:49 +09:00
|
|
|
return PerfInfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func PerfInfoWithContext(ctx context.Context) ([]Win32_PerfFormattedData_Counters_ProcessorInformation, error) {
|
2017-05-25 17:54:43 +05:30
|
|
|
var ret []Win32_PerfFormattedData_Counters_ProcessorInformation
|
2017-11-09 15:44:26 -08:00
|
|
|
|
2017-05-25 17:54:43 +05:30
|
|
|
q := wmi.CreateQuery(&ret, "")
|
2017-11-09 15:44:26 -08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
err := common.WMIQueryWithContext(ctx, q, &ret)
|
2018-02-12 15:04:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return Win32_PerfFormattedData_Counters_ProcessorInformation{}, err
|
|
|
|
}
|
|
|
|
|
2017-05-25 17:54:43 +05:30
|
|
|
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) {
|
2017-12-31 15:25:49 +09:00
|
|
|
return ProcInfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProcInfoWithContext(ctx context.Context) ([]Win32_PerfFormattedData_PerfOS_System, error) {
|
2017-05-25 17:54:43 +05:30
|
|
|
var ret []Win32_PerfFormattedData_PerfOS_System
|
|
|
|
q := wmi.CreateQuery(&ret, "")
|
2017-11-09 15:44:26 -08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), common.Timeout)
|
|
|
|
defer cancel()
|
|
|
|
err := common.WMIQueryWithContext(ctx, q, &ret)
|
2018-02-12 15:04:20 +01:00
|
|
|
if err != nil {
|
|
|
|
return Win32_PerfFormattedData_PerfOS_System{}, err
|
|
|
|
}
|
2017-05-25 17:54:43 +05:30
|
|
|
return ret, err
|
|
|
|
}
|
2017-06-05 14:18:20 +05:30
|
|
|
|
2017-06-05 15:46:35 +05:30
|
|
|
// perCPUTimes returns times stat per cpu, per core and overall for all CPUs
|
2017-06-05 14:18:20 +05:30
|
|
|
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
|
|
|
|
}
|