Skip to content

Commit

Permalink
Merge tag 'pull-replay-fixes-080124-1' of https://gitlab.com/stsquad/…
Browse files Browse the repository at this point in the history
…qemu into staging

Record/replay fixes for replay_kernel tests

  - add a 32 bit x86 replay test case
  - fix some typos
  - use modern snapshot setting for tests
  - update replay_dump for current ABI
  - remove stale replay variables
  - improve kdoc for ReplayState
  - introduce common error path for replay
  - always fully drain chardevs when in replay
  - catch unexpected waitio on playback
  - remove flaky tags from replay_kernel tests

# -----BEGIN PGP SIGNATURE-----
#
# iQEzBAABCgAdFiEEZoWumedRZ7yvyN81+9DbCVqeKkQFAmWcAJgACgkQ+9DbCVqe
# KkS/TQf+PuIPtuX71ENajfRBjz6450IbGqLUJ1HEaPGYGRj+fR6rg5g5u8qaBrT7
# TUv9ef9L22NtyL+Gbs1OGpGDWKoqV6RQc+A/MHa8IKFpcS24nUo3k4psIC6NSGRH
# 6w3++fPC1Q5cDk9Lei3Qt8fXzcnUZz+NTiIK05aC0xh7D6uGfdADvKqHeLav7qi+
# X2ztNdBsy/WJWCuWcMVzb/dGwDBtuyyxvqTD4EF+zn+gSYq9od2G8XdF+0o6ZVLM
# mXEHwNwB6UjOkLt2cYaay59SXcJFvwxKbEGTDnA7T+kgd3rknuBaWdVBIazoSPQh
# +522nPz5qq/3wO1l7+iQXuvd38fWyw==
# =nKRx
# -----END PGP SIGNATURE-----
# gpg: Signature made Mon 08 Jan 2024 14:03:04 GMT
# gpg:                using RSA key 6685AE99E75167BCAFC8DF35FBD0DB095A9E2A44
# gpg: Good signature from "Alex Bennée (Master Work Key) <[email protected]>" [full]
# Primary key fingerprint: 6685 AE99 E751 67BC AFC8  DF35 FBD0 DB09 5A9E 2A44

* tag 'pull-replay-fixes-080124-1' of https://gitlab.com/stsquad/qemu:
  tests/avocado: remove skips from replay_kernel
  chardev: force write all when recording replay logs
  replay: stop us hanging in rr_wait_io_event
  replay/replay-char: use report_sync_error
  replay: introduce a central report point for sync errors
  replay: make has_unread_data a bool
  replay: add proper kdoc for ReplayState
  replay: remove host_clock_last
  scripts/replay_dump: track total number of instructions
  scripts/replay-dump: update to latest format
  tests/avocado: modernise the drive args for replay_linux
  tests/avocado: fix typo in replay_linux
  tests/avocado: add a simple i386 replay kernel test

Signed-off-by: Peter Maydell <[email protected]>
  • Loading branch information
pm215 committed Jan 9, 2024
2 parents b345ffa + c2ef5ee commit c1df5b4
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 47 deletions.
2 changes: 1 addition & 1 deletion accel/tcg/tcg-accel-ops-rr.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static void rr_wait_io_event(void)
{
CPUState *cpu;

while (all_cpu_threads_idle()) {
while (all_cpu_threads_idle() && replay_can_wait()) {
rr_stop_kick_timer();
qemu_cond_wait_iothread(first_cpu->halt_cond);
}
Expand Down
12 changes: 12 additions & 0 deletions chardev/char.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ int qemu_chr_write(Chardev *s, const uint8_t *buf, int len, bool write_all)
return res;
}

if (replay_mode == REPLAY_MODE_RECORD) {
/*
* When recording we don't want temporary conditions to
* perturb the result. By ensuring we write everything we can
* while recording we avoid playback being out of sync if it
* doesn't encounter the same temporary conditions (usually
* triggered by external programs not reading the chardev fast
* enough and pipes filling up).
*/
write_all = true;
}

res = qemu_chr_write_buffer(s, buf, len, &offset, write_all);

if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
Expand Down
5 changes: 5 additions & 0 deletions include/sysemu/replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ int replay_get_instructions(void);
/*! Updates instructions counter in replay mode. */
void replay_account_executed_instructions(void);

/**
* replay_can_wait: check if we should pause for wait-io
*/
bool replay_can_wait(void);

/* Processing clocks and other time sources */

/*! Save the specified clock */
Expand Down
6 changes: 2 additions & 4 deletions replay/replay-char.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ void replay_char_write_event_load(int *res, int *offset)
*offset = replay_get_dword();
replay_finish_event();
} else {
error_report("Missing character write event in the replay log");
exit(1);
replay_sync_error("Missing character write event in the replay log");
}
}

