qqq: module for synchronizing with a simulation clock
PATCH v4 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.
--- Makefile.objs | 1 + Makefile.target | 3 + cpus.c | 8 +++ docs/simulation-sync.txt | 59 +++++++++++++++++++ freezer.c | 43 ++++++++++++++ include/qemu/timer.h | 36 ++++++++++-- include/sysemu/cpus.h | 1 + kvm-all.c | 14 +++++ qemu-options.hx | 16 +++++ qqq.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++ qqq.h | 37 ++++++++++++ vl.c | 32 ++++++++++ 12 files changed, 394 insertions(+), 6 deletions(-) create mode 100644 docs/simulation-sync.txt create mode 100644 freezer.c create mode 100644 qqq.c create mode 100644 qqq.h
Makefile.objs +1 -0
@@ -3,6 +3,7 @@ stub-obj-y = stubs/ crypto/ util-obj-y = util/ qobject/ qapi/ util-obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o +util-obj-y += freezer.o ####################################################################### # block-obj-y is code used by both qemu system emulation and qemu-img
Makefile.target +3 -0
@@ -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
@@ -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 +59 -0
59 lines not on this page · raw message
freezer.c new file +43 -0
43 lines not on this page · raw message
include/qemu/timer.h +30 -6
@@ -795,13 +795,25 @@ static inline int64_t get_max_clock_jump(void)* Low level clock functions */ +/* +* Freeze time by subtracting frozen duration from real interval measurements. +*/ +void freeze_time (void ); +void thaw_time (void ); +int64_t get_iced_ticks (void ); +int64_t get_iced_ns (void ); +int64_t ns_is_frozen (void ); +int64_t ticks_is_frozen (void ); +/* get host real time in nanosecond */ static inline int64_t get_clock_realtime (void ) {struct timeval tv; - +if (ns_is_frozen () >0 ) { +return ns_is_frozen (); + }gettimeofday (&tv,NULL ); -return tv.tv_sec *1000000000LL + (tv.tv_usec *1000 ); +return ((tv.tv_sec *1000000000LL + (tv.tv_usec *1000 ))-get_iced_ns ()); }/* Warning: don't insert tracepoints into these functions, they are
@@ -826,8 +838,11 @@ static inline int64_t get_clock(void)#ifdef CLOCK_MONOTONIC if (use_rt_clock) {struct timespec ts; +if (ns_is_frozen () >0 ) { +return ns_is_frozen (); + }clock_gettime (CLOCK_MONOTONIC , &ts); -return ts.tv_sec *1000000000LL + ts.tv_nsec; +return (ts.tv_sec *1000000000LL + ts.tv_nsec)-get_iced_ns (); } else#endif {
@@ -878,8 +893,11 @@ static inline int64_t cpu_get_host_ticks(void)static inline int64_t cpu_get_host_ticks (void ) {int64_t val; +if (ticks_is_frozen () >0 ) { +return ticks_is_frozen (); + } asmvolatile ("rdtsc" :"=A" (val)); -return val; +return val-get_iced_ticks (); }#elif defined(__x86_64__)
@@ -888,11 +906,14 @@ static inline int64_t cpu_get_host_ticks(void) {uint32_t low,high;int64_t val; +if (ticks_is_frozen () >0 ) { +return ticks_is_frozen (); + } asmvolatile ("rdtsc" :"=a" (low),"=d" (high)); val = high; val <<=32 ; val |= low; -return val; +return val-get_iced_ticks (); }#elif defined(__hppa__)
@@ -909,8 +930,11 @@ static inline int64_t cpu_get_host_ticks(void)static inline int64_t cpu_get_host_ticks (void ) {int64_t val; +if (ticks_is_frozen () >0 ) { +return ticks_is_frozen (); + } asmvolatile ("mov %0 = ar.itc" :"=r" (val) ::"memory" ); -return val; +return val-get_iced_ticks (); }#elif defined(__s390__)
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 +14 -0
@@ -16,8 +16,11 @@#include "qemu/osdep.h" #include <sys/ioctl.h> +#include <sys/syscall.h> +#include <time.h> #include <linux/kvm.h> +#include "qqq.h" #include "qemu-common.h" #include "qemu/atomic.h" #include "qemu/option.h"
@@ -1926,6 +1929,17 @@ 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. Allowing this seems to avoid +* some timing problems. (Because it gives the HPET unit time to +* catch up with the VCPU while the VCPU is paused?) +*/ +qqq_sync (); + } + run_ret =kvm_vcpu_ioctl (cpu,KVM_RUN ,0 ); attrs =kvm_arch_post_run (cpu, run);
qemu-options.hx +16 -0
@@ -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 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. 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=on|off,rr=record|replay,rrfile=<filename>]\n" \ " enable virtual instruction counter with 2^N clock ticks per\n" \
qqq.c new file +150 -0
150 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 +32 -0
@@ -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,23 @@ 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 ="read" , + .type =QEMU_OPT_NUMBER , + }, { + .name ="write" , + .type =QEMU_OPT_NUMBER , + }, + {/* end of list */ } + }, +}; +static QemuOptsList qemu_rtc_opts = { .name ="rtc" , .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
@@ -3005,6 +3024,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 +3064,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 +3929,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 +4444,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
Your series seems to have some coding style problems. See output below for more information:
Hi,
Your series seems to have some coding style problems. See output below for more information:
Message-id: [email protected]
Type: series
Subject: [Qemu-devel] [PATCH v4] qqq: module for synchronizing with a simulation clock
=== TEST SCRIPT BEGIN ===
#!/bin/bash
BASE=base
n=1
total=$(git log --oneline $BASE.. | wc -l)
failed=0
# Useful git options
git config --local diff.renamelimit 0
git config --local diff.renames True
commits="$(git log --format=%H --reverse $BASE..)" for c in $commits; do
echo "Checking PATCH $n/$total: $(git log -n 1 --format=%s $c)..."
if ! git show $c --format=email | ./scripts/checkpatch.pl --mailback -; then
failed=1
echo
fi
n=$((n+1))done
exit $failed
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384 From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
62b7e47 qqq: module for synchronizing with a simulation clock
=== OUTPUT BEGIN ===
Checking PATCH 1/1: qqq: module for synchronizing with a simulation clock...
ERROR: spaces required around that '-' (ctx:VxV)
#197: FILE: include/qemu/timer.h:816:
+ return ((tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000))-get_iced_ns());
^ERROR: return is not a function, parentheses are not required #197: FILE: include/qemu/timer.h:816:
+ return ((tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000))-get_iced_ns());
ERROR: spaces required around that '-' (ctx:VxV)
#210: FILE: include/qemu/timer.h:845:
+ return (ts.tv_sec * 1000000000LL + ts.tv_nsec)-get_iced_ns();
^ERROR: suspect code indent for conditional statements (4, 9) #218: FILE: include/qemu/timer.h:896:
+ if (ticks_is_frozen() > 0) {
+ return ticks_is_frozen();
ERROR: spaces required around that '-' (ctx:VxV)
#223: FILE: include/qemu/timer.h:900:
+ return val-get_iced_ticks();
^ERROR: suspect code indent for conditional statements (4, 9) #231: FILE: include/qemu/timer.h:909:
+ if (ticks_is_frozen() > 0) {
+ return ticks_is_frozen();
ERROR: spaces required around that '-' (ctx:VxV)
#239: FILE: include/qemu/timer.h:916:
+ return val-get_iced_ticks();
^ERROR: suspect code indent for conditional statements (4, 9) #247: FILE: include/qemu/timer.h:933:
+ if (ticks_is_frozen() > 0) {
+ return ticks_is_frozen();
ERROR: spaces required around that '-' (ctx:VxV)
#252: FILE: include/qemu/timer.h:937:
+ return val-get_iced_ticks();
^ERROR: do not initialise statics to 0 or NULL
#349: FILE: qqq.c:15:
+static bool enabled = false, syncing = false;
ERROR: do not use C99 // comments
#350: FILE: qqq.c:16:
+static int elapsed; // This must be zero on initialization
ERROR: trailing whitespace
#405: FILE: qqq.c:71:
+ if (time_advance < 0) $
ERROR: braces {} are necessary for all arms of this statement #405: FILE: qqq.c:71:
+ if (time_advance < 0)
[...]
WARNING: line over 80 characters
#425: FILE: qqq.c:91:
+ /* Set the sync flag that will cause the vcpu to wait for synchronization
total: 13 errors, 1 warnings, 519 lines checked
Your patch has style problems, please review. If any of these errors are false positives report them to the maintainer, see CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
---
Email generated automatically by Patchew [http://patchew.org/].
Please send your feedback to [email protected]
docs/simulation-sync.txt line 29
Quoted 12 lines · James J. Nutaro
docs/simulation-sync.txt new file +12
-0+(1) Create two pairs of pipes with the Linux pipe function. + The code segment that does this might look like + + int pipefd1[2]; + int pipefd2[2]; + pipe(pipefd1); + pipe(pipefd2); + +(2) Fork QEMU with the appropriate command line arguments. + The -qqq part of the argument will look something like + + -qqq write=pipefd1[1],read=pipefd2[0]
If you need a pair of pipes, you might as well use a Unix socket or even a generic chardev. This would give something like
-chardev socket,path=extclock.socket \ -icount 1,extclock=chardev
docs/simulation-sync.txt line 40
Quoted 7 lines
docs/simulation-sync.txt new file +7
-0+The synchronization protocol is very simple. To start, the +external simulator writes an integer to its write pipe with +the amount of time in microseconds that QEMU is allowed to +advance. The code segment that does this might look like: + + int ta = 1000; // Advance by 1 millisecond + write(pipefd2[1],&ta,sizeof(int));
This makes the protocol depend on system endianness. Please make it endian independent.
Regarding the implementation, note that the "frozen clock" is already implemented by cpu_disable_ticks() and cpu_enable_ticks(), so it would be (much) better if you can reuse that.
These are not small details, granted, but the concept looks interesting.
I'm sorry I didn't notice the first 3 versions. I look forward to
review the next iteration of the patch!
Paolo
Quoted 11 lines
docs/simulation-sync.txt new file +11
-0+The external simulator can then advance its clock by this +same amount. During this time, QEMU and the external simulator +will be executing in parallel. When the external simulator +completes its time advance, it waits for QEMU by reading from +its read pipe. The value read will be the actual number of +virtual microseconds by which QEMU has advanced its virtual clock. +This will be greater than or equal to the requested advance. +The code that does this might look like: + + read(pipefd1[0],&ta,sizeof(int)); +