github.com/songzhibin97/gkit@v1.2.13/sys/cpu/psutilcpu.go (about)

     1  package cpu
     2  
     3  import (
     4  	"time"
     5  
     6  	c "github.com/shirou/gopsutil/cpu"
     7  )
     8  
     9  type psutilCPU struct {
    10  	interval time.Duration
    11  }
    12  
    13  func newPsutilCPU(interval time.Duration) (cpu *psutilCPU, err error) {
    14  	cpu = &psutilCPU{interval: interval}
    15  	_, err = cpu.Usage()
    16  	if err != nil {
    17  		return
    18  	}
    19  	return
    20  }
    21  
    22  func (ps *psutilCPU) Usage() (u uint64, err error) {
    23  	var percents []float64
    24  	percents, err = c.Percent(ps.interval, false)
    25  	if err == nil {
    26  		u = uint64(percents[0] * 10)
    27  	}
    28  	return
    29  }
    30  
    31  func (ps *psutilCPU) Info() (info Info) {
    32  	stats, err := c.Info()
    33  	if err != nil {
    34  		return
    35  	}
    36  	cores, err := c.Counts(true)
    37  	if err != nil {
    38  		return
    39  	}
    40  	info = Info{
    41  		Frequency: uint64(stats[0].Mhz),
    42  		Quota:     float64(cores),
    43  	}
    44  	return
    45  }