From a3a673a2f591a40b6315a664017cffb2eeebbb3e Mon Sep 17 00:00:00 2001 From: Super-long <0x4f4f4f4f@gmail.com> Date: Tue, 24 May 2022 16:23:10 +0800 Subject: [PATCH] Makes fullload stable. Signed-off-by: Super-long <0x4f4f4f4f@gmail.com> --- exec/cpu/cpu.go | 67 ++++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 42 deletions(-) diff --git a/exec/cpu/cpu.go b/exec/cpu/cpu.go index ebbe96a..47c0214 100644 --- a/exec/cpu/cpu.go +++ b/exec/cpu/cpu.go @@ -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) + } } @@ -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