Skip to contentSkip to conversation

migration: adding migration to/from a file (v2)

Qemu-devel Uri Lublin 4 participants 14 messages Feb 19, 2009 – Feb 20, 2009 Kbuild

lore
Share & tools
lore

Uri Lublin

author
#0 lore raw

Migration to file, uses migration-to-fd (supports live migration). Migration from file, uses qemu-fopen directly.

pre-save:
(qemu) stop # unless you really want live migration
(qemu) migrate_set_speed 3.5G # not needed, speed things up

save:
(qemu) migrate [-d] file:/path/to/state-file

load:
[shell] <qemu> <params> -incoming file:/path/to/state-file

changes from v1:
- making sure write would not block (using select)
- pass max_throttle as is (can be set by user)
- not checking qemu_mallocz
- compile for non-windows as select is being used.

Quoted 1 line

From adfdf0c43b39b3bd3a52612e3748cf7929af9c8c Mon Sep 17 00:00:00 2001

From: Uri Lublin <[email protected]>
Date: Sun, 18 Jan 2009 18:54:14 +0200
Subject: [PATCH] migration: adding migration to/from a file (v2)

Migration to file, uses migration-to-fd.
Migration from file, uses qemu-fopen directly.

The file should be used only once and removed (or used with -snapshot, or a the disk-image should be copoied), as the disk image is not saved, only the VM state.

This version uses select to make sure write() will not block. Thus this solution is limited to non-Windows hosts.

---
 Makefile         |    2 +-
 migration-file.c |  152 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 migration.c      |    4 ++
 migration.h      |    5 ++
 4 files changed, 162 insertions(+), 1 deletions(-)
 create mode 100644 migration-file.c
Makefile +1 -1
index 4f7a55a..6d4a8c8 100644
@@ -94,7 +94,7 @@ endif
 ifdef CONFIG_WIN32
 OBJS+=tap-win32.o
 else
-OBJS+=migration-exec.o
+OBJS+=migration-exec.o migration-file.o
 endif
 
 AUDIO_OBJS = audio.o noaudio.o wavaudio.o mixeng.o
migration-file.c new file +152 -0

152 lines not on this page · raw message

migration.c +4 -0
index 0ef777a..31c1c2b 100644
@@ -43,6 +43,8 @@ void qemu_start_incoming_migration(const char *uri)
 #if !defined(WIN32)
     else if (strstart(uri, "exec:", &p))
         exec_start_incoming_migration(p);
+    else if (strstart(uri, "file:", &p))
+        file_start_incoming_migration(p);
 #endif
     else
         fprintf(stderr, "unknown migration protocol: %s\n", uri);
@@ -58,6 +60,8 @@ void do_migrate(int detach, const char *uri)
 #if !defined(WIN32)
     else if (strstart(uri, "exec:", &p))
         s = exec_start_outgoing_migration(p, max_throttle, detach);
+    else if (strstart(uri, "file:", &p))
+        s = file_start_outgoing_migration(p, max_throttle, detach);
 #endif
     else
         term_printf("unknown migration protocol: %s\n", uri);
migration.h +5 -0
index d9771ad..c22beb9 100644
@@ -67,6 +67,11 @@ MigrationState *tcp_start_outgoing_migration(const char *host_port,
 					     int64_t bandwidth_limit,
 					     int detach);
 
+MigrationState *file_start_outgoing_migration(const char *filename,
+                                              int64_t bandwidth_limit,
+                                              int detach);
+int file_start_incoming_migration(const char *filename);
+
 void migrate_fd_error(FdMigrationState *s);
 
 void migrate_fd_cleanup(FdMigrationState *s);
Signature · 1 line
1.6.0.6
#1 lore raw
Quoted 3 lines · Uri Lublin

Migration to file, uses migration-to-fd (supports live migration). Migration from file, uses qemu-fopen directly.

Eh? Haven't we already talked about why this doesn't work? Maybe there's a v3 that you meant to send?

