diff mbox series

[v3,7/8] multifd: Add zlib compression support

Message ID 20190515121544.4597-8-quintela@redhat.com
State New
Headers show
Series WIP: Multifd compression support | expand

Commit Message

Juan Quintela May 15, 2019, 12:15 p.m. UTC
Signed-off-by: Juan Quintela <quintela@redhat.com>
---
 hw/core/qdev-properties.c |  2 +-
 migration/migration.c     |  9 ++++++++
 migration/migration.h     |  1 +
 migration/ram.c           | 47 +++++++++++++++++++++++++++++++++++++++
 qapi/migration.json       |  2 +-
 tests/migration-test.c    |  6 +++++
 6 files changed, 65 insertions(+), 2 deletions(-)

Comments

Wei Yang May 21, 2019, 3:07 a.m. UTC | #1
On Wed, May 15, 2019 at 02:15:43PM +0200, Juan Quintela wrote:
>Signed-off-by: Juan Quintela <quintela@redhat.com>
>---
> hw/core/qdev-properties.c |  2 +-
> migration/migration.c     |  9 ++++++++
> migration/migration.h     |  1 +
> migration/ram.c           | 47 +++++++++++++++++++++++++++++++++++++++
> qapi/migration.json       |  2 +-
> tests/migration-test.c    |  6 +++++
> 6 files changed, 65 insertions(+), 2 deletions(-)
>
>diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
>index ebeeb5c88d..e40aa806e2 100644
>--- a/hw/core/qdev-properties.c
>+++ b/hw/core/qdev-properties.c
>@@ -651,7 +651,7 @@ const PropertyInfo qdev_prop_fdc_drive_type = {
> const PropertyInfo qdev_prop_multifd_compress = {
>     .name = "MultifdCompress",
>     .description = "multifd_compress values, "
>-                   "none",
>+                   "none/zlib",
>     .enum_table = &MultifdCompress_lookup,
>     .get = get_enum,
>     .set = set_enum,
>diff --git a/migration/migration.c b/migration/migration.c
>index d6f8ef342a..69d85cbe5e 100644
>--- a/migration/migration.c
>+++ b/migration/migration.c
>@@ -2141,6 +2141,15 @@ bool migrate_use_multifd(void)
>     return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
> }
> 
>+bool migrate_use_multifd_zlib(void)
>+{
>+    MigrationState *s;
>+
>+    s = migrate_get_current();
>+
>+    return s->parameters.multifd_compress == MULTIFD_COMPRESS_ZLIB;
>+}
>+
> bool migrate_pause_before_switchover(void)
> {
>     MigrationState *s;
>diff --git a/migration/migration.h b/migration/migration.h
>index 438f17edad..fc4fb841d4 100644
>--- a/migration/migration.h
>+++ b/migration/migration.h
>@@ -269,6 +269,7 @@ bool migrate_ignore_shared(void);
> 
> bool migrate_auto_converge(void);
> bool migrate_use_multifd(void);
>+bool migrate_use_multifd_zlib(void);
> bool migrate_pause_before_switchover(void);
> int migrate_multifd_channels(void);
> 
>diff --git a/migration/ram.c b/migration/ram.c
>index 6679e4f213..fdb5bf07a5 100644
>--- a/migration/ram.c
>+++ b/migration/ram.c
>@@ -582,6 +582,7 @@ exit:
> #define MULTIFD_VERSION 1
> 
> #define MULTIFD_FLAG_SYNC (1 << 0)
>+#define MULTIFD_FLAG_ZLIB (1 << 1)
> 

If no one use this in this patch, prefer to put it where it will be used.

> /* This value needs to be a multiple of qemu_target_page_size() */
> #define MULTIFD_PACKET_SIZE (512 * 1024)
>@@ -663,6 +664,12 @@ typedef struct {
>     uint64_t num_pages;
>     /* syncs main thread and channels */
>     QemuSemaphore sem_sync;
>+    /* stream for compression */
>+    z_stream zs;
>+    /* compressed buffer */
>+    uint8_t *zbuff;
>+    /* size of compressed buffer */
>+    uint32_t zbuff_len;
> }  MultiFDSendParams;
> 
> typedef struct {
>@@ -698,6 +705,12 @@ typedef struct {
>     uint64_t num_pages;
>     /* syncs main thread and channels */
>     QemuSemaphore sem_sync;
>+    /* stream for compression */
>+    z_stream zs;
>+    /* compressed buffer */
>+    uint8_t *zbuff;
>+    /* size of compressed buffer */
>+    uint32_t zbuff_len;
> } MultiFDRecvParams;
> 
> typedef struct {
>@@ -1071,6 +1084,9 @@ void multifd_save_cleanup(void)
>         p->packet_len = 0;
>         g_free(p->packet);
>         p->packet = NULL;
>+        deflateEnd(&p->zs);
>+        g_free(p->zbuff);
>+        p->zbuff = NULL;
>     }
>     qemu_sem_destroy(&multifd_send_state->channels_ready);
>     qemu_sem_destroy(&multifd_send_state->sem_sync);
>@@ -1240,6 +1256,7 @@ int multifd_save_setup(void)
> 
>     for (i = 0; i < thread_count; i++) {
>         MultiFDSendParams *p = &multifd_send_state->params[i];
>+        z_stream *zs = &p->zs;
> 
>         qemu_mutex_init(&p->mutex);
>         qemu_sem_init(&p->sem, 0);
>@@ -1253,6 +1270,17 @@ int multifd_save_setup(void)
>         p->packet = g_malloc0(p->packet_len);
>         p->name = g_strdup_printf("multifdsend_%d", i);
>         socket_send_channel_create(multifd_new_send_channel_async, p);
>+        zs->zalloc = Z_NULL;
>+        zs->zfree = Z_NULL;
>+        zs->opaque = Z_NULL;

Since zlib is not default option, is it better to setup these when zlib is
set?

>+        if (deflateInit(zs, migrate_compress_level()) != Z_OK) {
>+            printf("deflate init failed\n");
>+            return -1;
>+        }
>+        /* We will never have more than page_count pages */
>+        p->zbuff_len = page_count * qemu_target_page_size();
>+        p->zbuff_len *= 2;
>+        p->zbuff = g_malloc0(p->zbuff_len);
>     }
>     return 0;
> }
>@@ -1322,6 +1350,9 @@ int multifd_load_cleanup(Error **errp)
>         p->packet_len = 0;
>         g_free(p->packet);
>         p->packet = NULL;
>+        inflateEnd(&p->zs);
>+        g_free(p->zbuff);
>+        p->zbuff = NULL;
>     }
>     qemu_sem_destroy(&multifd_recv_state->sem_sync);
>     g_free(multifd_recv_state->params);
>@@ -1440,6 +1471,7 @@ int multifd_load_setup(void)
> 
>     for (i = 0; i < thread_count; i++) {
>         MultiFDRecvParams *p = &multifd_recv_state->params[i];
>+        z_stream *zs = &p->zs;
> 
>         qemu_mutex_init(&p->mutex);
>         qemu_sem_init(&p->sem_sync, 0);
>@@ -1449,6 +1481,21 @@ int multifd_load_setup(void)
>                       + sizeof(ram_addr_t) * page_count;
>         p->packet = g_malloc0(p->packet_len);
>         p->name = g_strdup_printf("multifdrecv_%d", i);
>+
>+        zs->zalloc = Z_NULL;
>+        zs->zfree = Z_NULL;
>+        zs->opaque = Z_NULL;
>+        zs->avail_in = 0;
>+        zs->next_in = Z_NULL;
>+        if (inflateInit(zs) != Z_OK) {
>+            printf("inflate init failed\n");
>+            return -1;
>+        }
>+        /* We will never have more than page_count pages */
>+        p->zbuff_len = page_count * qemu_target_page_size();
>+        /* We know compression "could" use more space */
>+        p->zbuff_len *= 2;
>+        p->zbuff = g_malloc0(p->zbuff_len);
>     }
>     return 0;
> }
>diff --git a/qapi/migration.json b/qapi/migration.json
>index 8ec1944b7a..e6c27fae06 100644
>--- a/qapi/migration.json
>+++ b/qapi/migration.json
>@@ -493,7 +493,7 @@
> #
> ##
> { 'enum': 'MultifdCompress',
>-  'data': [ 'none' ] }
>+  'data': [ 'none', 'zlib' ] }
> 
> ##
> # @MigrationParameter:
>diff --git a/tests/migration-test.c b/tests/migration-test.c
>index 8a1ccc2516..2dd4d4c5b4 100644
>--- a/tests/migration-test.c
>+++ b/tests/migration-test.c
>@@ -1119,6 +1119,11 @@ static void test_multifd_tcp_none(void)
>     test_multifd_tcp("none");
> }
> 
>+static void test_multifd_tcp_zlib(void)
>+{
>+    test_multifd_tcp("zlib");
>+}
>+
> int main(int argc, char **argv)
> {
>     char template[] = "/tmp/migration-test-XXXXXX";
>@@ -1174,6 +1179,7 @@ int main(int argc, char **argv)
>     /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
>     qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
>     qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none);
>+    qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib);

