Skip to contentSkip to conversation

cpus: reset throttle_thread_scheduled after sleep

PATCH In review Felipe Franciosi 5 participants 10 messages May 19, 2017 – Jun 7, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpus: reset throttle_thread_scheduled after sleep

Currently, the throttle_thread_scheduled flag is reset back to 0 before sleeping (as part of the throttling logic). Given that throttle_timer (well, any timer) may tick with a slight delay, it so happens that under heavy throttling (ie. close or on CPU_THROTTLE_PCT_MAX) the tick may schedule a further cpu_throttle_thread() work item after the flag reset, but before the previous sleep completed. This results on the vCPU thread sleeping continuously for potentially several seconds in a row.

The chances of that happening can be drastically minimised by resetting the flag after the sleep.

---
 cpus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +1 -1
index 516e5cb..f42eebd 100644
@@ -677,9 +677,9 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
     sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
 
     qemu_mutex_unlock_iothread();
-    atomic_set(&cpu->throttle_thread_scheduled, 0);
     g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
     qemu_mutex_lock_iothread();
+    atomic_set(&cpu->throttle_thread_scheduled, 0);
 }
 
 static void cpu_throttle_timer_tick(void *opaque)
Signature · 1 line
1.9.5
Acked-by on v1 1/1
#1 lore raw
Quoted 23 lines · Felipe Franciosi

Currently, the throttle_thread_scheduled flag is reset back to 0 before sleeping (as part of the throttling logic). Given that throttle_timer (well, any timer) may tick with a slight delay, it so happens that under heavy throttling (ie. close or on CPU_THROTTLE_PCT_MAX) the tick may schedule a further cpu_throttle_thread() work item after the flag reset, but before the previous sleep completed. This results on the vCPU thread sleeping continuously for potentially several seconds in a row.

The chances of that happening can be drastically minimised by resetting the flag after the sleep.

---
 cpus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +0 -0
index 516e5cb..f42eebd 100644

cpus.c static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) · line 685

Quoted 10 lines
cpus.c +1 -1
@@ -677,9 +677,9 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
     sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
 
     qemu_mutex_unlock_iothread();
-    atomic_set(&cpu->throttle_thread_scheduled, 0);
     g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
     qemu_mutex_lock_iothread();
+    atomic_set(&cpu->throttle_thread_scheduled, 0);
 }
 
 static void cpu_throttle_timer_tick(void *opaque)

This seems to make sense to me.

I'm CC'ing Juan, Amit and David as they are all active in the migration area and may have opinions on this. Juan and David were also reviewers for the original series.

Signature · 1 line
-- Jason J. Herne ([email protected])
#2 lore raw
Quoted 37 lines · * Jason J. Herne ([email protected]) wrote:
Quoted 23 lines · Felipe Franciosi

Currently, the throttle_thread_scheduled flag is reset back to 0 before sleeping (as part of the throttling logic). Given that throttle_timer (well, any timer) may tick with a slight delay, it so happens that under heavy throttling (ie. close or on CPU_THROTTLE_PCT_MAX) the tick may schedule a further cpu_throttle_thread() work item after the flag reset, but before the previous sleep completed. This results on the vCPU thread sleeping continuously for potentially several seconds in a row.

The chances of that happening can be drastically minimised by resetting the flag after the sleep.

---
 cpus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +0 -0
index 516e5cb..f42eebd 100644

cpus.c static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) · line 685

Quoted 10 lines
cpus.c +1 -1
@@ -677,9 +677,9 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
     sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
 
     qemu_mutex_unlock_iothread();
-    atomic_set(&cpu->throttle_thread_scheduled, 0);
     g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
     qemu_mutex_lock_iothread();
+    atomic_set(&cpu->throttle_thread_scheduled, 0);
 }
 
 static void cpu_throttle_timer_tick(void *opaque)

This seems to make sense to me.

Quoted 6 lines

I'm CC'ing Juan, Amit and David as they are all active in the migration area and may have
opinions on this. Juan and David were also reviewers for the original series.

The description is interesting and sounds reasonable; it'll be interesting to see what difference it makes to the autoconverge behaviour for those workloads that need this level of throttle.

Dave

Quoted 3 lines
Signature · 1 line
-- Jason J. Herne ([email protected])

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#3 lore raw
Quoted 43 lines · Dr. David Alan Gilbert
Quoted 39 lines · * Jason J. Herne ([email protected]) wrote:
Quoted 23 lines · Felipe Franciosi

Currently, the throttle_thread_scheduled flag is reset back to 0 before sleeping (as part of the throttling logic). Given that throttle_timer (well, any timer) may tick with a slight delay, it so happens that under heavy throttling (ie. close or on CPU_THROTTLE_PCT_MAX) the tick may schedule a further cpu_throttle_thread() work item after the flag reset, but before the previous sleep completed. This results on the vCPU thread sleeping continuously for potentially several seconds in a row.

The chances of that happening can be drastically minimised by resetting the flag after the sleep.

---
cpus.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +0 -0
index 516e5cb..f42eebd 100644