Regards,

Anthony Liguori

Quoted 17 lines

pre-save:
(qemu) stop # unless you really want live migration
(qemu) migrate_set_speed 3.5G # not needed, speed things up

save:
(qemu) migrate [-d] file:/path/to/state-file

load:
[shell] <qemu> <params> -incoming file:/path/to/state-file

changes from v1:
- making sure write would not block (using select)
- pass max_throttle as is (can be set by user)
- not checking qemu_mallocz
- compile for non-windows as select is being used.

author
#2 lore raw
Quoted 7 lines · Anthony Liguori
Quoted 3 lines · Uri Lublin

Migration to file, uses migration-to-fd (supports live migration). Migration from file, uses qemu-fopen directly.

Eh? Haven't we already talked about why this doesn't work? Maybe there's a v3 that you meant to send?

Actually I do have a v3 which uses posix-aio-compat.c
It's a much more complicated solution then just writing to a file though.
Also I am not sure if I need to use a signal or not as the migration (to-fd) code is polling. And if I use signal should I use SIGUSR2 or a different one and use a pipe similar to block-raw-posix.c ?

Your concern in the previous patch was that write() to a regular file might block. That's why I'm calling select before calling write. Do you think select will mark the fd as writeable but write would still block ?

Thanks,
Uri.

#3 lore raw
Quoted 6 lines · Uri Lublin
Quoted 5 lines · Anthony Liguori
Quoted 3 lines · Uri Lublin

Migration to file, uses migration-to-fd (supports live migration). Migration from file, uses qemu-fopen directly.

Quoted 6 lines
Quoted 2 lines

Eh? Haven't we already talked about why this doesn't work? Maybe there's a v3 that you meant to send?

Actually I do have a v3 which uses posix-aio-compat.c
It's a much more complicated solution then just writing to a file though.
Also I am not sure if I need to use a signal or not as the migration (to-fd) code is polling. And if I use signal should I use SIGUSR2 or a different one and use a pipe similar to block-raw-posix.c ?

How is the migration code polling? It will attempt to do writes until a write returns EAGAIN. At this point, it will wait for notification that the more writes are available. Remember, migration is a streaming protocol, not a random access, so it only makes sense to have one outstanding request at a time.

Your code would look something like:

write() -> submit aio request
until aio completes, write returns EAGAIN
when aio completes, notify migration code that we are writable again

Quoted 2 lines

Your concern in the previous patch was that write() to a regular file might block. That's why I'm calling select before calling write. Do you think select will mark the fd as writeable but write would still block ?

select() doesn't help. It will return that the file descriptor is writable and then the subsequent write will block.

Regards,

Anthony Liguori

Quoted 3 lines

Thanks,
Uri.

author
#4 lore raw
Quoted 14 lines · Anthony Liguori
Quoted 6 lines · Uri Lublin
Quoted 5 lines · Anthony Liguori
Quoted 3 lines · Uri Lublin

Migration to file, uses migration-to-fd (supports live migration). Migration from file, uses qemu-fopen directly.

Quoted 6 lines
Quoted 2 lines

Eh? Haven't we already talked about why this doesn't work? Maybe there's a v3 that you meant to send?

Actually I do have a v3 which uses posix-aio-compat.c
It's a much more complicated solution then just writing to a file though.
Also I am not sure if I need to use a signal or not as the migration (to-fd) code is polling. And if I use signal should I use SIGUSR2 or a different one and use a pipe similar to block-raw-posix.c ?

Quoted 8 lines

How is the migration code polling? It will attempt to do writes until a write returns EAGAIN. At this point, it will wait for notification that the more writes are available. Remember, migration is a streaming protocol, not a random access, so it only makes sense to have one outstanding request at a time.

Your code would look something like:

write() -> submit aio request
until aio completes, write returns EAGAIN
when aio completes, notify migration code that we are writable again

