github.com/whatap/golib@v0.0.22/util/panicutil/SelfInfoLinux.go (about)

     1  //+build !windows
     2  
     3  package panicutil
     4  
     5  import (
     6  	"os"
     7  
     8  	"github.com/shirou/gopsutil/process"
     9  )
    10  
    11  //GetSelfCPUUsage GetSelfCPUUsage
    12  func GetSelfCPUUsage() (float32, error) {
    13  	if thisProcess == nil {
    14  		p, err := process.NewProcess(int32(os.Getpid()))
    15  		if err != nil {
    16  			return 0, err
    17  		}
    18  		thisProcess = p
    19  	}
    20  
    21  	cpuUsagePercent, err := thisProcess.CPUPercent()
    22  
    23  	return float32(cpuUsagePercent), err
    24  }