-
Notifications
You must be signed in to change notification settings - Fork 0
/
replay-events.c
319 lines (281 loc) · 7.8 KB
/
replay-events.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* replay-events.c
*
* Copyright (c) 2010-2015 Institute for System Programming
* of the Russian Academy of Sciences.
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*
*/
#include "qemu/osdep.h"
#include "qemu-common.h"
#include "qemu/error-report.h"
#include "sysemu/replay.h"
#include "replay-internal.h"
#include "block/aio.h"
#include "ui/input.h"
typedef struct Event {
ReplayAsyncEventKind event_kind;
void *opaque;
void *opaque2;
uint64_t id;
QTAILQ_ENTRY(Event) events;
} Event;
static QTAILQ_HEAD(, Event) events_list = QTAILQ_HEAD_INITIALIZER(events_list);
static unsigned int read_event_kind = -1;
static uint64_t read_id = -1;
static int read_checkpoint = -1;
static bool events_enabled;
/* Functions */
static void replay_run_event(Event *event)
{
switch (event->event_kind) {
case REPLAY_ASYNC_EVENT_BH:
aio_bh_call(event->opaque);
break;
case REPLAY_ASYNC_EVENT_INPUT:
qemu_input_event_send_impl(NULL, (InputEvent *)event->opaque);
qapi_free_InputEvent((InputEvent *)event->opaque);
break;
case REPLAY_ASYNC_EVENT_INPUT_SYNC:
qemu_input_event_sync_impl();
break;
case REPLAY_ASYNC_EVENT_CHAR_READ:
replay_event_char_read_run(event->opaque);
break;
case REPLAY_ASYNC_EVENT_BLOCK:
aio_bh_call(event->opaque);
break;
default:
error_report("Replay: invalid async event ID (%d) in the queue",
event->event_kind);
exit(1);
break;
}
}
void replay_enable_events(void)
{
events_enabled = true;
}
bool replay_has_events(void)
{
return !QTAILQ_EMPTY(&events_list);
}
void replay_flush_events(void)
{
replay_mutex_lock();
while (!QTAILQ_EMPTY(&events_list)) {
Event *event = QTAILQ_FIRST(&events_list);
replay_mutex_unlock();
replay_run_event(event);
replay_mutex_lock();
QTAILQ_REMOVE(&events_list, event, events);
g_free(event);
}
replay_mutex_unlock();
}
void replay_disable_events(void)
{
if (replay_mode != REPLAY_MODE_NONE) {
events_enabled = false;
/* Flush events queue before waiting of completion */
replay_flush_events();
}
}
void replay_clear_events(void)
{
replay_mutex_lock();
while (!QTAILQ_EMPTY(&events_list)) {
Event *event = QTAILQ_FIRST(&events_list);
QTAILQ_REMOVE(&events_list, event, events);
g_free(event);
}
replay_mutex_unlock();
}
/*! Adds specified async event to the queue */
void replay_add_event(ReplayAsyncEventKind event_kind,
void *opaque,
void *opaque2, uint64_t id)
{
assert(event_kind < REPLAY_ASYNC_COUNT);
if (!replay_file || replay_mode == REPLAY_MODE_NONE
|| !events_enabled) {
Event e;
e.event_kind = event_kind;
e.opaque = opaque;
e.opaque2 = opaque2;
e.id = id;
replay_run_event(&e);
return;
}
Event *event = g_malloc0(sizeof(Event));
event->event_kind = event_kind;
event->opaque = opaque;
event->opaque2 = opaque2;
event->id = id;
replay_mutex_lock();
QTAILQ_INSERT_TAIL(&events_list, event, events);
replay_mutex_unlock();
}
void replay_bh_schedule_event(QEMUBH *bh)
{
if (replay_mode != REPLAY_MODE_NONE && events_enabled) {
uint64_t id = replay_get_current_step();
replay_add_event(REPLAY_ASYNC_EVENT_BH, bh, NULL, id);
} else {
qemu_bh_schedule(bh);
}
}
void replay_add_input_event(struct InputEvent *event)
{
replay_add_event(REPLAY_ASYNC_EVENT_INPUT, event, NULL, 0);
}
void replay_add_input_sync_event(void)
{
replay_add_event(REPLAY_ASYNC_EVENT_INPUT_SYNC, NULL, NULL, 0);
}
void replay_block_event(QEMUBH *bh, uint64_t id)
{
if (replay_mode != REPLAY_MODE_NONE && events_enabled) {
replay_add_event(REPLAY_ASYNC_EVENT_BLOCK, bh, NULL, id);
} else {
qemu_bh_schedule(bh);
}
}
static void replay_save_event(Event *event, int checkpoint)
{
if (replay_mode != REPLAY_MODE_PLAY) {
/* put the event into the file */
replay_put_event(EVENT_ASYNC);
replay_put_byte(checkpoint);
replay_put_byte(event->event_kind);
/* save event-specific data */
switch (event->event_kind) {
case REPLAY_ASYNC_EVENT_BH:
replay_put_qword(event->id);
break;
case REPLAY_ASYNC_EVENT_INPUT:
replay_save_input_event(event->opaque);
break;
case REPLAY_ASYNC_EVENT_INPUT_SYNC:
break;
case REPLAY_ASYNC_EVENT_CHAR_READ:
replay_event_char_read_save(event->opaque);
break;
case REPLAY_ASYNC_EVENT_BLOCK:
replay_put_qword(event->id);
break;
default:
error_report("Unknown ID %" PRId64 " of replay event", event->id);
exit(1);
}
}
}
/* Called with replay mutex locked */
void replay_save_events(int checkpoint)
{
while (!QTAILQ_EMPTY(&events_list)) {
Event *event = QTAILQ_FIRST(&events_list);
replay_save_event(event, checkpoint);
replay_mutex_unlock();
replay_run_event(event);
replay_mutex_lock();
QTAILQ_REMOVE(&events_list, event, events);
g_free(event);
}
}
static Event *replay_read_event(int checkpoint)
{
Event *event;
if (read_event_kind == -1) {
read_checkpoint = replay_get_byte();
read_event_kind = replay_get_byte();
read_id = -1;
replay_check_error();
}
if (checkpoint != read_checkpoint) {
return NULL;
}
/* Events that has not to be in the queue */
switch (read_event_kind) {
case REPLAY_ASYNC_EVENT_BH:
if (read_id == -1) {
read_id = replay_get_qword();
}
break;
case REPLAY_ASYNC_EVENT_INPUT:
event = g_malloc0(sizeof(Event));
event->event_kind = read_event_kind;
event->opaque = replay_read_input_event();
return event;
case REPLAY_ASYNC_EVENT_INPUT_SYNC:
event = g_malloc0(sizeof(Event));
event->event_kind = read_event_kind;
event->opaque = 0;
return event;
case REPLAY_ASYNC_EVENT_CHAR_READ:
event = g_malloc0(sizeof(Event));
event->event_kind = read_event_kind;
event->opaque = replay_event_char_read_load();
return event;
case REPLAY_ASYNC_EVENT_BLOCK:
if (read_id == -1) {
read_id = replay_get_qword();
}
break;
default:
error_report("Unknown ID %d of replay event", read_event_kind);
exit(1);
break;
}
QTAILQ_FOREACH(event, &events_list, events) {
if (event->event_kind == read_event_kind
&& (read_id == -1 || read_id == event->id)) {
break;
}
}
if (event) {
QTAILQ_REMOVE(&events_list, event, events);
} else {
return NULL;
}
/* Read event-specific data */
return event;
}
/* Called with replay mutex locked */
void replay_read_events(int checkpoint)
{
while (replay_state.data_kind == EVENT_ASYNC) {
Event *event = replay_read_event(checkpoint);
if (!event) {
break;
}
replay_mutex_unlock();
replay_run_event(event);
replay_mutex_lock();
g_free(event);
replay_finish_event();
read_event_kind = -1;
}
}
void replay_init_events(void)
{
read_event_kind = -1;
}
void replay_finish_events(void)
{
events_enabled = false;
replay_clear_events();
}
bool replay_events_enabled(void)
{
return events_enabled;
}
uint64_t blkreplay_next_id(void)
{
if (replay_events_enabled()) {
return replay_state.block_request_id++;
}
return 0;
}