Skip to contentSkip to conversation

tcg: fix icount super slowdown

PATCH In review Paolo Bonzini 4 participants 26 messages Mar 3, 2017 – Mar 14, 2017

lore
Share & tools
lore
#0 lore raw

cover tcg: fix icount super slowdown

icount has become much slower after tcg_cpu_exec has stopped using the BQL. There is also a latent bug that is masked by the slowness.

The slowness happens because every occurrence of a QEMU_CLOCK_VIRTUAL timer now has to wake up the I/O thread and wait for it. The rendez-vous is mediated by the BQL QemuMutex:

- handle_icount_deadline wakes up the I/O thread with BQL taken
- the I/O thread wakes up and waits on the BQL
- the VCPU thread releases the BQL a little later
- the I/O thread raises an interrupt, which calls qemu_cpu_kick
- the VCPU thread notices the interrupt, takes the BQL to process it and waits on it

All this back and forth is extremely expensive, causing a 6 to 8-fold slowdown when icount is turned on.

One may think that the issue is that the VCPU thread is too dependent on the BQL, but then the latent bug comes in. I first tried removing the BQL completely from the x86 cpu_exec. Every guest thern hung, and the only way to fix it (and make everything slow again) was to add a dummy BQL lock/unlock pair to qemu_tcg_wait_io_event.

This is because in -icount mode you really have to process the events before the CPU restarts executing the next instruction. Therefore, this series moves the processing of QEMU_CLOCK_VIRTUAL timers straight in the vCPU thread when running in icount mode. This is only limited to the main TimerListGroup. QEMU_CLOCK_VIRTUAL timers in AioContexts still run outside the vCPU thread.

With this change, icount mode is pretty much running as fast as in 2.8. I tested the patches are on top of Alex's series with both x86 and aarch64 guests, but they should be pretty much independent.

The good thing is that the infrastructure to do this is basically already there, in the form of QEMUTimerListNotifyCB. It only needs to be generalized a bit (patches 2 and 3) and bugfixed (patch 1 and 4---the latter is necessary to avoid the "I/O thread spun for 1000 iterations and consequent slowing down of vCPU thread).

The bad things are:

- I am not sure of what was different before the patch that removed the BQL from tcg_cpu_exec (and I don't really have time to profile it right now---I should not be fixing this in fact...).

- the solution sounds a bit ugly and it probably is---though the patch itself is pretty small, adding only about 30 lines of new code.

Paolo

Paolo Bonzini (5):
qemu-timer: fix off-by-one
qemu-timer: do not include sysemu/cpus.h from util/qemu-timer.h
cpus: define QEMUTimerListNotifyCB for QEMU system emulation
main-loop: remove now unnecessary optimization
icount: process QEMU_CLOCK_VIRTUAL timers in vCPU thread

 cpu-exec.c                   |  1 +
 cpus.c                       | 29 +++++++++++++++++++++++++++--
 hw/core/ptimer.c             |  1 +
 include/qemu/timer.h         | 29 ++++++++++++++++++++++++++---
 include/sysemu/cpus.h        |  3 +++
 kvm-all.c                    |  1 +
 monitor.c                    |  1 +
 replay/replay.c              |  1 +
 stubs/cpu-get-icount.c       |  6 ++++++
 tests/test-aio-multithread.c |  2 +-
 tests/test-aio.c             |  2 +-
 translate-all.c              |  1 +
 util/async.c                 |  2 +-
 util/main-loop.c             |  3 ++-
 util/qemu-timer.c            | 17 ++++++++++-------
 vl.c                         |  5 +----
 16 files changed, 84 insertions(+), 20 deletions(-)
#1 lore raw

PATCH 1/5 qemu-timer: fix off-by-one

If the first timer is exactly at the current value of the clock, the deadline is met and the timer should fire. This fixes itself without icount, but with icount execution of instructions will stop exactly at the deadline.

---
 util/qemu-timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/qemu-timer.c +1 -1
index 6cf70b9..2f20151 100644
@@ -199,7 +199,7 @@ bool timerlist_expired(QEMUTimerList *timer_list)
     expire_time = timer_list->active_timers->expire_time;
     qemu_mutex_unlock(&timer_list->active_timers_lock);
 
-    return expire_time < qemu_clock_get_ns(timer_list->clock->type);
+    return expire_time <= qemu_clock_get_ns(timer_list->clock->type);
 }
 
 bool qemu_clock_expired(QEMUClockType type)
Signature · 1 line
2.9.3
Reviewed-by on v1 1/5
#2 lore raw

Re: [Qemu-devel] [PATCH 1/5] qemu-timer: fix off-by-one

Quoted 5 lines · Paolo Bonzini

If the first timer is exactly at the current value of the clock, the deadline is met and the timer should fire. This fixes itself without icount, but with icount execution of instructions will stop exactly at the deadline.

Quoted 22 lines
---
 util/qemu-timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/qemu-timer.c +1 -1
index 6cf70b9..2f20151 100644
@@ -199,7 +199,7 @@ bool timerlist_expired(QEMUTimerList *timer_list)
     expire_time = timer_list->active_timers->expire_time;
     qemu_mutex_unlock(&timer_list->active_timers_lock);
 
-    return expire_time < qemu_clock_get_ns(timer_list->clock->type);
+    return expire_time <= qemu_clock_get_ns(timer_list->clock->type);
 }
 
 bool qemu_clock_expired(QEMUClockType type)
Signature · 1 line
2.9.3
Reviewed-by on v1 1/5
#3 lore raw

Re: [Qemu-devel] [PATCH 1/5] qemu-timer: fix off-by-one

In reply to Paolo Bonzini
Quoted 5 lines · Paolo Bonzini

If the first timer is exactly at the current value of the clock, the deadline is met and the timer should fire. This fixes itself without icount, but with icount execution of instructions will stop exactly at the deadline.

Quoted 9 lines
---
 util/qemu-timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/qemu-timer.c +0 -0
index 6cf70b9..2f20151 100644

util/qemu-timer.c bool timerlist_expired(QEMUTimerList *timer_list) · line 205

Quoted 8 lines
util/qemu-timer.c +1 -1
@@ -199,7 +199,7 @@ bool timerlist_expired(QEMUTimerList *timer_list)
     expire_time = timer_list->active_timers->expire_time;
     qemu_mutex_unlock(&timer_list->active_timers_lock);
 
-    return expire_time < qemu_clock_get_ns(timer_list->clock->type);
+    return expire_time <= qemu_clock_get_ns(timer_list->clock->type);
 }
 
 bool qemu_clock_expired(QEMUClockType type)

--
Alex Bennée

#4 lore raw

PATCH 2/5 qemu-timer: do not include sysemu/cpus.h from util/qemu-timer.h

In reply to Paolo Bonzini

This dependency is the wrong way, and we will need util/qemu-timer.h from sysemu/cpus.h in the next patch.

---
 cpu-exec.c            | 1 +
 hw/core/ptimer.c      | 1 +
 include/qemu/timer.h  | 1 -
 include/sysemu/cpus.h | 2 ++
 kvm-all.c             | 1 +
 monitor.c             | 1 +
 replay/replay.c       | 1 +
 translate-all.c       | 1 +
 util/main-loop.c      | 1 +
 util/qemu-timer.c     | 1 +
 10 files changed, 10 insertions(+), 1 deletion(-)
cpu-exec.c +1 -0
index 1a5ad48..6dbb4da 100644
@@ -33,6 +33,7 @@
 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
 #include "hw/i386/apic.h"
 #endif
+#include "sysemu/cpus.h"
 #include "sysemu/replay.h"
 
 /* -icount align implementation. */
hw/core/ptimer.c +1 -0
index 59ccb00..7221c68 100644
@@ -13,6 +13,7 @@
 #include "sysemu/replay.h"
 #include "sysemu/qtest.h"
 #include "block/aio.h"
+#include "sysemu/cpus.h"
 
 #define DELTA_ADJUST     1
 #define DELTA_NO_ADJUST -1
include/qemu/timer.h +0 -1
index 26e6285..91cd8c8 100644
@@ -4,7 +4,6 @@
 #include "qemu-common.h"
 #include "qemu/notify.h"
 #include "qemu/host-utils.h"
-#include "sysemu/cpus.h"
 
 #define NANOSECONDS_PER_SECOND 1000000000LL
 
include/sysemu/cpus.h +2 -0
index a73b5d4..e521a91 100644
@@ -1,6 +1,8 @@
 #ifndef QEMU_CPUS_H
 #define QEMU_CPUS_H
 
+#include "qemu/timer.h"
+
 /* cpus.c */
 bool qemu_in_vcpu_thread(void);
 void qemu_init_cpu_loop(void);
kvm-all.c +1 -0
index 0c94637..0fd6655 100644
@@ -29,6 +29,7 @@
 #include "hw/s390x/adapter.h"
 #include "exec/gdbstub.h"
 #include "sysemu/kvm_int.h"
+#include "sysemu/cpus.h"
 #include "qemu/bswap.h"
 #include "exec/memory.h"
 #include "exec/ram_addr.h"
monitor.c +1 -0
index b68944d..d8fba78 100644
@@ -77,6 +77,7 @@
 #include "qapi-event.h"
 #include "qmp-introspect.h"
 #include "sysemu/qtest.h"
+#include "sysemu/cpus.h"
 #include "qemu/cutils.h"
 #include "qapi/qmp/dispatch.h"
 
replay/replay.c +1 -0
index 1835b99..78e2a7e 100644
@@ -16,6 +16,7 @@
 #include "replay-internal.h"
 #include "qemu/timer.h"
 #include "qemu/main-loop.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "qemu/error-report.h"
 
translate-all.c +1 -0
index 956d54b..63b8f56 100644
@@ -57,6 +57,7 @@
 #include "qemu/timer.h"
 #include "qemu/main-loop.h"
 #include "exec/log.h"
+#include "sysemu/cpus.h"
 
 /* #define DEBUG_TB_INVALIDATE */
 /* #define DEBUG_TB_FLUSH */
util/main-loop.c +1 -0
index ad10bca..0b2d8c0 100644
@@ -28,6 +28,7 @@
 #include "qemu/timer.h"
 #include "qemu/sockets.h"	// struct in_addr needed for libslirp.h
 #include "sysemu/qtest.h"
