@@ -40,6 +40,7 @@
#include "qemu/bitmap.h"
#include "qemu/seqlock.h"
#include "qapi-event.h"
+#include "replay/replay.h"
#ifndef _WIN32
#include "qemu/compatfd.h"
@@ -206,11 +207,19 @@ int64_t cpu_get_clock(void)
int64_t ti;
unsigned start;
+ if (replay_mode == REPLAY_MODE_PLAY) {
+ return replay_read_clock(REPLAY_CLOCK_VIRTUAL);
+ }
+
do {
start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
ti = cpu_get_clock_locked();
} while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
+ if (replay_mode == REPLAY_MODE_RECORD) {
+ replay_save_clock(REPLAY_CLOCK_VIRTUAL, ti);
+ }
+
return ti;
}
@@ -4,6 +4,7 @@
#include "qemu/typedefs.h"
#include "qemu-common.h"
#include "qemu/notify.h"
+#include "replay/replay.h"
/* timers */
@@ -699,8 +700,8 @@ static inline int64_t get_ticks_per_sec(void)
* Low level clock functions
*/
-/* real time host monotonic timer */
-static inline int64_t get_clock_realtime(void)
+/* real time host monotonic timer implementation */
+static inline int64_t get_clock_realtime_impl(void)
{
struct timeval tv;
@@ -708,6 +709,23 @@ static inline int64_t get_clock_realtime(void)
return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
}
+/* real time host monotonic timer interface */
+static inline int64_t get_clock_realtime(void)
+{
+ int64_t result;
+
+ if (replay_mode == REPLAY_MODE_RECORD) {
+ result = get_clock_realtime_impl();
+ replay_save_clock(REPLAY_CLOCK_REALTIME, result);
+ } else if (replay_mode == REPLAY_MODE_PLAY) {
+ result = replay_read_clock(REPLAY_CLOCK_REALTIME);
+ } else {
+ result = get_clock_realtime_impl();
+ }
+
+ return result;
+}
+
/* Warning: don't insert tracepoints into these functions, they are
also used by simpletrace backend and tracepoints would cause
an infinite recursion! */
@@ -749,6 +767,8 @@ int64_t cpu_get_clock(void);
/*******************************************/
/* host CPU ticks (if available) */
+#define cpu_get_real_ticks cpu_get_real_ticks_impl
+
#if defined(_ARCH_PPC)
static inline int64_t cpu_get_real_ticks(void)
@@ -902,6 +922,24 @@ static inline int64_t cpu_get_real_ticks (void)
}
#endif
+#undef cpu_get_real_ticks
+
+static inline int64_t cpu_get_real_ticks(void)
+{
+ int64_t val;
+
+ if (replay_mode == REPLAY_MODE_RECORD) {
+ val = cpu_get_real_ticks_impl();
+ replay_save_clock(REPLAY_CLOCK_REAL_TICKS, val);
+ } else if (replay_mode == REPLAY_MODE_PLAY) {
+ val = replay_read_clock(REPLAY_CLOCK_REAL_TICKS);
+ } else {
+ val = cpu_get_real_ticks_impl();
+ }
+
+ return val;
+}
+
#ifdef CONFIG_PROFILER
static inline int64_t profile_getclock(void)
{
@@ -25,6 +25,7 @@
#include "sysemu/sysemu.h"
#include "monitor/monitor.h"
#include "ui/console.h"
+#include "replay/replay.h"
#include "hw/hw.h"
@@ -562,7 +563,7 @@ int64_t qemu_clock_get_ns(QEMUClockType type)
now = get_clock_realtime();
last = clock->last;
clock->last = now;
- if (now < last) {
+ if (now < last && replay_mode == REPLAY_MODE_NONE) {
notifier_list_notify(&clock->reset_notifiers, &now);
}
return now;
@@ -1,3 +1,4 @@
obj-y += replay.o
obj-y += replay-internal.o
obj-y += replay-events.o
+obj-y += replay-time.o
@@ -24,12 +24,17 @@
#define EVENT_ASYNC_OPT 25
/* for instruction event */
#define EVENT_INSTRUCTION 32
+/* for clock read/writes */
+#define EVENT_CLOCK 64
+/* some of grteater codes are reserved for clocks */
/* Asynchronous events IDs */
#define REPLAY_ASYNC_COUNT 0
typedef struct ReplayState {
+ /*! Cached clock values. */
+ int64_t cached_clock[REPLAY_CLOCK_COUNT];
/*! Nonzero, when next instruction is repeated one and was already
processed. */
int skipping_instruction;
@@ -78,6 +83,12 @@ bool skip_async_events(int stop_event);
reports an error and stops the execution. */
void skip_async_events_until(unsigned int kind);
+/*! Reads next clock value from the file.
+ If clock kind read from the file is different from the parameter,
+ the value is not used.
+ If the parameter is -1, the clock value is read to the cache anyway. */
+void replay_read_next_clock(unsigned int kind);
+
/* Asynchronous events queue */
/*! Initializes events' processing internals */
new file mode 100755
@@ -0,0 +1,75 @@
+/*
+ * replay-time.c
+ *
+ * Copyright (c) 2010-2014 Institute for System Programming
+ * of the Russian Academy of Sciences.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu-common.h"
+#include "replay.h"
+#include "replay-internal.h"
+
+
+void replay_save_clock(unsigned int kind, int64_t clock)
+{
+ replay_save_instructions();
+
+ if (kind >= REPLAY_CLOCK_COUNT) {
+ fprintf(stderr, "invalid clock ID %d for replay\n", kind);
+ exit(1);
+ }
+
+ if (replay_file) {
+ replay_put_event(EVENT_CLOCK + kind);
+ replay_put_qword(clock);
+ }
+}
+
+void replay_read_next_clock(unsigned int kind)
+{
+ replay_fetch_data_kind();
+ if (replay_file) {
+ unsigned int read_kind = replay_data_kind - EVENT_CLOCK;
+
+ if (kind != -1 && read_kind != kind) {
+ return;
+ }
+ if (read_kind >= REPLAY_CLOCK_COUNT) {
+ fprintf(stderr,
+ "invalid clock ID %d was read from replay\n", read_kind);
+ exit(1);
+ }
+
+ int64_t clock = replay_get_qword();
+
+ replay_check_error();
+ replay_has_unread_data = 0;
+
+ replay_state.cached_clock[read_kind] = clock;
+ }
+}
+
+/*! Reads next clock event from the input. */
+int64_t replay_read_clock(unsigned int kind)
+{
+ if (kind >= REPLAY_CLOCK_COUNT) {
+ fprintf(stderr, "invalid clock ID %d for replay\n", kind);
+ exit(1);
+ }
+
+ if (replay_file) {
+ if (skip_async_events(EVENT_CLOCK + kind)) {
+ replay_read_next_clock(kind);
+ }
+ int64_t ret = replay_state.cached_clock[kind];
+
+ return ret;
+ }
+
+ fprintf(stderr, "REPLAY INTERNAL ERROR %d\n", __LINE__);
+ exit(1);
+}
@@ -16,6 +16,16 @@
#include <stdint.h>
#include "qapi-types.h"
+/* replay clock kinds */
+/* rdtsc */
+#define REPLAY_CLOCK_REAL_TICKS 0
+/* host_clock */
+#define REPLAY_CLOCK_REALTIME 1
+/* vm_clock */
+#define REPLAY_CLOCK_VIRTUAL 2
+
+#define REPLAY_CLOCK_COUNT 3
+
extern ReplayMode replay_mode;
extern char *replay_image_suffix;
@@ -48,6 +58,13 @@ bool replay_interrupt(void);
Returns true, when interrupt request is pending */
bool replay_has_interrupt(void);
+/* Processing clocks and other time sources */
+
+/*! Save the specified clock */
+void replay_save_clock(unsigned int kind, int64_t clock);
+/*! Read the specified clock from the log or return cached data */
+int64_t replay_read_clock(unsigned int kind);
+
/* Asynchronous events queue */
/*! Disables storing events in the queue */
@@ -6,3 +6,12 @@ ReplaySubmode replay_get_play_submode(void)
{
return 0;
}
+
+void replay_save_clock(unsigned int kind, int64_t clock)
+{
+}
+
+int64_t replay_read_clock(unsigned int kind)
+{
+ return 0;
+}
Clock ticks are considered as the sources of non-deterministic data for virtual machine. This patch implements saving the clock values when they are acquired (virtual, host clock, rdtsc, and some other timers). When replaying the execution corresponding values are read from log and transfered to the module, which wants to read the values. Such a design required the clock polling to be synchronized. Sometimes it is not true - e.g. when timeouts for timer lists are checked. In this case we use a cached value of the clock, passing it to the client code. Signed-off-by: Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru> --- cpus.c | 9 ++++++ include/qemu/timer.h | 42 +++++++++++++++++++++++++- qemu-timer.c | 3 +- replay/Makefile.objs | 1 + replay/replay-internal.h | 11 +++++++ replay/replay-time.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++ replay/replay.h | 17 ++++++++++ stubs/replay.c | 9 ++++++ 8 files changed, 164 insertions(+), 3 deletions(-) create mode 100755 replay/replay-time.c