qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Pavel Dovgalyuk <Pavel.Dovgaluk@ispras.ru>
To: qemu-devel@nongnu.org
Cc: edgar.iglesias@xilinx.com, peter.maydell@linaro.org,
	igor.rubinov@gmail.com, alex.bennee@linaro.org,
	mark.burton@greensocs.com, real@ispras.ru, batuzovk@ispras.ru,
	maria.klimushenkova@ispras.ru, pavel.dovgaluk@ispras.ru,
	pbonzini@redhat.com, hines@cert.org, fred.konrad@greensocs.com
Subject: [Qemu-devel] [PATCH v17 10/21] replay: asynchronous events infrastructure
Date: Mon, 07 Sep 2015 11:41:05 +0300	[thread overview]
Message-ID: <20150907084105.1664.550.stgit@PASHA-ISP> (raw)
In-Reply-To: <20150907084005.1664.19540.stgit@PASHA-ISP>

This patch adds module for saving and replaying asynchronous events.
These events include network packets, keyboard and mouse input,
USB packets, thread pool and bottom halves callbacks.
All events are stored in the queue to be processed at synchronization points
such as beginning of TB execution, or checkpoint in the iothread.

Signed-off-by: Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru>
---
 replay/Makefile.objs     |    1 
 replay/replay-events.c   |  225 ++++++++++++++++++++++++++++++++++++++++++++++
 replay/replay-internal.h |   29 ++++++
 replay/replay.h          |    6 +
 4 files changed, 261 insertions(+), 0 deletions(-)
 create mode 100755 replay/replay-events.c

diff --git a/replay/Makefile.objs b/replay/Makefile.objs
index 9711e14..1ce61ec 100755
--- a/replay/Makefile.objs
+++ b/replay/Makefile.objs
@@ -1,3 +1,4 @@
 obj-$(CONFIG_SOFTMMU) += replay.o
 obj-$(CONFIG_SOFTMMU) += replay-internal.o
+obj-$(CONFIG_SOFTMMU) += replay-events.o
 obj-$(CONFIG_USER_ONLY) += replay-user.o