Actually zlib is not enabled at this moment, the test here may not touch the
real functionality.

> 
>     ret = g_test_run();
> 
>-- 
>2.21.0
>
Dr. David Alan Gilbert May 29, 2019, 4:48 p.m. UTC | #2
* Juan Quintela (quintela@redhat.com) wrote:
> Signed-off-by: Juan Quintela <quintela@redhat.com>
> ---
>  hw/core/qdev-properties.c |  2 +-
>  migration/migration.c     |  9 ++++++++
>  migration/migration.h     |  1 +
>  migration/ram.c           | 47 +++++++++++++++++++++++++++++++++++++++
>  qapi/migration.json       |  2 +-
>  tests/migration-test.c    |  6 +++++
>  6 files changed, 65 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
> index ebeeb5c88d..e40aa806e2 100644
> --- a/hw/core/qdev-properties.c
> +++ b/hw/core/qdev-properties.c
> @@ -651,7 +651,7 @@ const PropertyInfo qdev_prop_fdc_drive_type = {
>  const PropertyInfo qdev_prop_multifd_compress = {
>      .name = "MultifdCompress",
>      .description = "multifd_compress values, "
> -                   "none",
> +                   "none/zlib",
>      .enum_table = &MultifdCompress_lookup,
>      .get = get_enum,
>      .set = set_enum,
> diff --git a/migration/migration.c b/migration/migration.c
> index d6f8ef342a..69d85cbe5e 100644
> --- a/migration/migration.c
> +++ b/migration/migration.c
> @@ -2141,6 +2141,15 @@ bool migrate_use_multifd(void)
>      return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
>  }
>  
> +bool migrate_use_multifd_zlib(void)
> +{
> +    MigrationState *s;
> +
> +    s = migrate_get_current();
> +
> +    return s->parameters.multifd_compress == MULTIFD_COMPRESS_ZLIB;
> +}
> +
>  bool migrate_pause_before_switchover(void)
>  {
>      MigrationState *s;
> diff --git a/migration/migration.h b/migration/migration.h
> index 438f17edad..fc4fb841d4 100644
> --- a/migration/migration.h
> +++ b/migration/migration.h
> @@ -269,6 +269,7 @@ bool migrate_ignore_shared(void);
>  
>  bool migrate_auto_converge(void);
>  bool migrate_use_multifd(void);
> +bool migrate_use_multifd_zlib(void);
>  bool migrate_pause_before_switchover(void);
>  int migrate_multifd_channels(void);
>  
> diff --git a/migration/ram.c b/migration/ram.c
> index 6679e4f213..fdb5bf07a5 100644
> --- a/migration/ram.c
> +++ b/migration/ram.c
> @@ -582,6 +582,7 @@ exit:
>  #define MULTIFD_VERSION 1
>  
>  #define MULTIFD_FLAG_SYNC (1 << 0)
> +#define MULTIFD_FLAG_ZLIB (1 << 1)
>  
>  /* This value needs to be a multiple of qemu_target_page_size() */
>  #define MULTIFD_PACKET_SIZE (512 * 1024)
> @@ -663,6 +664,12 @@ typedef struct {
>      uint64_t num_pages;
>      /* syncs main thread and channels */
>      QemuSemaphore sem_sync;
> +    /* stream for compression */
> +    z_stream zs;
> +    /* compressed buffer */
> +    uint8_t *zbuff;
> +    /* size of compressed buffer */
> +    uint32_t zbuff_len;

Does this set need to be in a union or something so that you select
them for different compression types?

>  }  MultiFDSendParams;
>  
>  typedef struct {
> @@ -698,6 +705,12 @@ typedef struct {
>      uint64_t num_pages;
>      /* syncs main thread and channels */
>      QemuSemaphore sem_sync;
> +    /* stream for compression */

de-compression?

> +    z_stream zs;
> +    /* compressed buffer */
> +    uint8_t *zbuff;
> +    /* size of compressed buffer */
> +    uint32_t zbuff_len;
>  } MultiFDRecvParams;
>  
>  typedef struct {
> @@ -1071,6 +1084,9 @@ void multifd_save_cleanup(void)
>          p->packet_len = 0;
>          g_free(p->packet);
>          p->packet = NULL;
> +        deflateEnd(&p->zs);
> +        g_free(p->zbuff);
> +        p->zbuff = NULL;
>      }
>      qemu_sem_destroy(&multifd_send_state->channels_ready);
>      qemu_sem_destroy(&multifd_send_state->sem_sync);
> @@ -1240,6 +1256,7 @@ int multifd_save_setup(void)
>  
>      for (i = 0; i < thread_count; i++) {
>          MultiFDSendParams *p = &multifd_send_state->params[i];
> +        z_stream *zs = &p->zs;
>  
>          qemu_mutex_init(&p->mutex);
>          qemu_sem_init(&p->sem, 0);
> @@ -1253,6 +1270,17 @@ int multifd_save_setup(void)
>          p->packet = g_malloc0(p->packet_len);
>          p->name = g_strdup_printf("multifdsend_%d", i);
>          socket_send_channel_create(multifd_new_send_channel_async, p);
> +        zs->zalloc = Z_NULL;
> +        zs->zfree = Z_NULL;
> +        zs->opaque = Z_NULL;
> +        if (deflateInit(zs, migrate_compress_level()) != Z_OK) {
> +            printf("deflate init failed\n");
> +            return -1;
> +        }
> +        /* We will never have more than page_count pages */
> +        p->zbuff_len = page_count * qemu_target_page_size();
> +        p->zbuff_len *= 2;

Should the ops gain a 'save_init' and 'load_init' so that you can
only do this lot if the compression is enabled?

> +        p->zbuff = g_malloc0(p->zbuff_len);

I'd prefer g_try_malloc and do failure given it's not a tiny buffer.

>      }
>      return 0;
>  }
> @@ -1322,6 +1350,9 @@ int multifd_load_cleanup(Error **errp)
>          p->packet_len = 0;
>          g_free(p->packet);
>          p->packet = NULL;
> +        inflateEnd(&p->zs);
> +        g_free(p->zbuff);
> +        p->zbuff = NULL;
>      }
>      qemu_sem_destroy(&multifd_recv_state->sem_sync);
>      g_free(multifd_recv_state->params);
> @@ -1440,6 +1471,7 @@ int multifd_load_setup(void)
>  
>      for (i = 0; i < thread_count; i++) {
>          MultiFDRecvParams *p = &multifd_recv_state->params[i];
> +        z_stream *zs = &p->zs;
>  
>          qemu_mutex_init(&p->mutex);
>          qemu_sem_init(&p->sem_sync, 0);
> @@ -1449,6 +1481,21 @@ int multifd_load_setup(void)
>                        + sizeof(ram_addr_t) * page_count;
>          p->packet = g_malloc0(p->packet_len);
>          p->name = g_strdup_printf("multifdrecv_%d", i);
> +
> +        zs->zalloc = Z_NULL;
> +        zs->zfree = Z_NULL;
> +        zs->opaque = Z_NULL;
> +        zs->avail_in = 0;
> +        zs->next_in = Z_NULL;
> +        if (inflateInit(zs) != Z_OK) {
> +            printf("inflate init failed\n");
> +            return -1;
> +        }
> +        /* We will never have more than page_count pages */
> +        p->zbuff_len = page_count * qemu_target_page_size();
> +        /* We know compression "could" use more space */
> +        p->zbuff_len *= 2;
> +        p->zbuff = g_malloc0(p->zbuff_len);
>      }
>      return 0;
>  }
> diff --git a/qapi/migration.json b/qapi/migration.json
> index 8ec1944b7a..e6c27fae06 100644
> --- a/qapi/migration.json
> +++ b/qapi/migration.json
> @@ -493,7 +493,7 @@
>  #
>  ##
>  { 'enum': 'MultifdCompress',
> -  'data': [ 'none' ] }
> +  'data': [ 'none', 'zlib' ] }
>  
>  ##
>  # @MigrationParameter:
> diff --git a/tests/migration-test.c b/tests/migration-test.c
> index 8a1ccc2516..2dd4d4c5b4 100644
> --- a/tests/migration-test.c
> +++ b/tests/migration-test.c
> @@ -1119,6 +1119,11 @@ static void test_multifd_tcp_none(void)
>      test_multifd_tcp("none");
>  }
>  
> +static void test_multifd_tcp_zlib(void)
> +{
> +    test_multifd_tcp("zlib");
> +}
> +
>  int main(int argc, char **argv)
>  {
>      char template[] = "/tmp/migration-test-XXXXXX";
> @@ -1174,6 +1179,7 @@ int main(int argc, char **argv)
>      /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
>      qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
>      qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none);
> +    qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib);
>  
>      ret = g_test_run();
>  
> -- 
> 2.21.0
> 
--
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK
Juan Quintela June 11, 2019, 4:46 p.m. UTC | #3
"Dr. David Alan Gilbert" <dgilbert@redhat.com> wrote:
> * Juan Quintela (quintela@redhat.com) wrote:
>> Signed-off-by: Juan Quintela <quintela@redhat.com>
>> ---
>>  hw/core/qdev-properties.c |  2 +-
>>  migration/migration.c     |  9 ++++++++
>>  migration/migration.h     |  1 +
>>  migration/ram.c           | 47 +++++++++++++++++++++++++++++++++++++++
>>  qapi/migration.json       |  2 +-
>>  tests/migration-test.c    |  6 +++++
>>  6 files changed, 65 insertions(+), 2 deletions(-)
>> 