cpus.c static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) · line 685

Quoted 10 lines
cpus.c +1 -1
@@ -677,9 +677,9 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
    sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
 
    qemu_mutex_unlock_iothread();
-    atomic_set(&cpu->throttle_thread_scheduled, 0);
    g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
    qemu_mutex_lock_iothread();
+    atomic_set(&cpu->throttle_thread_scheduled, 0);
 }
 
 static void cpu_throttle_timer_tick(void *opaque)

This seems to make sense to me.

Quoted 8 lines
Quoted 4 lines

I'm CC'ing Juan, Amit and David as they are all active in the migration area and may have
opinions on this. Juan and David were also reviewers for the original series.

The description is interesting and sounds reasonable; it'll be interesting to see what difference it makes to the autoconverge behaviour for those workloads that need this level of throttle.

To get some hard data, we wrote a little application that:
1) spawns multiple threads (one per vCPU)
2) each thread mmap()s+mlock()s a certain workset (eg. 30GB/#threads for a 32GB VM)
3) each thread writes a word to the beginning of every page in a tight loop
4) the parent thread periodically reports the number of dirtied pages

Even on a dedicated 10G link, that is pretty much guaranteed to require 99% throttle to converge.

Before the patch, Qemu migrates the VM (depicted above) fairly quickly (~40s) after reaching 99% throttle. The application reported a few seconds at a time with lockups which we initially thought was just that thread not running between Qemu-induced vCPU sleeps (and later attributed it to the reported bug).

Then we used a 1G link. This time, the migration had to run for a lot longer even at 99%. That made the bug more likely to happen and we observed soft lockups (reported by the guest's kernel on the console) of 70+ seconds.

Using the patch, and back on a 10G link, the migration completes after a few more iterations than before (took just under 2mins after reaching 99%). If you want further validation of the bug, instrumenting cpus-common.c:process_queued_cpu_work() could be done to show that cpu_throttle_thread() is running back-to-back under these cases.

In summary we believe this patch is immediately required to prevent the lockups. A more elaborate throttling solution should be considered as future work. Perhaps a per-vCPU timer which throttles more precisely or a new convergence design altogether.

Thanks,
Felipe

Quoted 8 lines

Dave

Quoted 3 lines
Signature · 1 line
-- Jason J. Herne ([email protected])

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#4 lore raw
Quoted 58 lines · * Felipe Franciosi ([email protected]) wrote:
Quoted 43 lines · Dr. David Alan Gilbert
Quoted 39 lines · * Jason J. Herne ([email protected]) wrote:
Quoted 23 lines · Felipe Franciosi

Currently, the throttle_thread_scheduled flag is reset back to 0 before sleeping (as part of the throttling logic). Given that throttle_timer (well, any timer) may tick with a slight delay, it so happens that under heavy throttling (ie. close or on CPU_THROTTLE_PCT_MAX) the tick may schedule a further cpu_throttle_thread() work item after the flag reset, but before the previous sleep completed. This results on the vCPU thread sleeping continuously for potentially several seconds in a row.

The chances of that happening can be drastically minimised by resetting the flag after the sleep.

---
cpus.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +0 -0
index 516e5cb..f42eebd 100644

cpus.c static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) · line 685

Quoted 10 lines
cpus.c +1 -1
@@ -677,9 +677,9 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque)
    sleeptime_ns = (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS);
 
    qemu_mutex_unlock_iothread();
-    atomic_set(&cpu->throttle_thread_scheduled, 0);
    g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */
    qemu_mutex_lock_iothread();
+    atomic_set(&cpu->throttle_thread_scheduled, 0);
 }
 
 static void cpu_throttle_timer_tick(void *opaque)

This seems to make sense to me.

Quoted 8 lines
Quoted 4 lines

I'm CC'ing Juan, Amit and David as they are all active in the migration area and may have
opinions on this. Juan and David were also reviewers for the original series.

The description is interesting and sounds reasonable; it'll be interesting to see what difference it makes to the autoconverge behaviour for those workloads that need this level of throttle.

To get some hard data, we wrote a little application that:
1) spawns multiple threads (one per vCPU)
2) each thread mmap()s+mlock()s a certain workset (eg. 30GB/#threads for a 32GB VM)
3) each thread writes a word to the beginning of every page in a tight loop
4) the parent thread periodically reports the number of dirtied pages

Quoted 7 lines

Even on a dedicated 10G link, that is pretty much guaranteed to require 99% throttle to converge.

Before the patch, Qemu migrates the VM (depicted above) fairly quickly (~40s) after reaching 99% throttle. The application reported a few seconds at a time with lockups which we initially thought was just that thread not running between Qemu-induced vCPU sleeps (and later attributed it to the reported bug).

