migration: merge QEMUFileBuffered into MigrationState

Avoid splitting the state of outgoing migration, more or less arbitrarily,
between two data structures.  QEMUFileBuffered anyway is used only during
migration.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
This commit is contained in:
Juan Quintela 2012-12-19 09:55:50 +01:00
parent e659586e63
commit 9848a40427
2 changed files with 32 additions and 37 deletions

View File

@ -16,6 +16,7 @@
#include "qapi/qmp/qdict.h" #include "qapi/qmp/qdict.h"
#include "qemu-common.h" #include "qemu-common.h"
#include "qemu/thread.h"
#include "qemu/notify.h" #include "qemu/notify.h"
#include "qapi/error.h" #include "qapi/error.h"
#include "migration/vmstate.h" #include "migration/vmstate.h"
@ -31,6 +32,13 @@ typedef struct MigrationState MigrationState;
struct MigrationState struct MigrationState
{ {
int64_t bandwidth_limit; int64_t bandwidth_limit;
size_t bytes_xfer;
size_t xfer_limit;
uint8_t *buffer;
size_t buffer_size;
size_t buffer_capacity;
QemuThread thread;
QEMUFile *file; QEMUFile *file;
int fd; int fd;
int state; int state;

View File

@ -521,18 +521,8 @@ int64_t migrate_xbzrle_cache_size(void)
/* migration thread support */ /* migration thread support */
typedef struct QEMUFileBuffered {
MigrationState *migration_state;
QEMUFile *file;
size_t bytes_xfer;
size_t xfer_limit;
uint8_t *buffer;
size_t buffer_size;
size_t buffer_capacity;
QemuThread thread;
} QEMUFileBuffered;
static ssize_t buffered_flush(QEMUFileBuffered *s) static ssize_t buffered_flush(MigrationState *s)
{ {
size_t offset = 0; size_t offset = 0;
ssize_t ret = 0; ssize_t ret = 0;
@ -541,8 +531,7 @@ static ssize_t buffered_flush(QEMUFileBuffered *s)
while (s->bytes_xfer < s->xfer_limit && offset < s->buffer_size) { while (s->bytes_xfer < s->xfer_limit && offset < s->buffer_size) {
size_t to_send = MIN(s->buffer_size - offset, s->xfer_limit - s->bytes_xfer); size_t to_send = MIN(s->buffer_size - offset, s->xfer_limit - s->bytes_xfer);
ret = migrate_fd_put_buffer(s->migration_state, s->buffer + offset, ret = migrate_fd_put_buffer(s, s->buffer + offset, to_send);
to_send);
if (ret <= 0) { if (ret <= 0) {
DPRINTF("error flushing data, %zd\n", ret); DPRINTF("error flushing data, %zd\n", ret);
break; break;
@ -566,7 +555,7 @@ static ssize_t buffered_flush(QEMUFileBuffered *s)
static int buffered_put_buffer(void *opaque, const uint8_t *buf, static int buffered_put_buffer(void *opaque, const uint8_t *buf,
int64_t pos, int size) int64_t pos, int size)
{ {
QEMUFileBuffered *s = opaque; MigrationState *s = opaque;
ssize_t error; ssize_t error;
DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos); DPRINTF("putting %d bytes at %" PRId64 "\n", size, pos);
@ -598,7 +587,7 @@ static int buffered_put_buffer(void *opaque, const uint8_t *buf,
static int buffered_close(void *opaque) static int buffered_close(void *opaque)
{ {
QEMUFileBuffered *s = opaque; MigrationState *s = opaque;
ssize_t ret = 0; ssize_t ret = 0;
int ret2; int ret2;
@ -612,20 +601,20 @@ static int buffered_close(void *opaque)
} }
} }
ret2 = migrate_fd_close(s->migration_state); ret2 = migrate_fd_close(s);
if (ret >= 0) { if (ret >= 0) {
ret = ret2; ret = ret2;
} }
ret = migrate_fd_close(s->migration_state); ret = migrate_fd_close(s);
s->migration_state->complete = true; s->complete = true;
return ret; return ret;
} }
static int buffered_get_fd(void *opaque) static int buffered_get_fd(void *opaque)
{ {
QEMUFileBuffered *s = opaque; MigrationState *s = opaque;
return s->migration_state->fd; return s->fd;
} }
/* /*
@ -636,7 +625,7 @@ static int buffered_get_fd(void *opaque)
*/ */
static int buffered_rate_limit(void *opaque) static int buffered_rate_limit(void *opaque)
{ {
QEMUFileBuffered *s = opaque; MigrationState *s = opaque;
int ret; int ret;
ret = qemu_file_get_error(s->file); ret = qemu_file_get_error(s->file);
@ -653,7 +642,7 @@ static int buffered_rate_limit(void *opaque)
static int64_t buffered_set_rate_limit(void *opaque, int64_t new_rate) static int64_t buffered_set_rate_limit(void *opaque, int64_t new_rate)
{ {
QEMUFileBuffered *s = opaque; MigrationState *s = opaque;
if (qemu_file_get_error(s->file)) { if (qemu_file_get_error(s->file)) {
goto out; goto out;
} }
@ -669,7 +658,7 @@ out:
static int64_t buffered_get_rate_limit(void *opaque) static int64_t buffered_get_rate_limit(void *opaque)
{ {
QEMUFileBuffered *s = opaque; MigrationState *s = opaque;
return s->xfer_limit; return s->xfer_limit;
} }
@ -741,7 +730,7 @@ static bool migrate_fd_put_ready(MigrationState *s, uint64_t max_size)
static void *buffered_file_thread(void *opaque) static void *buffered_file_thread(void *opaque)
{ {
QEMUFileBuffered *s = opaque; MigrationState *s = opaque;
int64_t initial_time = qemu_get_clock_ms(rt_clock); int64_t initial_time = qemu_get_clock_ms(rt_clock);
int64_t max_size = 0; int64_t max_size = 0;
bool last_round = false; bool last_round = false;
@ -749,7 +738,7 @@ static void *buffered_file_thread(void *opaque)
while (true) { while (true) {
int64_t current_time = qemu_get_clock_ms(rt_clock); int64_t current_time = qemu_get_clock_ms(rt_clock);
if (s->migration_state->complete) { if (s->complete) {
break; break;
} }
if (current_time >= initial_time + BUFFER_DELAY) { if (current_time >= initial_time + BUFFER_DELAY) {
@ -776,12 +765,11 @@ static void *buffered_file_thread(void *opaque)
DPRINTF("file is ready\n"); DPRINTF("file is ready\n");
if (s->bytes_xfer < s->xfer_limit) { if (s->bytes_xfer < s->xfer_limit) {
DPRINTF("notifying client\n"); DPRINTF("notifying client\n");
last_round = migrate_fd_put_ready(s->migration_state, max_size); last_round = migrate_fd_put_ready(s, max_size);
} }
} }
g_free(s->buffer); g_free(s->buffer);
g_free(s);
return NULL; return NULL;
} }
@ -794,22 +782,21 @@ static const QEMUFileOps buffered_file_ops = {
.set_rate_limit = buffered_set_rate_limit, .set_rate_limit = buffered_set_rate_limit,
}; };
void migrate_fd_connect(MigrationState *migration_state) void migrate_fd_connect(MigrationState *s)
{ {
QEMUFileBuffered *s; s->state = MIG_STATE_ACTIVE;
s->bytes_xfer = 0;
s->buffer = NULL;
s->buffer_size = 0;
s->buffer_capacity = 0;
migration_state->state = MIG_STATE_ACTIVE; s->first_time = true;
migration_state->first_time = true;
s = g_malloc0(sizeof(*s));
s->migration_state = migration_state; s->xfer_limit = s->bandwidth_limit / XFER_LIMIT_RATIO;
s->xfer_limit = s->migration_state->bandwidth_limit / XFER_LIMIT_RATIO; s->complete = false;
s->migration_state->complete = false;
s->file = qemu_fopen_ops(s, &buffered_file_ops); s->file = qemu_fopen_ops(s, &buffered_file_ops);
migration_state->file = s->file;
qemu_thread_create(&s->thread, buffered_file_thread, s, qemu_thread_create(&s->thread, buffered_file_thread, s,
QEMU_THREAD_DETACHED); QEMU_THREAD_DETACHED);
notifier_list_notify(&migration_state_notifiers, s); notifier_list_notify(&migration_state_notifiers, s);