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-common.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 unsigned int read_event_kind
= -1;
30 static uint64_t read_id
= -1;
31 static int read_checkpoint
= -1;
33 static bool events_enabled
;
37 static void replay_run_event(Event
*event
)
39 switch (event
->event_kind
) {
40 case REPLAY_ASYNC_EVENT_BH
:
41 aio_bh_call(event
->opaque
);
43 case REPLAY_ASYNC_EVENT_INPUT
:
44 qemu_input_event_send_impl(NULL
, (InputEvent
*)event
->opaque
);
45 qapi_free_InputEvent((InputEvent
*)event
->opaque
);
47 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
48 qemu_input_event_sync_impl();
51 error_report("Replay: invalid async event ID (%d) in the queue",
58 void replay_enable_events(void)
60 events_enabled
= true;
63 bool replay_has_events(void)
65 return !QTAILQ_EMPTY(&events_list
);
68 void replay_flush_events(void)
71 while (!QTAILQ_EMPTY(&events_list
)) {
72 Event
*event
= QTAILQ_FIRST(&events_list
);
73 replay_mutex_unlock();
74 replay_run_event(event
);
76 QTAILQ_REMOVE(&events_list
, event
, events
);
79 replay_mutex_unlock();
82 void replay_disable_events(void)
84 if (replay_mode
!= REPLAY_MODE_NONE
) {
85 events_enabled
= false;
86 /* Flush events queue before waiting of completion */
87 replay_flush_events();
91 void replay_clear_events(void)
94 while (!QTAILQ_EMPTY(&events_list
)) {
95 Event
*event
= QTAILQ_FIRST(&events_list
);
96 QTAILQ_REMOVE(&events_list
, event
, events
);
100 replay_mutex_unlock();
103 /*! Adds specified async event to the queue */
104 static 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
;
128 QTAILQ_INSERT_TAIL(&events_list
, event
, events
);
129 replay_mutex_unlock();
132 void replay_bh_schedule_event(QEMUBH
*bh
)
134 if (replay_mode
!= REPLAY_MODE_NONE
) {
135 uint64_t id
= replay_get_current_step();
136 replay_add_event(REPLAY_ASYNC_EVENT_BH
, bh
, NULL
, id
);
138 qemu_bh_schedule(bh
);
142 void replay_add_input_event(struct InputEvent
*event
)
144 replay_add_event(REPLAY_ASYNC_EVENT_INPUT
, event
, NULL
, 0);
147 void replay_add_input_sync_event(void)
149 replay_add_event(REPLAY_ASYNC_EVENT_INPUT_SYNC
, NULL
, NULL
, 0);
152 static void replay_save_event(Event
*event
, int checkpoint
)
154 if (replay_mode
!= REPLAY_MODE_PLAY
) {
155 /* put the event into the file */
156 replay_put_event(EVENT_ASYNC
);
157 replay_put_byte(checkpoint
);
158 replay_put_byte(event
->event_kind
);
160 /* save event-specific data */
161 switch (event
->event_kind
) {
162 case REPLAY_ASYNC_EVENT_BH
:
163 replay_put_qword(event
->id
);
165 case REPLAY_ASYNC_EVENT_INPUT
:
166 replay_save_input_event(event
->opaque
);
168 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
171 error_report("Unknown ID %d of replay event", read_event_kind
);
177 /* Called with replay mutex locked */
178 void replay_save_events(int checkpoint
)
180 while (!QTAILQ_EMPTY(&events_list
)) {
181 Event
*event
= QTAILQ_FIRST(&events_list
);
182 replay_save_event(event
, checkpoint
);
184 replay_mutex_unlock();
185 replay_run_event(event
);
187 QTAILQ_REMOVE(&events_list
, event
, events
);
192 static Event
*replay_read_event(int checkpoint
)
195 if (read_event_kind
== -1) {
196 read_checkpoint
= replay_get_byte();
197 read_event_kind
= replay_get_byte();
199 replay_check_error();
202 if (checkpoint
!= read_checkpoint
) {
206 /* Events that has not to be in the queue */
207 switch (read_event_kind
) {
208 case REPLAY_ASYNC_EVENT_BH
:
210 read_id
= replay_get_qword();
213 case REPLAY_ASYNC_EVENT_INPUT
:
214 event
= g_malloc0(sizeof(Event
));
215 event
->event_kind
= read_event_kind
;
216 event
->opaque
= replay_read_input_event();
218 case REPLAY_ASYNC_EVENT_INPUT_SYNC
:
219 event
= g_malloc0(sizeof(Event
));
220 event
->event_kind
= read_event_kind
;
224 error_report("Unknown ID %d of replay event", read_event_kind
);
229 QTAILQ_FOREACH(event
, &events_list
, events
) {
230 if (event
->event_kind
== read_event_kind
231 && (read_id
== -1 || read_id
== event
->id
)) {
237 QTAILQ_REMOVE(&events_list
, event
, events
);
242 /* Read event-specific data */
247 /* Called with replay mutex locked */
248 void replay_read_events(int checkpoint
)
250 while (replay_data_kind
== EVENT_ASYNC
) {
251 Event
*event
= replay_read_event(checkpoint
);
255 replay_mutex_unlock();
256 replay_run_event(event
);
260 replay_finish_event();
261 read_event_kind
= -1;
265 void replay_init_events(void)
267 read_event_kind
= -1;
270 void replay_finish_events(void)
272 events_enabled
= false;
273 replay_clear_events();
276 bool replay_events_enabled(void)
278 return events_enabled
;