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/error-report.h"
14 #include "sysemu/replay.h"
15 #include "replay-internal.h"
16 #include "block/aio.h"
19 typedef struct Event
{
20 ReplayAsyncEventKind event_kind
;
25 QTAILQ_ENTRY(Event
) events
;
28 static QTAILQ_HEAD(, Event
) events_list
= QTAILQ_HEAD_INITIALIZER(events_list
);
29 static bool events_enabled
;
33 static void replay_run_event(Event
*event
)
35 switch (event
->event_kind
) {
36 case REPLAY_ASYNC_EVENT_BH
:
37 aio_bh_call(event
->opaque
);
39 case REPLAY_ASYNC_EVENT_BH_ONESHOT
:
40 ((QEMUBHFunc
*)event
->opaque
)(event
->opaque2
);
42 case REPLAY_ASYNC_EVENT_INPUT
:
43 qemu_input_event_send_impl(NULL
, (InputEvent
*)event
->opaque
);
44 qapi_free_InputEvent((InputEvent
*)event
->opaque
);
46 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
47 qemu_input_event_sync_impl();
49 case REPLAY_ASYNC_EVENT_CHAR_READ
:
50 replay_event_char_read_run(event
->opaque
);
52 case REPLAY_ASYNC_EVENT_BLOCK
:
53 aio_bh_call(event
->opaque
);
55 case REPLAY_ASYNC_EVENT_NET
:
56 replay_event_net_run(event
->opaque
);
59 error_report("Replay: invalid async event ID (%d) in the queue",
66 void replay_enable_events(void)
68 if (replay_mode
!= REPLAY_MODE_NONE
) {
69 events_enabled
= true;
73 bool replay_has_events(void)
75 return !QTAILQ_EMPTY(&events_list
);
78 void replay_flush_events(void)
80 if (replay_mode
== REPLAY_MODE_NONE
) {
84 g_assert(replay_mutex_locked());
86 while (!QTAILQ_EMPTY(&events_list
)) {
87 Event
*event
= QTAILQ_FIRST(&events_list
);
88 replay_run_event(event
);
89 QTAILQ_REMOVE(&events_list
, event
, events
);
94 void replay_disable_events(void)
96 if (replay_mode
!= REPLAY_MODE_NONE
) {
97 events_enabled
= false;
98 /* Flush events queue before waiting of completion */
99 replay_flush_events();
103 /*! Adds specified async event to the queue */
104 void replay_add_event(ReplayAsyncEventKind event_kind
,
106 void *opaque2
, uint64_t id
)
108 assert(event_kind
< REPLAY_ASYNC_COUNT
);
110 if (!replay_file
|| replay_mode
== REPLAY_MODE_NONE
111 || !events_enabled
) {
113 e
.event_kind
= event_kind
;
117 replay_run_event(&e
);
121 Event
*event
= g_malloc0(sizeof(Event
));
122 event
->event_kind
= event_kind
;
123 event
->opaque
= opaque
;
124 event
->opaque2
= opaque2
;
127 g_assert(replay_mutex_locked());
128 QTAILQ_INSERT_TAIL(&events_list
, event
, events
);
131 void replay_bh_schedule_event(QEMUBH
*bh
)
133 if (events_enabled
) {
134 uint64_t id
= replay_get_current_icount();
135 replay_add_event(REPLAY_ASYNC_EVENT_BH
, bh
, NULL
, id
);
137 qemu_bh_schedule(bh
);
141 void replay_bh_schedule_oneshot_event(AioContext
*ctx
,
142 QEMUBHFunc
*cb
, void *opaque
)
144 if (events_enabled
) {
145 uint64_t id
= replay_get_current_icount();
146 replay_add_event(REPLAY_ASYNC_EVENT_BH_ONESHOT
, cb
, opaque
, id
);
148 aio_bh_schedule_oneshot(ctx
, cb
, opaque
);
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 (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 case REPLAY_ASYNC_EVENT_BH_ONESHOT
:
183 replay_put_qword(event
->id
);
185 case REPLAY_ASYNC_EVENT_INPUT
:
186 replay_save_input_event(event
->opaque
);
188 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
190 case REPLAY_ASYNC_EVENT_CHAR_READ
:
191 replay_event_char_read_save(event
->opaque
);
193 case REPLAY_ASYNC_EVENT_BLOCK
:
194 replay_put_qword(event
->id
);
196 case REPLAY_ASYNC_EVENT_NET
:
197 replay_event_net_save(event
->opaque
);
200 error_report("Unknown ID %" PRId64
" of replay event", event
->id
);
206 /* Called with replay mutex locked */
207 void replay_save_events(int checkpoint
)
209 g_assert(replay_mutex_locked());
210 g_assert(checkpoint
!= CHECKPOINT_CLOCK_WARP_START
);
211 g_assert(checkpoint
!= CHECKPOINT_CLOCK_VIRTUAL
);
212 while (!QTAILQ_EMPTY(&events_list
)) {
213 Event
*event
= QTAILQ_FIRST(&events_list
);
214 replay_save_event(event
, checkpoint
);
215 replay_run_event(event
);
216 QTAILQ_REMOVE(&events_list
, event
, events
);
221 static Event
*replay_read_event(int checkpoint
)
224 if (replay_state
.read_event_kind
== -1) {
225 replay_state
.read_event_checkpoint
= replay_get_byte();
226 replay_state
.read_event_kind
= replay_get_byte();
227 replay_state
.read_event_id
= -1;
228 replay_check_error();
231 if (checkpoint
!= replay_state
.read_event_checkpoint
) {
235 /* Events that has not to be in the queue */
236 switch (replay_state
.read_event_kind
) {
237 case REPLAY_ASYNC_EVENT_BH
:
238 case REPLAY_ASYNC_EVENT_BH_ONESHOT
:
239 if (replay_state
.read_event_id
== -1) {
240 replay_state
.read_event_id
= replay_get_qword();
243 case REPLAY_ASYNC_EVENT_INPUT
:
244 event
= g_malloc0(sizeof(Event
));
245 event
->event_kind
= replay_state
.read_event_kind
;
246 event
->opaque
= replay_read_input_event();
248 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
249 event
= g_malloc0(sizeof(Event
));
250 event
->event_kind
= replay_state
.read_event_kind
;
253 case REPLAY_ASYNC_EVENT_CHAR_READ
:
254 event
= g_malloc0(sizeof(Event
));
255 event
->event_kind
= replay_state
.read_event_kind
;
256 event
->opaque
= replay_event_char_read_load();
258 case REPLAY_ASYNC_EVENT_BLOCK
:
259 if (replay_state
.read_event_id
== -1) {
260 replay_state
.read_event_id
= replay_get_qword();
263 case REPLAY_ASYNC_EVENT_NET
:
264 event
= g_malloc0(sizeof(Event
));
265 event
->event_kind
= replay_state
.read_event_kind
;
266 event
->opaque
= replay_event_net_load();
269 error_report("Unknown ID %d of replay event",
270 replay_state
.read_event_kind
);
275 QTAILQ_FOREACH(event
, &events_list
, events
) {
276 if (event
->event_kind
== replay_state
.read_event_kind
277 && (replay_state
.read_event_id
== -1
278 || replay_state
.read_event_id
== event
->id
)) {
284 QTAILQ_REMOVE(&events_list
, event
, events
);
289 /* Read event-specific data */
294 /* Called with replay mutex locked */
295 void replay_read_events(int checkpoint
)
297 g_assert(replay_mutex_locked());
298 while (replay_state
.data_kind
== EVENT_ASYNC
) {
299 Event
*event
= replay_read_event(checkpoint
);
303 replay_finish_event();
304 replay_state
.read_event_kind
= -1;
305 replay_run_event(event
);
311 void replay_init_events(void)
313 replay_state
.read_event_kind
= -1;
316 void replay_finish_events(void)
318 events_enabled
= false;
319 replay_flush_events();
322 bool replay_events_enabled(void)
324 return events_enabled
;
327 uint64_t blkreplay_next_id(void)
329 if (replay_events_enabled()) {
330 return replay_state
.block_request_id
++;