>>  /* This value needs to be a multiple of qemu_target_page_size() */
>>  #define MULTIFD_PACKET_SIZE (512 * 1024)
>> @@ -663,6 +664,12 @@ typedef struct {
>>      uint64_t num_pages;
>>      /* syncs main thread and channels */
>>      QemuSemaphore sem_sync;
>> +    /* stream for compression */
>> +    z_stream zs;
>> +    /* compressed buffer */
>> +    uint8_t *zbuff;
>> +    /* size of compressed buffer */
>> +    uint32_t zbuff_len;
>
> Does this set need to be in a union or something so that you select
> them for different compression types?

Yeap.  Done.

>
>>  }  MultiFDSendParams;
>>  
>>  typedef struct {
>> @@ -698,6 +705,12 @@ typedef struct {
>>      uint64_t num_pages;
>>      /* syncs main thread and channels */
>>      QemuSemaphore sem_sync;
>> +    /* stream for compression */
>
> de-compression?

Changed.  I think that "compression methods" mean both, but who I am to
discuss with a native speaker O:-)

>>          socket_send_channel_create(multifd_new_send_channel_async, p);
>> +        zs->zalloc = Z_NULL;
>> +        zs->zfree = Z_NULL;
>> +        zs->opaque = Z_NULL;
>> +        if (deflateInit(zs, migrate_compress_level()) != Z_OK) {
>> +            printf("deflate init failed\n");
>> +            return -1;
>> +        }
>> +        /* We will never have more than page_count pages */
>> +        p->zbuff_len = page_count * qemu_target_page_size();
>> +        p->zbuff_len *= 2;
>
> Should the ops gain a 'save_init' and 'load_init' so that you can
> only do this lot if the compression is enabled?

send_setup()/send_cleanup()
recv_setup()/recv_cleanup()

I have tried to be consistent ....

>
>> +        p->zbuff = g_malloc0(p->zbuff_len);
>
> I'd prefer g_try_malloc and do failure given it's not a tiny buffer.

I can change, no problem there.
Changing prototypes to get an Error *.


Thanks, Juan.
Juan Quintela June 11, 2019, 4:50 p.m. UTC | #4
Wei Yang <richardw.yang@linux.intel.com> wrote:
> On Wed, May 15, 2019 at 02:15:43PM +0200, Juan Quintela wrote:

>> 
>> #define MULTIFD_FLAG_SYNC (1 << 0)
>>+#define MULTIFD_FLAG_ZLIB (1 << 1)
>> 
>
> If no one use this in this patch, prefer to put it where it will be used.

Oops, you are right, I have to use it.

>>@@ -1253,6 +1270,17 @@ int multifd_save_setup(void)
>>         p->packet = g_malloc0(p->packet_len);
>>         p->name = g_strdup_printf("multifdsend_%d", i);
>>         socket_send_channel_create(multifd_new_send_channel_async, p);
>>+        zs->zalloc = Z_NULL;
>>+        zs->zfree = Z_NULL;
>>+        zs->opaque = Z_NULL;
>
> Since zlib is not default option, is it better to setup these when zlib is
> set?

Moved to an opaque void *data pointer, thanks.

>>+        if (deflateInit(zs, migrate_compress_level()) != Z_OK) {
>>+            printf("deflate init failed\n");
>>+            return -1;
>>+        }
>>+        /* We will never have more than page_count pages */
>>+        p->zbuff_len = page_count * qemu_target_page_size();
>>+        p->zbuff_len *= 2;
>>+        p->zbuff = g_malloc0(p->zbuff_len);
>>     }
>>     return 0;
>> }
>>@@ -1322,6 +1350,9 @@ int multifd_load_cleanup(Error **errp)
>>         p->packet_len = 0;
>>         g_free(p->packet);
>>         p->packet = NULL;
>>+        inflateEnd(&p->zs);
>>+        g_free(p->zbuff);
>>+        p->zbuff = NULL;
>>     }
>>     qemu_sem_destroy(&multifd_recv_state->sem_sync);
>>     g_free(multifd_recv_state->params);
>>@@ -1440,6 +1471,7 @@ int multifd_load_setup(void)
>> 
>>     for (i = 0; i < thread_count; i++) {
>>         MultiFDRecvParams *p = &multifd_recv_state->params[i];
>>+        z_stream *zs = &p->zs;
>> 
>>         qemu_mutex_init(&p->mutex);
>>         qemu_sem_init(&p->sem_sync, 0);
>>@@ -1449,6 +1481,21 @@ int multifd_load_setup(void)
>>                       + sizeof(ram_addr_t) * page_count;
>>         p->packet = g_malloc0(p->packet_len);
>>         p->name = g_strdup_printf("multifdrecv_%d", i);
>>+
>>+        zs->zalloc = Z_NULL;
>>+        zs->zfree = Z_NULL;
>>+        zs->opaque = Z_NULL;
>>+        zs->avail_in = 0;
>>+        zs->next_in = Z_NULL;
>>+        if (inflateInit(zs) != Z_OK) {
>>+            printf("inflate init failed\n");
>>+            return -1;
>>+        }
>>+        /* We will never have more than page_count pages */
>>+        p->zbuff_len = page_count * qemu_target_page_size();
>>+        /* We know compression "could" use more space */
>>+        p->zbuff_len *= 2;
>>+        p->zbuff = g_malloc0(p->zbuff_len);
>>     }
>>     return 0;
>> }
>>diff --git a/qapi/migration.json b/qapi/migration.json
>>index 8ec1944b7a..e6c27fae06 100644
>>--- a/qapi/migration.json
>>+++ b/qapi/migration.json
>>@@ -493,7 +493,7 @@
>> #
>> ##
>> { 'enum': 'MultifdCompress',
>>-  'data': [ 'none' ] }
>>+  'data': [ 'none', 'zlib' ] }
>> 
>> ##
>> # @MigrationParameter:
>>diff --git a/tests/migration-test.c b/tests/migration-test.c
>>index 8a1ccc2516..2dd4d4c5b4 100644
>>--- a/tests/migration-test.c
>>+++ b/tests/migration-test.c
>>@@ -1119,6 +1119,11 @@ static void test_multifd_tcp_none(void)
>>     test_multifd_tcp("none");
>> }
>> 
>>+static void test_multifd_tcp_zlib(void)
>>+{
>>+    test_multifd_tcp("zlib");
>>+}
>>+
>> int main(int argc, char **argv)
>> {
>>     char template[] = "/tmp/migration-test-XXXXXX";
>>@@ -1174,6 +1179,7 @@ int main(int argc, char **argv)
>>     /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
>>     qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
>>     qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none);
>>+    qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib);
>
> Actually zlib is not enabled at this moment, the test here may not touch the
> real functionality.

