Define system CPU resources in percent 0-100, acquiring resources check with available system resources

This commit is contained in:
Ingo Oppermann
2023-04-28 10:27:33 +02:00
parent 7527f7833b
commit 74f98e8c7e
6 changed files with 82 additions and 107 deletions

View File

@@ -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()