+#include "sysemu/cpus.h"
 #include "slirp/libslirp.h"
 #include "qemu/main-loop.h"
 #include "block/aio.h"
util/qemu-timer.c +1 -0
index 2f20151..ac99340 100644
@@ -27,6 +27,7 @@
 #include "qemu/timer.h"
 #include "sysemu/replay.h"
 #include "sysemu/sysemu.h"
+#include "sysemu/cpus.h"
 
 #ifdef CONFIG_POSIX
 #include <pthread.h>
Signature · 1 line
2.9.3
Reviewed-by on v1 2/5
#5 lore raw

Re: [Qemu-devel] [PATCH 2/5] qemu-timer: do not include sysemu/cpus.h from util/qemu-timer.h

Quoted 4 lines · Paolo Bonzini

This dependency is the wrong way, and we will need util/qemu-timer.h from sysemu/cpus.h in the next patch.

Quoted 139 lines
---
 cpu-exec.c            | 1 +
 hw/core/ptimer.c      | 1 +
 include/qemu/timer.h  | 1 -
 include/sysemu/cpus.h | 2 ++
 kvm-all.c             | 1 +
 monitor.c             | 1 +
 replay/replay.c       | 1 +
 translate-all.c       | 1 +
 util/main-loop.c      | 1 +
 util/qemu-timer.c     | 1 +
 10 files changed, 10 insertions(+), 1 deletion(-)
cpu-exec.c +1 -0
index 1a5ad48..6dbb4da 100644
@@ -33,6 +33,7 @@
 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
 #include "hw/i386/apic.h"
 #endif
+#include "sysemu/cpus.h"
 #include "sysemu/replay.h"
 
 /* -icount align implementation. */
hw/core/ptimer.c +1 -0
index 59ccb00..7221c68 100644
@@ -13,6 +13,7 @@
 #include "sysemu/replay.h"
 #include "sysemu/qtest.h"
 #include "block/aio.h"
+#include "sysemu/cpus.h"
 
 #define DELTA_ADJUST     1
 #define DELTA_NO_ADJUST -1
include/qemu/timer.h +0 -1
index 26e6285..91cd8c8 100644
@@ -4,7 +4,6 @@
 #include "qemu-common.h"
 #include "qemu/notify.h"
 #include "qemu/host-utils.h"
-#include "sysemu/cpus.h"
 
 #define NANOSECONDS_PER_SECOND 1000000000LL
 
include/sysemu/cpus.h +2 -0
index a73b5d4..e521a91 100644
@@ -1,6 +1,8 @@
 #ifndef QEMU_CPUS_H
 #define QEMU_CPUS_H
 
+#include "qemu/timer.h"
+
 /* cpus.c */
 bool qemu_in_vcpu_thread(void);
 void qemu_init_cpu_loop(void);
kvm-all.c +1 -0
index 0c94637..0fd6655 100644
@@ -29,6 +29,7 @@
 #include "hw/s390x/adapter.h"
 #include "exec/gdbstub.h"
 #include "sysemu/kvm_int.h"
+#include "sysemu/cpus.h"
 #include "qemu/bswap.h"
 #include "exec/memory.h"
 #include "exec/ram_addr.h"
monitor.c +1 -0
index b68944d..d8fba78 100644
@@ -77,6 +77,7 @@
 #include "qapi-event.h"
 #include "qmp-introspect.h"
 #include "sysemu/qtest.h"
+#include "sysemu/cpus.h"
 #include "qemu/cutils.h"
 #include "qapi/qmp/dispatch.h"
 
replay/replay.c +1 -0
index 1835b99..78e2a7e 100644
@@ -16,6 +16,7 @@
 #include "replay-internal.h"
 #include "qemu/timer.h"
 #include "qemu/main-loop.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "qemu/error-report.h"
 
translate-all.c +1 -0
index 956d54b..63b8f56 100644
@@ -57,6 +57,7 @@
 #include "qemu/timer.h"
 #include "qemu/main-loop.h"
 #include "exec/log.h"
+#include "sysemu/cpus.h"
 
 /* #define DEBUG_TB_INVALIDATE */
 /* #define DEBUG_TB_FLUSH */
util/main-loop.c +1 -0
index ad10bca..0b2d8c0 100644
@@ -28,6 +28,7 @@
 #include "qemu/timer.h"
 #include "qemu/sockets.h"	// struct in_addr needed for libslirp.h
 #include "sysemu/qtest.h"
+#include "sysemu/cpus.h"
 #include "slirp/libslirp.h"
 #include "qemu/main-loop.h"
 #include "block/aio.h"
util/qemu-timer.c +1 -0
index 2f20151..ac99340 100644
@@ -27,6 +27,7 @@
 #include "qemu/timer.h"
 #include "sysemu/replay.h"
 #include "sysemu/sysemu.h"
+#include "sysemu/cpus.h"
 
 #ifdef CONFIG_POSIX
 #include <pthread.h>
Signature · 1 line
2.9.3
#6 lore raw

Re: [Qemu-devel] [PATCH 2/5] qemu-timer: do not include sysemu/cpus.h from util/qemu-timer.h

In reply to Paolo Bonzini
Quoted 2 lines · Paolo Bonzini

This dependency is the wrong way, and we will need util/qemu-timer.h from sysemu/cpus.h in the next patch.

This shuffling breaks a couple of targets:

make -k -j9
CHK version_gen.h
CC alpha-softmmu/target/alpha/translate.o
/home/alex/lsrc/qemu/qemu.git/target/alpha/translate.c: In function ‘gen_mfpr’:
/home/alex/lsrc/qemu/qemu.git/target/alpha/translate.c:1321:13: error: ‘use_icount’ undeclared (first use in this function)

         if (use_icount) {
             ^

/home/alex/lsrc/qemu/qemu.git/target/alpha/translate.c:1321:13: note: each undeclared identifier is reported only once for each function it appears in /home/alex/lsrc/qemu/qemu.git/rules.mak:69: recipe for target 'target/alpha/translate.o' failed
make[1]: *** [target/alpha/translate.o] Error 1
make[1]: Target 'all' not remade because of errors.
Makefile:322: recipe for target 'subdir-alpha-softmmu' failed
make: *** [subdir-alpha-softmmu] Error 2
CC ppc64-softmmu/hw/ppc/pnv.o
/home/alex/lsrc/qemu/qemu.git/hw/ppc/pnv.c: In function ‘ppc_powernv_init’:
/home/alex/lsrc/qemu/qemu.git/hw/ppc/pnv.c:446:39: error: ‘smp_cores’ undeclared (first use in this function)

         object_property_set_int(chip, smp_cores, "nr-cores", &error_fatal);
                                       ^

/home/alex/lsrc/qemu/qemu.git/hw/ppc/pnv.c:446:39: note: each undeclared identifier is reported only once for each function it appears in /home/alex/lsrc/qemu/qemu.git/hw/ppc/pnv.c: In function ‘pnv_chip_realize’:
/home/alex/lsrc/qemu/qemu.git/hw/ppc/pnv.c:686:57: error: ‘smp_threads’ undeclared (first use in this function)
object_property_set_int(OBJECT(pnv_core), smp_threads, "nr-threads",
^
/home/alex/lsrc/qemu/qemu.git/rules.mak:69: recipe for target 'hw/ppc/pnv.o' failed
make[1]: *** [hw/ppc/pnv.o] Error 1
make[1]: Target 'all' not remade because of errors.
Makefile:322: recipe for target 'subdir-ppc64-softmmu' failed
make: *** [subdir-ppc64-softmmu] Error 2
make: Target 'all' not remade because of errors.

Compilation exited abnormally with code 2 at Fri Mar 3 14:49:30

Quoted 129 lines
---
 cpu-exec.c            | 1 +
 hw/core/ptimer.c      | 1 +
 include/qemu/timer.h  | 1 -
 include/sysemu/cpus.h | 2 ++
 kvm-all.c             | 1 +
 monitor.c             | 1 +
 replay/replay.c       | 1 +
 translate-all.c       | 1 +
 util/main-loop.c      | 1 +
 util/qemu-timer.c     | 1 +
 10 files changed, 10 insertions(+), 1 deletion(-)
cpu-exec.c +1 -0
index 1a5ad48..6dbb4da 100644
@@ -33,6 +33,7 @@
 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
 #include "hw/i386/apic.h"
 #endif
+#include "sysemu/cpus.h"
 #include "sysemu/replay.h"
 
 /* -icount align implementation. */
hw/core/ptimer.c +1 -0
index 59ccb00..7221c68 100644
@@ -13,6 +13,7 @@
 #include "sysemu/replay.h"
 #include "sysemu/qtest.h"
 #include "block/aio.h"
+#include "sysemu/cpus.h"
 
 #define DELTA_ADJUST     1
 #define DELTA_NO_ADJUST -1
include/qemu/timer.h +0 -1
index 26e6285..91cd8c8 100644
@@ -4,7 +4,6 @@
 #include "qemu-common.h"
 #include "qemu/notify.h"
 #include "qemu/host-utils.h"
-#include "sysemu/cpus.h"
 
 #define NANOSECONDS_PER_SECOND 1000000000LL
 
include/sysemu/cpus.h +2 -0
index a73b5d4..e521a91 100644
@@ -1,6 +1,8 @@
 #ifndef QEMU_CPUS_H
 #define QEMU_CPUS_H
 
+#include "qemu/timer.h"
+
 /* cpus.c */
 bool qemu_in_vcpu_thread(void);
 void qemu_init_cpu_loop(void);
kvm-all.c +1 -0
index 0c94637..0fd6655 100644
@@ -29,6 +29,7 @@
 #include "hw/s390x/adapter.h"
 #include "exec/gdbstub.h"
 #include "sysemu/kvm_int.h"
+#include "sysemu/cpus.h"
 #include "qemu/bswap.h"
 #include "exec/memory.h"
 #include "exec/ram_addr.h"
monitor.c +1 -0
index b68944d..d8fba78 100644
@@ -77,6 +77,7 @@
 #include "qapi-event.h"
 #include "qmp-introspect.h"
 #include "sysemu/qtest.h"
+#include "sysemu/cpus.h"
 #include "qemu/cutils.h"
 #include "qapi/qmp/dispatch.h"
 
replay/replay.c +1 -0
index 1835b99..78e2a7e 100644
@@ -16,6 +16,7 @@
 #include "replay-internal.h"
 #include "qemu/timer.h"
 #include "qemu/main-loop.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "qemu/error-report.h"
 
translate-all.c +1 -0
index 956d54b..63b8f56 100644
@@ -57,6 +57,7 @@
 #include "qemu/timer.h"
 #include "qemu/main-loop.h"
 #include "exec/log.h"
+#include "sysemu/cpus.h"
 
 /* #define DEBUG_TB_INVALIDATE */
 /* #define DEBUG_TB_FLUSH */
util/main-loop.c +1 -0
index ad10bca..0b2d8c0 100644
@@ -28,6 +28,7 @@
 #include "qemu/timer.h"
 #include "qemu/sockets.h"	// struct in_addr needed for libslirp.h
 #include "sysemu/qtest.h"
+#include "sysemu/cpus.h"
 #include "slirp/libslirp.h"
 #include "qemu/main-loop.h"
 #include "block/aio.h"
util/qemu-timer.c +0 -0
index 2f20151..ac99340 100644

util/qemu-timer.c line 33

Quoted 7 lines
util/qemu-timer.c +1 -0
@@ -27,6 +27,7 @@
 #include "qemu/timer.h"
 #include "sysemu/replay.h"
 #include "sysemu/sysemu.h"
+#include "sysemu/cpus.h"
 
 #ifdef CONFIG_POSIX
 #include <pthread.h>

--
Alex Bennée

#7 lore raw

Re: [Qemu-devel] [PATCH 2/5] qemu-timer: do not include sysemu/cpus.h from util/qemu-timer.h

Quoted 7 lines · Alex Bennée
Quoted 2 lines · Paolo Bonzini

This dependency is the wrong way, and we will need util/qemu-timer.h from sysemu/cpus.h in the next patch.

This shuffling breaks a couple of targets:

Oops. I only tested x86 and aarch64 before. This will have to wait for after soft freeze.

Paolo

Quoted 174 lines

make -k -j9
CHK version_gen.h
CC alpha-softmmu/target/alpha/translate.o
/home/alex/lsrc/qemu/qemu.git/target/alpha/translate.c: In function ‘gen_mfpr’:
/home/alex/lsrc/qemu/qemu.git/target/alpha/translate.c:1321:13: error: ‘use_icount’ undeclared (first use in this function)

         if (use_icount) {
             ^

/home/alex/lsrc/qemu/qemu.git/target/alpha/translate.c:1321:13: note: each undeclared identifier is reported only once for each function it appears in /home/alex/lsrc/qemu/qemu.git/rules.mak:69: recipe for target 'target/alpha/translate.o' failed
make[1]: *** [target/alpha/translate.o] Error 1
make[1]: Target 'all' not remade because of errors.
Makefile:322: recipe for target 'subdir-alpha-softmmu' failed
make: *** [subdir-alpha-softmmu] Error 2
CC ppc64-softmmu/hw/ppc/pnv.o
/home/alex/lsrc/qemu/qemu.git/hw/ppc/pnv.c: In function ‘ppc_powernv_init’:
/home/alex/lsrc/qemu/qemu.git/hw/ppc/pnv.c:446:39: error: ‘smp_cores’ undeclared (first use in this function)

         object_property_set_int(chip, smp_cores, "nr-cores", &error_fatal);
                                       ^

/home/alex/lsrc/qemu/qemu.git/hw/ppc/pnv.c:446:39: note: each undeclared identifier is reported only once for each function it appears in /home/alex/lsrc/qemu/qemu.git/hw/ppc/pnv.c: In function ‘pnv_chip_realize’:
/home/alex/lsrc/qemu/qemu.git/hw/ppc/pnv.c:686:57: error: ‘smp_threads’ undeclared (first use in this function)
object_property_set_int(OBJECT(pnv_core), smp_threads, "nr-threads",
^
/home/alex/lsrc/qemu/qemu.git/rules.mak:69: recipe for target 'hw/ppc/pnv.o' failed
make[1]: *** [hw/ppc/pnv.o] Error 1
make[1]: Target 'all' not remade because of errors.
Makefile:322: recipe for target 'subdir-ppc64-softmmu' failed
make: *** [subdir-ppc64-softmmu] Error 2
make: Target 'all' not remade because of errors.

Compilation exited abnormally with code 2 at Fri Mar 3 14:49:30

Quoted 129 lines
---
 cpu-exec.c            | 1 +
 hw/core/ptimer.c      | 1 +
 include/qemu/timer.h  | 1 -
 include/sysemu/cpus.h | 2 ++
 kvm-all.c             | 1 +
 monitor.c             | 1 +
 replay/replay.c       | 1 +
 translate-all.c       | 1 +
 util/main-loop.c      | 1 +
 util/qemu-timer.c     | 1 +
 10 files changed, 10 insertions(+), 1 deletion(-)
cpu-exec.c +1 -0
index 1a5ad48..6dbb4da 100644
@@ -33,6 +33,7 @@
 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
 #include "hw/i386/apic.h"
 #endif
+#include "sysemu/cpus.h"
 #include "sysemu/replay.h"
 
 /* -icount align implementation. */
hw/core/ptimer.c +1 -0
index 59ccb00..7221c68 100644
@@ -13,6 +13,7 @@
 #include "sysemu/replay.h"
 #include "sysemu/qtest.h"
 #include "block/aio.h"
+#include "sysemu/cpus.h"
 
 #define DELTA_ADJUST     1
 #define DELTA_NO_ADJUST -1
include/qemu/timer.h +0 -1
index 26e6285..91cd8c8 100644
@@ -4,7 +4,6 @@
 #include "qemu-common.h"
 #include "qemu/notify.h"
 #include "qemu/host-utils.h"
-#include "sysemu/cpus.h"
 
 #define NANOSECONDS_PER_SECOND 1000000000LL
 
include/sysemu/cpus.h +2 -0
index a73b5d4..e521a91 100644
@@ -1,6 +1,8 @@
 #ifndef QEMU_CPUS_H
 #define QEMU_CPUS_H
 
+#include "qemu/timer.h"
+
 /* cpus.c */
 bool qemu_in_vcpu_thread(void);
 void qemu_init_cpu_loop(void);
kvm-all.c +1 -0
index 0c94637..0fd6655 100644
@@ -29,6 +29,7 @@
 #include "hw/s390x/adapter.h"
 #include "exec/gdbstub.h"
 #include "sysemu/kvm_int.h"
+#include "sysemu/cpus.h"
 #include "qemu/bswap.h"
 #include "exec/memory.h"
 #include "exec/ram_addr.h"
monitor.c +1 -0
index b68944d..d8fba78 100644
@@ -77,6 +77,7 @@
 #include "qapi-event.h"
 #include "qmp-introspect.h"
 #include "sysemu/qtest.h"
+#include "sysemu/cpus.h"
 #include "qemu/cutils.h"
 #include "qapi/qmp/dispatch.h"
 
replay/replay.c +1 -0
index 1835b99..78e2a7e 100644
@@ -16,6 +16,7 @@
 #include "replay-internal.h"
 #include "qemu/timer.h"
 #include "qemu/main-loop.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "qemu/error-report.h"
 
translate-all.c +1 -0
index 956d54b..63b8f56 100644
@@ -57,6 +57,7 @@
 #include "qemu/timer.h"
 #include "qemu/main-loop.h"
 #include "exec/log.h"
+#include "sysemu/cpus.h"
 
 /* #define DEBUG_TB_INVALIDATE */
 /* #define DEBUG_TB_FLUSH */
util/main-loop.c +1 -0
index ad10bca..0b2d8c0 100644
@@ -28,6 +28,7 @@
 #include "qemu/timer.h"
 #include "qemu/sockets.h"	// struct in_addr needed for libslirp.h
 #include "sysemu/qtest.h"
+#include "sysemu/cpus.h"
 #include "slirp/libslirp.h"
 #include "qemu/main-loop.h"
 #include "block/aio.h"
util/qemu-timer.c +0 -0
index 2f20151..ac99340 100644

util/qemu-timer.c line 33

Quoted 7 lines
util/qemu-timer.c +1 -0
@@ -27,6 +27,7 @@
 #include "qemu/timer.h"
 #include "sysemu/replay.h"
 #include "sysemu/sysemu.h"
+#include "sysemu/cpus.h"
 
 #ifdef CONFIG_POSIX
 #include <pthread.h>

--
Alex Bennée

#8 lore raw

PATCH 1/1 fixup! qemu-timer: do not include sysemu/cpus.h from util/qemu-timer.h

In reply to Paolo Bonzini
---
 hw/ppc/pnv.c             | 1 +
 target/alpha/translate.c | 1 +
 2 files changed, 2 insertions(+)
hw/ppc/pnv.c +1 -0
index 09f0d22def..3fa722af82 100644
@@ -21,6 +21,7 @@
 #include "qapi/error.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/numa.h"
+#include "sysemu/cpus.h"
 #include "hw/hw.h"
 #include "target/ppc/cpu.h"
 #include "qemu/log.h"
target/alpha/translate.c +1 -0
index 055286a7b8..df5d695344 100644
@@ -19,6 +19,7 @@
 
 #include "qemu/osdep.h"
 #include "cpu.h"
+#include "sysemu/cpus.h"
 #include "disas/disas.h"
 #include "qemu/host-utils.h"
 #include "exec/exec-all.h"
Signature · 1 line
2.11.0
#9 lore raw

Re: [Qemu-devel] [PATCH] fixup! qemu-timer: do not include sysemu/cpus.h from util/qemu-timer.h

Quoted 4 lines · Alex Bennée
---
 hw/ppc/pnv.c             | 1 +
 target/alpha/translate.c | 1 +
 2 files changed, 2 insertions(+)

I'm guessing you didn't intend to post a fixup! patch without a signoff line? :-)

thanks
-- PMM

Quoted 27 lines
hw/ppc/pnv.c +1 -0
index 09f0d22def..3fa722af82 100644
@@ -21,6 +21,7 @@
 #include "qapi/error.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/numa.h"
+#include "sysemu/cpus.h"
 #include "hw/hw.h"
 #include "target/ppc/cpu.h"
 #include "qemu/log.h"
target/alpha/translate.c +1 -1
index 055286a7b8..df5d695344 100644
@@ -19,6 +19,7 @@
 
 #include "qemu/osdep.h"
 #include "cpu.h"
+#include "sysemu/cpus.h"
 #include "disas/disas.h"
 #include "qemu/host-utils.h"
 #include "exec/exec-all.h"
--

2.11.0

Reviewed-by on v1 2/5
#10 lore raw

Re: [Qemu-devel] [PATCH 2/5] qemu-timer: do not include sysemu/cpus.h from util/qemu-timer.h

In reply to Paolo Bonzini
Quoted 4 lines · Paolo Bonzini

This dependency is the wrong way, and we will need util/qemu-timer.h from sysemu/cpus.h in the next patch.

With the compile fixes I posted as a !fixup patch:

Quoted 127 lines
---
 cpu-exec.c            | 1 +
 hw/core/ptimer.c      | 1 +
 include/qemu/timer.h  | 1 -
 include/sysemu/cpus.h | 2 ++
 kvm-all.c             | 1 +
 monitor.c             | 1 +
 replay/replay.c       | 1 +
 translate-all.c       | 1 +
 util/main-loop.c      | 1 +
 util/qemu-timer.c     | 1 +
 10 files changed, 10 insertions(+), 1 deletion(-)
cpu-exec.c +1 -0
index 1a5ad48..6dbb4da 100644
@@ -33,6 +33,7 @@
 #if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
 #include "hw/i386/apic.h"
 #endif
+#include "sysemu/cpus.h"
 #include "sysemu/replay.h"
 
 /* -icount align implementation. */
hw/core/ptimer.c +1 -0
index 59ccb00..7221c68 100644
@@ -13,6 +13,7 @@
 #include "sysemu/replay.h"
 #include "sysemu/qtest.h"
 #include "block/aio.h"
+#include "sysemu/cpus.h"
 
 #define DELTA_ADJUST     1
 #define DELTA_NO_ADJUST -1
include/qemu/timer.h +0 -1
index 26e6285..91cd8c8 100644
@@ -4,7 +4,6 @@
 #include "qemu-common.h"
 #include "qemu/notify.h"
 #include "qemu/host-utils.h"
-#include "sysemu/cpus.h"
 
 #define NANOSECONDS_PER_SECOND 1000000000LL
 
include/sysemu/cpus.h +2 -0
index a73b5d4..e521a91 100644
@@ -1,6 +1,8 @@
 #ifndef QEMU_CPUS_H
 #define QEMU_CPUS_H
 
+#include "qemu/timer.h"
+
 /* cpus.c */
 bool qemu_in_vcpu_thread(void);
 void qemu_init_cpu_loop(void);
kvm-all.c +1 -0
index 0c94637..0fd6655 100644
@@ -29,6 +29,7 @@
 #include "hw/s390x/adapter.h"
 #include "exec/gdbstub.h"
 #include "sysemu/kvm_int.h"
+#include "sysemu/cpus.h"
 #include "qemu/bswap.h"
 #include "exec/memory.h"
 #include "exec/ram_addr.h"
monitor.c +1 -0
index b68944d..d8fba78 100644
@@ -77,6 +77,7 @@
 #include "qapi-event.h"
 #include "qmp-introspect.h"
 #include "sysemu/qtest.h"
+#include "sysemu/cpus.h"
 #include "qemu/cutils.h"
 #include "qapi/qmp/dispatch.h"
 
replay/replay.c +1 -0
index 1835b99..78e2a7e 100644
@@ -16,6 +16,7 @@
 #include "replay-internal.h"
 #include "qemu/timer.h"
 #include "qemu/main-loop.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "qemu/error-report.h"
 
translate-all.c +1 -0
index 956d54b..63b8f56 100644
@@ -57,6 +57,7 @@
 #include "qemu/timer.h"
 #include "qemu/main-loop.h"
 #include "exec/log.h"
+#include "sysemu/cpus.h"
 
 /* #define DEBUG_TB_INVALIDATE */
 /* #define DEBUG_TB_FLUSH */
util/main-loop.c +1 -0
index ad10bca..0b2d8c0 100644
@@ -28,6 +28,7 @@
 #include "qemu/timer.h"
 #include "qemu/sockets.h"	// struct in_addr needed for libslirp.h
 #include "sysemu/qtest.h"
+#include "sysemu/cpus.h"
 #include "slirp/libslirp.h"
 #include "qemu/main-loop.h"
 #include "block/aio.h"
util/qemu-timer.c +0 -0
index 2f20151..ac99340 100644

util/qemu-timer.c line 33

Quoted 7 lines
util/qemu-timer.c +1 -0
@@ -27,6 +27,7 @@
 #include "qemu/timer.h"
 #include "sysemu/replay.h"
 #include "sysemu/sysemu.h"
+#include "sysemu/cpus.h"
 
 #ifdef CONFIG_POSIX
 #include <pthread.h>

--
Alex Bennée

#11 lore raw

PATCH 3/5 cpus: define QEMUTimerListNotifyCB for QEMU system emulation

In reply to Paolo Bonzini

There is no change for now, because the callback just invokes qemu_notify_event.

---
 cpus.c                       |  5 +++++
 include/qemu/timer.h         |  4 ++--
 include/sysemu/cpus.h        |  1 +
 stubs/cpu-get-icount.c       |  6 ++++++
 tests/test-aio-multithread.c |  2 +-
 tests/test-aio.c             |  2 +-
 util/async.c                 |  2 +-
 util/main-loop.c             |  2 +-
 util/qemu-timer.c            | 10 +++++-----
 9 files changed, 23 insertions(+), 11 deletions(-)
cpus.c +5 -0
index 2459f56..747addd 100644
@@ -804,6 +804,11 @@ static void qemu_cpu_kick_rr_cpu(void)
     } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
 }
 
+void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
+{
+    qemu_notify_event();
+}
+
 static void kick_tcg_thread(void *opaque)
 {
     timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
include/qemu/timer.h +2 -2
index 91cd8c8..1441b42 100644
@@ -59,7 +59,7 @@ struct QEMUTimerListGroup {
 };
 
 typedef void QEMUTimerCB(void *opaque);
-typedef void QEMUTimerListNotifyCB(void *opaque);
+typedef void QEMUTimerListNotifyCB(void *opaque, QEMUClockType type);
 
 struct QEMUTimer {
     int64_t expire_time;        /* in nanoseconds */
@@ -776,7 +776,7 @@ static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
  *
  * Initialise the clock & timer infrastructure
  */
-void init_clocks(void);
+void init_clocks(QEMUTimerListNotifyCB *notify_cb);
 
 int64_t cpu_get_ticks(void);
 /* Caller must hold BQL */
include/sysemu/cpus.h +1 -0
index e521a91..a8053f1 100644
@@ -22,6 +22,7 @@ void dump_drift_info(FILE *f, fprintf_function cpu_fprintf);
 
 /* Unblock cpu */
 void qemu_cpu_kick_self(void);
+void qemu_timer_notify_cb(void *opaque, QEMUClockType type);
 
 void cpu_synchronize_all_states(void);
 void cpu_synchronize_all_post_reset(void);
stubs/cpu-get-icount.c +6 -0
index 2e8b63b..0b7239d 100644
@@ -2,6 +2,7 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 #include "sysemu/cpus.h"
+#include "qemu/main-loop.h"
 
 int use_icount;
 
@@ -9,3 +10,8 @@ int64_t cpu_get_icount(void)
 {
     abort();
 }
+
+void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
+{
+    qemu_notify_event();
+}
tests/test-aio-multithread.c +1 -1
index 8b0b40e..549d784 100644
@@ -438,7 +438,7 @@ static void test_multi_mutex_10(void)
 
 int main(int argc, char **argv)
 {
-    init_clocks();
+    init_clocks(NULL);
 
     g_test_init(&argc, &argv, NULL);
     g_test_add_func("/aio/multi/lifecycle", test_lifecycle);
tests/test-aio.c +1 -1
index 2754f15..54e20d6 100644
@@ -835,7 +835,7 @@ int main(int argc, char **argv)
     Error *local_error = NULL;
     GSource *src;
 
-    init_clocks();
+    init_clocks(NULL);
 
     ctx = aio_context_new(&local_error);
     if (!ctx) {
util/async.c +1 -1
index 7d469eb..663e297 100644
@@ -351,7 +351,7 @@ void aio_notify_accept(AioContext *ctx)
     }
 }
 
-static void aio_timerlist_notify(void *opaque)
+static void aio_timerlist_notify(void *opaque, QEMUClockType type)
 {
     aio_notify(opaque);
 }
util/main-loop.c +1 -1
index 0b2d8c0..a8c1c5b 100644
@@ -147,7 +147,7 @@ int qemu_init_main_loop(Error **errp)
     GSource *src;
     Error *local_error = NULL;
 
-    init_clocks();
+    init_clocks(qemu_timer_notify_cb);
 
     ret = qemu_signal_init();
     if (ret) {
util/qemu-timer.c +5 -5
index ac99340..dc3181e 100644
@@ -122,7 +122,7 @@ void timerlist_free(QEMUTimerList *timer_list)
     g_free(timer_list);
 }
 
-static void qemu_clock_init(QEMUClockType type)
+static void qemu_clock_init(QEMUClockType type, QEMUTimerListNotifyCB *notify_cb)
 {
     QEMUClock *clock = qemu_clock_ptr(type);
 
@@ -134,7 +134,7 @@ static void qemu_clock_init(QEMUClockType type)
     clock->last = INT64_MIN;
     QLIST_INIT(&clock->timerlists);
     notifier_list_init(&clock->reset_notifiers);
-    main_loop_tlg.tl[type] = timerlist_new(type, NULL, NULL);
+    main_loop_tlg.tl[type] = timerlist_new(type, notify_cb, NULL);
 }
 
 bool qemu_clock_use_for_deadline(QEMUClockType type)
@@ -278,7 +278,7 @@ QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type)
 void timerlist_notify(QEMUTimerList *timer_list)
 {
     if (timer_list->notify_cb) {
-        timer_list->notify_cb(timer_list->notify_opaque);
+        timer_list->notify_cb(timer_list->notify_opaque, timer_list->clock->type);
     } else {
         qemu_notify_event();
     }
@@ -635,11 +635,11 @@ void qemu_clock_unregister_reset_notifier(QEMUClockType type,
     notifier_remove(notifier);
 }
 
-void init_clocks(void)
+void init_clocks(QEMUTimerListNotifyCB *notify_cb)
 {
     QEMUClockType type;
     for (type = 0; type < QEMU_CLOCK_MAX; type++) {
-        qemu_clock_init(type);
+        qemu_clock_init(type, notify_cb);
     }
 
 #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
Signature · 1 line
2.9.3
Reviewed-by on v1 3/5
#12 lore raw

Re: [Qemu-devel] [PATCH 3/5] cpus: define QEMUTimerListNotifyCB for QEMU system emulation

Quoted 4 lines · Paolo Bonzini

There is no change for now, because the callback just invokes qemu_notify_event.

Quoted 185 lines
---
 cpus.c                       |  5 +++++
 include/qemu/timer.h         |  4 ++--
 include/sysemu/cpus.h        |  1 +
 stubs/cpu-get-icount.c       |  6 ++++++
 tests/test-aio-multithread.c |  2 +-
 tests/test-aio.c             |  2 +-
 util/async.c                 |  2 +-
 util/main-loop.c             |  2 +-
 util/qemu-timer.c            | 10 +++++-----
 9 files changed, 23 insertions(+), 11 deletions(-)
cpus.c +5 -0
index 2459f56..747addd 100644
@@ -804,6 +804,11 @@ static void qemu_cpu_kick_rr_cpu(void)
     } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
 }
 
+void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
+{
+    qemu_notify_event();
+}
+
 static void kick_tcg_thread(void *opaque)
 {
     timer_mod(tcg_kick_vcpu_timer, qemu_tcg_next_kick());
include/qemu/timer.h +2 -2
index 91cd8c8..1441b42 100644
@@ -59,7 +59,7 @@ struct QEMUTimerListGroup {
 };
 
 typedef void QEMUTimerCB(void *opaque);
-typedef void QEMUTimerListNotifyCB(void *opaque);
+typedef void QEMUTimerListNotifyCB(void *opaque, QEMUClockType type);
 
 struct QEMUTimer {
     int64_t expire_time;        /* in nanoseconds */
@@ -776,7 +776,7 @@ static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
  *
  * Initialise the clock & timer infrastructure
  */
-void init_clocks(void);
+void init_clocks(QEMUTimerListNotifyCB *notify_cb);
 
 int64_t cpu_get_ticks(void);
 /* Caller must hold BQL */
include/sysemu/cpus.h +1 -0
index e521a91..a8053f1 100644
@@ -22,6 +22,7 @@ void dump_drift_info(FILE *f, fprintf_function cpu_fprintf);
 
 /* Unblock cpu */
 void qemu_cpu_kick_self(void);
+void qemu_timer_notify_cb(void *opaque, QEMUClockType type);
 
 void cpu_synchronize_all_states(void);
 void cpu_synchronize_all_post_reset(void);
stubs/cpu-get-icount.c +6 -0
index 2e8b63b..0b7239d 100644
@@ -2,6 +2,7 @@
 #include "qemu-common.h"
 #include "qemu/timer.h"
 #include "sysemu/cpus.h"
+#include "qemu/main-loop.h"
 
 int use_icount;
 
@@ -9,3 +10,8 @@ int64_t cpu_get_icount(void)
 {
     abort();
 }
+
+void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
+{
+    qemu_notify_event();
+}
tests/test-aio-multithread.c +1 -1
index 8b0b40e..549d784 100644
@@ -438,7 +438,7 @@ static void test_multi_mutex_10(void)
 
 int main(int argc, char **argv)
 {
-    init_clocks();
+    init_clocks(NULL);
 
     g_test_init(&argc, &argv, NULL);
     g_test_add_func("/aio/multi/lifecycle", test_lifecycle);
tests/test-aio.c +1 -1
index 2754f15..54e20d6 100644
@@ -835,7 +835,7 @@ int main(int argc, char **argv)
     Error *local_error = NULL;
     GSource *src;
 
-    init_clocks();
+    init_clocks(NULL);
 
     ctx = aio_context_new(&local_error);
     if (!ctx) {
util/async.c +1 -1
index 7d469eb..663e297 100644
@@ -351,7 +351,7 @@ void aio_notify_accept(AioContext *ctx)
     }
 }
 
-static void aio_timerlist_notify(void *opaque)
+static void aio_timerlist_notify(void *opaque, QEMUClockType type)
 {
     aio_notify(opaque);
 }
util/main-loop.c +1 -1
index 0b2d8c0..a8c1c5b 100644
@@ -147,7 +147,7 @@ int qemu_init_main_loop(Error **errp)
     GSource *src;
     Error *local_error = NULL;
 
-    init_clocks();
+    init_clocks(qemu_timer_notify_cb);
 
     ret = qemu_signal_init();
     if (ret) {
util/qemu-timer.c +5 -5
index ac99340..dc3181e 100644
@@ -122,7 +122,7 @@ void timerlist_free(QEMUTimerList *timer_list)
     g_free(timer_list);
 }
 
-static void qemu_clock_init(QEMUClockType type)
+static void qemu_clock_init(QEMUClockType type, QEMUTimerListNotifyCB *notify_cb)
 {
     QEMUClock *clock = qemu_clock_ptr(type);
 
@@ -134,7 +134,7 @@ static void qemu_clock_init(QEMUClockType type)
     clock->last = INT64_MIN;
     QLIST_INIT(&clock->timerlists);
     notifier_list_init(&clock->reset_notifiers);
-    main_loop_tlg.tl[type] = timerlist_new(type, NULL, NULL);
+    main_loop_tlg.tl[type] = timerlist_new(type, notify_cb, NULL);
 }
 
 bool qemu_clock_use_for_deadline(QEMUClockType type)
@@ -278,7 +278,7 @@ QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type)
 void timerlist_notify(QEMUTimerList *timer_list)
 {
     if (timer_list->notify_cb) {
-        timer_list->notify_cb(timer_list->notify_opaque);
+        timer_list->notify_cb(timer_list->notify_opaque, timer_list->clock->type);
     } else {
         qemu_notify_event();
     }
@@ -635,11 +635,11 @@ void qemu_clock_unregister_reset_notifier(QEMUClockType type,
     notifier_remove(notifier);
 }
 
-void init_clocks(void)
+void init_clocks(QEMUTimerListNotifyCB *notify_cb)
 {
     QEMUClockType type;
     for (type = 0; type < QEMU_CLOCK_MAX; type++) {
-        qemu_clock_init(type);
+        qemu_clock_init(type, notify_cb);
     }
 
 #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
Signature · 1 line
2.9.3
#13 lore raw

PATCH 4/5 main-loop: remove now unnecessary optimization

In reply to Paolo Bonzini

This optimization is not necessary anymore, because the vCPU now drops the I/O thread lock even with TCG. Drop it to simplify the code and avoid the "I/O thread spun for 1000 iterations" warning.

---
 vl.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
vl.c +1 -4
index bbbf1ba..b21b57e 100644
@@ -1884,17 +1884,14 @@ static bool main_loop_should_exit(void)
 
 static void main_loop(void)
 {
-    bool nonblocking;
-    int last_io = 0;
 #ifdef CONFIG_PROFILER
     int64_t ti;
 #endif
     do {
-        nonblocking = tcg_enabled() && last_io > 0;
 #ifdef CONFIG_PROFILER
         ti = profile_getclock();
 #endif
-        last_io = main_loop_wait(nonblocking);
+        main_loop_wait(false);
 #ifdef CONFIG_PROFILER
         dev_time += profile_getclock() - ti;
 #endif
Signature · 1 line
2.9.3
Reviewed-by on v1 4/5
#14 lore raw

Re: [Qemu-devel] [PATCH 4/5] main-loop: remove now unnecessary optimization

Quoted 5 lines · Paolo Bonzini

This optimization is not necessary anymore, because the vCPU now drops the I/O thread lock even with TCG. Drop it to simplify the code and avoid the "I/O thread spun for 1000 iterations" warning.

Quoted 32 lines
---
 vl.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
vl.c +1 -4
index bbbf1ba..b21b57e 100644
@@ -1884,17 +1884,14 @@ static bool main_loop_should_exit(void)
 
 static void main_loop(void)
 {
-    bool nonblocking;
-    int last_io = 0;
 #ifdef CONFIG_PROFILER
     int64_t ti;
 #endif
     do {
-        nonblocking = tcg_enabled() && last_io > 0;
 #ifdef CONFIG_PROFILER
         ti = profile_getclock();
 #endif
-        last_io = main_loop_wait(nonblocking);
+        main_loop_wait(false);
 #ifdef CONFIG_PROFILER
         dev_time += profile_getclock() - ti;
 #endif
Signature · 1 line
2.9.3
Reviewed-by on v1 4/5
#15 lore raw

Re: [Qemu-devel] [PATCH 4/5] main-loop: remove now unnecessary optimization

In reply to Paolo Bonzini
Quoted 5 lines · Paolo Bonzini

This optimization is not necessary anymore, because the vCPU now drops the I/O thread lock even with TCG. Drop it to simplify the code and avoid the "I/O thread spun for 1000 iterations" warning.

Quoted 17 lines
---
 vl.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
vl.c +0 -2
index bbbf1ba..b21b57e 100644
@@ -1884,17 +1884,14 @@ static bool main_loop_should_exit(void)
 
 static void main_loop(void)
 {
-    bool nonblocking;
-    int last_io = 0;
 #ifdef CONFIG_PROFILER
     int64_t ti;
 #endif

vl.c static bool main_loop_should_exit(void) · line 1897

Quoted 10 lines
vl.c +1 -2
     do {
-        nonblocking = tcg_enabled() && last_io > 0;
 #ifdef CONFIG_PROFILER
         ti = profile_getclock();
 #endif
-        last_io = main_loop_wait(nonblocking);
+        main_loop_wait(false);
 #ifdef CONFIG_PROFILER
         dev_time += profile_getclock() - ti;
 #endif

--
Alex Bennée

#16 lore raw

PATCH 5/5 icount: process QEMU_CLOCK_VIRTUAL timers in vCPU thread

In reply to Paolo Bonzini

icount has become much slower after tcg_cpu_exec has stopped using the BQL. There is also a latent bug that is masked by the slowness.

The slowness happens because every occurrence of a QEMU_CLOCK_VIRTUAL timer now has to wake up the I/O thread and wait for it. The rendez-vous is mediated by the BQL QemuMutex:

- handle_icount_deadline wakes up the I/O thread with BQL taken
- the I/O thread wakes up and waits on the BQL
- the VCPU thread releases the BQL a little later
- the I/O thread raises an interrupt, which calls qemu_cpu_kick
- the VCPU thread notices the interrupt, takes the BQL to process it and waits on it

All this back and forth is extremely expensive, causing a 6 to 8-fold slowdown when icount is turned on.

One may think that the issue is that the VCPU thread is too dependent on the BQL, but then the latent bug comes in. I first tried removing the BQL completely from the x86 cpu_exec, only to see everything break. The only way to fix it (and make everything slow again) was to add a dummy BQL lock/unlock pair.

This is because in -icount mode you really have to process the events before the CPU restarts executing the next instruction. Therefore, this series moves the processing of QEMU_CLOCK_VIRTUAL timers straight in the vCPU thread when running in icount mode.

The required changes include:

- make the timer notification callback wake up TCG's single vCPU thread when run from another thread. By using async_run_on_cpu, the callback can override all_cpu_threads_idle() when the CPU is halted.

- move handle_icount_deadline after qemu_tcg_wait_io_event, so that the timer notification callback is invoked after the dummy work item wakes up the vCPU thread

- make handle_icount_deadline run the timers instead of just waking the I/O thread.

- stop processing the timers in the main loop

---
 cpus.c               | 26 +++++++++++++++++++++++---
 include/qemu/timer.h | 24 ++++++++++++++++++++++++
 util/qemu-timer.c    |  4 +++-
 3 files changed, 50 insertions(+), 4 deletions(-)
cpus.c +23 -3
index 747addd..209c196 100644
@@ -804,9 +804,23 @@ static void qemu_cpu_kick_rr_cpu(void)
     } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
 }
 
+static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
+{
+}
+
 void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
 {
-    qemu_notify_event();
+    if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {
+        qemu_notify_event();
+        return;
+    }
+
+    if (!qemu_in_vcpu_thread() && first_cpu) {
+        /* run_on_cpu will also kick the CPU out of halt so that
+         * handle_icount_deadline runs
+         */
+        async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
+    }
 }
 
 static void kick_tcg_thread(void *opaque)
@@ -1220,12 +1234,15 @@ static int64_t tcg_get_icount_limit(void)
 
 static void handle_icount_deadline(void)
 {
+    assert(qemu_in_vcpu_thread());
     if (use_icount) {
         int64_t deadline =
             qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
 
         if (deadline == 0) {
+            /* Wake up other AioContexts.  */
             qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
+            qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
         }
     }
 }
@@ -1338,6 +1355,11 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
         /* Account partial waits to QEMU_CLOCK_VIRTUAL.  */
         qemu_account_warp_timer();
 
+        /* Run the timers here.  This is much more efficient than
+         * waking up the I/O thread and waiting for completion.
+         */
+        handle_icount_deadline();
+
         if (!cpu) {
             cpu = first_cpu;
         }
@@ -1379,8 +1401,6 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
             atomic_mb_set(&cpu->exit_request, 0);
         }
 
-        handle_icount_deadline();
-
         qemu_tcg_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus));
         deal_with_unplugged_cpus();
     }
include/qemu/timer.h +24 -0
index 1441b42..e1742f2 100644
@@ -533,6 +533,12 @@ static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
  * Create a new timer and associate it with the default
  * timer list for the clock type @type.
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Returns: a pointer to the timer
  */
 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
@@ -550,6 +556,12 @@ static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
  * Create a new timer with nanosecond scale on the default timer list
  * associated with the clock.
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Returns: a pointer to the newly created timer
  */
 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
@@ -564,6 +576,12 @@ static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
  * @cb: the callback to call when the timer expires
  * @opaque: the opaque pointer to pass to the callback
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Create a new timer with microsecond scale on the default timer list
  * associated with the clock.
  *
@@ -581,6 +599,12 @@ static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
  * @cb: the callback to call when the timer expires
  * @opaque: the opaque pointer to pass to the callback
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Create a new timer with millisecond scale on the default timer list
  * associated with the clock.
  *
util/qemu-timer.c +3 -1
index dc3181e..82d5650 100644
@@ -658,7 +658,9 @@ bool qemu_clock_run_all_timers(void)
     QEMUClockType type;
 
     for (type = 0; type < QEMU_CLOCK_MAX; type++) {
-        progress |= qemu_clock_run_timers(type);
+        if (qemu_clock_use_for_deadline(type)) {
+            progress |= qemu_clock_run_timers(type);
+        }
     }
 
     return progress;
Signature · 1 line
2.9.3
#17 lore raw

Re: [Qemu-devel] [PATCH 5/5] icount: process QEMU_CLOCK_VIRTUAL timers in vCPU thread

Quoted 67 lines · Paolo Bonzini

icount has become much slower after tcg_cpu_exec has stopped using the BQL. There is also a latent bug that is masked by the slowness.

The slowness happens because every occurrence of a QEMU_CLOCK_VIRTUAL timer now has to wake up the I/O thread and wait for it. The rendez-vous is mediated by the BQL QemuMutex:

- handle_icount_deadline wakes up the I/O thread with BQL taken
- the I/O thread wakes up and waits on the BQL
- the VCPU thread releases the BQL a little later
- the I/O thread raises an interrupt, which calls qemu_cpu_kick
- the VCPU thread notices the interrupt, takes the BQL to process it and waits on it

All this back and forth is extremely expensive, causing a 6 to 8-fold slowdown when icount is turned on.

One may think that the issue is that the VCPU thread is too dependent on the BQL, but then the latent bug comes in. I first tried removing the BQL completely from the x86 cpu_exec, only to see everything break. The only way to fix it (and make everything slow again) was to add a dummy BQL lock/unlock pair.

This is because in -icount mode you really have to process the events before the CPU restarts executing the next instruction. Therefore, this series moves the processing of QEMU_CLOCK_VIRTUAL timers straight in the vCPU thread when running in icount mode.

The required changes include:

- make the timer notification callback wake up TCG's single vCPU thread when run from another thread. By using async_run_on_cpu, the callback can override all_cpu_threads_idle() when the CPU is halted.

- move handle_icount_deadline after qemu_tcg_wait_io_event, so that the timer notification callback is invoked after the dummy work item wakes up the vCPU thread

- make handle_icount_deadline run the timers instead of just waking the I/O thread.

- stop processing the timers in the main loop

---
 cpus.c               | 26 +++++++++++++++++++++++---
 include/qemu/timer.h | 24 ++++++++++++++++++++++++
 util/qemu-timer.c    |  4 +++-
 3 files changed, 50 insertions(+), 4 deletions(-)
cpus.c +5 -1
index 747addd..209c196 100644
@@ -804,9 +804,23 @@ static void qemu_cpu_kick_rr_cpu(void)
     } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
 }
 
+static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
+{
+}
+
 void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
 {
-    qemu_notify_event();
+    if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {

cpus.c static void qemu_cpu_kick_rr_cpu(void) · line 823

Quoted 10 lines
cpus.c +10 -0
+        qemu_notify_event();
+        return;
+    }
+
+    if (!qemu_in_vcpu_thread() && first_cpu) {
+        /* run_on_cpu will also kick the CPU out of halt so that
+         * handle_icount_deadline runs
+         */
+        async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
+    }

This doesn't read quite right - otherwise you get the same effect by calling qemu_cpu_kick(), or even modify and call qemu_cpu_kick_rr_cpu()?

The only real effect of having something in the work queue is you run the outside of the loop before returning into the tcg_cpu_exec.

Quoted 32 lines
cpus.c +8 -0
 }
 
 static void kick_tcg_thread(void *opaque)
@@ -1220,12 +1234,15 @@ static int64_t tcg_get_icount_limit(void)
 
 static void handle_icount_deadline(void)
 {
+    assert(qemu_in_vcpu_thread());
     if (use_icount) {
         int64_t deadline =
             qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
 
         if (deadline == 0) {
+            /* Wake up other AioContexts.  */
             qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
+            qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
         }
     }
 }
@@ -1338,6 +1355,11 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
         /* Account partial waits to QEMU_CLOCK_VIRTUAL.  */
         qemu_account_warp_timer();
 
+        /* Run the timers here.  This is much more efficient than
+         * waking up the I/O thread and waiting for completion.
+         */
+        handle_icount_deadline();
+
         if (!cpu) {
             cpu = first_cpu;
         }

cpus.c static void *qemu_tcg_rr_cpu_thread_fn(void *arg) · line 1383

Quoted 5 lines
cpus.c +0 -2
@@ -1379,8 +1401,6 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
             atomic_mb_set(&cpu->exit_request, 0);
         }
 
-        handle_icount_deadline();
-

I guess we could just as easily move the handling to after qemu_tcg_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus))?