Basically that's what I've done in my v3.
But since I fake EAGAIN, and the fd is writeable by select, the scenario is as follows:

file_write (s->write of fd-migration) returns EAGAIN migrate_fd_put_buffer calls qemu_set_fd_handler2 with migrate_fd_put_notify upon the next main_loop_wait fd is found writable and migrate_fd_put_notify is called. qemu_file_put_notify is called.
buffered_put_buffer is called.
buffered_flush is called.
s->freeze_output=0
migrate_fd_put_buffer is called
file_write is called again -- here I can check if previous aio write finished.

Quoted 4 lines
Quoted 2 lines

Your concern in the previous patch was that write() to a regular file might block. That's why I'm calling select before calling write. Do you think select will mark the fd as writeable but write would still block ?

select() doesn't help. It will return that the file descriptor is writable and then the subsequent write will block.

Sure looks like a bug.
I'll send my v3 using posix-aio-compat for review soon.

Thanks,
Uri.

#5 lore raw
Quoted 19 lines · Uri Lublin
Quoted 18 lines · Anthony Liguori
Quoted 6 lines · Uri Lublin
Quoted 5 lines · Anthony Liguori
Quoted 3 lines · Uri Lublin

Migration to file, uses migration-to-fd (supports live migration). Migration from file, uses qemu-fopen directly.

Quoted 6 lines
Quoted 2 lines

Eh? Haven't we already talked about why this doesn't work? Maybe there's a v3 that you meant to send?

Actually I do have a v3 which uses posix-aio-compat.c
It's a much more complicated solution then just writing to a file though.
Also I am not sure if I need to use a signal or not as the migration (to-fd) code is polling. And if I use signal should I use SIGUSR2 or a different one and use a pipe similar to block-raw-posix.c ?

How is the migration code polling? It will attempt to do writes until a write returns EAGAIN. At this point, it will wait for notification that the more writes are available. Remember, migration is a streaming protocol, not a random access, so it only makes sense to have one outstanding request at a time.

Your code would look something like:

Quoted 7 lines
Quoted 4 lines

write() -> submit aio request
until aio completes, write returns EAGAIN
when aio completes, notify migration code that we are writable again

Basically that's what I've done in my v3.
But since I fake EAGAIN, and the fd is writeable by select, the scenario is as follows:

