summaryrefslogtreecommitdiff
path: root/server/tests/replay.c
blob: ba4a32d22cee702ecb3f31d7d31fbfdd65bd5da1 (plain)
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
/* Replay a previously recorded file (via SPICE_WORKER_RECORD_FILENAME)
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>

#include <spice/macros.h>
#include "red_replay_qxl.h"
#include "test_display_base.h"

static SpiceCoreInterface *core;
static SpiceServer *server;
static Replay *replay;

static void do_wakeup(void *opaque);

#define MEM_SLOT_GROUP_ID 0

/* Parts cribbed from spice-display.h/.c/qxl.c */

static QXLWorker *qxl_worker = NULL;

static QXLDevMemSlot slot = {
.slot_group_id = MEM_SLOT_GROUP_ID,
.slot_id = 0,
.generation = 0,
.virt_start = 0,
.virt_end = ~0,
.addr_delta = 0,
.qxl_ram_size = ~0,
};

static void attache_worker(QXLInstance *qin, QXLWorker *_qxl_worker)
{
    static int count = 0;
    if (++count > 1) {
        printf("%s ignored\n", __func__);
        return;
    }
    printf("%s\n", __func__);
    qxl_worker = _qxl_worker;
    qxl_worker->add_memslot(qxl_worker, &slot);
    qxl_worker->start(qxl_worker);
}

static void set_compression_level(QXLInstance *qin, int level)
{
    printf("%s\n", __func__);
}

static void set_mm_time(QXLInstance *qin, uint32_t mm_time)
{
}

// same as qemu/ui/spice-display.h
#define MAX_SURFACE_NUM 1024

static void get_init_info(QXLInstance *qin, QXLDevInitInfo *info)
{
    bzero(info, sizeof(*info));
    info->num_memslots = 1;
    info->num_memslots_groups = 1;
    info->memslot_id_bits = 1;
    info->memslot_gen_bits = 1;
    info->n_surfaces = MAX_SURFACE_NUM;
}

QXLCommandExt ext_cmd_last;
SpiceTimer *wakeup_timer;
pthread_mutex_t ext_cmd_mutex;
pid_t client_pid;

// called from spice_server thread (i.e. red_worker thread)
static int get_command(QXLInstance *qin, struct QXLCommandExt *ext)
{
    if (!wakeup_timer) {
        return FALSE;
    }
    pthread_mutex_lock(&ext_cmd_mutex);
    if (replay->eof || ext_cmd_last.cmd.data == 0) {
        pthread_mutex_unlock(&ext_cmd_mutex);
        return FALSE;
    }
    *ext = ext_cmd_last;
    ext_cmd_last.cmd.data = 0;
    pthread_mutex_unlock(&ext_cmd_mutex);
    core->timer_start(wakeup_timer, 1);
    return TRUE;
}

static void create_wakeup_timer(void)
{
    wakeup_timer = core->timer_add(do_wakeup, NULL);
    core->timer_start(wakeup_timer, 1);
}

static int req_cmd_notification(QXLInstance *qin)
{
    if (!wakeup_timer) {
        create_wakeup_timer();
    } else {
        core->timer_start(wakeup_timer, 1);
    }
    return TRUE;
}

static void end_replay(void)
{
    int child_status;

    fclose(replay->fd);
    free(replay);
    pthread_mutex_destroy(&ext_cmd_mutex);
    if (client_pid) {
        kill(client_pid, SIGINT);
        waitpid(client_pid, &child_status, 0);
    }
    exit(0);
}

static void do_wakeup(void *opaque)
{
    QXLCommandExt ext_cmd_cur;

    // We read the file here since this is not the worker thread, so we
    // can issue dispatcher calls like create_primary/destroy_primary
    pthread_mutex_lock(&ext_cmd_mutex);
    if (ext_cmd_last.cmd.data != 0) {
        pthread_mutex_unlock(&ext_cmd_mutex);
        // last command not read yet, sleep some more
        core->timer_start(wakeup_timer, 50);
        return;
    }
    pthread_mutex_unlock(&ext_cmd_mutex);
    replay_next_cmd(replay, qxl_worker, &ext_cmd_cur);
    if (replay->eof || ext_cmd_cur.cmd.data == 0) {
        if (ext_cmd_cur.cmd.data == 0) {
            fprintf(stderr, "null command returned from create_cmd_from_file\n");
        } else {
            fprintf(stderr, "end of file reached\n");
        }
        end_replay();
    }
    pthread_mutex_lock(&ext_cmd_mutex);
    ext_cmd_last = ext_cmd_cur;
    pthread_mutex_unlock(&ext_cmd_mutex);
    core->timer_start(wakeup_timer, 1);
    qxl_worker->wakeup(qxl_worker);
}