I noticed we still call handle_icount_deadline in the multi-thread case which is probably superfluous unless/until we figure out a way for it to work with MTTCG.

Quoted 64 lines
cpus.c +0 -0
         qemu_tcg_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus));
         deal_with_unplugged_cpus();
     }
include/qemu/timer.h +24 -0
index 1441b42..e1742f2 100644
@@ -533,6 +533,12 @@ static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
  * Create a new timer and associate it with the default
  * timer list for the clock type @type.
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Returns: a pointer to the timer
  */
 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
@@ -550,6 +556,12 @@ static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
  * Create a new timer with nanosecond scale on the default timer list
  * associated with the clock.
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Returns: a pointer to the newly created timer
  */
 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
@@ -564,6 +576,12 @@ static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
  * @cb: the callback to call when the timer expires
  * @opaque: the opaque pointer to pass to the callback
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Create a new timer with microsecond scale on the default timer list
  * associated with the clock.
  *
@@ -581,6 +599,12 @@ static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
  * @cb: the callback to call when the timer expires
  * @opaque: the opaque pointer to pass to the callback
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Create a new timer with millisecond scale on the default timer list
  * associated with the clock.
  *
util/qemu-timer.c +0 -0
index dc3181e..82d5650 100644

util/qemu-timer.c bool qemu_clock_run_all_timers(void) · line 663