Oh, yes, this is a problem. You cannot use the existing migrate_fd code unfortunately :-(

Or maybe you can. If you change all instances of qemu_set_fd_handler2() in migrate.c (in the migrate_fd_ routines) to basically, s->set_fd_handler(), you can introduce a simple wrapper that calls qemu_set_fd_handler2() for everything else, but for yourself, use it as a mechanism to keep track of what the "writable" callback should be. This is the callback you would invoke when the aio request completes.

Quoted 1 line

Sure looks like a bug.

I wish! It's Unix suckiness.

Regards,

Anthony Liguori

Quoted 4 lines

I'll send my v3 using posix-aio-compat for review soon.

Thanks,
Uri.

#6 lore raw
Quoted 2 lines · Anthony Liguori
Quoted 1 line

Sure looks like a bug.

I wish! It's Unix suckiness.

Windows is the same.
It's a more of a conceptual problem than it looks, not merely an API bug.

It comes down to "what would 'readable' and 'writable' mean on a file?".

For a pipe or socket, readability depends on whether it's connected and some data has been received from another process. That's well-defined.

For a file, readability depends on the OS knowing in advance that you _want_ to read the file so it can issue a request to the underlying device - but it doesn't know you want to read the file until you call read() at a specific offset and length, so readability is not well-defined. Writability is similar but also depends on dynamic memory availability.

That's why AIO is more fundemantally required for files than streams.

AIO is how you tell the OS "I'm going to want to read or write this bit".

-- Jamie

#7 lore raw

In message: <[email protected]>
Jamie Lokier <[email protected]> writes:
: Anthony Liguori wrote:
: > >Sure looks like a bug.
: > I wish! It's Unix suckiness.
:
: Windows is the same.
: It's a more of a conceptual problem than it looks, not merely an API bug. :
: It comes down to "what would 'readable' and 'writable' mean on a file?".

"Would a read or write operation block?" is a better way to look at the interface that select() or poll() provides. For a regular file, the answer is "no" since the writes are so fast and often asynchronous...

Warner

#8 lore raw
Quoted 9 lines · M. Warner Losh

In message: <[email protected]>
Jamie Lokier <[email protected]> writes:
: Anthony Liguori wrote:
: > >Sure looks like a bug.
: > I wish! It's Unix suckiness.
: : Windows is the same.
: It's a more of a conceptual problem than it looks, not merely an API bug. : : It comes down to "what would 'readable' and 'writable' mean on a file?".

Quoted 5 lines

"Would a read or write operation block?" is a better way to look at the interface that select() or poll() provides. For a regular file, the answer is "no" since the writes are so fast and often asynchronous...

Except when they aren't..

The real issue is that read/write offers streaming semantics, not random access. You cannot guarantee that a read is going to complete unless you do read ahead. So the semantics would be something like pread(fd, buf, X) = EAGAIN (kernel starts the operation for X), later, pread(fd, buf, X) = OK. Sort of a weird interface.

For write, it's even more bizarre because you can't "write-ahead". If you're dealing with O_SYNC or O_DIRECT, there's simply no semantic that makes sense.

So fundamentally, read/write is a bad interface for random IO.

Regards,

Anthony Liguori

Quoted 4 lines

Warner

#9 lore raw

In message: <[email protected]>
Anthony Liguori <[email protected]> writes:
: M. Warner Losh wrote:
: > In message: <[email protected]>
: > Jamie Lokier <[email protected]> writes:
: > : Anthony Liguori wrote:
: > : > >Sure looks like a bug.
: > : > I wish! It's Unix suckiness.
: > :
: > : Windows is the same.
: > : It's a more of a conceptual problem than it looks, not merely an API bug. : > :
: > : It comes down to "what would 'readable' and 'writable' mean on a file?". : >
: > "Would a read or write operation block?" is a better way to look at : > the interface that select() or poll() provides. For a regular file, : > the answer is "no" since the writes are so fast and often
: > asynchronous...
: >
:
: Except when they aren't..

Right. That bit was added later, I think... My note was more of an explanation of how we got here, not that it was perfect and a good thing..

: The real issue is that read/write offers streaming semantics, not random : access. You cannot guarantee that a read is going to complete unless : you do read ahead. So the semantics would be something like pread(fd, : buf, X) = EAGAIN (kernel starts the operation for X), later, pread(fd, : buf, X) = OK. Sort of a weird interface.
:
: For write, it's even more bizarre because you can't "write-ahead". If : you're dealing with O_SYNC or O_DIRECT, there's simply no semantic that : makes sense.
:
: So fundamentally, read/write is a bad interface for random IO.

Also agreed.

: Regards,
:
: Anthony Liguori
:
: > Warner
: >
: >
: >
:
:
:
:

author
#10 lore raw
In reply to Anthony Liguori
Quoted 25 lines · Anthony Liguori
Quoted 24 lines · Uri Lublin
Quoted 22 lines · Anthony Liguori
Quoted 6 lines · Uri Lublin
Quoted 5 lines · Anthony Liguori
Quoted 3 lines · Uri Lublin

Migration to file, uses migration-to-fd (supports live migration). Migration from file, uses qemu-fopen directly.

Quoted 6 lines
Quoted 2 lines

Eh? Haven't we already talked about why this doesn't work? Maybe there's a v3 that you meant to send?

Actually I do have a v3 which uses posix-aio-compat.c
It's a much more complicated solution then just writing to a file though.
Also I am not sure if I need to use a signal or not as the migration (to-fd) code is polling. And if I use signal should I use SIGUSR2 or a different one and use a pipe similar to block-raw-posix.c ?

How is the migration code polling? It will attempt to do writes until a write returns EAGAIN. At this point, it will wait for notification that the more writes are available. Remember, migration is a streaming protocol, not a random access, so it only makes sense to have one outstanding request at a time.

Your code would look something like:

write() -> submit aio request
until aio completes, write returns EAGAIN
when aio completes, notify migration code that we are writable again

Quoted 7 lines
Quoted 2 lines

Basically that's what I've done in my v3.
But since I fake EAGAIN, and the fd is writeable by select, the scenario is as follows:

Oh, yes, this is a problem. You cannot use the existing migrate_fd code unfortunately :-(

Or maybe you can. If you change all instances of qemu_set_fd_handler2() in migrate.c (in the migrate_fd_ routines) to basically, s->set_fd_handler(), you can introduce a simple wrapper that calls qemu_set_fd_handler2() for everything else, but for yourself, use it as a mechanism to keep track of what the "writable" callback should be. This is the callback you would invoke when the aio request completes.

So it looks simpler to just (v4 implementation) spawn a single writing thread, and use a pipe to "connect" it to the migration code (as the pipe supports non-blocking write).

Would that be acceptable, or you prefer I'll implement s->set_fd_handler() ?
In that case will need another thread and pipe for compatfd (qemu_signalfd), and allocate a buffer to hold the data until it's written (as the original buffer may change).

Thanks,
Uri.

#11 lore raw
In reply to Anthony Liguori
Quoted 17 lines · Anthony Liguori
Quoted 10 lines · Uri Lublin
Quoted 8 lines · Anthony Liguori
Quoted 3 lines · Uri Lublin

Migration to file, uses migration-to-fd (supports live migration). Migration from file, uses qemu-fopen directly.

Eh? Haven't we already talked about why this doesn't work? Maybe there's a v3 that you meant to send?

Quoted 5 lines

Actually I do have a v3 which uses posix-aio-compat.c
It's a much more complicated solution then just writing to a file though. Also I am not sure if I need to use a signal or not as the migration (to-fd) code is polling. And if I use signal should I use SIGUSR2 or a different one and use a pipe similar to block-raw-posix.c ?

Quoted 7 lines

How is the migration code polling? It will attempt to do writes until a write returns EAGAIN. At this point, it will wait for notification that the more writes are available.

Remember, migration is a streaming
protocol, not a random access, so it only makes sense to have one outstanding request at a time.

Fwow, often with streaming (in general) the highest performance comes from having two or more outstanding requests at a time, so there are no gaps between requests being processed at the OS and device level.

However, unless migrate-to-file uses O_DIRECT that will be hidden by the OS's buffering.

Then the remaining overhead from AIOs (or fake AIOs using threads) is lots context switching, one per write completed.

-- Jamie

#12 lore raw
Quoted 11 lines · Jamie Lokier
Quoted 3 lines

Remember, migration is a streaming protocol, not a random access, so it only makes sense to have one outstanding request at a time.

Fwow, often with streaming (in general) the highest performance comes from having two or more outstanding requests at a time, so there are no gaps between requests being processed at the OS and device level.

However, unless migrate-to-file uses O_DIRECT that will be hidden by the OS's buffering.

It's already buffered FWIW by the savevm infrastructure. You'll won't usually see small operations.

Regards,

Anthony Liguori

Quoted 7 lines

Then the remaining overhead from AIOs (or fake AIOs using threads) is lots context switching, one per write completed.

-- Jamie

#13 lore raw
In reply to Uri Lublin
Quoted 3 lines · Uri Lublin

Your concern in the previous patch was that write() to a regular file might block. That's why I'm calling select before calling write. Do you think select will mark the fd as writeable but write would still block ?

select() on a regular file _always_ marks the fd as writable. That's one of the rules of select(). For non-blocking file access you can only use threads or AIO, or a helper process.

-- Jamie