4 * Copyright (c) 2010-2015 Institute for System Programming
5 * of the Russian Academy of Sciences.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "qemu-common.h"
14 #include "qemu/error-report.h"
15 #include "sysemu/replay.h"
16 #include "replay-internal.h"
17 #include "block/aio.h"
20 typedef struct Event
{
21 ReplayAsyncEventKind event_kind
;
26 QTAILQ_ENTRY(Event
) events
;
29 static QTAILQ_HEAD(, Event
) events_list
= QTAILQ_HEAD_INITIALIZER(events_list
);
30 static unsigned int read_event_kind
= -1;
31 static uint64_t read_id
= -1;
32 static int read_checkpoint
= -1;
34 static bool events_enabled
;
38 static void replay_run_event(Event
*event
)
40 switch (event
->event_kind
) {
41 case REPLAY_ASYNC_EVENT_BH
:
42 aio_bh_call(event
->opaque
);
44 case REPLAY_ASYNC_EVENT_INPUT
:
45 qemu_input_event_send_impl(NULL
, (InputEvent
*)event
->opaque
);
46 qapi_free_InputEvent((InputEvent
*)event
->opaque
);
48 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
49 qemu_input_event_sync_impl();
51 case REPLAY_ASYNC_EVENT_CHAR_READ
:
52 replay_event_char_read_run(event
->opaque
);
54 case REPLAY_ASYNC_EVENT_BLOCK
:
55 aio_bh_call(event
->opaque
);
58 error_report("Replay: invalid async event ID (%d) in the queue",
65 void replay_enable_events(void)
67 events_enabled
= true;
70 bool replay_has_events(void)
72 return !QTAILQ_EMPTY(&events_list
);
75 void replay_flush_events(void)
78 while (!QTAILQ_EMPTY(&events_list
)) {
79 Event
*event
= QTAILQ_FIRST(&events_list
);
80 replay_mutex_unlock();
81 replay_run_event(event
);
83 QTAILQ_REMOVE(&events_list
, event
, events
);
86 replay_mutex_unlock();
89 void replay_disable_events(void)
91 if (replay_mode
!= REPLAY_MODE_NONE
) {
92 events_enabled
= false;
93 /* Flush events queue before waiting of completion */
94 replay_flush_events();
98 void replay_clear_events(void)
101 while (!QTAILQ_EMPTY(&events_list
)) {
102 Event
*event
= QTAILQ_FIRST(&events_list
);
103 QTAILQ_REMOVE(&events_list
, event
, events
);
107 replay_mutex_unlock();
110 /*! Adds specified async event to the queue */
111 void replay_add_event(ReplayAsyncEventKind event_kind
,
113 void *opaque2
, uint64_t id
)
115 assert(event_kind
< REPLAY_ASYNC_COUNT
);
117 if (!replay_file
|| replay_mode
== REPLAY_MODE_NONE
118 || !events_enabled
) {
120 e
.event_kind
= event_kind
;
124 replay_run_event(&e
);
128 Event
*event
= g_malloc0(sizeof(Event
));
129 event
->event_kind
= event_kind
;
130 event
->opaque
= opaque
;
131 event
->opaque2
= opaque2
;
135 QTAILQ_INSERT_TAIL(&events_list
, event
, events
);
136 replay_mutex_unlock();
139 void replay_bh_schedule_event(QEMUBH
*bh
)
141 if (replay_mode
!= REPLAY_MODE_NONE
&& events_enabled
) {
142 uint64_t id
= replay_get_current_step();
143 replay_add_event(REPLAY_ASYNC_EVENT_BH
, bh
, NULL
, id
);
145 qemu_bh_schedule(bh
);
149 void replay_add_input_event(struct InputEvent
*event
)
151 replay_add_event(REPLAY_ASYNC_EVENT_INPUT
, event
, NULL
, 0);
154 void replay_add_input_sync_event(void)
156 replay_add_event(REPLAY_ASYNC_EVENT_INPUT_SYNC
, NULL
, NULL
, 0);
159 void replay_block_event(QEMUBH
*bh
, uint64_t id
)
161 if (replay_mode
!= REPLAY_MODE_NONE
&& events_enabled
) {
162 replay_add_event(REPLAY_ASYNC_EVENT_BLOCK
, bh
, NULL
, id
);
164 qemu_bh_schedule(bh
);
168 static void replay_save_event(Event
*event
, int checkpoint
)
170 if (replay_mode
!= REPLAY_MODE_PLAY
) {
171 /* put the event into the file */
172 replay_put_event(EVENT_ASYNC
);
173 replay_put_byte(checkpoint
);
174 replay_put_byte(event
->event_kind
);
176 /* save event-specific data */
177 switch (event
->event_kind
) {
178 case REPLAY_ASYNC_EVENT_BH
:
179 replay_put_qword(event
->id
);
181 case REPLAY_ASYNC_EVENT_INPUT
:
182 replay_save_input_event(event
->opaque
);
184 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
186 case REPLAY_ASYNC_EVENT_CHAR_READ
:
187 replay_event_char_read_save(event
->opaque
);
189 case REPLAY_ASYNC_EVENT_BLOCK
:
190 replay_put_qword(event
->id
);
193 error_report("Unknown ID %" PRId64
" of replay event", event
->id
);
199 /* Called with replay mutex locked */
200 void replay_save_events(int checkpoint
)
202 while (!QTAILQ_EMPTY(&events_list
)) {
203 Event
*event
= QTAILQ_FIRST(&events_list
);
204 replay_save_event(event
, checkpoint
);
206 replay_mutex_unlock();
207 replay_run_event(event
);
209 QTAILQ_REMOVE(&events_list
, event
, events
);
214 static Event
*replay_read_event(int checkpoint
)
217 if (read_event_kind
== -1) {
218 read_checkpoint
= replay_get_byte();
219 read_event_kind
= replay_get_byte();
221 replay_check_error();
224 if (checkpoint
!= read_checkpoint
) {
228 /* Events that has not to be in the queue */
229 switch (read_event_kind
) {
230 case REPLAY_ASYNC_EVENT_BH
:
232 read_id
= replay_get_qword();
235 case REPLAY_ASYNC_EVENT_INPUT
:
236 event
= g_malloc0(sizeof(Event
));
237 event
->event_kind
= read_event_kind
;
238 event
->opaque
= replay_read_input_event();
240 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
241 event
= g_malloc0(sizeof(Event
));
242 event
->event_kind
= read_event_kind
;
245 case REPLAY_ASYNC_EVENT_CHAR_READ
:
246 event
= g_malloc0(sizeof(Event
));
247 event
->event_kind
= read_event_kind
;
248 event
->opaque
= replay_event_char_read_load();
250 case REPLAY_ASYNC_EVENT_BLOCK
:
252 read_id
= replay_get_qword();
256 error_report("Unknown ID %d of replay event", read_event_kind
);
261 QTAILQ_FOREACH(event
, &events_list
, events
) {
262 if (event
->event_kind
== read_event_kind
263 && (read_id
== -1 || read_id
== event
->id
)) {
269 QTAILQ_REMOVE(&events_list
, event
, events
);
274 /* Read event-specific data */
279 /* Called with replay mutex locked */
280 void replay_read_events(int checkpoint
)
282 while (replay_state
.data_kind
== EVENT_ASYNC
) {
283 Event
*event
= replay_read_event(checkpoint
);
287 replay_mutex_unlock();
288 replay_run_event(event
);
292 replay_finish_event();
293 read_event_kind
= -1;
297 void replay_init_events(void)
299 read_event_kind
= -1;
302 void replay_finish_events(void)
304 events_enabled
= false;
305 replay_clear_events();
308 bool replay_events_enabled(void)
310 return events_enabled
;
313 uint64_t blkreplay_next_id(void)
315 if (replay_events_enabled()) {
316 return replay_state
.block_request_id
++;