github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/sysinfo/cpu/cpu.go (about) 1 package cpu 2 3 import ( 4 "time" 5 6 "github.com/shirou/gopsutil/cpu" 7 ) 8 9 // Useage Total总量,Idle空闲,Used使用率,Collercter总量,使用量 10 type Useage struct { 11 Total float64 `json:"total"` 12 Idle float64 `json:"idle"` 13 UsedPercent float64 `json:"percent"` 14 } 15 16 // GetInfo 获取当前系统CPU使用的情况数据 17 func GetInfo(t ...time.Duration) (useage Useage) { 18 cpus, _ := cpu.Times(true) 19 useage = Useage{} 20 for _, value := range cpus { 21 useage.Total += value.Total() 22 useage.Idle += value.Idle 23 } 24 du := time.Second 25 if len(t) > 0 { 26 du = t[0] 27 } 28 upc, _ := cpu.Percent(du, true) 29 var total float64 30 for _, v := range upc { 31 total += v 32 } 33 useage.UsedPercent = total / float64(len(upc)) 34 return 35 }