qqq: module for synchronizing with a simulation
PATCH v8 1/1 qqq: module for synchronizing with a simulation
This patch adds an interface for pacing the execution of QEMU to match an external simulation clock. Its aim is to permit QEMU to be used as a module within a larger simulation system.
v6 -> v7 Added a check for command line arguments that are consistent with -qqq and print an error message if incompatible arguments are provide. [Paolo]
v7 -> v8 Updated patch to track changes in the main branch of qemu development.
--- Makefile.target | 3 + cpus.c | 8 +++ docs/simulation-sync.txt | 59 ++++++++++++++++ include/sysemu/cpus.h | 1 + kvm-all.c | 10 +++ qemu-options.hx | 15 ++++ qqq.c | 177 +++++++++++++++++++++++++++++++++++++++++++++++ qqq.h | 37 ++++++++++ vl.c | 35 ++++++++++ 9 files changed, 345 insertions(+) create mode 100644 docs/simulation-sync.txt create mode 100644 qqq.c create mode 100644 qqq.h
Makefile.target +3 -0
@@ -149,6 +149,9 @@ obj-y += dump.o obj-y += migration/ram.o migration/savevm.o LIBS := $(libs_softmmu) $(LIBS) +# qqq support +obj-y += qqq.o + # xen support obj-$(CONFIG_XEN) += xen-common.o obj-$(CONFIG_XEN_I386) += xen-hvm.o xen-mapcache.o
cpus.c +8 -0
@@ -1765,3 +1765,11 @@ void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)cpu_fprintf (f,"Max guest advance NA\n" ); } } + +void kick_all_vcpus (void ) +{ +CPUState *cpu; +CPU_FOREACH (cpu) { +qemu_cpu_kick (cpu); + } +}
docs/simulation-sync.txt new file +59 -0
59 lines not on this page · raw message
include/sysemu/cpus.h +1 -0
@@ -2,6 +2,7 @@#define QEMU_CPUS_H /* cpus.c */ +void kick_all_vcpus (void );bool qemu_in_vcpu_thread (void );void qemu_init_cpu_loop (void );void resume_all_vcpus (void );
kvm-all.c +10 -0
@@ -18,6 +18,7 @@#include <linux/kvm.h> +#include "qqq.h" #include "qemu-common.h" #include "qemu/atomic.h" #include "qemu/option.h"
@@ -1926,6 +1927,15 @@ int kvm_cpu_exec(CPUState *cpu)qemu_cpu_kick_self (); } +if (qqq_enabled ()) { +/* Pause here while qqq is synchronizing with a simulation clock. +* We do not want to execute instructions past the synchronization +* deadline, but its ok to update the states of other equipment +* like timers, i/o devices, etc. +*/ +qqq_sync (); + } + run_ret =kvm_vcpu_ioctl (cpu,KVM_RUN ,0 ); attrs =kvm_arch_post_run (cpu, run);
qemu-options.hx +15 -0
@@ -4100,6 +4100,21 @@ contents of @code{iv.b64} to the second secret ETEXI +DEF("qqq", HAS_ARG, QEMU_OPTION_qqq, \ + "-qqq sock=fd\n" \ + " enable synchronization of the virtual clock \n" \ + " with an external simulation clock\n", QEMU_ARCH_ALL) +STEXI +@item -qqq sock=@var{fd0} +@findex -qqq +Qemu will use the supplied socket to synchronize its virtual clock with +an external simulation clock. Qemu will wait until a time slice size in +microseconds is supplied on the socket. Then it will execute for at +least that number of virtual microseconds before writing the actual +virtual time that has elapsed in microseconds to the socket. This +cycle will repeat until a zero time advance is requested, which +will cause qemu to exit. +ETEXI HXCOMM This is the last statement. Insert new options before this line! STEXI
qqq.c new file +177 -0
177 lines not on this page · raw message
qqq.h new file +37 -0
@@ -0,0 +1,37 @@ +/* +* This work is licensed under the terms of the GNU GPL +* version 2. Seethe COPYING file in the top-level directory. +* +* A module for pacing the rate of advance of the computer +* clock in reference to an external simulation clock. The +* basic approach used here is adapted from QBox from Green +* Socs. The mode of operation is as follows: +* +* The simulator uses pipes to exchange time advance data. +* The external simulator starts the exchange by forking a +* QEMU process and passing is descriptors for a read and +* write pipe. Then the external simulator writes an integer +* (native endian) to the pipe to indicate the number of +* microseconds that QEMU should advance. QEMU advances its +* virtual clock by this amount and writes to its write pipe +* the actual number of microseconds that have advanced. +* This process continues until a pipe on either side is +* closed, which will either cause QEMU to exit (if the +* external simulator closes a pipe) or raise SIGPIPE in the +* external simulator (if QEMU closes a pipe). +* +* Authors: +* James Nutaro <[email protected]> +* +*/ +#ifndef QQQ_H +#define QQQ_H + +#include "qemu/osdep.h" +#include "qemu-options.h" + +void qqq_sync (void ); +bool qqq_enabled (void ); +void setup_qqq (QemuOpts *opts); + +#endif
vl.c +35 -0
@@ -126,6 +126,8 @@ int main(int argc, char **argv)#include "qapi/qmp/qerror.h" #include "sysemu/iothread.h" +#include "qqq.h" +#define MAX_VIRTIO_CONSOLES 1#define MAX_SCLP_CONSOLES 1
@@ -237,6 +239,20 @@ static struct { { .driver = "virtio-vga", .flag = &default_vga }, }; +static QemuOptsList qemu_qqq_opts = { + .name ="qqq" , + .implied_opt_name ="" , + .merge_lists = true, + .head =QTAILQ_HEAD_INITIALIZER (qemu_qqq_opts.head), + .desc = { + { + .name ="sock" , + .type =QEMU_OPT_NUMBER , + }, + {/* end of list */ } + }, +}; +static QemuOptsList qemu_rtc_opts = { .name ="rtc" , .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
@@ -2940,6 +2956,7 @@ int main(int argc, char **argv, char **envp)DisplayState *ds;int cyls, heads, secs, translation;QemuOpts *hda_opts =NULL , *opts, *machine_opts, *icount_opts =NULL ; +QemuOpts *qqq_opts =NULL ;QemuOptsList *olist;int optind;const char *optarg;
@@ -2979,6 +2996,7 @@ int main(int argc, char **argv, char **envp)module_call_init (MODULE_INIT_QOM );module_call_init (MODULE_INIT_QAPI ); +qemu_add_opts (&qemu_qqq_opts);qemu_add_opts (&qemu_drive_opts);qemu_add_drive_opts (&qemu_legacy_drive_opts);qemu_add_drive_opts (&qemu_common_drive_opts);
@@ -3842,6 +3860,13 @@ int main(int argc, char **argv, char **envp)exit (1 ); }break ; +case QEMU_OPTION_qqq: + qqq_opts =qemu_opts_parse_noisily (qemu_find_opts ("qqq" ), + optarg, true); +if (!qqq_opts) { +exit (1 ); + } +break ;case QEMU_OPTION_incoming:if (!incoming) {runstate_set (RUN_STATE_INMIGRATE );
@@ -4355,6 +4380,16 @@ int main(int argc, char **argv, char **envp)/* spice needs the timers to be initialized by this point */ qemu_spice_init (); +if (qqq_opts) { +if (!(rtc_clock ==QEMU_CLOCK_VIRTUAL && ( + (icount_opts && !qemu_opt_get_bool (icount_opts,"sleep" , true)) || +kvm_enabled ()))) { +error_report ("-qqq requires options -rtc clock=vm and either " +"icount -1,sleep=off or -enable-kvm" ); +exit (1 ); + } +setup_qqq (qqq_opts); + }cpu_ticks_init ();if (icount_opts) {if (!tcg_enabled ()) {
Signature · 1 line
2.7.4
Quoted 3 lines · James J. Nutaro
This patch adds an interface for pacing the execution of QEMU to match an external simulation clock. Its aim is to permit QEMU to be used as a module within a larger simulation system.
(snip)
docs/simulation-sync.txt line 11
Quoted 13 lines
+++
b/docs/simulation-sync.txtdocs/simulation-sync.txt new file +11
-0@@ -0,0 +1,59 @@ += Synchronizing the virtual clock with an external source = + +QEMU has a protocol for synchronizing its virtual clock +with the clock of a simulator in which QEMU is embedded +as a component. This options is enabled with the -qqq +argument, and it should generally be accompanied by the +following additional command line arguments: + +-icount 1,sleep=off -rtc clock=vm + or +-enable-kvm -rtc clock=vm
Isn't this a KVM-only feature though? I don't see calls to qqq_sync() anywhere but from kvm-all.c, but maybe I'm missing something.
Also, a few more comments:
- Why "qqq" as the name for this feature? I fail to see any connection between the name and the feature.
- If Windows users ever wanted this feature, would we have to change the user-facing API (i.e. the -qqq parameter)?
- As is, isn't the code Unix-only rather than Linux-only?
Thanks,
Emilio
Thanks for the review Emilio.
Quoted 2 lines
Quoted 2 lines
Isn't this a KVM-only feature though? I don't see calls to
qqq_sync()anywhere but fromkvm-all.c, but maybe I'm missing something.
There is another call in the timer callback in qqq.c that gets invoked when using the -icount mode.
Quoted 4 lines
Quoted 4 lines
Also, a few more comments:
- Why "qqq" as the name for this feature? I fail to see any connection between the name and the feature.
There was a similar package called qbox, and I got stuck thinking of a name. So I somewhat unimaginatively just stuttered on the q. I'm open to a better name.
Quoted 2 lines
Quoted 2 lines
If Windows users ever wanted this feature, would we have to
change the user-facing API (i.e. the -qqq parameter)?
It will (or should) ignore the -qqq option on Windows. But I'll double check that.
Quoted 1 line
Quoted 1 line
As is, isn't the code Unix-only rather than Linux-only?
Yes, Unix-only. You are correct.
Jim
________________________________________
From: Emilio G. Cota <[email protected]>
Sent: Friday, June 02, 2017 10:42 AM
To: Nutaro, James J.
Cc: [email protected]; [email protected]
Subject: Re: [Qemu-devel] [PATCH V8] qqq: module for synchronizing with a simulation
Quoted 3 lines · James J. Nutaro
This patch adds an interface for pacing the execution of QEMU to match an external simulation clock. Its aim is to permit QEMU to be used as a module within a larger simulation system.
(snip)
docs/simulation-sync.txt line 11
Quoted 13 lines
+++
b/docs/simulation-sync.txtdocs/simulation-sync.txt new file +11
-0@@ -0,0 +1,59 @@ += Synchronizing the virtual clock with an external source = + +QEMU has a protocol for synchronizing its virtual clock +with the clock of a simulator in which QEMU is embedded +as a component. This options is enabled with the -qqq +argument, and it should generally be accompanied by the +following additional command line arguments: + +-icount 1,sleep=off -rtc clock=vm + or +-enable-kvm -rtc clock=vm
Isn't this a KVM-only feature though? I don't see calls to qqq_sync() anywhere but from kvm-all.c, but maybe I'm missing something.
Also, a few more comments:
- Why "qqq" as the name for this feature? I fail to see any connection between the name and the feature.
- If Windows users ever wanted this feature, would we have to change the user-facing API (i.e. the -qqq parameter)?
- As is, isn't the code Unix-only rather than Linux-only?
Thanks,
Emilio
From: "James J. Nutaro" <[email protected]>
This revision of the patch fixes and error in which a callback is registered to act read the synchronization socket when qemu indicates it has data. This causes a deadlock and the callback was removed.
James J. Nutaro (1):
Synchronize the qemu clock with an external simulation clock
Makefile.target | 3 + cpus.c | 8 +++ docs/simulation-sync.txt | 59 +++++++++++++++++ include/sysemu/cpus.h | 1 + kvm-all.c | 10 +++ qemu-options.hx | 15 +++++ qqq.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++ qqq.h | 37 +++++++++++ vl.c | 35 ++++++++++ 9 files changed, 330 insertions(+) create mode 100644 docs/simulation-sync.txt create mode 100644 qqq.c create mode 100644 qqq.h
Signature · 1 line
2.7.4
Quoted 1 line
From
0b0d02c755249bd18aa09bf44925e2ec5954064dMon Sep 17 00:00:00 2001
In-Reply-To: <[email protected]>
References: <[email protected]>
From: "James J. Nutaro" <[email protected]>
Date: Tue, 19 Sep 2017 17:00:03 -0400
Subject: [PATCH] Synchronize the qemu clock with an external simulation clock
--- Makefile.target | 3 + cpus.c | 8 +++ docs/simulation-sync.txt | 59 +++++++++++++++++ include/sysemu/cpus.h | 1 + kvm-all.c | 10 +++ qemu-options.hx | 15 +++++ qqq.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++ qqq.h | 37 +++++++++++ vl.c | 35 ++++++++++ 9 files changed, 330 insertions(+) create mode 100644 docs/simulation-sync.txt create mode 100644 qqq.c create mode 100644 qqq.h
Makefile.target +3 -0
@@ -149,6 +149,9 @@ obj-y += dump.o obj-y += migration/ram.o migration/savevm.o LIBS := $(libs_softmmu) $(LIBS) +# qqq support +obj-y += qqq.o + # xen support obj-$(CONFIG_XEN) += xen-common.o obj-$(CONFIG_XEN_I386) += xen-hvm.o xen-mapcache.o
cpus.c +8 -0
@@ -2020,3 +2020,11 @@ void dump_drift_info(FILE *f, fprintf_function cpu_fprintf)cpu_fprintf (f,"Max guest advance NA\n" ); } } + +void kick_all_vcpus (void ) +{ +CPUState *cpu; +CPU_FOREACH (cpu) { +qemu_cpu_kick (cpu); + } +}
docs/simulation-sync.txt new file +59 -0
59 lines not on this page · raw message
include/sysemu/cpus.h +1 -0
@@ -10,6 +10,7 @@ void resume_all_vcpus(void);void pause_all_vcpus (void );void cpu_stop_current (void );void cpu_ticks_init (void ); +void kick_all_vcpus (void );void configure_icount (QemuOpts *opts,Error **errp);extern int use_icount;
kvm-all.c +10 -0
@@ -18,6 +18,7 @@#include <linux/kvm.h> +#include "qqq.h" #include "qemu-common.h" #include "qemu/atomic.h" #include "qemu/option.h"
@@ -1984,6 +1985,15 @@ int kvm_cpu_exec(CPUState *cpu)kvm_cpu_kick_self (); } +if (qqq_enabled ()) { +/* Pause here while qqq is synchronizing with a simulation clock. +* We do not want to execute instructions past the synchronization +* deadline, but its ok to update the states of other equipment +* like timers, i/o devices, etc. +*/ +qqq_sync (); + } +/* Read cpu->exit_request before KVM_RUN reads run->immediate_exit. * Matching barrier in kvm_eat_signals. */
qemu-options.hx +15 -0
@@ -4152,6 +4152,21 @@ contents of @code{iv.b64} to the second secret ETEXI +DEF("qqq", HAS_ARG, QEMU_OPTION_qqq, \ + "-qqq sock=fd\n" \ + " enable synchronization of the virtual clock \n" \ + " with an external simulation clock\n", QEMU_ARCH_ALL) +STEXI +@item -qqq sock=@var{fd0} +@findex -qqq +Qemu will use the supplied socket to synchronize its virtual clock with +an external simulation clock. Qemu will wait until a time slice size in +microseconds is supplied on the socket. Then it will execute for at +least that number of virtual microseconds before writing the actual +virtual time that has elapsed in microseconds to the socket. This +cycle will repeat until a zero time advance is requested, which +will cause qemu to exit. +ETEXI HXCOMM This is the last statement. Insert new options before this line! STEXI
qqq.c new file +162 -0
162 lines not on this page · raw message
qqq.h new file +37 -0
@@ -0,0 +1,37 @@ +/* +* This work is licensed under the terms of the GNU GPL +* version 2. Seethe COPYING file in the top-level directory. +* +* A module for pacing the rate of advance of the computer +* clock in reference to an external simulation clock. The +* basic approach used here is adapted from QBox from Green +* Socs. The mode of operation is as follows: +* +* The simulator uses pipes to exchange time advance data. +* The external simulator starts the exchange by forking a +* QEMU process and passing is descriptors for a read and +* write pipe. Then the external simulator writes an integer +* (native endian) to the pipe to indicate the number of +* microseconds that QEMU should advance. QEMU advances its +* virtual clock by this amount and writes to its write pipe +* the actual number of microseconds that have advanced. +* This process continues until a pipe on either side is +* closed, which will either cause QEMU to exit (if the +* external simulator closes a pipe) or raise SIGPIPE in the +* external simulator (if QEMU closes a pipe). +* +* Authors: +* James Nutaro <[email protected]> +* +*/ +#ifndef QQQ_H +#define QQQ_H + +#include "qemu/osdep.h" +#include "qemu-options.h" + +void qqq_sync (void ); +bool qqq_enabled (void ); +void setup_qqq (QemuOpts *opts); + +#endif
vl.c +35 -0
@@ -130,6 +130,8 @@ int main(int argc, char **argv)#include "qapi/qmp/qerror.h" #include "sysemu/iothread.h" +#include "qqq.h" +#define MAX_VIRTIO_CONSOLES 1#define MAX_SCLP_CONSOLES 1
@@ -242,6 +244,20 @@ static struct { { .driver = "virtio-vga", .flag = &default_vga }, }; +static QemuOptsList qemu_qqq_opts = { + .name ="qqq" , + .implied_opt_name ="" , + .merge_lists = true, + .head =QTAILQ_HEAD_INITIALIZER (qemu_qqq_opts.head), + .desc = { + { + .name ="sock" , + .type =QEMU_OPT_NUMBER , + }, + {/* end of list */ } + }, +}; +static QemuOptsList qemu_rtc_opts = { .name ="rtc" , .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
@@ -2951,6 +2967,7 @@ int main(int argc, char **argv, char **envp)int cyls, heads, secs, translation;QemuOpts *opts, *machine_opts;QemuOpts *hda_opts =NULL , *icount_opts =NULL , *accel_opts =NULL ; +QemuOpts *qqq_opts =NULL ;QemuOptsList *olist;int optind;const char *optarg;
@@ -2997,6 +3014,7 @@ int main(int argc, char **argv, char **envp)module_call_init (MODULE_INIT_QOM );monitor_init_qmp_commands (); +qemu_add_opts (&qemu_qqq_opts);qemu_add_opts (&qemu_drive_opts);qemu_add_drive_opts (&qemu_legacy_drive_opts);qemu_add_drive_opts (&qemu_common_drive_opts);
@@ -3904,6 +3922,13 @@ int main(int argc, char **argv, char **envp)exit (1 ); }break ; +case QEMU_OPTION_qqq: + qqq_opts =qemu_opts_parse_noisily (qemu_find_opts ("qqq" ), + optarg, true); +if (!qqq_opts) { +exit (1 ); + } +break ;case QEMU_OPTION_incoming:if (!incoming) {runstate_set (RUN_STATE_INMIGRATE );
@@ -4417,6 +4442,16 @@ int main(int argc, char **argv, char **envp)/* spice needs the timers to be initialized by this point */ qemu_spice_init (); +if (qqq_opts) { +if (!(rtc_clock ==QEMU_CLOCK_VIRTUAL && ( + (icount_opts && !qemu_opt_get_bool (icount_opts,"sleep" , true)) || +kvm_enabled ()))) { +error_report ("-qqq requires options -rtc clock=vm and either " +"icount -1,sleep=off or -enable-kvm" ); +exit (1 ); + } +setup_qqq (qqq_opts); + }cpu_ticks_init ();if (icount_opts) {if (!tcg_enabled ()) {
Signature · 1 line
2.7.4
Quoted 9 lines · On Tue, Sep 19, 2017 at 17:43:06 -0400, [email protected] wrote:
+= Synchronizing the virtual clock with an external source =
docs/simulation-sync.txt new file +8
-0+ +QEMU has a protocol for synchronizing its virtual clock +with the clock of a simulator in which QEMU is embedded +as a component. This options is enabled with the -qqq +argument, and it should generally be accompanied by the +following additional command line arguments: + +-icount 1,sleep=off -rtc clock=vm
docs/simulation-sync.txt line 19
Quoted 10 lines
docs/simulation-sync.txt new file +10
-0+ or +-enable-kvm -rtc clock=vm + +The -qqq argument is used to supply a file descriptor +for a Unix socket, which is used for synchronization. +The procedure for launching QEMU in is synchronization +mode has three steps: + +(1) Create a socket pair with the Linux socketpair function. + The code segment that does this might look like
I thought a little bit about this patch. I think the feature is useful and should eventually be provided by QEMU.
The part I'm not sure about is the socket interface; for instance, it isn't cross-platform.
A better alternative might be to expose this functionality to plugins, and let the plugins deal with the external world--socket or whatever IPC is appropriate on the platform they're meant to be run.
Of course the plugin interface is still under discussion. I think though it should be fairly easy to expose this functionality to plugins. I'll take a look at in the next few weeks, hopefully also supporting MTTCG instances (with some collaboration/hack from the plugin, I'm afraid).
Thanks,
Emilio