savevm: New save live migration method: pending

Code just now does (simplified for clarity)

    if (qemu_savevm_state_iterate(s->file) == 1) {
       vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
       qemu_savevm_state_complete(s->file);
    }

Problem here is that qemu_savevm_state_iterate() returns 1 when it
knows that remaining memory to sent takes less than max downtime.

But this means that we could end spending 2x max_downtime, one
downtime in qemu_savevm_iterate, and the other in
qemu_savevm_state_complete.

Changed code to:

    pending_size = qemu_savevm_state_pending(s->file, max_size);
    DPRINTF("pending size %lu max %lu\n", pending_size, max_size);
    if (pending_size >= max_size) {
        ret = qemu_savevm_state_iterate(s->file);
     } else {
        vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
        qemu_savevm_state_complete(s->file);
     }

So what we do is: at current network speed, we calculate the maximum
number of bytes we can sent: max_size.

Then we ask every save_live section how much they have pending.  If
they are less than max_size, we move to complete phase, otherwise we
do an iterate one.

This makes things much simpler, because now individual sections don't
have to caluclate the bandwidth (it was implossible to do right from
there).

Signed-off-by: Juan Quintela <quintela@redhat.com>
migration: include qemu-file.h

They don't use/know anything about buffered-file.

Signed-off-by: Juan Quintela <quintela@redhat.com>
migration-fd: remove duplicate include

Signed-off-by: Juan Quintela <quintela@redhat.com>
memory: introduce memory_region_test_and_clear_dirty

This function avoids having to do two calls, one to test the dirty bit, and
other to reset it.

Signed-off-by: Juan Quintela <quintela@redhat.com>
ram: Use memory_region_test_and_clear_dirty

This avoids having to do two walks over the dirty bitmap, one reading
the dirty bits, and another cleaning them.

Signed-off-by: Juan Quintela <quintela@redhat.com>
ram: optimize migration bitmap walking

Instead of testing each page individually, we search what is the next
dirty page with a bitmap operation.  We have to reorganize the code to
move from a "for" loop, to a while(dirty) loop.

Signed-off-by: Juan Quintela <quintela@redhat.com>
buffered_file: move to migration.c

We want to make QEMUBufferedFile a thinner wrapper, using MigrationState
directly.  Move its contents to migration.c.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: make functions static

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: inline qemu_fopen_ops_buffered into migrate_fd_connect

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: add XFER_LIMIT_RATIO

The "magic" divisions by 10 are there because of the value of BUFFER_DELAY.
Introduce a constant to explain them better.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: fix qemu_get_fd for BufferedFile

Not really used, but nice to have it correct. :)

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
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>
qemu-file: expose qemu_file_set_error

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: use qemu_file_set_error

Remove the return value of buffered_flush, pass it via the error code
of s->file.  Once this is done, the error can be retrieved simply
via migrate_fd_close's call to qemu_fclose.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: inline migrate_fd_put_ready into caller

Prepare for simplifying the body of the migration thread.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: simplify beginning of inlined migrate_fd_put_ready

last_round remain false until explicitly set to true, and s->state
can be checked outside the iothread mutex.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: make first_time a local variable

It is not written (except for ensuring it is initially true) outside of
buffered_file_thread.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
qemu-file: expose qemu_fflush

And since it is now public, make it set the last_error of the QEMUFile.

We will need this to simplify the switch to MIG_STATE_COMPLETED.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: flush all data to fd when buffered_flush is called

Including data that resided in the QEMUFile's own buffer.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
migration: simplify error handling

Always use qemu_file_get_error to detect errors.  Also remove the
"complete" member, since we know that it is set (via migrate_fd_cleanup)
only when the state changes.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>