static void release_resource(QXLInstance *qin, struct QXLReleaseInfoExt release_info)
{
    QXLCommandExt *ext = (void*)release_info.info->id;
    return; // TODO - release resources from 
    //printf("%s\n", __func__);
    switch (ext->cmd.type) {
        case QXL_CMD_DRAW:
            break;
        case QXL_CMD_SURFACE:
            free(ext);
            break;
        default:
            abort();
    }
}

static int get_cursor_command(QXLInstance *qin, struct QXLCommandExt *ext)
{
    return FALSE;
}

static int req_cursor_notification(QXLInstance *qin)
{
    return TRUE;
}

static void notify_update(QXLInstance *qin, uint32_t update_id)
{
}

static int flush_resources(QXLInstance *qin)
{
    return TRUE;
}

static QXLInterface display_sif = {
    .base = {
        .type = SPICE_INTERFACE_QXL,
        .description = "replay",
        .major_version = SPICE_INTERFACE_QXL_MAJOR,
        .minor_version = SPICE_INTERFACE_QXL_MINOR
    },
    .attache_worker = attache_worker,
    .set_compression_level = set_compression_level,
    .set_mm_time = set_mm_time,
    .get_init_info = get_init_info,
    .get_command = get_command,
    .req_cmd_notification = req_cmd_notification,
    .release_resource = release_resource,
    .get_cursor_command = get_cursor_command,
    .req_cursor_notification = req_cursor_notification,
    .notify_update = notify_update,
    .flush_resources = flush_resources,
};

static QXLInstance display_sin = {
    .base = {
        .sif = &display_sif.base,
    },
    .id = 0,
};

static void replay_channel_event(int event, SpiceChannelEventInfo *info)
{
    static int channels = 0;

    if (info->type == SPICE_CHANNEL_DISPLAY) {
        create_wakeup_timer();
    }
}

static void start_client(char **argv)
{
    pid_t pid;

    if ((pid = fork()) == 0) {
        fclose(replay->fd); // close the command file
        /* child */
        fprintf(stderr, "launching %s\n", argv[0]);
        execv(argv[0], argv);
    } else {
        client_pid = pid;
    }
}

void usage(void *program)
{
    fprintf(stderr, "usage: %s <cmdlog_file> [<port> <client args>]\n", program);
    exit(1);
}

int main(int argc, char **argv)
{
    int port;
    char **client_argv = NULL;
    FILE *fd;

    core = basic_event_loop_init();
    core->channel_event = replay_channel_event;
    SpiceServer* server = spice_server_new();

    ext_cmd_last.cmd.data = 0;
    pthread_mutex_init(&ext_cmd_mutex, NULL);
    if (argc == 3) {
        usage(argv[0]);
        return 1; /* never reached */
    }
    if (argc >= 4) {
        port = atoi(argv[2]);
        if (port == 0) {
            usage(argv[0]);
        }
        client_argv = &argv[3];
    }
    if (strncmp(argv[1], "-", 1) == 0) {
        fd = stdin;
    } else {
        fd = fopen(argv[1], "r");
    }
    if (fd == NULL) {
        fprintf(stderr, "error opening %s\n", argv[1]);
        return 1;
    }
    replay = replay_from_fd(fd);
    printf("REPLAY: listening on port %d (unsecure)\n", port);
    spice_server_set_port(server, port);
    spice_server_set_noauth(server);
    spice_server_init(server, core);

    spice_server_add_interface(server, &display_sin.base);
    if (client_argv) {
        start_client(client_argv);
    } else {
        create_wakeup_timer();
    }
    basic_event_loop_mainloop();
    end_replay();
    return 0;
}