diff mbox

[v1,08/22] migration: convert post-copy to use QIOChannelBuffer

Message ID 1452599056-27357-9-git-send-email-berrange@redhat.com
State New
Headers show

Commit Message

Daniel P. Berrangé Jan. 12, 2016, 11:44 a.m. UTC
The post-copy code does some I/O to/from an intermediate
in-memory buffer rather than direct to the underlying
I/O channel. Switch this code to use QIOChannelBuffer
instead of QEMUSizedBuffer.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
 docs/migration.txt      |  4 ++--
 include/sysemu/sysemu.h |  2 +-
 migration/migration.c   | 15 +++++++--------
 migration/savevm.c      | 41 +++++++++++++----------------------------
 4 files changed, 23 insertions(+), 39 deletions(-)

Comments

Dr. David Alan Gilbert Jan. 25, 2016, 7:38 p.m. UTC | #1
* Daniel P. Berrange (berrange@redhat.com) wrote:
> The post-copy code does some I/O to/from an intermediate
> in-memory buffer rather than direct to the underlying
> I/O channel. Switch this code to use QIOChannelBuffer
> instead of QEMUSizedBuffer.
> 
> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
> ---
>  docs/migration.txt      |  4 ++--
>  include/sysemu/sysemu.h |  2 +-
>  migration/migration.c   | 15 +++++++--------
>  migration/savevm.c      | 41 +++++++++++++----------------------------
>  4 files changed, 23 insertions(+), 39 deletions(-)
> 
> diff --git a/docs/migration.txt b/docs/migration.txt
> index fda8d61..11703de 100644
> --- a/docs/migration.txt
> +++ b/docs/migration.txt
> @@ -403,8 +403,8 @@ listen thread:                     --- page -- page -- page -- page -- page --
>  
>  On receipt of CMD_PACKAGED (1)
>     All the data associated with the package - the ( ... ) section in the
> -diagram - is read into memory (into a QEMUSizedBuffer), and the main thread
> -recurses into qemu_loadvm_state_main to process the contents of the package (2)
> +diagram - is read into memory, and the main thread recurses into
> +qemu_loadvm_state_main to process the contents of the package (2)
>  which contains commands (3,6) and devices (4...)
>  
>  On receipt of 'postcopy listen' - 3 -(i.e. the 1st command in the package)
> diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
> index 3bb8897..0f59912 100644
> --- a/include/sysemu/sysemu.h
> +++ b/include/sysemu/sysemu.h
> @@ -120,7 +120,7 @@ void qemu_savevm_command_send(QEMUFile *f, enum qemu_vm_cmd command,
>                                uint16_t len, uint8_t *data);
>  void qemu_savevm_send_ping(QEMUFile *f, uint32_t value);
>  void qemu_savevm_send_open_return_path(QEMUFile *f);
> -int qemu_savevm_send_packaged(QEMUFile *f, const QEMUSizedBuffer *qsb);
> +int qemu_savevm_send_packaged(QEMUFile *f, const char *buf, size_t len);
>  void qemu_savevm_send_postcopy_advise(QEMUFile *f);
>  void qemu_savevm_send_postcopy_listen(QEMUFile *f);
>  void qemu_savevm_send_postcopy_run(QEMUFile *f);
> diff --git a/migration/migration.c b/migration/migration.c
> index 715f069..e921b20 100644
> --- a/migration/migration.c
> +++ b/migration/migration.c
> @@ -33,6 +33,7 @@
>  #include "qom/cpu.h"
>  #include "exec/memory.h"
>  #include "exec/address-spaces.h"
> +#include "io/channel-buffer.h"
>  
>  #define MAX_THROTTLE  (32 << 20)      /* Migration transfer speed throttling */
>  
> @@ -1401,7 +1402,8 @@ static int await_return_path_close_on_source(MigrationState *ms)
>  static int postcopy_start(MigrationState *ms, bool *old_vm_running)
>  {
>      int ret;
> -    const QEMUSizedBuffer *qsb;
> +    QIOChannelBuffer *bioc;
> +    QEMUFile *fb;
>      int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
>      migrate_set_state(ms, MIGRATION_STATUS_ACTIVE,
>                        MIGRATION_STATUS_POSTCOPY_ACTIVE);
> @@ -1456,11 +1458,9 @@ static int postcopy_start(MigrationState *ms, bool *old_vm_running)
>       * So we wrap the device state up in a package with a length at the start;
>       * to do this we use a qemu_buf to hold the whole of the device state.
>       */
> -    QEMUFile *fb = qemu_bufopen("w", NULL);
> -    if (!fb) {
> -        error_report("Failed to create buffered file");
> -        goto fail;
> -    }
> +    bioc = qio_channel_buffer_new(4096);
> +    fb = qemu_fopen_channel_input(QIO_CHANNEL(bioc));

Why is that _input ?
Also, what's your naming converntion; my 'qsb' was just an
abbreviation of (Q)EMU(S)ized(B)uffer;  what's a bioc?

> +    object_unref(OBJECT(bioc));
>  
>      /*
>       * Make sure the receiver can get incoming pages before we send the rest
> @@ -1474,10 +1474,9 @@ static int postcopy_start(MigrationState *ms, bool *old_vm_running)
>      qemu_savevm_send_postcopy_run(fb);
>  
>      /* <><> end of stuff going into the package */
> -    qsb = qemu_buf_get(fb);
>  
>      /* Now send that blob */
> -    if (qemu_savevm_send_packaged(ms->file, qsb)) {
> +    if (qemu_savevm_send_packaged(ms->file, bioc->data, bioc->usage)) {
>          goto fail_closefb;
>      }
>      qemu_fclose(fb);
> diff --git a/migration/savevm.c b/migration/savevm.c
> index 0ad1b93..f2e1880 100644
> --- a/migration/savevm.c
> +++ b/migration/savevm.c
> @@ -50,6 +50,7 @@
>  #include "qemu/iov.h"
>  #include "block/snapshot.h"
>  #include "block/qapi.h"
> +#include "io/channel-buffer.h"
>  
>  
>  #ifndef ETH_P_RARP
> @@ -760,10 +761,8 @@ void qemu_savevm_send_open_return_path(QEMUFile *f)
>   *    0 on success
>   *    -ve on error
>   */
> -int qemu_savevm_send_packaged(QEMUFile *f, const QEMUSizedBuffer *qsb)
> +int qemu_savevm_send_packaged(QEMUFile *f, const char *buf, size_t len)
>  {
> -    size_t cur_iov;
> -    size_t len = qsb_get_length(qsb);
>      uint32_t tmp;
>  
>      if (len > MAX_VM_CMD_PACKAGED_SIZE) {
> @@ -777,18 +776,7 @@ int qemu_savevm_send_packaged(QEMUFile *f, const QEMUSizedBuffer *qsb)
>      trace_qemu_savevm_send_packaged();
>      qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
>  
> -    /* all the data follows (concatinating the iov's) */
> -    for (cur_iov = 0; cur_iov < qsb->n_iov; cur_iov++) {
> -        /* The iov entries are partially filled */
> -        size_t towrite = MIN(qsb->iov[cur_iov].iov_len, len);
> -        len -= towrite;
> -
> -        if (!towrite) {
> -            break;
> -        }
> -
> -        qemu_put_buffer(f, qsb->iov[cur_iov].iov_base, towrite);
> -    }
> +    qemu_put_buffer(f, (const uint8_t *)buf, len);
>  
>      return 0;
>  }
> @@ -1548,9 +1536,8 @@ static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
>  static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
>  {
>      int ret;
> -    uint8_t *buffer;
>      uint32_t length;
> -    QEMUSizedBuffer *qsb;
> +    QIOChannelBuffer *bioc;
>  
>      length = qemu_get_be32(mis->from_src_file);
>      trace_loadvm_handle_cmd_packaged(length);
> @@ -1559,28 +1546,26 @@ static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
>          error_report("Unreasonably large packaged state: %u", length);
>          return -1;
>      }
> -    buffer = g_malloc0(length);
> -    ret = qemu_get_buffer(mis->from_src_file, buffer, (int)length);
> +
> +    bioc = qio_channel_buffer_new(length);
> +    ret = qemu_get_buffer(mis->from_src_file,
> +                          (uint8_t *)bioc->data,
> +                          (int)length);

Actually, qemu_get_buffer takes a size_t now; so you can fix that at the same time.

>      if (ret != length) {
> -        g_free(buffer);
> +        object_unref(OBJECT(bioc));
>          error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%d\n",
>                  ret, length);
>          return (ret < 0) ? ret : -EAGAIN;
>      }
> +    bioc->usage += length;

I'm a bit surprised you've not wrapped feeding the data
into the qio_channel_buffer up a bit rather than having the callers
prod at it's buffer/usage elements directly.

Dave

>      trace_loadvm_handle_cmd_packaged_received(ret);
>  
> -    /* Setup a dummy QEMUFile that actually reads from the buffer */
> -    qsb = qsb_create(buffer, length);
> -    g_free(buffer); /* Because qsb_create copies */
> -    if (!qsb) {
> -        error_report("Unable to create qsb");
> -    }
> -    QEMUFile *packf = qemu_bufopen("r", qsb);
> +    QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
>  
>      ret = qemu_loadvm_state_main(packf, mis);
>      trace_loadvm_handle_cmd_packaged_main(ret);
>      qemu_fclose(packf);
> -    qsb_free(qsb);
> +    object_unref(OBJECT(bioc));
>  
>      return ret;
>  }
> -- 
> 2.5.0
> 
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
Daniel P. Berrangé Jan. 25, 2016, 10:15 p.m. UTC | #2
On Mon, Jan 25, 2016 at 07:38:59PM +0000, Dr. David Alan Gilbert wrote:
> * Daniel P. Berrange (berrange@redhat.com) wrote:
> > The post-copy code does some I/O to/from an intermediate
> > in-memory buffer rather than direct to the underlying
> > I/O channel. Switch this code to use QIOChannelBuffer
> > instead of QEMUSizedBuffer.
> > 
> > Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
> > ---
> >  docs/migration.txt      |  4 ++--
> >  include/sysemu/sysemu.h |  2 +-
> >  migration/migration.c   | 15 +++++++--------
> >  migration/savevm.c      | 41 +++++++++++++----------------------------
> >  4 files changed, 23 insertions(+), 39 deletions(-)
> > 
> > diff --git a/docs/migration.txt b/docs/migration.txt
> > index fda8d61..11703de 100644
> > --- a/docs/migration.txt
> > +++ b/docs/migration.txt
> > @@ -403,8 +403,8 @@ listen thread:                     --- page -- page -- page -- page -- page --
> >  
> >  On receipt of CMD_PACKAGED (1)
> >     All the data associated with the package - the ( ... ) section in the
> > -diagram - is read into memory (into a QEMUSizedBuffer), and the main thread
> > -recurses into qemu_loadvm_state_main to process the contents of the package (2)
> > +diagram - is read into memory, and the main thread recurses into
> > +qemu_loadvm_state_main to process the contents of the package (2)
> >  which contains commands (3,6) and devices (4...)
> >  
> >  On receipt of 'postcopy listen' - 3 -(i.e. the 1st command in the package)
> > diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
> > index 3bb8897..0f59912 100644
> > --- a/include/sysemu/sysemu.h
> > +++ b/include/sysemu/sysemu.h
> > @@ -120,7 +120,7 @@ void qemu_savevm_command_send(QEMUFile *f, enum qemu_vm_cmd command,
> >                                uint16_t len, uint8_t *data);
> >  void qemu_savevm_send_ping(QEMUFile *f, uint32_t value);
> >  void qemu_savevm_send_open_return_path(QEMUFile *f);
> > -int qemu_savevm_send_packaged(QEMUFile *f, const QEMUSizedBuffer *qsb);
> > +int qemu_savevm_send_packaged(QEMUFile *f, const char *buf, size_t len);
> >  void qemu_savevm_send_postcopy_advise(QEMUFile *f);
> >  void qemu_savevm_send_postcopy_listen(QEMUFile *f);
> >  void qemu_savevm_send_postcopy_run(QEMUFile *f);
> > diff --git a/migration/migration.c b/migration/migration.c
> > index 715f069..e921b20 100644
> > --- a/migration/migration.c
> > +++ b/migration/migration.c
> > @@ -33,6 +33,7 @@
> >  #include "qom/cpu.h"
> >  #include "exec/memory.h"
> >  #include "exec/address-spaces.h"
> > +#include "io/channel-buffer.h"
> >  
> >  #define MAX_THROTTLE  (32 << 20)      /* Migration transfer speed throttling */
> >  
> > @@ -1401,7 +1402,8 @@ static int await_return_path_close_on_source(MigrationState *ms)
> >  static int postcopy_start(MigrationState *ms, bool *old_vm_running)
> >  {
> >      int ret;
> > -    const QEMUSizedBuffer *qsb;
> > +    QIOChannelBuffer *bioc;
> > +    QEMUFile *fb;
> >      int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
> >      migrate_set_state(ms, MIGRATION_STATUS_ACTIVE,
> >                        MIGRATION_STATUS_POSTCOPY_ACTIVE);
> > @@ -1456,11 +1458,9 @@ static int postcopy_start(MigrationState *ms, bool *old_vm_running)
> >       * So we wrap the device state up in a package with a length at the start;
> >       * to do this we use a qemu_buf to hold the whole of the device state.
> >       */
> > -    QEMUFile *fb = qemu_bufopen("w", NULL);
> > -    if (!fb) {
> > -        error_report("Failed to create buffered file");
> > -        goto fail;
> > -    }
> > +    bioc = qio_channel_buffer_new(4096);
> > +    fb = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
> 
> Why is that _input ?

A mistake - should be output given that the old code used "w".

> Also, what's your naming converntion; my 'qsb' was just an
> abbreviation of (Q)EMU(S)ized(B)uffer;  what's a bioc?

In the vast majority of cases where you just have a pointer
to the generic base class QIOChannel, then I'll use 'ioc' as
the standard variable name - ie 'io' and 'c' for channel.

If I have a specialized subtype, I prefix a single letter
reflecting the name of the subtype. eg 's' for QIOChannelSocket
giving "sioc" and 'b' for QIOChannelBuffer giving "bioc" etc.

> > +    object_unref(OBJECT(bioc));
> >  
> >      /*
> >       * Make sure the receiver can get incoming pages before we send the rest
> > @@ -1474,10 +1474,9 @@ static int postcopy_start(MigrationState *ms, bool *old_vm_running)
> >      qemu_savevm_send_postcopy_run(fb);
> >  
> >      /* <><> end of stuff going into the package */
> > -    qsb = qemu_buf_get(fb);
> >  
> >      /* Now send that blob */
> > -    if (qemu_savevm_send_packaged(ms->file, qsb)) {
> > +    if (qemu_savevm_send_packaged(ms->file, bioc->data, bioc->usage)) {
> >          goto fail_closefb;
> >      }
> >      qemu_fclose(fb);
> > diff --git a/migration/savevm.c b/migration/savevm.c
> > index 0ad1b93..f2e1880 100644
> > --- a/migration/savevm.c
> > +++ b/migration/savevm.c
> > @@ -50,6 +50,7 @@
> >  #include "qemu/iov.h"
> >  #include "block/snapshot.h"
> >  #include "block/qapi.h"
> > +#include "io/channel-buffer.h"
> >  
> >  
> >  #ifndef ETH_P_RARP
> > @@ -760,10 +761,8 @@ void qemu_savevm_send_open_return_path(QEMUFile *f)
> >   *    0 on success
> >   *    -ve on error
> >   */
> > -int qemu_savevm_send_packaged(QEMUFile *f, const QEMUSizedBuffer *qsb)
> > +int qemu_savevm_send_packaged(QEMUFile *f, const char *buf, size_t len)
> >  {
> > -    size_t cur_iov;
> > -    size_t len = qsb_get_length(qsb);
> >      uint32_t tmp;
> >  
> >      if (len > MAX_VM_CMD_PACKAGED_SIZE) {
> > @@ -777,18 +776,7 @@ int qemu_savevm_send_packaged(QEMUFile *f, const QEMUSizedBuffer *qsb)
> >      trace_qemu_savevm_send_packaged();
> >      qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
> >  
> > -    /* all the data follows (concatinating the iov's) */
> > -    for (cur_iov = 0; cur_iov < qsb->n_iov; cur_iov++) {
> > -        /* The iov entries are partially filled */
> > -        size_t towrite = MIN(qsb->iov[cur_iov].iov_len, len);
> > -        len -= towrite;
> > -
> > -        if (!towrite) {
> > -            break;
> > -        }
> > -
> > -        qemu_put_buffer(f, qsb->iov[cur_iov].iov_base, towrite);
> > -    }
> > +    qemu_put_buffer(f, (const uint8_t *)buf, len);
> >  
> >      return 0;
> >  }
> > @@ -1548,9 +1536,8 @@ static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
> >  static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
> >  {
> >      int ret;
> > -    uint8_t *buffer;
> >      uint32_t length;
> > -    QEMUSizedBuffer *qsb;
> > +    QIOChannelBuffer *bioc;
> >  
> >      length = qemu_get_be32(mis->from_src_file);
> >      trace_loadvm_handle_cmd_packaged(length);
> > @@ -1559,28 +1546,26 @@ static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
> >          error_report("Unreasonably large packaged state: %u", length);
> >          return -1;
> >      }
> > -    buffer = g_malloc0(length);
> > -    ret = qemu_get_buffer(mis->from_src_file, buffer, (int)length);
> > +
> > +    bioc = qio_channel_buffer_new(length);
> > +    ret = qemu_get_buffer(mis->from_src_file,
> > +                          (uint8_t *)bioc->data,
> > +                          (int)length);
> 
> Actually, qemu_get_buffer takes a size_t now; so you can fix that at the same time.

Great.

> 
> >      if (ret != length) {
> > -        g_free(buffer);
> > +        object_unref(OBJECT(bioc));
> >          error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%d\n",
> >                  ret, length);
> >          return (ret < 0) ? ret : -EAGAIN;
> >      }
> > +    bioc->usage += length;
> 
> I'm a bit surprised you've not wrapped feeding the data
> into the qio_channel_buffer up a bit rather than having the callers
> prod at it's buffer/usage elements directly.

I figured it was better to avoid extra memcpy's in the migration code,
so decided to directly pass the internal buffer to get_qemu_buffer().


Regards,
Daniel
Dr. David Alan Gilbert Jan. 26, 2016, 6:59 p.m. UTC | #3
* Daniel P. Berrange (berrange@redhat.com) wrote:
> On Mon, Jan 25, 2016 at 07:38:59PM +0000, Dr. David Alan Gilbert wrote:
> > * Daniel P. Berrange (berrange@redhat.com) wrote:
> > > The post-copy code does some I/O to/from an intermediate
> > > in-memory buffer rather than direct to the underlying
> > > I/O channel. Switch this code to use QIOChannelBuffer
> > > instead of QEMUSizedBuffer.
> > > 
> > > Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
> > > ---
> > >  docs/migration.txt      |  4 ++--
> > >  include/sysemu/sysemu.h |  2 +-
> > >  migration/migration.c   | 15 +++++++--------
> > >  migration/savevm.c      | 41 +++++++++++++----------------------------
> > >  4 files changed, 23 insertions(+), 39 deletions(-)
> > > 
> > > diff --git a/docs/migration.txt b/docs/migration.txt
> > > index fda8d61..11703de 100644
> > > --- a/docs/migration.txt
> > > +++ b/docs/migration.txt
> > > @@ -403,8 +403,8 @@ listen thread:                     --- page -- page -- page -- page -- page --
> > >  
> > >  On receipt of CMD_PACKAGED (1)
> > >     All the data associated with the package - the ( ... ) section in the
> > > -diagram - is read into memory (into a QEMUSizedBuffer), and the main thread
> > > -recurses into qemu_loadvm_state_main to process the contents of the package (2)
> > > +diagram - is read into memory, and the main thread recurses into
> > > +qemu_loadvm_state_main to process the contents of the package (2)
> > >  which contains commands (3,6) and devices (4...)
> > >  
> > >  On receipt of 'postcopy listen' - 3 -(i.e. the 1st command in the package)
> > > diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
> > > index 3bb8897..0f59912 100644
> > > --- a/include/sysemu/sysemu.h
> > > +++ b/include/sysemu/sysemu.h
> > > @@ -120,7 +120,7 @@ void qemu_savevm_command_send(QEMUFile *f, enum qemu_vm_cmd command,
> > >                                uint16_t len, uint8_t *data);
> > >  void qemu_savevm_send_ping(QEMUFile *f, uint32_t value);
> > >  void qemu_savevm_send_open_return_path(QEMUFile *f);
> > > -int qemu_savevm_send_packaged(QEMUFile *f, const QEMUSizedBuffer *qsb);
> > > +int qemu_savevm_send_packaged(QEMUFile *f, const char *buf, size_t len);
> > >  void qemu_savevm_send_postcopy_advise(QEMUFile *f);
> > >  void qemu_savevm_send_postcopy_listen(QEMUFile *f);
> > >  void qemu_savevm_send_postcopy_run(QEMUFile *f);
> > > diff --git a/migration/migration.c b/migration/migration.c
> > > index 715f069..e921b20 100644
> > > --- a/migration/migration.c
> > > +++ b/migration/migration.c
> > > @@ -33,6 +33,7 @@
> > >  #include "qom/cpu.h"
> > >  #include "exec/memory.h"
> > >  #include "exec/address-spaces.h"
> > > +#include "io/channel-buffer.h"
> > >  
> > >  #define MAX_THROTTLE  (32 << 20)      /* Migration transfer speed throttling */
> > >  
> > > @@ -1401,7 +1402,8 @@ static int await_return_path_close_on_source(MigrationState *ms)
> > >  static int postcopy_start(MigrationState *ms, bool *old_vm_running)
> > >  {
> > >      int ret;
> > > -    const QEMUSizedBuffer *qsb;
> > > +    QIOChannelBuffer *bioc;
> > > +    QEMUFile *fb;
> > >      int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
> > >      migrate_set_state(ms, MIGRATION_STATUS_ACTIVE,
> > >                        MIGRATION_STATUS_POSTCOPY_ACTIVE);
> > > @@ -1456,11 +1458,9 @@ static int postcopy_start(MigrationState *ms, bool *old_vm_running)
> > >       * So we wrap the device state up in a package with a length at the start;
> > >       * to do this we use a qemu_buf to hold the whole of the device state.
> > >       */
> > > -    QEMUFile *fb = qemu_bufopen("w", NULL);
> > > -    if (!fb) {
> > > -        error_report("Failed to create buffered file");
> > > -        goto fail;
> > > -    }
> > > +    bioc = qio_channel_buffer_new(4096);
> > > +    fb = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
> > 
> > Why is that _input ?
> 
> A mistake - should be output given that the old code used "w".

OK, that makes sense then.

> > Also, what's your naming converntion; my 'qsb' was just an
> > abbreviation of (Q)EMU(S)ized(B)uffer;  what's a bioc?
> 
> In the vast majority of cases where you just have a pointer
> to the generic base class QIOChannel, then I'll use 'ioc' as
> the standard variable name - ie 'io' and 'c' for channel.
> 
> If I have a specialized subtype, I prefix a single letter
> reflecting the name of the subtype. eg 's' for QIOChannelSocket
> giving "sioc" and 'b' for QIOChannelBuffer giving "bioc" etc.

OK, my preference there would have been iocb just because it's the
same order as the thing it's abbreviating; but that's just a personal
preference.

> > > +    object_unref(OBJECT(bioc));
> > >  
> > >      /*
> > >       * Make sure the receiver can get incoming pages before we send the rest
> > > @@ -1474,10 +1474,9 @@ static int postcopy_start(MigrationState *ms, bool *old_vm_running)
> > >      qemu_savevm_send_postcopy_run(fb);
> > >  
> > >      /* <><> end of stuff going into the package */
> > > -    qsb = qemu_buf_get(fb);
> > >  
> > >      /* Now send that blob */
> > > -    if (qemu_savevm_send_packaged(ms->file, qsb)) {
> > > +    if (qemu_savevm_send_packaged(ms->file, bioc->data, bioc->usage)) {
> > >          goto fail_closefb;
> > >      }
> > >      qemu_fclose(fb);
> > > diff --git a/migration/savevm.c b/migration/savevm.c
> > > index 0ad1b93..f2e1880 100644
> > > --- a/migration/savevm.c
> > > +++ b/migration/savevm.c
> > > @@ -50,6 +50,7 @@
> > >  #include "qemu/iov.h"
> > >  #include "block/snapshot.h"
> > >  #include "block/qapi.h"
> > > +#include "io/channel-buffer.h"
> > >  
> > >  
> > >  #ifndef ETH_P_RARP
> > > @@ -760,10 +761,8 @@ void qemu_savevm_send_open_return_path(QEMUFile *f)
> > >   *    0 on success
> > >   *    -ve on error
> > >   */
> > > -int qemu_savevm_send_packaged(QEMUFile *f, const QEMUSizedBuffer *qsb)
> > > +int qemu_savevm_send_packaged(QEMUFile *f, const char *buf, size_t len)
> > >  {
> > > -    size_t cur_iov;
> > > -    size_t len = qsb_get_length(qsb);
> > >      uint32_t tmp;
> > >  
> > >      if (len > MAX_VM_CMD_PACKAGED_SIZE) {
> > > @@ -777,18 +776,7 @@ int qemu_savevm_send_packaged(QEMUFile *f, const QEMUSizedBuffer *qsb)
> > >      trace_qemu_savevm_send_packaged();
> > >      qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
> > >  
> > > -    /* all the data follows (concatinating the iov's) */
> > > -    for (cur_iov = 0; cur_iov < qsb->n_iov; cur_iov++) {
> > > -        /* The iov entries are partially filled */
> > > -        size_t towrite = MIN(qsb->iov[cur_iov].iov_len, len);
> > > -        len -= towrite;
> > > -
> > > -        if (!towrite) {
> > > -            break;
> > > -        }
> > > -
> > > -        qemu_put_buffer(f, qsb->iov[cur_iov].iov_base, towrite);
> > > -    }
> > > +    qemu_put_buffer(f, (const uint8_t *)buf, len);
> > >  
> > >      return 0;
> > >  }
> > > @@ -1548,9 +1536,8 @@ static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
> > >  static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
> > >  {
> > >      int ret;
> > > -    uint8_t *buffer;
> > >      uint32_t length;
> > > -    QEMUSizedBuffer *qsb;
> > > +    QIOChannelBuffer *bioc;
> > >  
> > >      length = qemu_get_be32(mis->from_src_file);
> > >      trace_loadvm_handle_cmd_packaged(length);
> > > @@ -1559,28 +1546,26 @@ static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
> > >          error_report("Unreasonably large packaged state: %u", length);
> > >          return -1;
> > >      }
> > > -    buffer = g_malloc0(length);
> > > -    ret = qemu_get_buffer(mis->from_src_file, buffer, (int)length);
> > > +
> > > +    bioc = qio_channel_buffer_new(length);
> > > +    ret = qemu_get_buffer(mis->from_src_file,
> > > +                          (uint8_t *)bioc->data,
> > > +                          (int)length);
> > 
> > Actually, qemu_get_buffer takes a size_t now; so you can fix that at the same time.
> 
> Great.
> 
> > 
> > >      if (ret != length) {
> > > -        g_free(buffer);
> > > +        object_unref(OBJECT(bioc));
> > >          error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%d\n",
> > >                  ret, length);
> > >          return (ret < 0) ? ret : -EAGAIN;
> > >      }
> > > +    bioc->usage += length;
> > 
> > I'm a bit surprised you've not wrapped feeding the data
> > into the qio_channel_buffer up a bit rather than having the callers
> > prod at it's buffer/usage elements directly.
> 
> I figured it was better to avoid extra memcpy's in the migration code,
> so decided to directly pass the internal buffer to get_qemu_buffer().

Yes, avoiding copies is good; I was just thinking more of a function that
incremented the usage or something like that.

Dave

> 
> 
> Regards,
> Daniel
> -- 
> |: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
> |: http://libvirt.org              -o-             http://virt-manager.org :|
> |: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
> |: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
diff mbox

Patch

diff --git a/docs/migration.txt b/docs/migration.txt
index fda8d61..11703de 100644
--- a/docs/migration.txt
+++ b/docs/migration.txt
@@ -403,8 +403,8 @@  listen thread:                     --- page -- page -- page -- page -- page --
 
 On receipt of CMD_PACKAGED (1)
    All the data associated with the package - the ( ... ) section in the
-diagram - is read into memory (into a QEMUSizedBuffer), and the main thread
-recurses into qemu_loadvm_state_main to process the contents of the package (2)
+diagram - is read into memory, and the main thread recurses into
+qemu_loadvm_state_main to process the contents of the package (2)
 which contains commands (3,6) and devices (4...)
 
 On receipt of 'postcopy listen' - 3 -(i.e. the 1st command in the package)
diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
index 3bb8897..0f59912 100644
--- a/include/sysemu/sysemu.h
+++ b/include/sysemu/sysemu.h
@@ -120,7 +120,7 @@  void qemu_savevm_command_send(QEMUFile *f, enum qemu_vm_cmd command,
                               uint16_t len, uint8_t *data);
 void qemu_savevm_send_ping(QEMUFile *f, uint32_t value);
 void qemu_savevm_send_open_return_path(QEMUFile *f);
-int qemu_savevm_send_packaged(QEMUFile *f, const QEMUSizedBuffer *qsb);
+int qemu_savevm_send_packaged(QEMUFile *f, const char *buf, size_t len);
 void qemu_savevm_send_postcopy_advise(QEMUFile *f);
 void qemu_savevm_send_postcopy_listen(QEMUFile *f);
 void qemu_savevm_send_postcopy_run(QEMUFile *f);
diff --git a/migration/migration.c b/migration/migration.c
index 715f069..e921b20 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -33,6 +33,7 @@ 
 #include "qom/cpu.h"
 #include "exec/memory.h"
 #include "exec/address-spaces.h"
+#include "io/channel-buffer.h"
 
 #define MAX_THROTTLE  (32 << 20)      /* Migration transfer speed throttling */
 
@@ -1401,7 +1402,8 @@  static int await_return_path_close_on_source(MigrationState *ms)
 static int postcopy_start(MigrationState *ms, bool *old_vm_running)
 {
     int ret;
-    const QEMUSizedBuffer *qsb;
+    QIOChannelBuffer *bioc;
+    QEMUFile *fb;
     int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
     migrate_set_state(ms, MIGRATION_STATUS_ACTIVE,
                       MIGRATION_STATUS_POSTCOPY_ACTIVE);
@@ -1456,11 +1458,9 @@  static int postcopy_start(MigrationState *ms, bool *old_vm_running)
      * So we wrap the device state up in a package with a length at the start;
      * to do this we use a qemu_buf to hold the whole of the device state.
      */
-    QEMUFile *fb = qemu_bufopen("w", NULL);
-    if (!fb) {
-        error_report("Failed to create buffered file");
-        goto fail;
-    }
+    bioc = qio_channel_buffer_new(4096);
+    fb = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
+    object_unref(OBJECT(bioc));
 
     /*
      * Make sure the receiver can get incoming pages before we send the rest
@@ -1474,10 +1474,9 @@  static int postcopy_start(MigrationState *ms, bool *old_vm_running)
     qemu_savevm_send_postcopy_run(fb);
 
     /* <><> end of stuff going into the package */
-    qsb = qemu_buf_get(fb);
 
     /* Now send that blob */
-    if (qemu_savevm_send_packaged(ms->file, qsb)) {
+    if (qemu_savevm_send_packaged(ms->file, bioc->data, bioc->usage)) {
         goto fail_closefb;
     }
     qemu_fclose(fb);
diff --git a/migration/savevm.c b/migration/savevm.c
index 0ad1b93..f2e1880 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -50,6 +50,7 @@ 
 #include "qemu/iov.h"
 #include "block/snapshot.h"
 #include "block/qapi.h"
+#include "io/channel-buffer.h"
 
 
 #ifndef ETH_P_RARP
@@ -760,10 +761,8 @@  void qemu_savevm_send_open_return_path(QEMUFile *f)
  *    0 on success
  *    -ve on error
  */
-int qemu_savevm_send_packaged(QEMUFile *f, const QEMUSizedBuffer *qsb)
+int qemu_savevm_send_packaged(QEMUFile *f, const char *buf, size_t len)
 {
-    size_t cur_iov;
-    size_t len = qsb_get_length(qsb);
     uint32_t tmp;
 
     if (len > MAX_VM_CMD_PACKAGED_SIZE) {
@@ -777,18 +776,7 @@  int qemu_savevm_send_packaged(QEMUFile *f, const QEMUSizedBuffer *qsb)
     trace_qemu_savevm_send_packaged();
     qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
 
-    /* all the data follows (concatinating the iov's) */
-    for (cur_iov = 0; cur_iov < qsb->n_iov; cur_iov++) {
-        /* The iov entries are partially filled */
-        size_t towrite = MIN(qsb->iov[cur_iov].iov_len, len);
-        len -= towrite;
-
-        if (!towrite) {
-            break;
-        }
-
-        qemu_put_buffer(f, qsb->iov[cur_iov].iov_base, towrite);
-    }
+    qemu_put_buffer(f, (const uint8_t *)buf, len);
 
     return 0;
 }
@@ -1548,9 +1536,8 @@  static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
 static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
 {
     int ret;
-    uint8_t *buffer;
     uint32_t length;
-    QEMUSizedBuffer *qsb;
+    QIOChannelBuffer *bioc;
 
     length = qemu_get_be32(mis->from_src_file);
     trace_loadvm_handle_cmd_packaged(length);
@@ -1559,28 +1546,26 @@  static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
         error_report("Unreasonably large packaged state: %u", length);
         return -1;
     }
-    buffer = g_malloc0(length);
-    ret = qemu_get_buffer(mis->from_src_file, buffer, (int)length);
+
+    bioc = qio_channel_buffer_new(length);
+    ret = qemu_get_buffer(mis->from_src_file,
+                          (uint8_t *)bioc->data,
+                          (int)length);
     if (ret != length) {
-        g_free(buffer);
+        object_unref(OBJECT(bioc));
         error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%d\n",
                 ret, length);
         return (ret < 0) ? ret : -EAGAIN;
     }
+    bioc->usage += length;
     trace_loadvm_handle_cmd_packaged_received(ret);
 
-    /* Setup a dummy QEMUFile that actually reads from the buffer */
-    qsb = qsb_create(buffer, length);
-    g_free(buffer); /* Because qsb_create copies */
-    if (!qsb) {
-        error_report("Unable to create qsb");
-    }
-    QEMUFile *packf = qemu_bufopen("r", qsb);
+    QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
 
     ret = qemu_loadvm_state_main(packf, mis);
     trace_loadvm_handle_cmd_packaged_main(ret);
     qemu_fclose(packf);
-    qsb_free(qsb);
+    object_unref(OBJECT(bioc));
 
     return ret;
 }