Skip to content

Commit

Permalink
Makes fullload stable.
Browse files Browse the repository at this point in the history
Signed-off-by: Super-long <[email protected]>
  • Loading branch information
Super-long committed May 24, 2022
1 parent f4204a4 commit a3a673a
Showing 1 changed file with 25 additions and 42 deletions.
67 changes: 25 additions & 42 deletions exec/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,54 +331,38 @@ func getQuota(ctx context.Context, slopePercent float64, precpu bool, cpuIndex i
log.Debugf(ctx, "cpu usage: %f , precpu: %v, cpuIndex %d", used, precpu, cpuIndex)
dx := (slopePercent - used) / 100
busy := int64(dx * float64(period))
fmt.Println("((((((((((((", used, dx, busy, cpuIndex)
//fmt.Println("((((((((((((", used, dx, busy, cpuIndex)
return busy
}

// The root cause of the complexity is that getUsed requires sleep.
func burn(ctx context.Context, quota <-chan int64, slopePercent float64, precpu bool, cpuIndex int) {
q := getQuota(ctx, slopePercent, precpu, cpuIndex)
ds := period - q
if ds < 0 {
ds = 0
}
fmt.Println(q, ds, slopePercent)
var beforeCpuPercent float64 = 0 // 上一个循环内的CPU百分比
// 用于更新内部时间,不然下面循环第一次Percent会得到0
cpu.Percent(time.Second, true)
for {
select {
case offset := <-quota:
q = q + offset
if q < 0 {
q = 0
}
ds = period - q
fmt.Println("////////////", q, ds, offset)
if ds < 0 {
ds = 0
}
default:
// Execute (100*q/(q+s))% of calculations in one second.
cpuPercent := float64(q)/float64(q+ds)*100
// It is possible that quota gets two values when stress_cpu is not
// started, so that the value of q is always greater than slopePercent.
if cpuPercent <= 0 || cpuPercent > slopePercent {
totalCpuPercent, err := cpu.Percent(0, true)
if err != nil {
log.Fatalf(ctx, "get cpu usage fail, %s", err.Error())
}
if totalCpuPercent[cpuIndex] >= slopePercent {
fmt.Println("bigger than slopePercent")
continue
}
cpuPercent = slopePercent - totalCpuPercent[cpuIndex]
q = int64(cpuPercent/float64(100)*float64(period))
ds = period - q

fmt.Println("xiufu: ", q, ds, cpuPercent, totalCpuPercent[cpuIndex])
}
fmt.Println("------------", q, ds, cpuPercent, float64(q)/float64(q+ds)*100)
stress_cpu(time.Duration(q+ds), cpuPercent)
//runtime.Gosched()
totalCpuPercent, err := cpu.Percent(0, true)
fmt.Println(totalCpuPercent)
if err != nil {
log.Fatalf(ctx, "get cpu usage fail, %s", err.Error())
runtime.Gosched()
continue
}
other := totalCpuPercent[cpuIndex] - beforeCpuPercent
fmt.Printf("========%f, %f\n", totalCpuPercent[cpuIndex], beforeCpuPercent)
if other < 0 {
other = 0
}
fmt.Printf("other : %f\n", other)
// other可能大于slopePercent
cpuPercent := slopePercent - other
if cpuPercent < 0 {
cpuPercent = 0
}
beforeCpuPercent = cpuPercent
fmt.Println("------------", other, cpuPercent)
stress_cpu(time.Second, cpuPercent)

}
}

Expand Down Expand Up @@ -564,7 +548,6 @@ func stress_cpu(interval time.Duration, cpuPercent float64) {
bias := 0.0
startTime := time.Now().UnixNano()
nanoInterval := int64(interval/time.Nanosecond)
fmt.Printf("============ nanoInterval[%d]\n", nanoInterval)
for {
if time.Now().UnixNano() - startTime > nanoInterval {
break
Expand Down

0 comments on commit a3a673a

Please sign in to comment.