Quoted 7 lines
util/qemu-timer.c +3 -1
@@ -658,7 +658,9 @@ bool qemu_clock_run_all_timers(void)
     QEMUClockType type;
 
     for (type = 0; type < QEMU_CLOCK_MAX; type++) {
-        progress |= qemu_clock_run_timers(type);
+        if (qemu_clock_use_for_deadline(type)) {
+            progress |= qemu_clock_run_timers(type);
+        }

minor nit: its not really qemu_clock_run_all_timers() now is it. We run all but the virtual timers (if icount is enabled).

util/qemu-timer.c bool qemu_clock_run_all_timers(void) · line 666

Quoted 3 lines
util/qemu-timer.c +0 -0
     }
 
     return progress;

--
Alex Bennée

#18 lore raw

Re: [Qemu-devel] [PATCH 5/5] icount: process QEMU_CLOCK_VIRTUAL timers in vCPU thread

Quoted 84 lines · Alex Bennée
Quoted 67 lines · Paolo Bonzini

icount has become much slower after tcg_cpu_exec has stopped using the BQL. There is also a latent bug that is masked by the slowness.

The slowness happens because every occurrence of a QEMU_CLOCK_VIRTUAL timer now has to wake up the I/O thread and wait for it. The rendez-vous is mediated by the BQL QemuMutex:

