qqq: module for synchronizing with a simulation clock
PATCH 1/1 qqq: module for synchronizing with a simulation clock
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.
Signed-off-by James J. Nutaro <[email protected]>
--- Makefile.target | 2 +- cpus.c | 13 ++++++- docs/simulation-sync.txt | 61 ++++++++++++++++++++++++++++++ include/qemu/thread.h | 11 ++++++ qemu-options.hx | 17 +++++++++ qqq.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ qqq.h | 39 +++++++++++++++++++ util/qemu-thread-posix.c | 16 ++++++++ vl.c | 35 ++++++++++++++++- 9 files changed, 288 insertions(+), 4 deletions(-) create mode 100644 docs/simulation-sync.txt create mode 100644 qqq.c create mode 100644 qqq.h
Makefile.target +1 -1
@@ -131,7 +131,7 @@ endif #CONFIG_BSD_USER ######################################################### # System emulator target ifdef CONFIG_SOFTMMU -obj-y += arch_init.o cpus.o monitor.o gdbstub.o balloon.o ioport.o numa.o +obj-y += arch_init.o qqq.o cpus.o monitor.o gdbstub.o balloon.o ioport.o numa.o obj-y += qtest.o bootdevice.o obj-y += hw/ obj-$(CONFIG_KVM) += kvm-all.o
cpus.c +11 -2
@@ -44,6 +44,8 @@#include "hw/nmi.h" #include "sysemu/replay.h" +#include "qqq.h" +#ifndef _WIN32#include "qemu/compatfd.h" #endif
@@ -995,10 +997,17 @@ static void qemu_wait_io_event_common(CPUState *cpu)static void qemu_tcg_wait_io_event (CPUState *cpu) {while (all_cpu_threads_idle ()) { -/* Start accounting real time to the virtual clock if the CPUs +/* Start accounting real time to the virtual clock if the CPUs are idle. */ qemu_clock_warp (QEMU_CLOCK_VIRTUAL ); -qemu_cond_wait (cpu->halt_cond, &qemu_global_mutex); +/* If qqq is running then this wait must timeout so that the +* simulation does not become stuck when the cpu idles */ +if (qqq_enabled ()) { +qemu_cond_wait_timeout_ns (cpu->halt_cond, + &qemu_global_mutex,10000 ); + }else { +qemu_cond_wait (cpu->halt_cond, &qemu_global_mutex); + } }while (iothread_requesting_mutex) {
docs/simulation-sync.txt new file +61 -0
61 lines not on this page · raw message
include/qemu/thread.h +11 -0
@@ -36,6 +36,17 @@ void qemu_cond_destroy(QemuCond *cond);void qemu_cond_signal (QemuCond *cond);void qemu_cond_broadcast (QemuCond *cond);void qemu_cond_wait (QemuCond *cond,QemuMutex *mutex); +/* This defaults to qemu_cond_wait() on Windows */ +#ifndef _WIN32 +void qemu_cond_wait_timeout_ns (QemuCond *cond,QemuMutex *mutex, +int64_t timeout_ns); +#else +void qemu_cond_wait_timeout_ns (QemuCond *cond,QemuMutex *mutex, +int64_t timeout_ns) +{ +qemu_cond_wait (cond, mutex); +} +#endif void qemu_sem_init (QemuSemaphore *sem,int init);void qemu_sem_post (QemuSemaphore *sem);
qemu-options.hx +17 -0
@@ -3225,6 +3225,23 @@ many timer interrupts were not processed by the Windows guest and will re-inject them. ETEXI +DEF("qqq", HAS_ARG, QEMU_OPTION_qqq, \ + "-qqq read=fd,write=fd\n" \ + " enable synchronization of the virtual clock \n" \ + " with an external simulation clock\n", QEMU_ARCH_ALL) +STEXI +@item -qqq read=@var{fd0},write=@var{fd1} +@findex -qqq +Qemu will use the supplied pipes to synchronize its virtual clock with +an external simulation clock. This requires the option +'icount 1,sleep=off'. Qemu will wait until a time slice size in +microseconds is supplied on the read pipe. 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 write pipe. This +cycle will repeat until a zero is elaspsed time is requested, which +will cause qemu to exit. +ETEXI + DEF("icount", HAS_ARG, QEMU_OPTION_icount, \ "-icount [shift=N|auto][,align=on|off][,sleep=no,rr=record|replay,rrfile=<filename>]\n" \ " enable virtual instruction counter with 2^N clock ticks per\n" \
qqq.c new file +98 -0
98 lines not on this page · raw message
qqq.h new file +39 -0
@@ -0,0 +1,39 @@ +/* +* 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 setup_qqq (QemuOpts *opts); +/* Returns true if qqq is enabled and false otherwise. +* It will be enabled only if setup_qqq() is able to +* attach to the shared memory segment. */ +bool qqq_enabled (void ); + +#endif
util/qemu-thread-posix.c +16 -0
@@ -134,6 +134,22 @@ void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)error_exit (err, __func__); } +void qemu_cond_wait_timeout_ns (QemuCond *cond,QemuMutex *mutex, +int64_t timeout_ns) +{ +static const long ns_sec =1000000000 ; +struct timeval tv; +struct timespec ts; +int err; +gettimeofday (&tv,NULL ); + ts.tv_sec = tv.tv_sec + (timeout_ns + (tv.tv_usec *1000 )) / ns_sec; + ts.tv_nsec = (timeout_ns + (tv.tv_usec *1000 )) % ns_sec; + err =pthread_cond_timedwait (&cond->cond, &mutex->lock, &ts); +if (err !=0 && err !=ETIMEDOUT ) { +error_exit (err, __func__); + } +} +void qemu_sem_init (QemuSemaphore *sem,int init) {int rc;
vl.c +34 -1
@@ -125,6 +125,8 @@ int main(int argc, char **argv)#include "sysemu/replay.h" #include "qapi/qmp/qerror.h" +#include "qqq.h" +#define MAX_VIRTIO_CONSOLES 1#define MAX_SCLP_CONSOLES 1
@@ -486,6 +488,23 @@ static QemuOptsList qemu_icount_opts = { }, }; +static QemuOptsList qemu_qqq_opts = { + .name ="qqq" , + .implied_opt_name ="" , + .merge_lists = true, + .head =QTAILQ_HEAD_INITIALIZER (qemu_qqq_opts.head), + .desc = { + { + .name ="read" , + .type =QEMU_OPT_NUMBER , + }, { + .name ="write" , + .type =QEMU_OPT_NUMBER , + }, + {/* end of list */ } + }, +}; +static QemuOptsList qemu_semihosting_config_opts = { .name ="semihosting-config" , .implied_opt_name ="enable" ,
@@ -2969,7 +2988,8 @@ int main(int argc, char **argv, char **envp)const char *boot_once =NULL ;DisplayState *ds;int cyls, heads, secs, translation; -QemuOpts *hda_opts =NULL , *opts, *machine_opts, *icount_opts =NULL ; +QemuOpts *hda_opts =NULL , *opts, *machine_opts, + *icount_opts =NULL , *qqq_opts =NULL ;QemuOptsList *olist;int optind;const char *optarg;
@@ -3031,6 +3051,7 @@ int main(int argc, char **argv, char **envp)qemu_add_opts (&qemu_name_opts);qemu_add_opts (&qemu_numa_opts);qemu_add_opts (&qemu_icount_opts); +qemu_add_opts (&qemu_qqq_opts);qemu_add_opts (&qemu_semihosting_config_opts);qemu_add_opts (&qemu_fw_cfg_opts);
@@ -3868,6 +3889,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 );
@@ -4385,6 +4413,11 @@ int main(int argc, char **argv, char **envp)/* spice needs the timers to be initialized by this point */ qemu_spice_init ();#endif +/* try to setup the qqq interface for syncing advance of the +* virtual clock with an external simulator */ +if (qqq_opts && icount_opts) { +setup_qqq (qqq_opts); + }cpu_ticks_init ();if (icount_opts) {
Signature · 1 line
2.5.0