From nobody Tue Oct 28 12:15:18 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 151558877800896.53561144070352; Wed, 10 Jan 2018 04:52:58 -0800 (PST) Received: from localhost ([::1]:39025 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZFsH-0001Tl-4a for importer@patchew.org; Wed, 10 Jan 2018 07:52:57 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40353) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZFnF-00068i-02 for qemu-devel@nongnu.org; Wed, 10 Jan 2018 07:47:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eZFnC-0004XC-J5 for qemu-devel@nongnu.org; Wed, 10 Jan 2018 07:47:45 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50222) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eZFnC-0004W7-AL for qemu-devel@nongnu.org; Wed, 10 Jan 2018 07:47:42 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6246A61D07 for ; Wed, 10 Jan 2018 12:47:41 +0000 (UTC) Received: from secure.mitica (ovpn-116-55.ams2.redhat.com [10.36.116.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id ACD877B608; Wed, 10 Jan 2018 12:47:39 +0000 (UTC) From: Juan Quintela To: qemu-devel@nongnu.org Date: Wed, 10 Jan 2018 13:47:16 +0100 Message-Id: <20180110124723.11879-8-quintela@redhat.com> In-Reply-To: <20180110124723.11879-1-quintela@redhat.com> References: <20180110124723.11879-1-quintela@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 10 Jan 2018 12:47:41 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v10 07/14] migration: Create thread infrastructure for multifd recv side X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: lvivier@redhat.com, dgilbert@redhat.com, peterx@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We make the locking and the transfer of information specific, even if we are still receiving things through the main thread. Signed-off-by: Juan Quintela -- We split when we create the main channel and where we start the main migration thread, so we wait for the creation of the other threads. Use multifd_clear_pages(). Don't remove object_unref() We use correctly the channel numbres Denife multifd_new_channel/multifd_recv_thread in this patch, that is where it is used. rename migrate_new_channel to migrate_new_send_channel Add ToDo comment Add trace --- migration/migration.c | 5 +- migration/migration.h | 1 + migration/ram.c | 129 +++++++++++++++++++++++++++++++++++++++++++++= +--- migration/socket.c | 3 ++ migration/trace-events | 2 + 5 files changed, 132 insertions(+), 8 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index 77fc17f723..1545f3a0b0 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -406,7 +406,7 @@ static void migration_incoming_setup(QEMUFile *f) qemu_file_set_blocking(f, false); } =20 -static void migration_incoming_process(void) +void migration_incoming_process(void) { Coroutine *co =3D qemu_coroutine_create(process_incoming_migration_co,= NULL); qemu_coroutine_enter(co); @@ -424,7 +424,8 @@ void migration_ioc_process_incoming(QIOChannel *ioc) =20 if (!mis->from_src_file) { QEMUFile *f =3D qemu_fopen_channel_input(ioc); - migration_fd_process_incoming(f); + migration_incoming_setup(f); + return; } multifd_recv_new_channel(ioc); } diff --git a/migration/migration.h b/migration/migration.h index 29a7b79a39..7de193a9c0 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -172,6 +172,7 @@ void migrate_set_state(int *state, int old_state, int n= ew_state); =20 void migration_fd_process_incoming(QEMUFile *f); void migration_ioc_process_incoming(QIOChannel *ioc); +void migration_incoming_process(void); =20 bool migration_has_all_channels(void); =20 diff --git a/migration/ram.c b/migration/ram.c index 19c8089c4b..8443806f12 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -660,13 +660,20 @@ static uint16_t multifd_send_page(RAMBlock *block, ra= m_addr_t offset, } =20 struct MultiFDRecvParams { + /* not changed */ uint8_t id; char *name; QemuThread thread; QIOChannel *c; + QemuSemaphore ready; QemuSemaphore sem; QemuMutex mutex; + /* proteced by param mutex */ bool quit; + /* how many patches has sent this channel */ + uint32_t packets_recv; + multifd_pages_t *pages; + bool done; }; typedef struct MultiFDRecvParams MultiFDRecvParams; =20 @@ -676,6 +683,7 @@ struct { int count; /* Should we finish */ bool quit; + multifd_pages_t *pages; } *multifd_recv_state; =20 static void terminate_multifd_recv_threads(Error *errp) @@ -721,15 +729,51 @@ int multifd_load_cleanup(Error **errp) socket_recv_channel_unref(p->c); g_free(p->name); p->name =3D NULL; + multifd_pages_clear(p->pages); + p->pages =3D NULL; } g_free(multifd_recv_state->params); multifd_recv_state->params =3D NULL; + multifd_pages_clear(multifd_recv_state->pages); + multifd_recv_state->pages =3D NULL; g_free(multifd_recv_state); multifd_recv_state =3D NULL; =20 return ret; } =20 +static void *multifd_recv_thread(void *opaque) +{ + MultiFDRecvParams *p =3D opaque; + + qemu_sem_post(&p->ready); + while (true) { + qemu_mutex_lock(&p->mutex); + if (p->quit) { + qemu_mutex_unlock(&p->mutex); + break; + } + if (p->pages->used) { + p->pages->used =3D 0; + + trace_multifd_recv(p->id, p->pages->seq, p->pages->used); + + /* ToDo: receive pages here */ + + p->done =3D true; + p->packets_recv++; + qemu_mutex_unlock(&p->mutex); + qemu_sem_post(&p->ready); + continue; + } + qemu_mutex_unlock(&p->mutex); + qemu_sem_wait(&p->sem); + } + trace_multifd_recv_thread(p->id, p->packets_recv); + + return NULL; +} + int multifd_load_setup(void) { int thread_count; @@ -742,6 +786,8 @@ int multifd_load_setup(void) multifd_recv_state->params =3D g_new0(MultiFDRecvParams, thread_count); multifd_recv_state->count =3D 0; multifd_recv_state->quit =3D false; + multifd_pages_init(&multifd_recv_state->pages, + migrate_multifd_page_count()); return 0; } =20 @@ -752,7 +798,80 @@ int multifd_created_channels(void) =20 void multifd_recv_new_channel(QIOChannel *ioc) { - socket_recv_channel_unref(ioc); + MultiFDRecvParams *p; + MultiFDInit_t msg; + Error *local_err =3D NULL; + size_t ret; + + ret =3D qio_channel_read_all(ioc, (char *)&msg, sizeof(msg), &local_er= r); + if (ret !=3D 0) { + terminate_multifd_recv_threads(local_err); + return; + } + + if (memcmp(msg.uuid, &qemu_uuid, sizeof(qemu_uuid))) { + char *uuid =3D qemu_uuid_unparse_strdup(&qemu_uuid); + error_setg(&local_err, "multifd: received uuid '%s' and expected " + "uuid '%s' for channel %hhd", msg.uuid, uuid, msg.id); + g_free(uuid); + terminate_multifd_recv_threads(local_err); + return; + } + + p =3D &multifd_recv_state->params[msg.id]; + if (p->id !=3D 0) { + error_setg(&local_err, "multifd: received id '%d' already setup'", + msg.id); + terminate_multifd_recv_threads(local_err); + return; + } + qemu_mutex_init(&p->mutex); + qemu_sem_init(&p->sem, 0); + qemu_sem_init(&p->ready, 0); + p->quit =3D false; + p->id =3D msg.id; + p->done =3D false; + multifd_pages_init(&p->pages, migrate_multifd_page_count()); + p->c =3D ioc; + multifd_recv_state->count++; + p->name =3D g_strdup_printf("multifdrecv_%d", msg.id); + socket_recv_channel_ref(ioc); + + qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p, + QEMU_THREAD_JOINABLE); + if (multifd_recv_state->count =3D=3D migrate_multifd_channels()) { + migration_incoming_process(); + } +} + +static void multifd_recv_page(RAMBlock *block, ram_addr_t offset, + uint8_t *address, uint16_t fd_num) +{ + int thread_count; + MultiFDRecvParams *p; + multifd_pages_t *pages =3D multifd_recv_state->pages; + + pages->iov[pages->used].iov_base =3D address; + pages->iov[pages->used].iov_len =3D TARGET_PAGE_SIZE; + pages->used++; + + if (fd_num =3D=3D MULTIFD_CONTINUE) { + return; + } + + thread_count =3D migrate_multifd_channels(); + assert(fd_num < thread_count); + p =3D &multifd_recv_state->params[fd_num]; + + qemu_sem_wait(&p->ready); + + qemu_mutex_lock(&p->mutex); + p->done =3D false; + p->pages->used =3D 0; + multifd_recv_state->pages =3D p->pages; + p->pages =3D pages; + qemu_mutex_unlock(&p->mutex); + qemu_sem_post(&p->sem); } =20 /** @@ -3042,6 +3161,7 @@ static int ram_load(QEMUFile *f, void *opaque, int ve= rsion_id) } =20 while (!postcopy_running && !ret && !(flags & RAM_SAVE_FLAG_EOS)) { + RAMBlock *block =3D NULL; ram_addr_t addr, total_ram_bytes; void *host =3D NULL; uint16_t fd_num; @@ -3066,7 +3186,7 @@ static int ram_load(QEMUFile *f, void *opaque, int ve= rsion_id) if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE | RAM_SAVE_FLAG_MULTIFD_PAGE)) { - RAMBlock *block =3D ram_block_from_stream(f, flags); + block =3D ram_block_from_stream(f, flags); =20 host =3D host_from_ram_block_offset(block, addr); if (!host) { @@ -3157,10 +3277,7 @@ static int ram_load(QEMUFile *f, void *opaque, int v= ersion_id) =20 case RAM_SAVE_FLAG_MULTIFD_PAGE: fd_num =3D qemu_get_be16(f); - if (fd_num !=3D 0) { - /* this is yet an unused variable, changed later */ - fd_num =3D fd_num; - } + multifd_recv_page(block, addr, host, fd_num); qemu_get_buffer(f, host, TARGET_PAGE_SIZE); break; =20 diff --git a/migration/socket.c b/migration/socket.c index aedabee8a1..ba23442175 100644 --- a/migration/socket.c +++ b/migration/socket.c @@ -192,6 +192,9 @@ out: if (migration_has_all_channels()) { /* Close listening socket as its no longer needed */ qio_channel_close(ioc, NULL); + if (!migrate_use_multifd()) { + migration_incoming_process(); + } return G_SOURCE_REMOVE; } else { return G_SOURCE_CONTINUE; diff --git a/migration/trace-events b/migration/trace-events index 61ee21a13e..06aef4d4a3 100644 --- a/migration/trace-events +++ b/migration/trace-events @@ -77,7 +77,9 @@ ram_load_postcopy_loop(uint64_t addr, int flags) "@%" PRI= x64 " %x" ram_postcopy_send_discard_bitmap(void) "" ram_save_page(const char *rbname, uint64_t offset, void *host) "%s: offset= : 0x%" PRIx64 " host: %p" ram_save_queue_pages(const char *rbname, size_t start, size_t len) "%s: st= art: 0x%zx len: 0x%zx" +multifd_recv(char id, int seq, int num) "channel %d sequence %d num pages = %d" multifd_send(char id, int seq, int num) "channel %d sequence %d num pages = %d" +multifd_recv_thread(char id, uint32_t packets) "channel %d packets %d" multifd_send_thread(char id, uint32_t packets) "channel %d packets %d" =20 # migration/migration.c --=20 2.14.3