github.com/smithx10/nomad@v0.9.1-rc1/client/stats/cpu_windows.go (about) 1 // +build windows 2 3 package stats 4 5 import ( 6 "fmt" 7 8 shelpers "github.com/hashicorp/nomad/helper/stats" 9 "github.com/shirou/gopsutil/cpu" 10 ) 11 12 func (h *HostStatsCollector) collectCPUStats() (cpus []*CPUStats, totalTicks float64, err error) { 13 // Get the per cpu stats 14 cpuStats, err := cpu.Times(true) 15 if err != nil { 16 return nil, 0.0, err 17 } 18 19 cs := make([]*CPUStats, len(cpuStats)) 20 for idx, cpuStat := range cpuStats { 21 22 // On windows they are already in percent 23 cs[idx] = &CPUStats{ 24 CPU: cpuStat.CPU, 25 User: cpuStat.User, 26 System: cpuStat.System, 27 Idle: cpuStat.Idle, 28 Total: cpuStat.Total(), 29 } 30 } 31 32 // Get the number of ticks 33 allCpu, err := cpu.Times(false) 34 if err != nil { 35 return nil, 0.0, err 36 } 37 if len(allCpu) != 1 { 38 return nil, 0.0, fmt.Errorf("unexpected number of cpus (%d)", len(allCpu)) 39 } 40 41 // We use the calculator because when retrieving against all cpus it is 42 // returned as ticks. 43 all := allCpu[0] 44 percentCalculator, ok := h.statsCalculator[all.CPU] 45 if !ok { 46 percentCalculator = NewHostCpuStatsCalculator() 47 h.statsCalculator[all.CPU] = percentCalculator 48 } 49 _, _, _, total := percentCalculator.Calculate(all) 50 ticks := (total / 100) * shelpers.TotalTicksAvailable() 51 52 return cs, ticks, nil 53 }