It is, see what the

   test_multifd_tcp("zlib");

line does for:


>>     test_multifd_tcp("none");
>> }
>> 
>>+static void test_multifd_tcp_zlib(void)
>>+{
>>+    test_multifd_tcp("zlib");
>>+}
>>+
>> int main(int argc, char **argv)
>> {
>>     char template[] = "/tmp/migration-test-XXXXXX";
>>@@ -1174,6 +1179,7 @@ int main(int argc, char **argv)
>>     /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
>>     qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
>>     qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none);
>>+    qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib);
>
> Actually zlib is not enabled at this moment, the test here may not touch the
> real functionality.

It is, see what the

   test_multifd_tcp("zlib");

line does for:

static void test_multifd_tcp(const char *method)
{
    ....
    migrate_set_parameter_str(from, "multifd-compress", method);
    migrate_set_parameter_str(to, "multifd-compress", method);

    ...
}

Thanks, Juan.
diff mbox series

Patch

diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
index ebeeb5c88d..e40aa806e2 100644
--- a/hw/core/qdev-properties.c
+++ b/hw/core/qdev-properties.c
@@ -651,7 +651,7 @@  const PropertyInfo qdev_prop_fdc_drive_type = {
 const PropertyInfo qdev_prop_multifd_compress = {
     .name = "MultifdCompress",
     .description = "multifd_compress values, "
-                   "none",
+                   "none/zlib",
     .enum_table = &MultifdCompress_lookup,
     .get = get_enum,
     .set = set_enum,
diff --git a/migration/migration.c b/migration/migration.c
index d6f8ef342a..69d85cbe5e 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -2141,6 +2141,15 @@  bool migrate_use_multifd(void)
     return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD];
 }
 
