Yuan Liu <[email protected]> writes: > the new function get_iov_count is used to get the number of > IOVs required by a specified multifd method > > Different multifd methods may require different numbers of IOVs. > Based on streaming compression of zlib and zstd, all pages will be > compressed to a data block, so an IOV is required to send this data > block. For no compression, each IOV is used to send a page, so the > number of IOVs required is the same as the number of pages.
Let's just move the responsibility of allocating p->iov to the client code. You can move the allocation into send_setup() and the free into send_cleanup(). > > Signed-off-by: Yuan Liu <[email protected]> > Reviewed-by: Nanhai Zou <[email protected]> > --- > migration/multifd-zlib.c | 18 +++++++++++++++++- > migration/multifd-zstd.c | 18 +++++++++++++++++- > migration/multifd.c | 24 +++++++++++++++++++++--- > migration/multifd.h | 2 ++ > 4 files changed, 57 insertions(+), 5 deletions(-) > > diff --git a/migration/multifd-zlib.c b/migration/multifd-zlib.c > index 012e3bdea1..35187f2aff 100644 > --- a/migration/multifd-zlib.c > +++ b/migration/multifd-zlib.c > @@ -313,13 +313,29 @@ static int zlib_recv_pages(MultiFDRecvParams *p, Error > **errp) > return 0; > } > > +/** > + * zlib_get_iov_count: get the count of IOVs > + * > + * For zlib streaming compression, all pages will be compressed into a data > + * block, and an IOV is requested for sending this block. > + * > + * Returns the count of the IOVs > + * > + * @page_count: Indicate the maximum count of pages processed by multifd > + */ > +static uint32_t zlib_get_iov_count(uint32_t page_count) > +{ > + return 1; > +} > + > static MultiFDMethods multifd_zlib_ops = { > .send_setup = zlib_send_setup, > .send_cleanup = zlib_send_cleanup, > .send_prepare = zlib_send_prepare, > .recv_setup = zlib_recv_setup, > .recv_cleanup = zlib_recv_cleanup, > - .recv_pages = zlib_recv_pages > + .recv_pages = zlib_recv_pages, > + .get_iov_count = zlib_get_iov_count > }; > > static void multifd_zlib_register(void) > diff --git a/migration/multifd-zstd.c b/migration/multifd-zstd.c > index dc8fe43e94..25ed1add2a 100644 > --- a/migration/multifd-zstd.c > +++ b/migration/multifd-zstd.c > @@ -304,13 +304,29 @@ static int zstd_recv_pages(MultiFDRecvParams *p, Error > **errp) > return 0; > } > > +/** > + * zstd_get_iov_count: get the count of IOVs > + * > + * For zstd streaming compression, all pages will be compressed into a data > + * block, and an IOV is requested for sending this block. > + * > + * Returns the count of the IOVs > + * > + * @page_count: Indicate the maximum count of pages processed by multifd > + */ > +static uint32_t zstd_get_iov_count(uint32_t page_count) > +{ > + return 1; > +} > + > static MultiFDMethods multifd_zstd_ops = { > .send_setup = zstd_send_setup, > .send_cleanup = zstd_send_cleanup, > .send_prepare = zstd_send_prepare, > .recv_setup = zstd_recv_setup, > .recv_cleanup = zstd_recv_cleanup, > - .recv_pages = zstd_recv_pages > + .recv_pages = zstd_recv_pages, > + .get_iov_count = zstd_get_iov_count > }; > > static void multifd_zstd_register(void) > diff --git a/migration/multifd.c b/migration/multifd.c > index adfe8c9a0a..787402247e 100644 > --- a/migration/multifd.c > +++ b/migration/multifd.c > @@ -209,13 +209,29 @@ static int nocomp_recv_pages(MultiFDRecvParams *p, > Error **errp) > return qio_channel_readv_all(p->c, p->iov, p->normal_num, errp); > } > > +/** > + * nocomp_get_iov_count: get the count of IOVs > + * > + * For no compression, the count of IOVs required is the same as the count of > + * pages > + * > + * Returns the count of the IOVs > + * > + * @page_count: Indicate the maximum count of pages processed by multifd > + */ > +static uint32_t nocomp_get_iov_count(uint32_t page_count) > +{ > + return page_count; > +} > + > static MultiFDMethods multifd_nocomp_ops = { > .send_setup = nocomp_send_setup, > .send_cleanup = nocomp_send_cleanup, > .send_prepare = nocomp_send_prepare, > .recv_setup = nocomp_recv_setup, > .recv_cleanup = nocomp_recv_cleanup, > - .recv_pages = nocomp_recv_pages > + .recv_pages = nocomp_recv_pages, > + .get_iov_count = nocomp_get_iov_count > }; > > static MultiFDMethods *multifd_ops[MULTIFD_COMPRESSION__MAX] = { > @@ -998,6 +1014,8 @@ bool multifd_send_setup(void) > Error *local_err = NULL; > int thread_count, ret = 0; > uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size(); > + /* We need one extra place for the packet header */ > + uint32_t iov_count = 1; > uint8_t i; > > if (!migrate_multifd()) { > @@ -1012,6 +1030,7 @@ bool multifd_send_setup(void) > qemu_sem_init(&multifd_send_state->channels_ready, 0); > qatomic_set(&multifd_send_state->exiting, 0); > multifd_send_state->ops = multifd_ops[migrate_multifd_compression()]; > + iov_count += multifd_send_state->ops->get_iov_count(page_count); > > for (i = 0; i < thread_count; i++) { > MultiFDSendParams *p = &multifd_send_state->params[i]; > @@ -1026,8 +1045,7 @@ bool multifd_send_setup(void) > p->packet->magic = cpu_to_be32(MULTIFD_MAGIC); > p->packet->version = cpu_to_be32(MULTIFD_VERSION); > p->name = g_strdup_printf("multifdsend_%d", i); > - /* We need one extra place for the packet header */ > - p->iov = g_new0(struct iovec, page_count + 1); > + p->iov = g_new0(struct iovec, iov_count); > p->page_size = qemu_target_page_size(); > p->page_count = page_count; > p->write_flags = 0; > diff --git a/migration/multifd.h b/migration/multifd.h > index 8a1cad0996..d82495c508 100644 > --- a/migration/multifd.h > +++ b/migration/multifd.h > @@ -201,6 +201,8 @@ typedef struct { > void (*recv_cleanup)(MultiFDRecvParams *p); > /* Read all pages */ > int (*recv_pages)(MultiFDRecvParams *p, Error **errp); > + /* Get the count of required IOVs */ > + uint32_t (*get_iov_count)(uint32_t page_count); > } MultiFDMethods; > > void multifd_register_ops(int method, MultiFDMethods *ops);
