Skip to content

Commit

Permalink
Fix the CPU burn instability.
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 910dab7 commit f2afb5b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions exec/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,20 +364,19 @@ func burn(ctx context.Context, quota <-chan int64, slopePercent float64, precpu
// higher CPU load than expectedPercent.
// cpuPercent cannot be less than zero.
fmt.Println("+++++++++++", cpuPercent)
if cpuPercent == 0 || cpuPercent > slopePercent {
if cpuPercent > slopePercent {
totalCpuPercent, err := cpu.Percent(0, true)
if err != nil {
log.Fatalf(ctx, "get cpu usage fail, %s", err.Error())
continue
}
if totalCpuPercent[cpuIndex] >= slopePercent {
fmt.Println("current CPU load is higher than slopePercent.")
// 正常情况也可能跑到这里导致CPU频率下降
fmt.Println("current CPU load is higher than slopePercent.", q, ds, cpuPercent)
log.Debugf(ctx, "current CPU load is higher than slopePercent.")
// When the specified CPU frequency is greater than the expected CPU
// frequency of chaos_os, we expect the behavior to be that chaos_os
// does not occupy the CPU.
time.Sleep(time.Second)
cpu.Percent(time.Second, true)
continue
}
other := totalCpuPercent[cpuIndex] - beforeCpuPercent
Expand All @@ -394,6 +393,7 @@ func burn(ctx context.Context, quota <-chan int64, slopePercent float64, precpu
fmt.Println("xiufu: ", q, ds, cpuPercent, slopePercent, totalCpuPercent[cpuIndex])
}
fmt.Println("------------", q, ds, cpuPercent, float64(q)/float64(q+ds)*100)
// 当cpuPercent为零的时候stress_cpu会跑的很快
stress_cpu(time.Duration(q+ds), cpuPercent)
beforeCpuPercent = cpuPercent
}
Expand Down Expand Up @@ -579,6 +579,10 @@ func stress_cpu_factorial(name string) {
// Make a single CPU load rate reach cpuPercent% within the time interval.
// This function can also be used to implement something similar to stress-ng --cpu-load.
func stress_cpu(interval time.Duration, cpuPercent float64) {
if cpuPercent == 0 {
time.Sleep(time.Duration(interval)*time.Nanosecond)
return
}
bias := 0.0
startTime := time.Now().UnixNano()
nanoInterval := int64(interval/time.Nanosecond)
Expand Down

0 comments on commit f2afb5b

Please sign in to comment.