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();
52 error_report("Replay: invalid async event ID (%d) in the queue",
59 void replay_enable_events(void)
61 events_enabled
= true;
64 bool replay_has_events(void)
66 return !QTAILQ_EMPTY(&events_list
);
69 void replay_flush_events(void)
72 while (!QTAILQ_EMPTY(&events_list
)) {
73 Event
*event
= QTAILQ_FIRST(&events_list
);
74 replay_mutex_unlock();
75 replay_run_event(event
);
77 QTAILQ_REMOVE(&events_list
, event
, events
);
80 replay_mutex_unlock();
83 void replay_disable_events(void)
85 if (replay_mode
!= REPLAY_MODE_NONE
) {
86 events_enabled
= false;
87 /* Flush events queue before waiting of completion */
88 replay_flush_events();
92 void replay_clear_events(void)
95 while (!QTAILQ_EMPTY(&events_list
)) {
96 Event
*event
= QTAILQ_FIRST(&events_list
);
97 QTAILQ_REMOVE(&events_list
, event
, events
);
101 replay_mutex_unlock();
104 /*! Adds specified async event to the queue */
105 static void replay_add_event(ReplayAsyncEventKind event_kind
,
107 void *opaque2
, uint64_t id
)
109 assert(event_kind
< REPLAY_ASYNC_COUNT
);
111 if (!replay_file
|| replay_mode
== REPLAY_MODE_NONE
112 || !events_enabled
) {
114 e
.event_kind
= event_kind
;
118 replay_run_event(&e
);
122 Event
*event
= g_malloc0(sizeof(Event
));
123 event
->event_kind
= event_kind
;
124 event
->opaque
= opaque
;
125 event
->opaque2
= opaque2
;
129 QTAILQ_INSERT_TAIL(&events_list
, event
, events
);
130 replay_mutex_unlock();
133 void replay_bh_schedule_event(QEMUBH
*bh
)
135 if (replay_mode
!= REPLAY_MODE_NONE
) {
136 uint64_t id
= replay_get_current_step();
137 replay_add_event(REPLAY_ASYNC_EVENT_BH
, bh
, NULL
, id
);
139 qemu_bh_schedule(bh
);
143 void replay_add_input_event(struct InputEvent
*event
)
145 replay_add_event(REPLAY_ASYNC_EVENT_INPUT
, event
, NULL
, 0);
148 void replay_add_input_sync_event(void)
150 replay_add_event(REPLAY_ASYNC_EVENT_INPUT_SYNC
, NULL
, NULL
, 0);
153 static void replay_save_event(Event
*event
, int checkpoint
)
155 if (replay_mode
!= REPLAY_MODE_PLAY
) {
156 /* put the event into the file */
157 replay_put_event(EVENT_ASYNC
);
158 replay_put_byte(checkpoint
);
159 replay_put_byte(event
->event_kind
);
161 /* save event-specific data */
162 switch (event
->event_kind
) {
163 case REPLAY_ASYNC_EVENT_BH
:
164 replay_put_qword(event
->id
);
166 case REPLAY_ASYNC_EVENT_INPUT
:
167 replay_save_input_event(event
->opaque
);
169 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
172 error_report("Unknown ID %d of replay event", read_event_kind
);
178 /* Called with replay mutex locked */
179 void replay_save_events(int checkpoint
)
181 while (!QTAILQ_EMPTY(&events_list
)) {
182 Event
*event
= QTAILQ_FIRST(&events_list
);
183 replay_save_event(event
, checkpoint
);
185 replay_mutex_unlock();
186 replay_run_event(event
);
188 QTAILQ_REMOVE(&events_list
, event
, events
);
193 static Event
*replay_read_event(int checkpoint
)
196 if (read_event_kind
== -1) {
197 read_checkpoint
= replay_get_byte();
198 read_event_kind
= replay_get_byte();
200 replay_check_error();
203 if (checkpoint
!= read_checkpoint
) {
207 /* Events that has not to be in the queue */
208 switch (read_event_kind
) {
209 case REPLAY_ASYNC_EVENT_BH
:
211 read_id
= replay_get_qword();
214 case REPLAY_ASYNC_EVENT_INPUT
:
215 event
= g_malloc0(sizeof(Event
));
216 event
->event_kind
= read_event_kind
;
217 event
->opaque
= replay_read_input_event();
219 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
220 event
= g_malloc0(sizeof(Event
));
221 event
->event_kind
= read_event_kind
;
225 error_report("Unknown ID %d of replay event", read_event_kind
);
230 QTAILQ_FOREACH(event
, &events_list
, events
) {
231 if (event
->event_kind
== read_event_kind
232 && (read_id
== -1 || read_id
== event
->id
)) {
238 QTAILQ_REMOVE(&events_list
, event
, events
);
243 /* Read event-specific data */
248 /* Called with replay mutex locked */
249 void replay_read_events(int checkpoint
)
251 while (replay_data_kind
== EVENT_ASYNC
) {
252 Event
*event
= replay_read_event(checkpoint
);
256 replay_mutex_unlock();
257 replay_run_event(event
);
261 replay_finish_event();
262 read_event_kind
= -1;
266 void replay_init_events(void)
268 read_event_kind
= -1;
271 void replay_finish_events(void)
273 events_enabled
= false;
274 replay_clear_events();
277 bool replay_events_enabled(void)
279 return events_enabled
;