-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3167 from hashicorp/b-windows-stats
Fix invalid CPU stats on Windows
- Loading branch information
Showing
4 changed files
with
196 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// +build !windows | ||
|
||
package stats | ||
|
||
import ( | ||
shelpers "github.com/hashicorp/nomad/helper/stats" | ||
"github.com/shirou/gopsutil/cpu" | ||
) | ||
|
||
func (h *HostStatsCollector) collectCPUStats() (cpus []*CPUStats, totalTicks float64, err error) { | ||
|
||
ticksConsumed := 0.0 | ||
cpuStats, err := cpu.Times(true) | ||
if err != nil { | ||
return nil, 0.0, err | ||
} | ||
cs := make([]*CPUStats, len(cpuStats)) | ||
for idx, cpuStat := range cpuStats { | ||
percentCalculator, ok := h.statsCalculator[cpuStat.CPU] | ||
if !ok { | ||
percentCalculator = NewHostCpuStatsCalculator() | ||
h.statsCalculator[cpuStat.CPU] = percentCalculator | ||
} | ||
idle, user, system, total := percentCalculator.Calculate(cpuStat) | ||
cs[idx] = &CPUStats{ | ||
CPU: cpuStat.CPU, | ||
User: user, | ||
System: system, | ||
Idle: idle, | ||
Total: total, | ||
} | ||
ticksConsumed += (total / 100.0) * (shelpers.TotalTicksAvailable() / float64(len(cpuStats))) | ||
} | ||
|
||
return cs, ticksConsumed, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// +build windows | ||
|
||
package stats | ||
|
||
import ( | ||
"fmt" | ||
|
||
shelpers "github.com/hashicorp/nomad/helper/stats" | ||
"github.com/shirou/gopsutil/cpu" | ||
) | ||
|
||
func (h *HostStatsCollector) collectCPUStats() (cpus []*CPUStats, totalTicks float64, err error) { | ||
// Get the per cpu stats | ||
cpuStats, err := cpu.Times(true) | ||
if err != nil { | ||
return nil, 0.0, err | ||
} | ||
|
||
cs := make([]*CPUStats, len(cpuStats)) | ||
for idx, cpuStat := range cpuStats { | ||
|
||
// On windows they are already in percent | ||
cs[idx] = &CPUStats{ | ||
CPU: cpuStat.CPU, | ||
User: cpuStat.User, | ||
System: cpuStat.System, | ||
Idle: cpuStat.Idle, | ||
Total: cpuStat.Total(), | ||
} | ||
} | ||
|
||
// Get the number of ticks | ||
allCpu, err := cpu.Times(false) | ||
if err != nil { | ||
return nil, 0.0, err | ||
} | ||
if len(allCpu) != 1 { | ||
return nil, 0.0, fmt.Errorf("unexpected number of cpus (%d)", len(allCpu)) | ||
} | ||
|
||
// We use the calculator because when retrieving against all cpus it is | ||
// returned as ticks. | ||
all := allCpu[0] | ||
percentCalculator, ok := h.statsCalculator[all.CPU] | ||
if !ok { | ||
percentCalculator = NewHostCpuStatsCalculator() | ||
h.statsCalculator[all.CPU] = percentCalculator | ||
} | ||
_, _, _, total := percentCalculator.Calculate(all) | ||
ticks := (total / 100) * shelpers.TotalTicksAvailable() | ||
|
||
return cs, ticks, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters