From patchwork Wed May 6 14:04:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Dovgalyuk X-Patchwork-Id: 468957 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 9A5341401AD for ; Thu, 7 May 2015 00:10:13 +1000 (AEST) Received: from localhost ([::1]:45335 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yq01b-0004it-Bf for incoming@patchwork.ozlabs.org; Wed, 06 May 2015 10:10:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47543) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ypzve-0002Zd-Jr for qemu-devel@nongnu.org; Wed, 06 May 2015 10:04:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ypzvb-0002CN-PX for qemu-devel@nongnu.org; Wed, 06 May 2015 10:04:02 -0400 Received: from mail.ispras.ru ([83.149.199.45]:49059) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ypzvb-0002CD-EA for qemu-devel@nongnu.org; Wed, 06 May 2015 10:03:59 -0400 Received: from [10.10.150.102] (unknown [85.142.117.224]) by mail.ispras.ru (Postfix) with ESMTPSA id 8F24E540151; Wed, 6 May 2015 17:03:58 +0300 (MSK) To: qemu-devel@nongnu.org From: Pavel Dovgalyuk Date: Wed, 06 May 2015 17:04:01 +0300 Message-ID: <20150506140401.17288.81756.stgit@PASHA-ISP> In-Reply-To: <20150506140238.17288.87548.stgit@PASHA-ISP> References: <20150506140238.17288.87548.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 Cc: peter.maydell@linaro.org, peter.crosthwaite@xilinx.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, fred.konrad@greensocs.com Subject: [Qemu-devel] [RFC PATCH v13 14/21] replay: checkpoints 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 introduces checkpoints that synchronize cpu thread and iothread. When checkpoint is met in the code all asynchronous events from the queue are executed. Signed-off-by: Pavel Dovgalyuk --- cpus.c | 5 +++++ main-loop.c | 6 ++++++ qemu-timer.c | 38 ++++++++++++++++++++++++++++++++++---- replay/replay-internal.h | 4 ++++ replay/replay.c | 34 ++++++++++++++++++++++++++++++++++ replay/replay.h | 19 +++++++++++++++++++ stubs/replay.c | 11 +++++++++++ vl.c | 13 ++++++++++++- 8 files changed, 125 insertions(+), 5 deletions(-) diff --git a/cpus.c b/cpus.c index 3fcb48a..6fa5b6a 100644 --- a/cpus.c +++ b/cpus.c @@ -395,6 +395,11 @@ void qemu_clock_warp(QEMUClockType type) return; } + /* warp clock deterministically in record/replay mode */ + if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP)) { + return; + } + /* * If the CPUs have been sleeping, advance QEMU_CLOCK_VIRTUAL timer now. * This ensures that the deadline for the timer is computed correctly below. diff --git a/main-loop.c b/main-loop.c index 981bcb5..06aad06 100644 --- a/main-loop.c +++ b/main-loop.c @@ -29,6 +29,7 @@ #include "slirp/libslirp.h" #include "qemu/main-loop.h" #include "block/aio.h" +#include "replay/replay.h" #ifndef _WIN32 @@ -497,6 +498,11 @@ int main_loop_wait(int nonblocking) slirp_pollfds_poll(gpollfds, (ret < 0)); #endif + /* CPU thread can infinitely wait for event after + missing the warp */ + if (replay_mode == REPLAY_MODE_PLAY) { + qemu_clock_warp(QEMU_CLOCK_VIRTUAL); + } qemu_clock_run_all_timers(); return ret; diff --git a/qemu-timer.c b/qemu-timer.c index d605afd..37d9098 100644 --- a/qemu-timer.c +++ b/qemu-timer.c @@ -478,10 +478,33 @@ bool timerlist_run_timers(QEMUTimerList *timer_list) void *opaque; qemu_event_reset(&timer_list->timers_done_ev); - if (!timer_list->clock->enabled) { + if (!timer_list->clock->enabled || !timer_list->active_timers) { goto out; } + switch (timer_list->clock->type) { + case QEMU_CLOCK_REALTIME: + break; + default: + case QEMU_CLOCK_VIRTUAL: + if ((replay_mode != REPLAY_MODE_NONE && !runstate_is_running()) + || !replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL)) { + goto out; + } + break; + case QEMU_CLOCK_HOST: + if ((replay_mode != REPLAY_MODE_NONE && !runstate_is_running()) + || !replay_checkpoint(CHECKPOINT_CLOCK_HOST)) { + goto out; + } + case QEMU_CLOCK_VIRTUAL_RT: + if ((replay_mode != REPLAY_MODE_NONE && !runstate_is_running()) + || !replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL_RT)) { + goto out; + } + break; + } + current_time = qemu_clock_get_ns(timer_list->clock->type); for(;;) { qemu_mutex_lock(&timer_list->active_timers_lock); @@ -545,11 +568,18 @@ int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg) { int64_t deadline = -1; QEMUClockType type; + bool play = replay_mode == REPLAY_MODE_PLAY; for (type = 0; type < QEMU_CLOCK_MAX; type++) { if (qemu_clock_use_for_deadline(tlg->tl[type]->clock->type)) { - deadline = qemu_soonest_timeout(deadline, - timerlist_deadline_ns( - tlg->tl[type])); + if (!play || tlg->tl[type]->clock->type == QEMU_CLOCK_REALTIME) { + deadline = qemu_soonest_timeout(deadline, + timerlist_deadline_ns( + tlg->tl[type])); + } else { + /* Read clock from the replay file and + do not calculate the deadline, based on virtual clock. */ + qemu_clock_get_ns(tlg->tl[type]->clock->type); + } } } return deadline; diff --git a/replay/replay-internal.h b/replay/replay-internal.h index 92d4749..f758371 100755 --- a/replay/replay-internal.h +++ b/replay/replay-internal.h @@ -29,6 +29,10 @@ enum ReplayEvents { /* some of grteater codes are reserved for clocks */ EVENT_CLOCK, EVENT_CLOCK_LAST = EVENT_CLOCK + REPLAY_CLOCK_COUNT - 1, + /* for checkpoint event */ + /* some of grteater codes are reserved for checkpoints */ + EVENT_CHECKPOINT, + EVENT_CHECKPOINT_LAST = EVENT_CHECKPOINT + CHECKPOINT_COUNT - 1, EVENT_COUNT }; diff --git a/replay/replay.c b/replay/replay.c index 5c3a8c9..e5c61c4 100755 --- a/replay/replay.c +++ b/replay/replay.c @@ -158,3 +158,37 @@ void replay_shutdown_request(void) replay_mutex_unlock(); } } + +bool replay_checkpoint(ReplayCheckpoint checkpoint) +{ + bool res = false; + assert(EVENT_CHECKPOINT + checkpoint <= EVENT_CHECKPOINT_LAST); + replay_save_instructions(); + + if (!replay_file) { + return true; + } + + replay_mutex_lock(); + + if (replay_mode == REPLAY_MODE_PLAY) { + if (replay_next_event_is(EVENT_CHECKPOINT + checkpoint)) { + replay_finish_event(); + } else if (replay_data_kind != EVENT_ASYNC) { + res = false; + goto out; + } + replay_read_events(checkpoint); + /* replay_read_events may leave some unread events. + Return false if not all of the events associated with + checkpoint were processed */ + res = replay_data_kind != EVENT_ASYNC; + } else if (replay_mode == REPLAY_MODE_RECORD) { + replay_put_event(EVENT_CHECKPOINT + checkpoint); + replay_save_events(checkpoint); + res = true; + } +out: + replay_mutex_unlock(); + return res; +} diff --git a/replay/replay.h b/replay/replay.h index fcc93d1..b6c4a8d 100755 --- a/replay/replay.h +++ b/replay/replay.h @@ -26,6 +26,19 @@ enum ReplayClockKind { }; typedef enum ReplayClockKind ReplayClockKind; +/* IDs of the checkpoints */ +enum ReplayCheckpoint { + CHECKPOINT_CLOCK_WARP, + CHECKPOINT_RESET_REQUESTED, + CHECKPOINT_CLOCK_VIRTUAL, + CHECKPOINT_CLOCK_HOST, + CHECKPOINT_CLOCK_VIRTUAL_RT, + CHECKPOINT_INIT, + CHECKPOINT_RESET, + CHECKPOINT_COUNT +}; +typedef enum ReplayCheckpoint ReplayCheckpoint; + extern ReplayMode replay_mode; /* Processing the instructions */ @@ -70,6 +83,12 @@ int64_t replay_read_clock(ReplayClockKind kind); /*! Called when qemu shutdown is requested. */ void replay_shutdown_request(void); +/*! Should be called at check points in the execution. + These check points are skipped, if they were not met. + Saves checkpoint in the SAVE mode and validates in the PLAY mode. + Returns 0 in PLAY mode if checkpoint was not found. + Returns 1 in all other cases. */ +bool replay_checkpoint(ReplayCheckpoint checkpoint); /* Asynchronous events queue */ diff --git a/stubs/replay.c b/stubs/replay.c index 121bca6..1be3575 100755 --- a/stubs/replay.c +++ b/stubs/replay.c @@ -1,4 +1,5 @@ #include "replay/replay.h" +#include "sysemu/sysemu.h" ReplayMode replay_mode; @@ -10,3 +11,13 @@ int64_t replay_read_clock(unsigned int kind) { return 0; } + +bool replay_checkpoint(ReplayCheckpoint checkpoint) +{ + return 0; +} + +int runstate_is_running(void) +{ + return 0; +} diff --git a/vl.c b/vl.c index 3647439..6846067 100644 --- a/vl.c +++ b/vl.c @@ -119,6 +119,7 @@ int main(int argc, char **argv) #include "qapi/opts-visitor.h" #include "qom/object_interfaces.h" #include "qapi-event.h" +#include "replay/replay.h" #define DEFAULT_RAM_SIZE 128 @@ -1758,7 +1759,9 @@ static bool main_loop_should_exit(void) return true; } } - if (qemu_reset_requested()) { + if (qemu_reset_requested_get() + && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) { + qemu_reset_requested(); pause_all_vcpus(); cpu_synchronize_all_states(); qemu_system_reset(VMRESET_REPORT); @@ -4239,6 +4242,10 @@ int main(int argc, char **argv, char **envp) } qemu_add_globals(); + /* This checkpoint is required by replay to separate prior clock + reading from the other reads, because timer polling functions query + clock values from the log. */ + replay_checkpoint(CHECKPOINT_INIT); qdev_machine_init(); current_machine->ram_size = ram_size; @@ -4348,6 +4355,10 @@ int main(int argc, char **argv, char **envp) /* Done notifiers can load ROMs */ rom_load_done(); + /* This checkpoint is required by replay to separate prior clock + reading from the other reads, because timer polling functions query + clock values from the log. */ + replay_checkpoint(CHECKPOINT_RESET); qemu_system_reset(VMRESET_SILENT); if (loadvm) { if (load_vmstate(loadvm) < 0) {