diff --git a/exec/cpu/cpu.go b/exec/cpu/cpu.go index ebbe96a..4232443 100644 --- a/exec/cpu/cpu.go +++ b/exec/cpu/cpu.go @@ -331,54 +331,49 @@ 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百分比 + 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 + } + // fz138 + // true: beforeCpuPercent + other = totalCpuPercent[cpuIndex] + // other = otalCpuPercent[cpuIndex] - beforeCpuPercent + // if other < 0 { + // other = 0 + // } + // + // cpuPercent = slopePercent - other + // + // cpuPercent = math.min(cpuPercent, slopePercent) + + // fmt.Println(totalCpuPercent) + 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 +559,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