- handle_icount_deadline wakes up the I/O thread with BQL taken
- the I/O thread wakes up and waits on the BQL
- the VCPU thread releases the BQL a little later
- the I/O thread raises an interrupt, which calls qemu_cpu_kick
- the VCPU thread notices the interrupt, takes the BQL to process it and waits on it

All this back and forth is extremely expensive, causing a 6 to 8-fold slowdown when icount is turned on.

One may think that the issue is that the VCPU thread is too dependent on the BQL, but then the latent bug comes in. I first tried removing the BQL completely from the x86 cpu_exec, only to see everything break. The only way to fix it (and make everything slow again) was to add a dummy BQL lock/unlock pair.

This is because in -icount mode you really have to process the events before the CPU restarts executing the next instruction. Therefore, this series moves the processing of QEMU_CLOCK_VIRTUAL timers straight in the vCPU thread when running in icount mode.

The required changes include:

- make the timer notification callback wake up TCG's single vCPU thread when run from another thread. By using async_run_on_cpu, the callback can override all_cpu_threads_idle() when the CPU is halted.

- move handle_icount_deadline after qemu_tcg_wait_io_event, so that the timer notification callback is invoked after the dummy work item wakes up the vCPU thread

- make handle_icount_deadline run the timers instead of just waking the I/O thread.

