From patchwork Thu Jul 17 11:04:36 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Dovgalyuk X-Patchwork-Id: 371156 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 8D9C1140093 for ; Thu, 17 Jul 2014 23:51:27 +1000 (EST) Received: from localhost ([::1]:44416 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X7m5k-0004k4-B5 for incoming@patchwork.ozlabs.org; Thu, 17 Jul 2014 09:51:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50923) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X7jek-0006d7-Sd for qemu-devel@nongnu.org; Thu, 17 Jul 2014 07:15:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X7jee-0008N5-Iv for qemu-devel@nongnu.org; Thu, 17 Jul 2014 07:15:22 -0400 Received: from mail.ispras.ru ([83.149.199.45]:47518) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X7jUH-00058U-Gm for qemu-devel@nongnu.org; Thu, 17 Jul 2014 07:04:33 -0400 Received: from [10.10.150.172] (unknown [80.250.189.177]) by mail.ispras.ru (Postfix) with ESMTPSA id B361D540151; Thu, 17 Jul 2014 15:04:32 +0400 (MSK) To: qemu-devel@nongnu.org From: Pavel Dovgalyuk Date: Thu, 17 Jul 2014 15:04:36 +0400 Message-ID: <20140717110436.8352.31173.stgit@PASHA-ISP> In-Reply-To: <20140717110153.8352.80175.stgit@PASHA-ISP> References: <20140717110153.8352.80175.stgit@PASHA-ISP> User-Agent: StGit/0.16 MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 83.149.199.45 X-Mailman-Approved-At: Thu, 17 Jul 2014 09:36:33 -0400 Cc: peter.maydell@linaro.org, peter.crosthwaite@xilinx.com, mark.burton@greensocs.com, real@ispras.ru, batuzovk@ispras.ru, pavel.dovgaluk@ispras.ru, pbonzini@redhat.com, fred.konrad@greensocs.com Subject: [Qemu-devel] [RFC PATCH v2 28/49] replay: asynchronous events infrastructure X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- replay/replay-events.c | 198 ++++++++++++++++++++++++++++++++++++++++++++++ replay/replay-internal.h | 17 ++++ replay/replay.h | 4 + 3 files changed, 218 insertions(+), 1 deletions(-) diff --git a/replay/replay-events.c b/replay/replay-events.c index eaffc48..eda574e 100755 --- a/replay/replay-events.c +++ b/replay/replay-events.c @@ -12,15 +12,211 @@ #include "replay.h" #include "replay-internal.h" +typedef struct Event { + int 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 QemuMutex lock; +static unsigned int read_event_kind = -1; +static uint64_t read_id = -1; +static int read_opt = -1; + +static bool replay_events_enabled = false; + +/* Functions */ + +static void replay_run_event(Event *event) +{ + switch (event->event_kind) { + default: + fprintf(stderr, "Replay: invalid async event ID (%d) in the queue\n", + event->event_kind); + exit(1); + break; + } +} + +void replay_enable_events(void) +{ + replay_events_enabled = true; +} + bool replay_has_events(void) { - return false; + return !QTAILQ_EMPTY(&events_list); +} + +void replay_flush_events(void) +{ + qemu_mutex_lock(&lock); + while (!QTAILQ_EMPTY(&events_list)) { + Event *event = QTAILQ_FIRST(&events_list); + replay_run_event(event); + QTAILQ_REMOVE(&events_list, event, events); + g_free(event); + } + qemu_mutex_unlock(&lock); +} + +void replay_disable_events(void) +{ + replay_events_enabled = false; + /* Flush events queue before waiting of completion */ + replay_flush_events(); +} + +void replay_clear_events(void) +{ + qemu_mutex_lock(&lock); + while (!QTAILQ_EMPTY(&events_list)) { + Event *event = QTAILQ_FIRST(&events_list); + QTAILQ_REMOVE(&events_list, event, events); + + g_free(event); + } + qemu_mutex_unlock(&lock); +} + +static void replay_add_event_internal(int event_kind, void *opaque, void *opaque2, uint64_t id) +{ + if (event_kind >= REPLAY_ASYNC_COUNT) { + fprintf(stderr, "Replay: invalid async event ID (%d)\n", event_kind); + exit(1); + } + if (!replay_file || replay_mode == REPLAY_NONE + || !replay_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; + + qemu_mutex_lock(&lock); + QTAILQ_INSERT_TAIL(&events_list, event, events); + qemu_mutex_unlock(&lock); +} + +void replay_add_event(int event_kind, void *opaque) +{ + replay_add_event_internal(event_kind, opaque, NULL, 0); } void replay_save_events(int opt) { + qemu_mutex_lock(&lock); + while (!QTAILQ_EMPTY(&events_list)) { + Event *event = QTAILQ_FIRST(&events_list); + if (replay_mode != REPLAY_PLAY) { + /* put the event into the file */ + if (opt == -1) { + replay_put_event(EVENT_ASYNC); + } else { + replay_put_event(EVENT_ASYNC_OPT); + replay_put_byte(opt); + } + replay_put_byte(event->event_kind); + + /* save event-specific data */ + switch (event->event_kind) { + } + } + + replay_run_event(event); + QTAILQ_REMOVE(&events_list, event, events); + g_free(event); + } + qemu_mutex_unlock(&lock); } void replay_read_events(int opt) { + replay_fetch_data_kind(); + while ((opt == -1 && replay_data_kind == EVENT_ASYNC) + || (opt != -1 && replay_data_kind == EVENT_ASYNC_OPT)) { + + if (read_event_kind == -1) { + if (opt != -1) { + read_opt = replay_get_byte(); + } + read_event_kind = replay_get_byte(); + read_id = -1; + replay_check_error(); + } + + if (opt != read_opt) + break; + /* Execute some events without searching them in the queue */ + switch (read_event_kind) { + default: + fprintf(stderr, "Unknown ID %d of replay event\n", read_event_kind); + exit(1); + break; + } + + qemu_mutex_lock(&lock); + + Event *event = NULL; + Event *curr = NULL; + QTAILQ_FOREACH(curr, &events_list, events) { + if (curr->event_kind == read_event_kind + && (read_id == -1 || read_id == curr->id)) { + event = curr; + break; + } + } + + if (event) { + /* read event-specific reading data */ + + QTAILQ_REMOVE(&events_list, event, events); + + qemu_mutex_unlock(&lock); + + /* reset unread data and other parameters to allow + reading other data from the log while + running the event */ + replay_has_unread_data = 0; + read_event_kind = -1; + read_id = -1; + read_opt = -1; + + replay_run_event(event); + g_free(event); + + replay_fetch_data_kind(); + } else { + qemu_mutex_unlock(&lock); + /* No such event found in the queue */ + break; + } + } +} + +void replay_init_events(void) +{ + read_event_kind = -1; + qemu_mutex_init(&lock); +} + +void replay_finish_events(void) +{ + replay_events_enabled = false; + replay_clear_events(); + qemu_mutex_destroy(&lock); } diff --git a/replay/replay-internal.h b/replay/replay-internal.h index 019ae20..cffc4e2 100755 --- a/replay/replay-internal.h +++ b/replay/replay-internal.h @@ -21,9 +21,14 @@ #define EVENT_EXCEPTION 23 /* for async events */ #define EVENT_ASYNC 24 +#define EVENT_ASYNC_OPT 25 /* for instruction event */ #define EVENT_INSTRUCTION 32 +/* Asynchronous events IDs */ + +#define REPLAY_ASYNC_COUNT 0 + typedef struct ReplayState { /*! Nonzero, when next instruction is repeated one and was already processed. */ @@ -75,11 +80,23 @@ void skip_async_events_until(unsigned int kind); /* 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 opt); /*! Read events from the file into the input queue */ void replay_read_events(int opt); +/*! Adds specified async event to the queue */ +void replay_add_event(int event_id, void *opaque); #endif diff --git a/replay/replay.h b/replay/replay.h index 10e6681..7f461c5 100755 --- a/replay/replay.h +++ b/replay/replay.h @@ -58,5 +58,9 @@ 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); #endif