My Go program needs to know the current cpu usage percentage of all system and user processes.
How can I obtain that?
Here is an OS independent solution using Cgo to harness the clock() function provided by C standard library:
//#include
import "C"
import "time"
var startTime = time.Now()
var startTicks = C.clock()
func CpuUsagePercent() float64 {
clockSeconds := float64(C.clock()-startTicks) / float64(C.CLOCKS_PER_SEC)
realSeconds := time.Since(startTime).Seconds()
return clockSeconds / realSeconds * 100
}