- stop processing the timers in the main loop

---
 cpus.c               | 26 +++++++++++++++++++++++---
 include/qemu/timer.h | 24 ++++++++++++++++++++++++
 util/qemu-timer.c    |  4 +++-
 3 files changed, 50 insertions(+), 4 deletions(-)
cpus.c +5 -1
index 747addd..209c196 100644
@@ -804,9 +804,23 @@ static void qemu_cpu_kick_rr_cpu(void)
     } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
 }
 
+static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
+{
+}
+
 void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
 {
-    qemu_notify_event();
+    if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {

cpus.c static void qemu_cpu_kick_rr_cpu(void) · line 823

Quoted 10 lines
cpus.c +10 -0
+        qemu_notify_event();
+        return;
+    }
+
+    if (!qemu_in_vcpu_thread() && first_cpu) {
+        /* run_on_cpu will also kick the CPU out of halt so that
+         * handle_icount_deadline runs
+         */
+        async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
+    }

This doesn't read quite right - otherwise you get the same effect by calling qemu_cpu_kick(), or even modify and call qemu_cpu_kick_rr_cpu()?

Quoted 2 lines

The only real effect of having something in the work queue is you run the outside of the loop before returning into the tcg_cpu_exec.

Yes, and "the outside of the loop" here means handle_icount_deadline.

But qemu_cpu_kick_rr_cpu won't signal condition variables, and qemu_cpu_kick does but it won't make cpu_thread_is_idle return false. Therefore, qemu_tcg_wait_io_event keeps looping.

Quoted 41 lines
Quoted 32 lines
cpus.c +8 -0
 }
 
 static void kick_tcg_thread(void *opaque)
@@ -1220,12 +1234,15 @@ static int64_t tcg_get_icount_limit(void)
 
 static void handle_icount_deadline(void)
 {
+    assert(qemu_in_vcpu_thread());
     if (use_icount) {
         int64_t deadline =
             qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
 
         if (deadline == 0) {
+            /* Wake up other AioContexts.  */
             qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
+            qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
         }
     }
 }
@@ -1338,6 +1355,11 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
         /* Account partial waits to QEMU_CLOCK_VIRTUAL.  */
         qemu_account_warp_timer();
 
+        /* Run the timers here.  This is much more efficient than
+         * waking up the I/O thread and waiting for completion.
+         */
+        handle_icount_deadline();
+
         if (!cpu) {
             cpu = first_cpu;
         }

cpus.c static void *qemu_tcg_rr_cpu_thread_fn(void *arg) · line 1383

Quoted 5 lines
cpus.c +0 -2
@@ -1379,8 +1401,6 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
             atomic_mb_set(&cpu->exit_request, 0);
         }
 
-        handle_icount_deadline();
-

I guess we could just as easily move the handling to after qemu_tcg_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus))?

Quoted 3 lines

I noticed we still call handle_icount_deadline in the multi-thread case which is probably superfluous unless/until we figure out a way for it to work with MTTCG.

Should I remove the call? Add an assert(!use_icount)?

Quoted 66 lines
Quoted 66 lines
cpus.c +0 -0
         qemu_tcg_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus));
         deal_with_unplugged_cpus();
     }
include/qemu/timer.h +24 -0
index 1441b42..e1742f2 100644
@@ -533,6 +533,12 @@ static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
  * Create a new timer and associate it with the default
  * timer list for the clock type @type.
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Returns: a pointer to the timer
  */
 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
@@ -550,6 +556,12 @@ static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
  * Create a new timer with nanosecond scale on the default timer list
  * associated with the clock.
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Returns: a pointer to the newly created timer
  */
 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
@@ -564,6 +576,12 @@ static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
  * @cb: the callback to call when the timer expires
  * @opaque: the opaque pointer to pass to the callback
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Create a new timer with microsecond scale on the default timer list
  * associated with the clock.
  *
@@ -581,6 +599,12 @@ static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
  * @cb: the callback to call when the timer expires
  * @opaque: the opaque pointer to pass to the callback
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Create a new timer with millisecond scale on the default timer list
  * associated with the clock.
  *
util/qemu-timer.c +0 -0
index dc3181e..82d5650 100644
@@ -658,7 +658,9 @@ bool qemu_clock_run_all_timers(void)
     QEMUClockType type;
 
Quoted 8 lines

util/qemu-timer.c bool qemu_clock_run_all_timers(void) · line 663

Quoted 5 lines
util/qemu-timer.c +3 -1
     for (type = 0; type < QEMU_CLOCK_MAX; type++) {
-        progress |= qemu_clock_run_timers(type);
+        if (qemu_clock_use_for_deadline(type)) {
+            progress |= qemu_clock_run_timers(type);
+        }

minor nit: its not really qemu_clock_run_all_timers() now is it. We run all but the virtual timers (if icount is enabled).

Well yeah, it's all those that pass qemu_clock_use_for_deadline.

Paolo

Quoted 8 lines

util/qemu-timer.c bool qemu_clock_run_all_timers(void) · line 666

Quoted 3 lines
util/qemu-timer.c +0 -0
     }
 
     return progress;

--
Alex Bennée

#19 lore raw

Re: [Qemu-devel] [PATCH 5/5] icount: process QEMU_CLOCK_VIRTUAL timers in vCPU thread

Quoted 85 lines · Paolo Bonzini
Quoted 84 lines · Alex Bennée
Quoted 67 lines · Paolo Bonzini

icount has become much slower after tcg_cpu_exec has stopped using the BQL. There is also a latent bug that is masked by the slowness.

The slowness happens because every occurrence of a QEMU_CLOCK_VIRTUAL timer now has to wake up the I/O thread and wait for it. The rendez-vous is mediated by the BQL QemuMutex:

- handle_icount_deadline wakes up the I/O thread with BQL taken
- the I/O thread wakes up and waits on the BQL
- the VCPU thread releases the BQL a little later
- the I/O thread raises an interrupt, which calls qemu_cpu_kick
- the VCPU thread notices the interrupt, takes the BQL to process it and waits on it

All this back and forth is extremely expensive, causing a 6 to 8-fold slowdown when icount is turned on.

One may think that the issue is that the VCPU thread is too dependent on the BQL, but then the latent bug comes in. I first tried removing the BQL completely from the x86 cpu_exec, only to see everything break. The only way to fix it (and make everything slow again) was to add a dummy BQL lock/unlock pair.

This is because in -icount mode you really have to process the events before the CPU restarts executing the next instruction. Therefore, this series moves the processing of QEMU_CLOCK_VIRTUAL timers straight in the vCPU thread when running in icount mode.

The required changes include:

- make the timer notification callback wake up TCG's single vCPU thread when run from another thread. By using async_run_on_cpu, the callback can override all_cpu_threads_idle() when the CPU is halted.

- move handle_icount_deadline after qemu_tcg_wait_io_event, so that the timer notification callback is invoked after the dummy work item wakes up the vCPU thread

- make handle_icount_deadline run the timers instead of just waking the I/O thread.

- stop processing the timers in the main loop

---
 cpus.c               | 26 +++++++++++++++++++++++---
 include/qemu/timer.h | 24 ++++++++++++++++++++++++
 util/qemu-timer.c    |  4 +++-
 3 files changed, 50 insertions(+), 4 deletions(-)