diff --git a/replay/replay-events.c b/replay/replay-events.c
new file mode 100755
index 0000000..f973cb0
--- /dev/null
+++ b/replay/replay-events.c
@@ -0,0 +1,225 @@
+/*
+ * replay-events.c
+ *
+ * Copyright (c) 2010-2015 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 "qemu/error-report.h"
+#include "replay.h"
+#include "replay-internal.h"
+
+typedef struct Event {
+    ReplayAsyncEventKind event_kind;
+    void *opaque;
+    void *opaque2;
+    uint64_t id;
+
+    QTAILQ_ENTRY(Event) events;
+} Event;
+
+static QTAILQ_HEAD(, Event) events_list = QTAILQ_HEAD_INITIALIZER(events_list);
+static unsigned int read_event_kind = -1;
+static uint64_t read_id = -1;
+static int read_checkpoint = -1;
+
+static bool events_enabled;
+
+/* Functions */
+
+static void replay_run_event(Event *event)
+{
+    switch (event->event_kind) {
+    default:
+        error_report("Replay: invalid async event ID (%d) in the queue",
+                    event->event_kind);
+        exit(1);
+        break;
+    }
+}
+
+void replay_enable_events(void)
+{
+    events_enabled = true;
+}
+
+bool replay_has_events(void)
+{
+    return !QTAILQ_EMPTY(&events_list);
+}
+
+void replay_flush_events(void)
+{
+    replay_mutex_lock();
+    while (!QTAILQ_EMPTY(&events_list)) {
+        Event *event = QTAILQ_FIRST(&events_list);
+        replay_mutex_unlock();
+        replay_run_event(event);
+        replay_mutex_lock();
+        QTAILQ_REMOVE(&events_list, event, events);
+        g_free(event);
+    }
+    replay_mutex_unlock();
+}
+
+void replay_disable_events(void)
+{
+    if (replay_mode != REPLAY_MODE_NONE) {
+        events_enabled = false;
+        /* Flush events queue before waiting of completion */
+        replay_flush_events();
+    }
+}
+
+void replay_clear_events(void)
+{
+    replay_mutex_lock();
+    while (!QTAILQ_EMPTY(&events_list)) {
+        Event *event = QTAILQ_FIRST(&events_list);
+        QTAILQ_REMOVE(&events_list, event, events);
+
+        g_free(event);
+    }
+    replay_mutex_unlock();
+}
+
+/*! Adds specified async event to the queue */
+static void replay_add_event(ReplayAsyncEventKind event_kind,
+                             void *opaque,
+                             void *opaque2, uint64_t id)
+{
+    assert(event_kind < REPLAY_ASYNC_COUNT);
+
+    if (!replay_file || replay_mode == REPLAY_MODE_NONE
+        || !events_enabled) {
+        Event e;
+        e.event_kind = event_kind;
+        e.opaque = opaque;
+        e.opaque2 = opaque2;
+        e.id = id;
+        replay_run_event(&e);
+        return;
+    }
+
+    Event *event = g_malloc0(sizeof(Event));
+    event->event_kind = event_kind;
+    event->opaque = opaque;
+    event->opaque2 = opaque2;
+    event->id = id;
+
+    replay_mutex_lock();
+    QTAILQ_INSERT_TAIL(&events_list, event, events);
+    replay_mutex_unlock();
+}
+
+static void replay_save_event(Event *event, int checkpoint)
+{
+    if (replay_mode != REPLAY_MODE_PLAY) {
+        /* put the event into the file */
+        replay_put_event(EVENT_ASYNC);
+        replay_put_byte(checkpoint);
+        replay_put_byte(event->event_kind);
+
+        /* save event-specific data */
+        switch (event->event_kind) {
+        default:
+            error_report("Unknown ID %d of replay event", read_event_kind);
+            exit(1);
+            break;
+        }
+    }
+}
+
+/* Called with replay mutex locked */
+void replay_save_events(int checkpoint)
+{
+    while (!QTAILQ_EMPTY(&events_list)) {
+        Event *event = QTAILQ_FIRST(&events_list);
+        replay_save_event(event, checkpoint);
+
+        replay_mutex_unlock();
+        replay_run_event(event);
+        replay_mutex_lock();
+        QTAILQ_REMOVE(&events_list, event, events);
+        g_free(event);
+    }
+}
+
+static Event *replay_read_event(int checkpoint)
+{
+    Event *event;
+    if (read_event_kind == -1) {
+        read_checkpoint = replay_get_byte();
+        read_event_kind = replay_get_byte();
+        read_id = -1;
+        replay_check_error();
+    }
+
+    if (checkpoint != read_checkpoint) {
+        return NULL;
+    }
+
+    /* Events that has not to be in the queue */
+    switch (read_event_kind) {
+    default:
+        error_report("Unknown ID %d of replay event", read_event_kind);
+        exit(1);
+        break;
+    }
+
+    QTAILQ_FOREACH(event, &events_list, events) {
+        if (event->event_kind == read_event_kind
+            && (read_id == -1 || read_id == event->id)) {
+            break;
+        }
+    }
+
+    if (event) {
+        QTAILQ_REMOVE(&events_list, event, events);
+    } else {
+        return NULL;
+    }
+
+    /* Read event-specific data */
+
+    return event;
+}
+
+/* Called with replay mutex locked */
+void replay_read_events(int checkpoint)
+{
+    while (replay_data_kind == EVENT_ASYNC) {
+        Event *event = replay_read_event(checkpoint);
+        if (!event) {
+            break;
+        }
+        replay_mutex_unlock();
+        replay_run_event(event);
+        replay_mutex_lock();
+
+        g_free(event);
+        replay_finish_event();
+        read_event_kind = -1;
+    }
+}
+
+void replay_init_events(void)
+{
+    read_event_kind = -1;
+}
+
+void replay_finish_events(void)
+{
+    events_enabled = false;
+    replay_clear_events();
+}
+
+bool replay_events_enabled(void)
+{
+    return events_enabled;
+}
diff --git a/replay/replay-internal.h b/replay/replay-internal.h
index 5ff1c14..23807ca 100755
--- a/replay/replay-internal.h
+++ b/replay/replay-internal.h
@@ -21,9 +21,19 @@ enum ReplayEvents {
     EVENT_INTERRUPT,
     /* for emulated exceptions */
     EVENT_EXCEPTION,
+    /* for async events */
+    EVENT_ASYNC,
     EVENT_COUNT
 };
 
