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
);
57 case REPLAY_ASYNC_EVENT_NET
:
58 replay_event_net_run(event
->opaque
);
61 error_report("Replay: invalid async event ID (%d) in the queue",
68 void replay_enable_events(void)
70 events_enabled
= true;
73 bool replay_has_events(void)
75 return !QTAILQ_EMPTY(&events_list
);
78 void replay_flush_events(void)
81 while (!QTAILQ_EMPTY(&events_list
)) {
82 Event
*event
= QTAILQ_FIRST(&events_list
);
83 replay_mutex_unlock();
84 replay_run_event(event
);
86 QTAILQ_REMOVE(&events_list
, event
, events
);
89 replay_mutex_unlock();
92 void replay_disable_events(void)
94 if (replay_mode
!= REPLAY_MODE_NONE
) {
95 events_enabled
= false;
96 /* Flush events queue before waiting of completion */
97 replay_flush_events();
101 void replay_clear_events(void)
104 while (!QTAILQ_EMPTY(&events_list
)) {
105 Event
*event
= QTAILQ_FIRST(&events_list
);
106 QTAILQ_REMOVE(&events_list
, event
, events
);
110 replay_mutex_unlock();
113 /*! Adds specified async event to the queue */
114 void replay_add_event(ReplayAsyncEventKind event_kind
,
116 void *opaque2
, uint64_t id
)
118 assert(event_kind
< REPLAY_ASYNC_COUNT
);
120 if (!replay_file
|| replay_mode
== REPLAY_MODE_NONE
121 || !events_enabled
) {
123 e
.event_kind
= event_kind
;
127 replay_run_event(&e
);
131 Event
*event
= g_malloc0(sizeof(Event
));
132 event
->event_kind
= event_kind
;
133 event
->opaque
= opaque
;
134 event
->opaque2
= opaque2
;
138 QTAILQ_INSERT_TAIL(&events_list
, event
, events
);
139 replay_mutex_unlock();
142 void replay_bh_schedule_event(QEMUBH
*bh
)
144 if (replay_mode
!= REPLAY_MODE_NONE
&& events_enabled
) {
145 uint64_t id
= replay_get_current_step();
146 replay_add_event(REPLAY_ASYNC_EVENT_BH
, bh
, NULL
, id
);
148 qemu_bh_schedule(bh
);
152 void replay_add_input_event(struct InputEvent
*event
)
154 replay_add_event(REPLAY_ASYNC_EVENT_INPUT
, event
, NULL
, 0);
157 void replay_add_input_sync_event(void)
159 replay_add_event(REPLAY_ASYNC_EVENT_INPUT_SYNC
, NULL
, NULL
, 0);
162 void replay_block_event(QEMUBH
*bh
, uint64_t id
)
164 if (replay_mode
!= REPLAY_MODE_NONE
&& events_enabled
) {
165 replay_add_event(REPLAY_ASYNC_EVENT_BLOCK
, bh
, NULL
, id
);
167 qemu_bh_schedule(bh
);
171 static void replay_save_event(Event
*event
, int checkpoint
)
173 if (replay_mode
!= REPLAY_MODE_PLAY
) {
174 /* put the event into the file */
175 replay_put_event(EVENT_ASYNC
);
176 replay_put_byte(checkpoint
);
177 replay_put_byte(event
->event_kind
);
179 /* save event-specific data */
180 switch (event
->event_kind
) {
181 case REPLAY_ASYNC_EVENT_BH
:
182 replay_put_qword(event
->id
);
184 case REPLAY_ASYNC_EVENT_INPUT
:
185 replay_save_input_event(event
->opaque
);
187 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
189 case REPLAY_ASYNC_EVENT_CHAR_READ
:
190 replay_event_char_read_save(event
->opaque
);
192 case REPLAY_ASYNC_EVENT_BLOCK
:
193 replay_put_qword(event
->id
);
195 case REPLAY_ASYNC_EVENT_NET
:
196 replay_event_net_save(event
->opaque
);
199 error_report("Unknown ID %" PRId64
" of replay event", event
->id
);
205 /* Called with replay mutex locked */
206 void replay_save_events(int checkpoint
)
208 while (!QTAILQ_EMPTY(&events_list
)) {
209 Event
*event
= QTAILQ_FIRST(&events_list
);
210 replay_save_event(event
, checkpoint
);
212 replay_mutex_unlock();
213 replay_run_event(event
);
215 QTAILQ_REMOVE(&events_list
, event
, events
);
220 static Event
*replay_read_event(int checkpoint
)
223 if (read_event_kind
== -1) {
224 read_checkpoint
= replay_get_byte();
225 read_event_kind
= replay_get_byte();
227 replay_check_error();
230 if (checkpoint
!= read_checkpoint
) {
234 /* Events that has not to be in the queue */
235 switch (read_event_kind
) {
236 case REPLAY_ASYNC_EVENT_BH
:
238 read_id
= replay_get_qword();
241 case REPLAY_ASYNC_EVENT_INPUT
:
242 event
= g_malloc0(sizeof(Event
));
243 event
->event_kind
= read_event_kind
;
244 event
->opaque
= replay_read_input_event();
246 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
247 event
= g_malloc0(sizeof(Event
));
248 event
->event_kind
= read_event_kind
;
251 case REPLAY_ASYNC_EVENT_CHAR_READ
:
252 event
= g_malloc0(sizeof(Event
));
253 event
->event_kind
= read_event_kind
;
254 event
->opaque
= replay_event_char_read_load();
256 case REPLAY_ASYNC_EVENT_BLOCK
:
258 read_id
= replay_get_qword();
261 case REPLAY_ASYNC_EVENT_NET
:
262 event
= g_malloc0(sizeof(Event
));
263 event
->event_kind
= read_event_kind
;
264 event
->opaque
= replay_event_net_load();
267 error_report("Unknown ID %d of replay event", read_event_kind
);
272 QTAILQ_FOREACH(event
, &events_list
, events
) {
273 if (event
->event_kind
== read_event_kind
274 && (read_id
== -1 || read_id
== event
->id
)) {
280 QTAILQ_REMOVE(&events_list
, event
, events
);
285 /* Read event-specific data */
290 /* Called with replay mutex locked */
291 void replay_read_events(int checkpoint
)
293 while (replay_state
.data_kind
== EVENT_ASYNC
) {
294 Event
*event
= replay_read_event(checkpoint
);
298 replay_mutex_unlock();
299 replay_run_event(event
);
303 replay_finish_event();
304 read_event_kind
= -1;
308 void replay_init_events(void)
310 read_event_kind
= -1;
313 void replay_finish_events(void)
315 events_enabled
= false;
316 replay_clear_events();
319 bool replay_events_enabled(void)
321 return events_enabled
;
324 uint64_t blkreplay_next_id(void)
326 if (replay_events_enabled()) {
327 return replay_state
.block_request_id
++;