Then we used a 1G link. This time, the migration had to run for a lot longer even at 99%. That made the bug more likely to happen and we observed soft lockups (reported by the guest's kernel on the console) of 70+ seconds.

Using the patch, and back on a 10G link, the migration completes after a few more iterations than before (took just under 2mins after reaching 99%). If you want further validation of the bug, instrumenting cpus-common.c:process_queued_cpu_work() could be done to show that cpu_throttle_thread() is running back-to-back under these cases.

OK, that's reasonable.

Quoted 1 line

In summary we believe this patch is immediately required to prevent the lockups.

Yes, agreed.

Quoted 1 line

A more elaborate throttling solution should be considered as future work. Perhaps a per-vCPU timer which throttles more precisely or a new convergence design altogether.

Dave

Quoted 13 lines

Thanks,
Felipe

Quoted 8 lines

Dave

Quoted 3 lines
Signature · 1 line
-- Jason J. Herne ([email protected])

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#5 lore raw
In reply to Felipe Franciosi
Quoted 10 lines · Felipe Franciosi

Currently, the throttle_thread_scheduled flag is reset back to 0 before sleeping (as part of the throttling logic). Given that throttle_timer (well, any timer) may tick with a slight delay, it so happens that under heavy throttling (ie. close or on CPU_THROTTLE_PCT_MAX) the tick may schedule a further cpu_throttle_thread() work item after the flag reset, but before the previous sleep completed. This results on the vCPU thread sleeping continuously for potentially several seconds in a row.

The chances of that happening can be drastically minimised by resetting the flag after the sleep.

True, on the other hand this may also increase the chance of not sleeping at all.

How overcommitted was the host system?

Paolo

Quoted 2 lines
#6 lore raw
Quoted 14 lines · Paolo Bonzini
Quoted 8 lines · Felipe Franciosi

Currently, the throttle_thread_scheduled flag is reset back to 0 before sleeping (as part of the throttling logic). Given that throttle_timer (well, any timer) may tick with a slight delay, it so happens that under heavy throttling (ie. close or on CPU_THROTTLE_PCT_MAX) the tick may schedule a further cpu_throttle_thread() work item after the flag reset, but before the previous sleep completed. This results on the vCPU thread sleeping continuously for potentially several seconds in a row.

Quoted 5 lines
Quoted 2 lines

The chances of that happening can be drastically minimised by resetting the flag after the sleep.

True, on the other hand this may also increase the chance of not sleeping at all.

The perfect solution (for this throttling strategy) would probably be a per-cpu timer. In the meantime, I think avoiding massive sleeps is a win. We observed stalls in excess of 70 secs at 99% throttle.

Quoted 1 line

How overcommitted was the host system?

Not overcommitted at all. And it's quite easy to reproduce. All you need is a workload heavy enough to prevent the migration from converging (or a slow network which you can emulate with a qdisc).

With a Linux guest, you should quickly see soft lockups being reported. With Windows, probably BSODs.

Thanks,
Felipe

Quoted 5 lines

Paolo

Quoted 2 lines
#7 lore raw
Quoted 3 lines · Felipe Franciosi

The perfect solution (for this throttling strategy) would probably be a per-cpu timer. In the meantime, I think avoiding massive sleeps is a win. We observed stalls in excess of 70 secs at 99% throttle.

Ah, so the issue is not overcommit, it's too high throttling. Then it makes sense.

Thanks,

Paolo

Quoted 7 lines
Quoted 1 line

How overcommitted was the host system?

Not overcommitted at all. And it's quite easy to reproduce. All you need is a workload heavy enough to prevent the migration from converging (or a slow network which you can emulate with a qdisc).

With a Linux guest, you should quickly see soft lockups being reported. With Windows, probably BSODs.

Reviewed-by on v1 1/1
#8 lore raw
In reply to Felipe Franciosi
Quoted 8 lines · Felipe Franciosi

Currently, the throttle_thread_scheduled flag is reset back to 0 before sleeping (as part of the throttling logic). Given that throttle_timer (well, any timer) may tick with a slight delay, it so happens that under heavy throttling (ie. close or on CPU_THROTTLE_PCT_MAX) the tick may schedule a further cpu_throttle_thread() work item after the flag reset, but before the previous sleep completed. This results on the vCPU thread sleeping continuously for potentially several seconds in a row.

Quoted 5 lines

The chances of that happening can be drastically minimised by resetting the flag after the sleep.

Paolo, I think that the analisys makes sense.

Should you pull this patch, or do you want me to pull it?

Thanks, Juan.

#9 lore raw
Quoted 12 lines · Juan Quintela
Quoted 11 lines · Felipe Franciosi

Currently, the throttle_thread_scheduled flag is reset back to 0 before sleeping (as part of the throttling logic). Given that throttle_timer (well, any timer) may tick with a slight delay, it so happens that under heavy throttling (ie. close or on CPU_THROTTLE_PCT_MAX) the tick may schedule a further cpu_throttle_thread() work item after the flag reset, but before the previous sleep completed. This results on the vCPU thread sleeping continuously for potentially several seconds in a row.

The chances of that happening can be drastically minimised by resetting the flag after the sleep.

Quoted 8 lines
Quoted 2 lines

Paolo, I think that the analisys makes sense.

Should you pull this patch, or do you want me to pull it?

I've already included in my jinxed (now at v6) pull request.

Paolo