+/* Asynchronous events IDs */
+
+enum ReplayAsyncEventKind {
+    REPLAY_ASYNC_COUNT
+};
+
+typedef enum ReplayAsyncEventKind ReplayAsyncEventKind;
+
 typedef struct ReplayState {
     /*! Current step - number of processed instructions and timer events. */
     uint64_t current_step;
@@ -75,4 +85,23 @@ void replay_save_instructions(void);
     \return true, if event was found */
 bool replay_next_event_is(int event);
 
+/* Asynchronous events queue */
+
+/*! Initializes events' processing internals */
+void replay_init_events(void);
+/*! Clears internal data structures for events handling */
+void replay_finish_events(void);
+/*! Enables storing events in the queue */
+void replay_enable_events(void);
+/*! Flushes events queue */
+void replay_flush_events(void);
+/*! Clears events list before loading new VM state */
+void replay_clear_events(void);
+/*! Returns true if there are any unsaved events in the queue */
+bool replay_has_events(void);
+/*! Saves events from queue into the file */
+void replay_save_events(int checkpoint);
+/*! Read events from the file into the input queue */
+void replay_read_events(int checkpoint);
+
 #endif
diff --git a/replay/replay.h b/replay/replay.h
index 8915523..c2a7651 100755
--- a/replay/replay.h
+++ b/replay/replay.h
@@ -43,5 +43,11 @@ bool replay_interrupt(void);
     Returns true, when interrupt request is pending */
 bool replay_has_interrupt(void);
 
+/* Asynchronous events queue */
+
+/*! Disables storing events in the queue */
+void replay_disable_events(void);
+/*! Returns true when saving events is enabled */
+bool replay_events_enabled(void);
 
 #endif

  parent reply	other threads:[~2015-09-07  8:41 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-07  8:40 [Qemu-devel] [PATCH v17 00/21] Deterministic replay core Pavel Dovgalyuk
2015-09-07  8:40 ` [Qemu-devel] [PATCH v17 01/21] i386: partial revert of interrupt poll fix Pavel Dovgalyuk
2015-09-07  8:40 ` [Qemu-devel] [PATCH v17 02/21] replay: global variables and function stubs Pavel Dovgalyuk
2015-09-07  8:40 ` [Qemu-devel] [PATCH v17 03/21] replay: internal functions for replay log Pavel Dovgalyuk
2015-09-07  8:40 ` [Qemu-devel] [PATCH v17 04/21] replay: introduce mutex to protect the " Pavel Dovgalyuk
2015-09-07  8:40 ` [Qemu-devel] [PATCH v17 05/21] replay: introduce icount event Pavel Dovgalyuk
2015-09-07  8:40 ` [Qemu-devel] [PATCH v17 06/21] cpu-exec: allow temporary disabling icount Pavel Dovgalyuk
2015-09-07  8:40 ` [Qemu-devel] [PATCH v17 07/21] cpu: replay instructions sequence Pavel Dovgalyuk
2015-09-07  8:40 ` [Qemu-devel] [PATCH v17 08/21] i386: interrupt poll processing Pavel Dovgalyuk
2015-09-07  8:40 ` [Qemu-devel] [PATCH v17 09/21] replay: interrupts and exceptions Pavel Dovgalyuk
2015-09-07  8:41 ` Pavel Dovgalyuk [this message]
2015-09-07  8:41 ` [Qemu-devel] [PATCH v17 11/21] replay: recording and replaying clock ticks Pavel Dovgalyuk
2015-09-07  8:41 ` [Qemu-devel] [PATCH v17 12/21] replay: shutdown event Pavel Dovgalyuk
2015-09-07  8:41 ` [Qemu-devel] [PATCH v17 13/21] icount: improve counting for record/replay Pavel Dovgalyuk
2015-09-07  8:41 ` [Qemu-devel] [PATCH v17 14/21] replay: checkpoints Pavel Dovgalyuk
2015-09-07  8:41 ` [Qemu-devel] [PATCH v17 15/21] bottom halves: introduce bh call function Pavel Dovgalyuk
2015-09-07  8:41 ` [Qemu-devel] [PATCH v17 16/21] replay: ptimer Pavel Dovgalyuk
2015-09-07  8:41 ` [Qemu-devel] [PATCH v17 17/21] typedef: add typedef for QemuOpts Pavel Dovgalyuk
2015-09-07  8:41 ` [Qemu-devel] [PATCH v17 18/21] replay: initialization and deinitialization Pavel Dovgalyuk
2015-09-07  8:41 ` [Qemu-devel] [PATCH v17 19/21] replay: replay blockers for devices Pavel Dovgalyuk
2015-09-07  8:42 ` [Qemu-devel] [PATCH v17 20/21] replay: command line options Pavel Dovgalyuk
2015-09-07  8:42 ` [Qemu-devel] [PATCH v17 21/21] replay: recording of the user input Pavel Dovgalyuk
2015-09-11  5:52 ` [Qemu-devel] [PATCH v17 00/21] Deterministic replay core Pavel Dovgaluk
     [not found] ` <45193.409038666$1441951862@news.gmane.org>
2015-09-11  6:31   ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150907084105.1664.550.stgit@PASHA-ISP \
    --to=pavel.dovgaluk@ispras.ru \
    --cc=alex.bennee@linaro.org \
    --cc=batuzovk@ispras.ru \
    --cc=edgar.iglesias@xilinx.com \
    --cc=fred.konrad@greensocs.com \
    --cc=hines@cert.org \
    --cc=igor.rubinov@gmail.com \
    --cc=maria.klimushenkova@ispras.ru \
    --cc=mark.burton@greensocs.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=real@ispras.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).