diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index 24fbb6d..7e3b74b 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -22,10 +22,34 @@ type Win32_Processor struct { MaxClockSpeed uint32 } -// TODO: Get percpu +// 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 func Times(percpu bool) ([]TimesStat, error) { - var ret []TimesStat + if percpu { + return perCPUTimes() + } + var ret []TimesStat var lpIdleTime common.FILETIME var lpKernelTime common.FILETIME var lpUserTime common.FILETIME @@ -83,3 +107,41 @@ func Info() ([]InfoStat, error) { return ret, nil } + +// 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) { + var ret []Win32_PerfFormattedData_Counters_ProcessorInformation + q := wmi.CreateQuery(&ret, "") + err := wmi.Query(q, &ret) + 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) { + var ret []Win32_PerfFormattedData_PerfOS_System + q := wmi.CreateQuery(&ret, "") + err := wmi.Query(q, &ret) + 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 +}