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 bool events_enabled
;
34 static void replay_run_event(Event
*event
)
36 switch (event
->event_kind
) {
37 case REPLAY_ASYNC_EVENT_BH
:
38 aio_bh_call(event
->opaque
);
40 case REPLAY_ASYNC_EVENT_INPUT
:
41 qemu_input_event_send_impl(NULL
, (InputEvent
*)event
->opaque
);
42 qapi_free_InputEvent((InputEvent
*)event
->opaque
);
44 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
45 qemu_input_event_sync_impl();
47 case REPLAY_ASYNC_EVENT_CHAR_READ
:
48 replay_event_char_read_run(event
->opaque
);
50 case REPLAY_ASYNC_EVENT_BLOCK
:
51 aio_bh_call(event
->opaque
);
53 case REPLAY_ASYNC_EVENT_NET
:
54 replay_event_net_run(event
->opaque
);
57 error_report("Replay: invalid async event ID (%d) in the queue",
64 void replay_enable_events(void)
66 if (replay_mode
!= REPLAY_MODE_NONE
) {
67 events_enabled
= true;
71 bool replay_has_events(void)
73 return !QTAILQ_EMPTY(&events_list
);
76 void replay_flush_events(void)
78 g_assert(replay_mutex_locked());
80 while (!QTAILQ_EMPTY(&events_list
)) {
81 Event
*event
= QTAILQ_FIRST(&events_list
);
82 replay_run_event(event
);
83 QTAILQ_REMOVE(&events_list
, event
, events
);
88 void replay_disable_events(void)
90 if (replay_mode
!= REPLAY_MODE_NONE
) {
91 events_enabled
= false;
92 /* Flush events queue before waiting of completion */
93 replay_flush_events();
97 /*! Adds specified async event to the queue */
98 void replay_add_event(ReplayAsyncEventKind event_kind
,
100 void *opaque2
, uint64_t id
)
102 assert(event_kind
< REPLAY_ASYNC_COUNT
);
104 if (!replay_file
|| replay_mode
== REPLAY_MODE_NONE
105 || !events_enabled
) {
107 e
.event_kind
= event_kind
;
111 replay_run_event(&e
);
115 Event
*event
= g_malloc0(sizeof(Event
));
116 event
->event_kind
= event_kind
;
117 event
->opaque
= opaque
;
118 event
->opaque2
= opaque2
;
121 g_assert(replay_mutex_locked());
122 QTAILQ_INSERT_TAIL(&events_list
, event
, events
);
125 void replay_bh_schedule_event(QEMUBH
*bh
)
127 if (events_enabled
) {
128 uint64_t id
= replay_get_current_step();
129 replay_add_event(REPLAY_ASYNC_EVENT_BH
, bh
, NULL
, id
);
131 qemu_bh_schedule(bh
);
135 void replay_add_input_event(struct InputEvent
*event
)
137 replay_add_event(REPLAY_ASYNC_EVENT_INPUT
, event
, NULL
, 0);
140 void replay_add_input_sync_event(void)
142 replay_add_event(REPLAY_ASYNC_EVENT_INPUT_SYNC
, NULL
, NULL
, 0);
145 void replay_block_event(QEMUBH
*bh
, uint64_t id
)
147 if (events_enabled
) {
148 replay_add_event(REPLAY_ASYNC_EVENT_BLOCK
, bh
, NULL
, id
);
150 qemu_bh_schedule(bh
);
154 static void replay_save_event(Event
*event
, int checkpoint
)
156 if (replay_mode
!= REPLAY_MODE_PLAY
) {
157 /* put the event into the file */
158 replay_put_event(EVENT_ASYNC
);
159 replay_put_byte(checkpoint
);
160 replay_put_byte(event
->event_kind
);
162 /* save event-specific data */
163 switch (event
->event_kind
) {
164 case REPLAY_ASYNC_EVENT_BH
:
165 replay_put_qword(event
->id
);
167 case REPLAY_ASYNC_EVENT_INPUT
:
168 replay_save_input_event(event
->opaque
);
170 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
172 case REPLAY_ASYNC_EVENT_CHAR_READ
:
173 replay_event_char_read_save(event
->opaque
);
175 case REPLAY_ASYNC_EVENT_BLOCK
:
176 replay_put_qword(event
->id
);
178 case REPLAY_ASYNC_EVENT_NET
:
179 replay_event_net_save(event
->opaque
);
182 error_report("Unknown ID %" PRId64
" of replay event", event
->id
);
188 /* Called with replay mutex locked */
189 void replay_save_events(int checkpoint
)
191 g_assert(replay_mutex_locked());
192 g_assert(checkpoint
!= CHECKPOINT_CLOCK_WARP_START
);
193 while (!QTAILQ_EMPTY(&events_list
)) {
194 Event
*event
= QTAILQ_FIRST(&events_list
);
195 replay_save_event(event
, checkpoint
);
196 replay_run_event(event
);
197 QTAILQ_REMOVE(&events_list
, event
, events
);
202 static Event
*replay_read_event(int checkpoint
)
205 if (replay_state
.read_event_kind
== -1) {
206 replay_state
.read_event_checkpoint
= replay_get_byte();
207 replay_state
.read_event_kind
= replay_get_byte();
208 replay_state
.read_event_id
= -1;
209 replay_check_error();
212 if (checkpoint
!= replay_state
.read_event_checkpoint
) {
216 /* Events that has not to be in the queue */
217 switch (replay_state
.read_event_kind
) {
218 case REPLAY_ASYNC_EVENT_BH
:
219 if (replay_state
.read_event_id
== -1) {
220 replay_state
.read_event_id
= replay_get_qword();
223 case REPLAY_ASYNC_EVENT_INPUT
:
224 event
= g_malloc0(sizeof(Event
));
225 event
->event_kind
= replay_state
.read_event_kind
;
226 event
->opaque
= replay_read_input_event();
228 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
229 event
= g_malloc0(sizeof(Event
));
230 event
->event_kind
= replay_state
.read_event_kind
;
233 case REPLAY_ASYNC_EVENT_CHAR_READ
:
234 event
= g_malloc0(sizeof(Event
));
235 event
->event_kind
= replay_state
.read_event_kind
;
236 event
->opaque
= replay_event_char_read_load();
238 case REPLAY_ASYNC_EVENT_BLOCK
:
239 if (replay_state
.read_event_id
== -1) {
240 replay_state
.read_event_id
= replay_get_qword();
243 case REPLAY_ASYNC_EVENT_NET
:
244 event
= g_malloc0(sizeof(Event
));
245 event
->event_kind
= replay_state
.read_event_kind
;
246 event
->opaque
= replay_event_net_load();
249 error_report("Unknown ID %d of replay event",
250 replay_state
.read_event_kind
);
255 QTAILQ_FOREACH(event
, &events_list
, events
) {
256 if (event
->event_kind
== replay_state
.read_event_kind
257 && (replay_state
.read_event_id
== -1
258 || replay_state
.read_event_id
== event
->id
)) {
264 QTAILQ_REMOVE(&events_list
, event
, events
);
269 /* Read event-specific data */
274 /* Called with replay mutex locked */
275 void replay_read_events(int checkpoint
)
277 g_assert(replay_mutex_locked());
278 while (replay_state
.data_kind
== EVENT_ASYNC
) {
279 Event
*event
= replay_read_event(checkpoint
);
283 replay_finish_event();
284 replay_state
.read_event_kind
= -1;
285 replay_run_event(event
);
291 void replay_init_events(void)
293 replay_state
.read_event_kind
= -1;
296 void replay_finish_events(void)
298 events_enabled
= false;
299 replay_flush_events();
302 bool replay_events_enabled(void)
304 return events_enabled
;
307 uint64_t blkreplay_next_id(void)
309 if (replay_events_enabled()) {
310 return replay_state
.block_request_id
++;