iotests: add transactional incremental backup test
[qemu/ar7.git] / replay / replay-events.c
blob402f644067b76612f7373348a0b0700616b59e88
1 /*
2 * replay-events.c
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"
17 #include "ui/input.h"
19 typedef struct Event {
20 ReplayAsyncEventKind event_kind;
21 void *opaque;
22 void *opaque2;
23 uint64_t id;
25 QTAILQ_ENTRY(Event) events;
26 } Event;
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;
35 /* Functions */
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);
42 break;
43 case REPLAY_ASYNC_EVENT_INPUT:
44 qemu_input_event_send_impl(NULL, (InputEvent *)event->opaque);
45 qapi_free_InputEvent((InputEvent *)event->opaque);
46 break;
47 case REPLAY_ASYNC_EVENT_INPUT_SYNC:
48 qemu_input_event_sync_impl();
49 break;
50 default:
51 error_report("Replay: invalid async event ID (%d) in the queue",
52 event->event_kind);
53 exit(1);
54 break;
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)
70 replay_mutex_lock();
71 while (!QTAILQ_EMPTY(&events_list)) {
72 Event *event = QTAILQ_FIRST(&events_list);
73 replay_mutex_unlock();
74 replay_run_event(event);
75 replay_mutex_lock();
76 QTAILQ_REMOVE(&events_list, event, events);
77 g_free(event);
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)
93 replay_mutex_lock();
94 while (!QTAILQ_EMPTY(&events_list)) {
95 Event *event = QTAILQ_FIRST(&events_list);
96 QTAILQ_REMOVE(&events_list, event, events);
98 g_free(event);
100 replay_mutex_unlock();
103 /*! Adds specified async event to the queue */
104 static void replay_add_event(ReplayAsyncEventKind event_kind,
105 void *opaque,
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) {
112 Event e;
113 e.event_kind = event_kind;
114 e.opaque = opaque;
115 e.opaque2 = opaque2;
116 e.id = id;
117 replay_run_event(&e);
118 return;
121 Event *event = g_malloc0(sizeof(Event));
122 event->event_kind = event_kind;
123 event->opaque = opaque;
124 event->opaque2 = opaque2;
125 event->id = id;
127 replay_mutex_lock();
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);
137 } else {
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);
164 break;
165 case REPLAY_ASYNC_EVENT_INPUT:
166 replay_save_input_event(event->opaque);
167 break;
168 case REPLAY_ASYNC_EVENT_INPUT_SYNC:
169 break;
170 default:
171 error_report("Unknown ID %d of replay event", read_event_kind);
172 exit(1);
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);
186 replay_mutex_lock();
187 QTAILQ_REMOVE(&events_list, event, events);
188 g_free(event);
192 static Event *replay_read_event(int checkpoint)
194 Event *event;
195 if (read_event_kind == -1) {
196 read_checkpoint = replay_get_byte();
197 read_event_kind = replay_get_byte();
198 read_id = -1;
199 replay_check_error();
202 if (checkpoint != read_checkpoint) {
203 return NULL;
206 /* Events that has not to be in the queue */
207 switch (read_event_kind) {
208 case REPLAY_ASYNC_EVENT_BH:
209 if (read_id == -1) {
210 read_id = replay_get_qword();
212 break;
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();
217 return event;
218 case REPLAY_ASYNC_EVENT_INPUT_SYNC:
219 event = g_malloc0(sizeof(Event));
220 event->event_kind = read_event_kind;
221 event->opaque = 0;
222 return event;
223 default:
224 error_report("Unknown ID %d of replay event", read_event_kind);
225 exit(1);
226 break;
229 QTAILQ_FOREACH(event, &events_list, events) {
230 if (event->event_kind == read_event_kind
231 && (read_id == -1 || read_id == event->id)) {
232 break;
236 if (event) {
237 QTAILQ_REMOVE(&events_list, event, events);
238 } else {
239 return NULL;
242 /* Read event-specific data */
244 return event;
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);
252 if (!event) {
253 break;
255 replay_mutex_unlock();
256 replay_run_event(event);
257 replay_mutex_lock();
259 g_free(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;