From 07253315715c6b4a19af170c53100199e48cf874 Mon Sep 17 00:00:00 2001 From: KenjiTakahashi Date: Sun, 2 Nov 2014 01:14:38 +0100 Subject: [PATCH] CPUTimes percpu for FreeBSD --- cpu_freebsd.go | 76 ++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/cpu_freebsd.go b/cpu_freebsd.go index ecf9304..2d5df5e 100644 --- a/cpu_freebsd.go +++ b/cpu_freebsd.go @@ -3,6 +3,7 @@ package gopsutil import ( + "fmt" "regexp" "strconv" "strings" @@ -23,45 +24,62 @@ const ( ClocksPerSec = 128 ) -// TODO: get per cpus func CPUTimes(percpu bool) ([]CPUTimesStat, error) { var ret []CPUTimesStat - cpuTime, err := doSysctrl("kern.cp_time") - if err != nil { - return ret, err + var sysctlCall string + var ncpu int + if percpu { + sysctlCall = "kern.cp_times" + ncpu, _ = CPUCounts(true) + } else { + sysctlCall = "kern.cp_time" + ncpu = 1 } - user, err := strconv.ParseFloat(cpuTime[CPUser], 32) - if err != nil { - return ret, err - } - nice, err := strconv.ParseFloat(cpuTime[CPNice], 32) - if err != nil { - return ret, err - } - sys, err := strconv.ParseFloat(cpuTime[CPSys], 32) - if err != nil { - return ret, err - } - idle, err := strconv.ParseFloat(cpuTime[CPIdle], 32) - if err != nil { - return ret, err - } - intr, err := strconv.ParseFloat(cpuTime[CPIntr], 32) + cpuTimes, err := doSysctrl(sysctlCall) if err != nil { return ret, err } - c := CPUTimesStat{ - User: float32(user / ClocksPerSec), - Nice: float32(nice / ClocksPerSec), - System: float32(sys / ClocksPerSec), - Idle: float32(idle / ClocksPerSec), - Irq: float32(intr / ClocksPerSec), - } + for i := 0; i < ncpu; i++ { + offset := CPUStates * i + user, err := strconv.ParseFloat(cpuTimes[CPUser+offset], 32) + if err != nil { + return ret, err + } + nice, err := strconv.ParseFloat(cpuTimes[CPNice+offset], 32) + if err != nil { + return ret, err + } + sys, err := strconv.ParseFloat(cpuTimes[CPSys+offset], 32) + if err != nil { + return ret, err + } + idle, err := strconv.ParseFloat(cpuTimes[CPIdle+offset], 32) + if err != nil { + return ret, err + } + intr, err := strconv.ParseFloat(cpuTimes[CPIntr+offset], 32) + if err != nil { + return ret, err + } + + c := CPUTimesStat{ + User: float32(user / ClocksPerSec), + Nice: float32(nice / ClocksPerSec), + System: float32(sys / ClocksPerSec), + Idle: float32(idle / ClocksPerSec), + Irq: float32(intr / ClocksPerSec), + } + if !percpu { + c.CPU = "cpu-total" + } else { + c.CPU = fmt.Sprintf("cpu%d", i) + } - ret = append(ret, c) + ret = append(ret, c) + } return ret, nil }