cpus.c +5 -1
index 747addd..209c196 100644
@@ -804,9 +804,23 @@ static void qemu_cpu_kick_rr_cpu(void)
     } while (cpu != atomic_mb_read(&tcg_current_rr_cpu));
 }
 
+static void do_nothing(CPUState *cpu, run_on_cpu_data unused)
+{
+}
+
 void qemu_timer_notify_cb(void *opaque, QEMUClockType type)
 {
-    qemu_notify_event();
+    if (!use_icount || type != QEMU_CLOCK_VIRTUAL) {

cpus.c static void qemu_cpu_kick_rr_cpu(void) · line 823

Quoted 10 lines
cpus.c +10 -0
+        qemu_notify_event();
+        return;
+    }
+
+    if (!qemu_in_vcpu_thread() && first_cpu) {
+        /* run_on_cpu will also kick the CPU out of halt so that
+         * handle_icount_deadline runs
+         */
+        async_run_on_cpu(first_cpu, do_nothing, RUN_ON_CPU_NULL);
+    }

This doesn't read quite right - otherwise you get the same effect by calling qemu_cpu_kick(), or even modify and call qemu_cpu_kick_rr_cpu()?

Quoted 8 lines
Quoted 2 lines

The only real effect of having something in the work queue is you run the outside of the loop before returning into the tcg_cpu_exec.

Yes, and "the outside of the loop" here means handle_icount_deadline.

But qemu_cpu_kick_rr_cpu won't signal condition variables, and qemu_cpu_kick does but it won't make cpu_thread_is_idle return false. Therefore, qemu_tcg_wait_io_event keeps looping.

How about:

Scheduling work with async_run_on_cpu ensures we exit qemu_tcg_wait_io_event if we have halted that handle_icount_deadline can run.

Quoted 42 lines
Quoted 42 lines
Quoted 33 lines
cpus.c +8 -0
 }
 
 static void kick_tcg_thread(void *opaque)
@@ -1220,12 +1234,15 @@ static int64_t tcg_get_icount_limit(void)
 
 static void handle_icount_deadline(void)
 {
+    assert(qemu_in_vcpu_thread());
     if (use_icount) {
         int64_t deadline =
             qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
 
         if (deadline == 0) {
+            /* Wake up other AioContexts.  */
             qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
+            qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
         }
     }
 }
@@ -1338,6 +1355,11 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
         /* Account partial waits to QEMU_CLOCK_VIRTUAL.  */
         qemu_account_warp_timer();
 
+        /* Run the timers here.  This is much more efficient than
+         * waking up the I/O thread and waiting for completion.
+         */
+        handle_icount_deadline();
+
         if (!cpu) {
             cpu = first_cpu;
         }

cpus.c static void *qemu_tcg_rr_cpu_thread_fn(void *arg) · line 1383

Quoted 5 lines
cpus.c +0 -2
@@ -1379,8 +1401,6 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)
             atomic_mb_set(&cpu->exit_request, 0);
         }
 
-        handle_icount_deadline();
-

I guess we could just as easily move the handling to after qemu_tcg_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus))?

Quoted 5 lines
Quoted 3 lines

I noticed we still call handle_icount_deadline in the multi-thread case which is probably superfluous unless/until we figure out a way for it to work with MTTCG.

Should I remove the call? Add an assert(!use_icount)?

Yes pretty much that as an independent patch.

Quoted 76 lines
Quoted 67 lines
Quoted 67 lines
cpus.c +0 -0
         qemu_tcg_wait_io_event(cpu ? cpu : QTAILQ_FIRST(&cpus));
         deal_with_unplugged_cpus();
     }
include/qemu/timer.h +24 -0
index 1441b42..e1742f2 100644
@@ -533,6 +533,12 @@ static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
  * Create a new timer and associate it with the default
  * timer list for the clock type @type.
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Returns: a pointer to the timer
  */
 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
@@ -550,6 +556,12 @@ static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
  * Create a new timer with nanosecond scale on the default timer list
  * associated with the clock.
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Returns: a pointer to the newly created timer
  */
 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
@@ -564,6 +576,12 @@ static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
  * @cb: the callback to call when the timer expires
  * @opaque: the opaque pointer to pass to the callback
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Create a new timer with microsecond scale on the default timer list
  * associated with the clock.
  *
@@ -581,6 +599,12 @@ static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
  * @cb: the callback to call when the timer expires
  * @opaque: the opaque pointer to pass to the callback
  *
+ * The default timer list has one special feature: in icount mode,
+ * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
+ * not true of other timer lists, which are typically associated
+ * with an AioContext---each of them runs its timer callbacks in its own
+ * AioContext thread.
+ *
  * Create a new timer with millisecond scale on the default timer list
  * associated with the clock.
  *
util/qemu-timer.c +0 -0
index dc3181e..82d5650 100644
@@ -658,7 +658,9 @@ bool qemu_clock_run_all_timers(void)
     QEMUClockType type;
 
Quoted 8 lines

util/qemu-timer.c bool qemu_clock_run_all_timers(void) · line 663

Quoted 5 lines
util/qemu-timer.c +3 -1
     for (type = 0; type < QEMU_CLOCK_MAX; type++) {
-        progress |= qemu_clock_run_timers(type);
+        if (qemu_clock_use_for_deadline(type)) {
+            progress |= qemu_clock_run_timers(type);
+        }

minor nit: its not really qemu_clock_run_all_timers() now is it. We run all but the virtual timers (if icount is enabled).

Quoted 3 lines

Well yeah, it's all those that pass qemu_clock_use_for_deadline.

Paolo

Have you done any testing with record/replay? So far I have one reproducible run and one failure. However it is not entirely clear to me how I am meant to cleanly halt and stop a machine so I don't corrupt the replay log.

--
Alex Bennée

#20 lore raw

Re: [Qemu-devel] [PATCH 5/5] icount: process QEMU_CLOCK_VIRTUAL timers in vCPU thread

Quoted 6 lines · Alex Bennée
Quoted 1 line

Well yeah, it's all those that pass qemu_clock_use_for_deadline.

Have you done any testing with record/replay? So far I have one reproducible run and one failure. However it is not entirely clear to me how I am meant to cleanly halt and stop a machine so I don't corrupt the replay log.

No, I haven't tried RR.

Paolo

#21 lore raw

Re: [Qemu-devel] [PATCH 5/5] icount: process QEMU_CLOCK_VIRTUAL timers in vCPU thread

Quoted 11 lines · Paolo Bonzini
Quoted 6 lines · Alex Bennée
Quoted 1 line

Well yeah, it's all those that pass qemu_clock_use_for_deadline.

Have you done any testing with record/replay? So far I have one reproducible run and one failure. However it is not entirely clear to me how I am meant to cleanly halt and stop a machine so I don't corrupt the replay log.

No, I haven't tried RR.

Tried now, it doesn't work but I won't have time to fix it.

I tried "-kernel /path/to/bzImage -icount 3,rr=record,rrfile=replay.bin -net none". record works, replay hangs. The hang is at a "pause" instruction. With "-d in_asm" it still hangs, but a bit later (still in the firmware).

Paolo

#22 lore raw

Re: [Qemu-devel] [PATCH 5/5] icount: process QEMU_CLOCK_VIRTUAL timers in vCPU thread

Quoted 11 lines · Paolo Bonzini
Quoted 10 lines · Paolo Bonzini
Quoted 6 lines · Alex Bennée
Quoted 1 line

Well yeah, it's all those that pass qemu_clock_use_for_deadline.

Have you done any testing with record/replay? So far I have one reproducible run and one failure. However it is not entirely clear to me how I am meant to cleanly halt and stop a machine so I don't corrupt the replay log.

Quoted 8 lines
Quoted 1 line

No, I haven't tried RR.

Tried now, it doesn't work but I won't have time to fix it.

I tried "-kernel /path/to/bzImage -icount 3,rr=record,rrfile=replay.bin -net none". record works, replay hangs. The hang is at a "pause" instruction. With "-d in_asm" it still hangs, but a bit later (still in the firmware).

I'm happy to keep digging.

So to be clear when you do the record step how do you save your state before you replay?

I was just entering the MMI and typing quit but that seems to leave the log in a broken state.

--
Alex Bennée

#23 lore raw

Re: [Qemu-devel] [PATCH 5/5] icount: process QEMU_CLOCK_VIRTUAL timers in vCPU thread

Quoted 9 lines · Alex Bennée
Quoted 5 lines

I tried "-kernel /path/to/bzImage -icount 3,rr=record,rrfile=replay.bin -net none". record works, replay hangs. The hang is at a "pause" instruction. With "-d in_asm" it still hangs, but a bit later (still in the firmware).

I'm happy to keep digging.

So to be clear when you do the record step how do you save your state before you replay?

Here it hangs on the replay way before the end of the record, so it wasn't an issue.

Paolo

#24 lore raw
In reply to Paolo Bonzini

Paolo Bonzini <[email protected]> writes:

Hmm your subject line was:

Subject: [PATCH 0/6] tcg: fix icount super slowdown

But:

Quoted 6 lines

Paolo Bonzini (5):
qemu-timer: fix off-by-one
qemu-timer: do not include sysemu/cpus.h from util/qemu-timer.h
cpus: define QEMUTimerListNotifyCB for QEMU system emulation
main-loop: remove now unnecessary optimization
icount: process QEMU_CLOCK_VIRTUAL timers in vCPU thread

Did the tooling missing something or is this just a git-format-patch burp?

--
Alex Bennée

#25 lore raw
Quoted 7 lines · Alex Bennée

Hmm your subject line was:

Subject: [PATCH 0/6] tcg: fix icount super slowdown

But:

Quoted 7 lines
Quoted 6 lines

Paolo Bonzini (5):
qemu-timer: fix off-by-one
qemu-timer: do not include sysemu/cpus.h from util/qemu-timer.h
cpus: define QEMUTimerListNotifyCB for QEMU system emulation
main-loop: remove now unnecessary optimization
icount: process QEMU_CLOCK_VIRTUAL timers in vCPU thread

Did the tooling missing something or is this just a git-format-patch burp?

Patch 1 was the nios2 fix that I ended up posting as a separate thread.

Paolo