Skip to contentSkip to conversation

qqq: module for synchronizing with a simulation

PATCH Superseded James J. Nutaro 2 participants 6 messages Jan 5, 2017 – Jan 17, 2017 Kbuild

v6 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 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.

---
 Makefile.target          |   3 +
 cpus.c                   |   8 +++
 docs/simulation-sync.txt |  57 +++++++++++++++
 include/sysemu/cpus.h    |   1 +
 kvm-all.c                |  10 +++
 qemu-options.hx          |  16 +++++
 qqq.c                    | 177 +++++++++++++++++++++++++++++++++++++++++++++++
 qqq.h                    |  37 ++++++++++
 vl.c                     |  29 ++++++++
 9 files changed, 338 insertions(+)
 create mode 100644 docs/simulation-sync.txt
 create mode 100644 qqq.c
 create mode 100644 qqq.h
Makefile.target +3 -0
index 8ae82cb..0a08fd3 100644
@@ -145,6 +145,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
index 5213351..8a98d7f 100644
@@ -1688,3 +1688,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 +57 -0

57 lines not on this page · raw message

include/sysemu/cpus.h +1 -0
index 3728a1e..bdf22c9 100644
@@ -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
index 330219e..b66b07f 100644
@@ -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 +16 -0
index c534a2f..46f12e0 100644
@@ -3389,6 +3389,22 @@ many timer interrupts were not processed by the Windows guest and will
 re-inject them.
 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
+
 DEF("icount", HAS_ARG, QEMU_OPTION_icount, \
     "-icount [shift=N|auto][,align=on|off][,sleep=on|off,rr=record|replay,rrfile=<filename>]\n" \
     "                enable virtual instruction counter with 2^N clock ticks per\n" \
qqq.c new file +177 -0

177 lines not on this page · raw message

qqq.h new file +37 -0
new file mode 100644
index 0000000..e106d3c
@@ -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 +29 -0
index d77dd86..21331fe 100644
@@ -124,6 +124,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
 
@@ -234,6 +236,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),
@@ -3005,6 +3021,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;
@@ -3044,6 +3061,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);
@@ -3908,6 +3926,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);
@@ -4416,6 +4441,10 @@ int main(int argc, char **argv, char **envp)
     /* spice needs the timers to be initialized by this point */
     qemu_spice_init();
 
+    if (qqq_opts) {
+        setup_qqq(qqq_opts);
+    }
+
     cpu_ticks_init();
     if (icount_opts) {
         if (kvm_enabled() || xen_enabled()) {
Signature · 1 line
2.7.4
#1 lore raw
Quoted 5 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.

What's the difference between v5 and v6?

docs/simulation-sync.txt line 25

Quoted 4 lines
docs/simulation-sync.txt new file +4 -0
+(2) Fork QEMU with the appropriate command line arguments.
+    The -qqq part of the argument will look something like
+
+       -qqq sock=socks[1]

No need for -qqq, just make it "-icount extclock=FD".

Paolo

#2 lore raw

V5 failed to build with MinGW, so I had to move the arpa/inet.h to a place inside the #ifndef block in qqq.c. Also fixed some formatting problems. Otherwise they are the same. Thanks for your reviews!

Jim

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Thursday, January 05, 2017 10:27 AM
To: Nutaro, James J.; [email protected]
Subject: Re: [PATCH v6] qqq: module for synchronizing with a simulation

Quoted 5 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.

What's the difference between v5 and v6?

docs/simulation-sync.txt line 25

Quoted 4 lines
docs/simulation-sync.txt new file +4 -0
+(2) Fork QEMU with the appropriate command line arguments.
+    The -qqq part of the argument will look something like
+
+       -qqq sock=socks[1]

No need for -qqq, just make it "-icount extclock=FD".

Paolo

#3 lore raw
In reply to Paolo Bonzini

Thanks again Paolo. When I change the command line switches, is it best to submit a whole new version of the patch? Or is there another method for managing patch revisions?

Thanks,

Jim

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Thursday, January 05, 2017 10:27 AM
To: Nutaro, James J.; [email protected]
Subject: Re: [PATCH v6] qqq: module for synchronizing with a simulation

Quoted 5 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.

What's the difference between v5 and v6?

docs/simulation-sync.txt line 25

Quoted 4 lines
docs/simulation-sync.txt new file +4 -0
+(2) Fork QEMU with the appropriate command line arguments.
+    The -qqq part of the argument will look something like
+
+       -qqq sock=socks[1]

No need for -qqq, just make it "-icount extclock=FD".

Paolo

#4 lore raw
Quoted 1 line · James J.

Thanks again Paolo. When I change the command line switches, is it best to submit a whole new version of the patch? Or is there another method for managing patch revisions?

Yes, submit a new version, then put

v6->v7: did this and that [Paolo]

at the end of the commit message, where the square brackets mean "suggested by...".

Thanks,

Paolo

Quoted 27 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Thursday, January 05, 2017 10:27 AM
To: Nutaro, James J.; [email protected]
Subject: Re: [PATCH v6] qqq: module for synchronizing with a simulation

Quoted 5 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.

What's the difference between v5 and v6?

docs/simulation-sync.txt line 25

Quoted 4 lines
docs/simulation-sync.txt new file +4 -0
+(2) Fork QEMU with the appropriate command line arguments.
+    The -qqq part of the argument will look something like
+
+       -qqq sock=socks[1]

No need for -qqq, just make it "-icount extclock=FD".

Paolo

#5 lore raw

Paolo,

There are several switches that need to be set for this module to work, and so - inspired by your comment - I decided to add a check for the necessary supporting switches before enabling the module. An error message is printed if everything is not as it should. I submitted the patch as v7.

Thanks for taking the time to review this.

Jim

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Monday, January 09, 2017 12:10 PM
To: Nutaro, James J.; [email protected]
Subject: Re: [PATCH v6] qqq: module for synchronizing with a simulation

Quoted 1 line · James J.

Thanks again Paolo. When I change the command line switches, is it best to submit a whole new version of the patch? Or is there another method for managing patch revisions?

Yes, submit a new version, then put

v6->v7: did this and that [Paolo]

at the end of the commit message, where the square brackets mean "suggested by...".

Thanks,

Paolo

Quoted 27 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Thursday, January 05, 2017 10:27 AM
To: Nutaro, James J.; [email protected]
Subject: Re: [PATCH v6] qqq: module for synchronizing with a simulation

Quoted 5 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.

What's the difference between v5 and v6?

docs/simulation-sync.txt line 25

Quoted 4 lines
docs/simulation-sync.txt new file +4 -0
+(2) Fork QEMU with the appropriate command line arguments.
+    The -qqq part of the argument will look something like
+
+       -qqq sock=socks[1]

No need for -qqq, just make it "-icount extclock=FD".

Paolo