Skip to content

Commit

Permalink
Merge pull request #601 from Lomanic/issue551
Browse files Browse the repository at this point in the history
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 type.
  • Loading branch information
Lomanic authored Nov 6, 2018
2 parents da37b31 + 79f8a73 commit 4254ce2
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions cpu/cpu_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 4254ce2

Please sign in to comment.