+bool migrate_use_multifd_zlib(void)
+{
+    MigrationState *s;
+
+    s = migrate_get_current();
+
+    return s->parameters.multifd_compress == MULTIFD_COMPRESS_ZLIB;
+}
+
 bool migrate_pause_before_switchover(void)
 {
     MigrationState *s;
diff --git a/migration/migration.h b/migration/migration.h
index 438f17edad..fc4fb841d4 100644
--- a/migration/migration.h
+++ b/migration/migration.h
@@ -269,6 +269,7 @@  bool migrate_ignore_shared(void);
 
 bool migrate_auto_converge(void);
 bool migrate_use_multifd(void);
+bool migrate_use_multifd_zlib(void);
 bool migrate_pause_before_switchover(void);
 int migrate_multifd_channels(void);
 
diff --git a/migration/ram.c b/migration/ram.c
index 6679e4f213..fdb5bf07a5 100644
--- a/migration/ram.c
+++ b/migration/ram.c
@@ -582,6 +582,7 @@  exit:
 #define MULTIFD_VERSION 1
 
 #define MULTIFD_FLAG_SYNC (1 << 0)
+#define MULTIFD_FLAG_ZLIB (1 << 1)
 
 /* This value needs to be a multiple of qemu_target_page_size() */
 #define MULTIFD_PACKET_SIZE (512 * 1024)
@@ -663,6 +664,12 @@  typedef struct {
     uint64_t num_pages;
     /* syncs main thread and channels */
     QemuSemaphore sem_sync;
+    /* stream for compression */
+    z_stream zs;
+    /* compressed buffer */
+    uint8_t *zbuff;
+    /* size of compressed buffer */
+    uint32_t zbuff_len;
 }  MultiFDSendParams;
 
 typedef struct {
@@ -698,6 +705,12 @@  typedef struct {
     uint64_t num_pages;
     /* syncs main thread and channels */
     QemuSemaphore sem_sync;
+    /* stream for compression */
+    z_stream zs;
+    /* compressed buffer */
+    uint8_t *zbuff;
+    /* size of compressed buffer */
+    uint32_t zbuff_len;
 } MultiFDRecvParams;
 
 typedef struct {
@@ -1071,6 +1084,9 @@  void multifd_save_cleanup(void)
         p->packet_len = 0;
         g_free(p->packet);
         p->packet = NULL;
+        deflateEnd(&p->zs);
+        g_free(p->zbuff);
+        p->zbuff = NULL;
     }
     qemu_sem_destroy(&multifd_send_state->channels_ready);
     qemu_sem_destroy(&multifd_send_state->sem_sync);
@@ -1240,6 +1256,7 @@  int multifd_save_setup(void)
 
     for (i = 0; i < thread_count; i++) {
         MultiFDSendParams *p = &multifd_send_state->params[i];
+        z_stream *zs = &p->zs;
 
         qemu_mutex_init(&p->mutex);
         qemu_sem_init(&p->sem, 0);
@@ -1253,6 +1270,17 @@  int multifd_save_setup(void)
         p->packet = g_malloc0(p->packet_len);
         p->name = g_strdup_printf("multifdsend_%d", i);
         socket_send_channel_create(multifd_new_send_channel_async, p);
+        zs->zalloc = Z_NULL;
+        zs->zfree = Z_NULL;
+        zs->opaque = Z_NULL;
+        if (deflateInit(zs, migrate_compress_level()) != Z_OK) {
+            printf("deflate init failed\n");
+            return -1;
+        }
+        /* We will never have more than page_count pages */
+        p->zbuff_len = page_count * qemu_target_page_size();
+        p->zbuff_len *= 2;
+        p->zbuff = g_malloc0(p->zbuff_len);
     }
     return 0;
 }
@@ -1322,6 +1350,9 @@  int multifd_load_cleanup(Error **errp)
         p->packet_len = 0;
         g_free(p->packet);
         p->packet = NULL;
+        inflateEnd(&p->zs);
+        g_free(p->zbuff);
+        p->zbuff = NULL;
     }
     qemu_sem_destroy(&multifd_recv_state->sem_sync);
     g_free(multifd_recv_state->params);
