mirror of
https://github.com/datarhei/core.git
synced 2025-10-17 13:31:51 +08:00
Define system CPU resources in percent 0-100, acquiring resources check with available system resources
This commit is contained in:
@@ -40,17 +40,15 @@ type process struct {
|
||||
statCurrentTime time.Time
|
||||
statPrevious cpuTimesStat
|
||||
statPreviousTime time.Time
|
||||
|
||||
imposeLimit bool
|
||||
nTicks uint64
|
||||
}
|
||||
|
||||
func (u *util) Process(pid int32, limit bool) (Process, error) {
|
||||
func (u *util) Process(pid int32) (Process, error) {
|
||||
p := &process{
|
||||
pid: pid,
|
||||
hasCgroup: u.hasCgroup,
|
||||
cpuLimit: u.cpuLimit,
|
||||
ncpu: u.ncpu,
|
||||
imposeLimit: limit,
|
||||
pid: pid,
|
||||
hasCgroup: u.hasCgroup,
|
||||
cpuLimit: u.cpuLimit,
|
||||
ncpu: u.ncpu,
|
||||
}
|
||||
|
||||
proc, err := psprocess.NewProcess(pid)
|
||||
@@ -68,7 +66,7 @@ func (u *util) Process(pid int32, limit bool) (Process, error) {
|
||||
}
|
||||
|
||||
func NewProcess(pid int32, limit bool) (Process, error) {
|
||||
return DefaultUtil.Process(pid, limit)
|
||||
return DefaultUtil.Process(pid)
|
||||
}
|
||||
|
||||
func (p *process) tick(ctx context.Context, interval time.Duration) {
|
||||
@@ -85,6 +83,7 @@ func (p *process) tick(ctx context.Context, interval time.Duration) {
|
||||
p.lock.Lock()
|
||||
p.statPrevious, p.statCurrent = p.statCurrent, stat
|
||||
p.statPreviousTime, p.statCurrentTime = p.statCurrentTime, t
|
||||
p.nTicks++
|
||||
p.lock.Unlock()
|
||||
}
|
||||
}
|
||||
@@ -137,6 +136,19 @@ func (p *process) cpuTimes() (*cpuTimesStat, error) {
|
||||
func (p *process) CPUPercent() (*CPUInfoStat, error) {
|
||||
var diff float64
|
||||
|
||||
for {
|
||||
p.lock.RLock()
|
||||
nTicks := p.nTicks
|
||||
p.lock.RUnlock()
|
||||
|
||||
if nTicks < 2 {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
|
||||
|
Reference in New Issue
Block a user