Expand All @@ -135,8 +134,7 @@ int replay_char_read_all_load(uint8_t *buf)
replay_finish_event();
return res;
} else {
error_report("Missing character read all event in the replay log");
exit(1);
replay_sync_error("Missing character read all event in the replay log");
}
}

Expand Down
5 changes: 3 additions & 2 deletions replay/replay-internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,12 @@ void replay_fetch_data_kind(void)
if (replay_file) {
if (!replay_state.has_unread_data) {
replay_state.data_kind = replay_get_byte();
replay_state.current_event++;
if (replay_state.data_kind == EVENT_INSTRUCTION) {
replay_state.instruction_count = replay_get_dword();
}
replay_check_error();
replay_state.has_unread_data = 1;
replay_state.has_unread_data = true;
if (replay_state.data_kind >= EVENT_COUNT) {
error_report("Replay: unknown event kind %d",
replay_state.data_kind);
Expand All @@ -191,7 +192,7 @@ void replay_fetch_data_kind(void)

void replay_finish_event(void)
{
replay_state.has_unread_data = 0;
replay_state.has_unread_data = false;
replay_fetch_data_kind();
}

Expand Down
50 changes: 36 additions & 14 deletions replay/replay-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ typedef enum ReplayAsyncEventKind {
REPLAY_ASYNC_COUNT
} ReplayAsyncEventKind;

/* Any changes to order/number of events will need to bump REPLAY_VERSION */
/*
* Any changes to order/number of events will need to bump
* REPLAY_VERSION to prevent confusion with old logs. Also don't
* forget to update replay_event_name() to make your debugging life
* easier.
*/
enum ReplayEvents {
/* for instruction event */
EVENT_INSTRUCTION,
Expand Down Expand Up @@ -63,26 +68,33 @@ enum ReplayEvents {
EVENT_COUNT
};

/**
* typedef ReplayState - global tracking Replay state
*
* This structure tracks where we are in the current ReplayState
* including the logged events from the recorded replay stream. Some
* of the data is also stored/restored from VMStateDescription when VM
* save/restore events take place.
*
* @cached_clock: Cached clocks values
* @current_icount: number of processed instructions
* @instruction_count: number of instructions until next event
* @current_event: current event index
* @data_kind: current event
* @has_unread_data: true if event not yet processed
* @file_offset: offset into replay log at replay snapshot
* @block_request_id: current serialised block request id
* @read_event_id: current async read event id
*/
typedef struct ReplayState {
/*! Cached clock values. */
int64_t cached_clock[REPLAY_CLOCK_COUNT];
/*! Current icount - number of processed instructions. */
uint64_t current_icount;
/*! Number of instructions to be executed before other events happen. */
int instruction_count;
/*! Type of the currently executed event. */
unsigned int current_event;
unsigned int data_kind;
/*! Flag which indicates that event is not processed yet. */
unsigned int has_unread_data;
/*! Temporary variable for saving current log offset. */
bool has_unread_data;
uint64_t file_offset;
/*! Next block operation id.
This counter is global, because requests from different
block devices should not get overlapping ids. */
uint64_t block_request_id;
/*! Prior value of the host clock */
uint64_t host_clock_last;
/*! Asynchronous event id read from the log */
uint64_t read_event_id;
} ReplayState;
extern ReplayState replay_state;
Expand Down Expand Up @@ -183,6 +195,16 @@ void replay_event_net_save(void *opaque);
/*! Reads network from the file. */
void *replay_event_net_load(void);

/* Diagnostics */

/**
* replay_sync_error(): report sync error and exit
*
* When we reach an error condition we want to report it centrally so
* we can also dump some useful information into the logs.
*/
G_NORETURN void replay_sync_error(const char *error);

/* VMState-related functions */

/* Registers replay VMState.
Expand Down
7 changes: 4 additions & 3 deletions replay/replay-snapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ static int replay_post_load(void *opaque, int version_id)

static const VMStateDescription vmstate_replay = {
.name = "replay",
.version_id = 2,
.minimum_version_id = 2,
.version_id = 3,
.minimum_version_id = 3,
.pre_save = replay_pre_save,
.post_load = replay_post_load,
.fields = (const VMStateField[]) {
VMSTATE_INT64_ARRAY(cached_clock, ReplayState, REPLAY_CLOCK_COUNT),
VMSTATE_UINT64(current_icount, ReplayState),
VMSTATE_INT32(instruction_count, ReplayState),
VMSTATE_UINT32(current_event, ReplayState),
VMSTATE_UINT32(data_kind, ReplayState),
VMSTATE_UINT32(has_unread_data, ReplayState),
VMSTATE_BOOL(has_unread_data, ReplayState),
VMSTATE_UINT64(file_offset, ReplayState),
VMSTATE_UINT64(block_request_id, ReplayState),
VMSTATE_UINT64(read_event_id, ReplayState),
Expand Down
132 changes: 132 additions & 0 deletions replay/replay.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,107 @@ static GSList *replay_blockers;
uint64_t replay_break_icount = -1ULL;
QEMUTimer *replay_break_timer;

/* Pretty print event names */

static const char *replay_async_event_name(ReplayAsyncEventKind event)
{
switch (event) {
#define ASYNC_EVENT(_x) case REPLAY_ASYNC_EVENT_ ## _x: return "ASYNC_EVENT_"#_x
ASYNC_EVENT(BH);
ASYNC_EVENT(BH_ONESHOT);
ASYNC_EVENT(INPUT);
ASYNC_EVENT(INPUT_SYNC);
ASYNC_EVENT(CHAR_READ);
ASYNC_EVENT(BLOCK);
ASYNC_EVENT(NET);
#undef ASYNC_EVENT
default:
g_assert_not_reached();
}
}

static const char *replay_clock_event_name(ReplayClockKind clock)
{
switch (clock) {
#define CLOCK_EVENT(_x) case REPLAY_CLOCK_ ## _x: return "CLOCK_" #_x
CLOCK_EVENT(HOST);
CLOCK_EVENT(VIRTUAL_RT);
#undef CLOCK_EVENT
default:
g_assert_not_reached();
}
}

/* Pretty print shutdown event names */
static const char *replay_shutdown_event_name(ShutdownCause cause)
{
switch (cause) {
#define SHUTDOWN_EVENT(_x) case SHUTDOWN_CAUSE_ ## _x: return "SHUTDOWN_CAUSE_" #_x
SHUTDOWN_EVENT(NONE);
SHUTDOWN_EVENT(HOST_ERROR);
SHUTDOWN_EVENT(HOST_QMP_QUIT);
SHUTDOWN_EVENT(HOST_QMP_SYSTEM_RESET);
SHUTDOWN_EVENT(HOST_SIGNAL);
SHUTDOWN_EVENT(HOST_UI);
SHUTDOWN_EVENT(GUEST_SHUTDOWN);
SHUTDOWN_EVENT(GUEST_RESET);
SHUTDOWN_EVENT(GUEST_PANIC);
SHUTDOWN_EVENT(SUBSYSTEM_RESET);
SHUTDOWN_EVENT(SNAPSHOT_LOAD);
#undef SHUTDOWN_EVENT
default:
g_assert_not_reached();
}
}

static const char *replay_checkpoint_event_name(enum ReplayCheckpoint checkpoint)
{
switch (checkpoint) {
#define CHECKPOINT_EVENT(_x) case CHECKPOINT_ ## _x: return "CHECKPOINT_" #_x
CHECKPOINT_EVENT(CLOCK_WARP_START);
CHECKPOINT_EVENT(CLOCK_WARP_ACCOUNT);
CHECKPOINT_EVENT(RESET_REQUESTED);
CHECKPOINT_EVENT(SUSPEND_REQUESTED);
CHECKPOINT_EVENT(CLOCK_VIRTUAL);
CHECKPOINT_EVENT(CLOCK_HOST);
CHECKPOINT_EVENT(CLOCK_VIRTUAL_RT);
CHECKPOINT_EVENT(INIT);
CHECKPOINT_EVENT(RESET);
#undef CHECKPOINT_EVENT
default:
g_assert_not_reached();
}
}

static const char *replay_event_name(enum ReplayEvents event)
{
/* First deal with the simple ones */
switch (event) {
#define EVENT(_x) case EVENT_ ## _x: return "EVENT_"#_x
EVENT(INSTRUCTION);
EVENT(INTERRUPT);
EVENT(EXCEPTION);
EVENT(CHAR_WRITE);
EVENT(CHAR_READ_ALL);
EVENT(AUDIO_OUT);
EVENT(AUDIO_IN);
EVENT(RANDOM);
#undef EVENT
default:
if (event >= EVENT_ASYNC && event <= EVENT_ASYNC_LAST) {
return replay_async_event_name(event - EVENT_ASYNC);
} else if (event >= EVENT_SHUTDOWN && event <= EVENT_SHUTDOWN_LAST) {
return replay_shutdown_event_name(event - EVENT_SHUTDOWN);
} else if (event >= EVENT_CLOCK && event <= EVENT_CLOCK_LAST) {
return replay_clock_event_name(event - EVENT_CLOCK);
} else if (event >= EVENT_CHECKPOINT && event <= EVENT_CHECKPOINT_LAST) {
return replay_checkpoint_event_name(event - EVENT_CHECKPOINT);
}
}

g_assert_not_reached();
}

bool replay_next_event_is(int event)
{
bool res = false;
Expand Down Expand Up @@ -226,6 +327,15 @@ bool replay_has_event(void)
return res;
}

G_NORETURN void replay_sync_error(const char *error)
{
error_report("%s (insn total %"PRId64"/%d left, event %d is %s)", error,
replay_state.current_icount, replay_state.instruction_count,
replay_state.current_event,
replay_event_name(replay_state.data_kind));
abort();
}

static void replay_enable(const char *fname, int mode)
{
const char *fmode = NULL;
Expand Down Expand Up @@ -258,6 +368,7 @@ static void replay_enable(const char *fname, int mode)
replay_state.data_kind = -1;
replay_state.instruction_count = 0;
replay_state.current_icount = 0;
replay_state.current_event = 0;
replay_state.has_unread_data = 0;

/* skip file header for RECORD and check it for PLAY */
Expand Down Expand Up @@ -338,6 +449,27 @@ void replay_start(void)
replay_enable_events();
}

/*
* For none/record the answer is yes.
*/
bool replay_can_wait(void)
{
if (replay_mode == REPLAY_MODE_PLAY) {
/*
* For playback we shouldn't ever be at a point we wait. If
* the instruction count has reached zero and we have an
* unconsumed event we should go around again and consume it.
*/
if (replay_state.instruction_count == 0 && replay_state.has_unread_data) {
return false;
} else {
replay_sync_error("Playback shouldn't have to iowait");
}
}
return true;
}


void replay_finish(void)
{
if (replay_mode == REPLAY_MODE_NONE) {
Expand Down
Loading

0 comments on commit c1df5b4

Please sign in to comment.