From 79f8a73e237ad12fef1cecf8bdba0a4d996234be Mon Sep 17 00:00:00 2001 From: Lomanic Date: Mon, 5 Nov 2018 12:23:42 +0100 Subject: [PATCH] Fix #551 Filter out '_Total' results from PerfInfoWithContext and unexport this function Also propagate context in perCPUTimes (renamed to perCPUTimesWithContext) and unexport Win32_PerfFormattedData_Counters_ProcessorInformation. --- cpu/cpu_windows.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index 8aa691c..975c2cb 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -23,8 +23,8 @@ type Win32_Processor struct { MaxClockSpeed uint32 } -// Win32_PerfFormattedData_Counters_ProcessorInformation stores instance value of the perf counters -type Win32_PerfFormattedData_Counters_ProcessorInformation struct { +// win32_PerfFormattedData_Counters_ProcessorInformation stores instance value of the perf counters +type win32_PerfFormattedData_Counters_ProcessorInformation struct { Name string PercentDPCTime uint64 PercentIdleTime uint64 @@ -51,7 +51,7 @@ func Times(percpu bool) ([]TimesStat, error) { func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { if percpu { - return perCPUTimes() + return perCPUTimesWithContext(ctx) } var ret []TimesStat @@ -119,17 +119,13 @@ func InfoWithContext(ctx context.Context) ([]InfoStat, error) { // 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) { - return PerfInfoWithContext(context.Background()) -} - -func PerfInfoWithContext(ctx context.Context) ([]Win32_PerfFormattedData_Counters_ProcessorInformation, error) { - var ret []Win32_PerfFormattedData_Counters_ProcessorInformation +func perfInfoWithContext(ctx context.Context) ([]win32_PerfFormattedData_Counters_ProcessorInformation, error) { + var ret []win32_PerfFormattedData_Counters_ProcessorInformation - q := wmi.CreateQuery(&ret, "") + q := wmi.CreateQuery(&ret, "WHERE NOT Name LIKE '%_Total'") err := common.WMIQueryWithContext(ctx, q, &ret) if err != nil { - return []Win32_PerfFormattedData_Counters_ProcessorInformation{}, err + return []win32_PerfFormattedData_Counters_ProcessorInformation{}, err } return ret, err @@ -152,9 +148,9 @@ func ProcInfoWithContext(ctx context.Context) ([]Win32_PerfFormattedData_PerfOS_ } // perCPUTimes returns times stat per cpu, per core and overall for all CPUs -func perCPUTimes() ([]TimesStat, error) { +func perCPUTimesWithContext(ctx context.Context) ([]TimesStat, error) { var ret []TimesStat - stats, err := PerfInfo() + stats, err := perfInfoWithContext(ctx) if err != nil { return nil, err }