@@ -1440,6 +1471,7 @@  int multifd_load_setup(void)
 
     for (i = 0; i < thread_count; i++) {
         MultiFDRecvParams *p = &multifd_recv_state->params[i];
+        z_stream *zs = &p->zs;
 
         qemu_mutex_init(&p->mutex);
         qemu_sem_init(&p->sem_sync, 0);
@@ -1449,6 +1481,21 @@  int multifd_load_setup(void)
                       + sizeof(ram_addr_t) * page_count;
         p->packet = g_malloc0(p->packet_len);
         p->name = g_strdup_printf("multifdrecv_%d", i);
+
+        zs->zalloc = Z_NULL;
+        zs->zfree = Z_NULL;
+        zs->opaque = Z_NULL;
+        zs->avail_in = 0;
+        zs->next_in = Z_NULL;
+        if (inflateInit(zs) != Z_OK) {
+            printf("inflate init failed\n");
+            return -1;
+        }
+        /* We will never have more than page_count pages */
+        p->zbuff_len = page_count * qemu_target_page_size();
+        /* We know compression "could" use more space */
+        p->zbuff_len *= 2;
+        p->zbuff = g_malloc0(p->zbuff_len);
     }
     return 0;
 }
diff --git a/qapi/migration.json b/qapi/migration.json
index 8ec1944b7a..e6c27fae06 100644
--- a/qapi/migration.json
+++ b/qapi/migration.json
@@ -493,7 +493,7 @@ 
 #
 ##
 { 'enum': 'MultifdCompress',
-  'data': [ 'none' ] }
+  'data': [ 'none', 'zlib' ] }
 
 ##
 # @MigrationParameter:
diff --git a/tests/migration-test.c b/tests/migration-test.c
index 8a1ccc2516..2dd4d4c5b4 100644
--- a/tests/migration-test.c
+++ b/tests/migration-test.c
@@ -1119,6 +1119,11 @@  static void test_multifd_tcp_none(void)
     test_multifd_tcp("none");
 }
 
+static void test_multifd_tcp_zlib(void)
+{
+    test_multifd_tcp("zlib");
+}
+
 int main(int argc, char **argv)
 {
     char template[] = "/tmp/migration-test-XXXXXX";
@@ -1174,6 +1179,7 @@  int main(int argc, char **argv)
     /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */
     qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix);
     qtest_add_func("/migration/multifd/tcp/none", test_multifd_tcp_none);
+    qtest_add_func("/migration/multifd/tcp/zlib", test_multifd_tcp_zlib);
 
     ret = g_test_run();