From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55497) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xnifm-0006XJ-RG for qemu-devel@nongnu.org; Mon, 10 Nov 2014 01:42:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Xnifc-0004PA-9Y for qemu-devel@nongnu.org; Mon, 10 Nov 2014 01:41:58 -0500 Received: from ozlabs.org ([2401:3900:2:1::2]:54658) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xnifb-0004Nx-NF for qemu-devel@nongnu.org; Mon, 10 Nov 2014 01:41:48 -0500 Date: Mon, 10 Nov 2014 17:05:30 +1100 From: David Gibson Message-ID: <20141110060530.GA31324@voom.redhat.com> References: <1412358473-31398-1-git-send-email-dgilbert@redhat.com> <1412358473-31398-34-git-send-email-dgilbert@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="AqsLC8rIMeq19msA" Content-Disposition: inline In-Reply-To: <1412358473-31398-34-git-send-email-dgilbert@redhat.com> Subject: Re: [Qemu-devel] [PATCH v4 33/47] Postcopy: Postcopy startup in migration thread List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Dr. David Alan Gilbert (git)" Cc: aarcange@redhat.com, yamahata@private.email.ne.jp, lilei@linux.vnet.ibm.com, quintela@redhat.com, cristian.klein@cs.umu.se, qemu-devel@nongnu.org, amit.shah@redhat.com, yanghy@cn.fujitsu.com --AqsLC8rIMeq19msA Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Oct 03, 2014 at 06:47:39PM +0100, Dr. David Alan Gilbert (git) wrot= e: > From: "Dr. David Alan Gilbert" >=20 > Rework the migration thread to setup and start postcopy. >=20 > Signed-off-by: Dr. David Alan Gilbert > --- > include/migration/migration.h | 3 + > migration.c | 201 ++++++++++++++++++++++++++++++++++++= ++---- > 2 files changed, 185 insertions(+), 19 deletions(-) >=20 > diff --git a/include/migration/migration.h b/include/migration/migration.h > index b01cc17..f401775 100644 > --- a/include/migration/migration.h > +++ b/include/migration/migration.h > @@ -125,6 +125,9 @@ struct MigrationState > /* Flag set once the migration has been asked to enter postcopy */ > volatile bool start_postcopy; > =20 > + /* Flag set once the migration thread is running (and needs joining)= */ > + volatile bool started_migration_thread; > + > /* bitmap of pages that have been sent at least once > * only maintained and used in postcopy at the moment > * where it's used to send the dirtymap at the start > diff --git a/migration.c b/migration.c > index 63d70b6..1731017 100644 > --- a/migration.c > +++ b/migration.c > @@ -475,7 +475,10 @@ static void migrate_fd_cleanup(void *opaque) > if (s->file) { > trace_migrate_fd_cleanup(); > qemu_mutex_unlock_iothread(); > - qemu_thread_join(&s->thread); > + if (s->started_migration_thread) { > + qemu_thread_join(&s->thread); > + s->started_migration_thread =3D false; > + } > qemu_mutex_lock_iothread(); > =20 > qemu_fclose(s->file); > @@ -872,7 +875,6 @@ out: > return NULL; > } > =20 > -__attribute__ (( unused )) /* Until later in patch series */ > static int open_outgoing_return_path(MigrationState *ms) > { > =20 > @@ -890,7 +892,6 @@ static int open_outgoing_return_path(MigrationState *= ms) > return 0; > } > =20 > -__attribute__ (( unused )) /* Until later in patch series */ > static void await_outgoing_return_path_close(MigrationState *ms) > { > /* > @@ -908,6 +909,97 @@ static void await_outgoing_return_path_close(Migrati= onState *ms) > DPRINTF("%s: Exit", __func__); > } > =20 > +/* Switch from normal iteration to postcopy > + * Returns non-0 on error > + */ > +static int postcopy_start(MigrationState *ms) > +{ > + int ret; > + const QEMUSizedBuffer *qsb; > + migrate_set_state(ms, MIG_STATE_ACTIVE, MIG_STATE_POSTCOPY_ACTIVE); > + > + DPRINTF("postcopy_start\n"); > + qemu_mutex_lock_iothread(); > + DPRINTF("postcopy_start: setting run state\n"); > + ret =3D vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); > + > + if (ret < 0) { > + migrate_set_state(ms, MIG_STATE_POSTCOPY_ACTIVE, MIG_STATE_ERROR= ); > + qemu_mutex_unlock_iothread(); > + return -1; > + } > + > + /* > + * in Finish migrate and with the io-lock held everything should > + * be quiet, but we've potentially still got dirty pages and we > + * need to tell the destination to throw any pages it's already rece= ived > + * that are dirty > + */ > + if (ram_postcopy_send_discard_bitmap(ms)) { > + DPRINTF("postcopy send discard bitmap failed\n"); > + migrate_set_state(ms, MIG_STATE_POSTCOPY_ACTIVE, MIG_STATE_ERROR= ); > + qemu_mutex_unlock_iothread(); > + return -1; > + } > + > + DPRINTF("postcopy_start: sending req 2\n"); > + qemu_savevm_send_reqack(ms->file, 2); Are these reqacks just for debugging, or do they affect the protocol? > + /* > + * send rest of state - note things that are doing postcopy > + * will notice we're in MIG_STATE_POSTCOPY_ACTIVE and not actually > + * wrap their state up here > + */ > + qemu_file_set_rate_limit(ms->file, INT64_MAX); > + DPRINTF("postcopy_start: do state_complete\n"); > + > + /* > + * We need to leave the fd free for page transfers during the > + * loading of the device state, so wrap all the remaining > + * commands and state into a package that gets sent in one go > + */ > + QEMUFile *fb =3D qemu_bufopen("w", NULL); > + if (!fb) { > + error_report("Failed to create buffered file"); > + migrate_set_state(ms, MIG_STATE_POSTCOPY_ACTIVE, MIG_STATE_ERROR= ); > + qemu_mutex_unlock_iothread(); > + return -1; > + } > + > + qemu_savevm_state_complete(fb); > + DPRINTF("postcopy_start: sending req 3\n"); > + qemu_savevm_send_reqack(fb, 3); > + > + qemu_savevm_send_postcopy_ram_run(fb); > + > + /* <><> end of stuff going into the package */ > + qsb =3D qemu_buf_get(fb); > + > + /* Now send that blob */ > + if (qsb_get_length(qsb) > MAX_VM_CMD_PACKAGED_SIZE) { > + DPRINTF("postcopy_start: Unreasonably large packaged state: %lu\= n", > + (unsigned long)(qsb_get_length(qsb))); > + migrate_set_state(ms, MIG_STATE_POSTCOPY_ACTIVE, MIG_STATE_ERROR= ); > + qemu_mutex_unlock_iothread(); > + qemu_fclose(fb); > + return -1; > + } > + qemu_savevm_send_packaged(ms->file, qsb); > + qemu_fclose(fb); > + > + qemu_mutex_unlock_iothread(); > + > + DPRINTF("postcopy_start not finished sending ack\n"); > + qemu_savevm_send_reqack(ms->file, 4); > + > + ret =3D qemu_file_get_error(ms->file); > + if (ret) { > + error_report("postcopy_start: Migration stream errored"); > + migrate_set_state(ms, MIG_STATE_POSTCOPY_ACTIVE, MIG_STATE_ERROR= ); > + } > + > + return ret; > +} > + > /* > * Master migration thread on the source VM. > * It drives the migration and pumps the data down the outgoing channel. > @@ -915,16 +1007,36 @@ static void await_outgoing_return_path_close(Migra= tionState *ms) > static void *migration_thread(void *opaque) > { > MigrationState *s =3D opaque; > + /* Used by the bandwidth calcs, updated later */ > int64_t initial_time =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME); > int64_t setup_start =3D qemu_clock_get_ms(QEMU_CLOCK_HOST); > int64_t initial_bytes =3D 0; > int64_t max_size =3D 0; > int64_t start_time =3D initial_time; > + > bool old_vm_running =3D false; > =20 > + /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */ > + enum MigrationPhase current_active_type =3D MIG_STATE_ACTIVE; > + > qemu_savevm_state_begin(s->file, &s->params); > =20 > + if (migrate_postcopy_ram()) { > + /* Now tell the dest that it should open it's end so it can repl= y */ s/it's/its/ > + qemu_savevm_send_openrp(s->file); > + > + /* And ask it to send an ack that will make stuff easier to debu= g */ > + qemu_savevm_send_reqack(s->file, 1); > + > + /* Tell the destination that we *might* want to do postcopy late= r; > + * if the other end can't do postcopy it should fail now, nice a= nd > + * early. > + */ > + qemu_savevm_send_postcopy_ram_advise(s->file); > + } > + > s->setup_time =3D qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; > + current_active_type =3D MIG_STATE_ACTIVE; > migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ACTIVE); > =20 > DPRINTF("setup complete\n"); > @@ -945,37 +1057,74 @@ static void *migration_thread(void *opaque) > " nonpost=3D%" PRIu64 ")\n", > pending_size, max_size, pend_post, pend_nonpost); > if (pending_size && pending_size >=3D max_size) { > + /* Still a significant amount to transfer */ > + > + current_time =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME); > + if (migrate_postcopy_ram() && > + s->state !=3D MIG_STATE_POSTCOPY_ACTIVE && > + pend_nonpost =3D=3D 0 && s->start_postcopy) { Hrm. This is checking for pend_nonpost =3D=3D 0, rather than just close to zero. IIUC this will only work if all "live sendable" state is also postcopyable. But if we have live sendable data that's not postcopyable - like the power hash page table - we'll need some threshold here, like we currently have for entering the stopped vm phase of a precopy migration. Or am I missing something? > + > + if (!postcopy_start(s)) { > + current_active_type =3D MIG_STATE_POSTCOPY_ACTIV= E; > + } > + > + continue; > + } > + /* Just another iteration step */ > qemu_savevm_state_iterate(s->file); > } else { > int ret; > =20 > - DPRINTF("done iterating\n"); > - qemu_mutex_lock_iothread(); > - start_time =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME); > - qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); > - old_vm_running =3D runstate_is_running(); > + DPRINTF("done iterating pending size %" PRIu64 "\n", > + pending_size); > + > + if (s->state =3D=3D MIG_STATE_ACTIVE) { > + qemu_mutex_lock_iothread(); > + start_time =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME= ); > + qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); > + old_vm_running =3D runstate_is_running(); > + > + ret =3D vm_stop_force_state(RUN_STATE_FINISH_MIGRATE= ); > + if (ret >=3D 0) { > + qemu_file_set_rate_limit(s->file, INT64_MAX); > + qemu_savevm_state_complete(s->file); > + } > + qemu_mutex_unlock_iothread(); > + > + if (ret < 0) { > + migrate_set_state(s, current_active_type, > + MIG_STATE_ERROR); > + break; > + } > + } else if (s->state =3D=3D MIG_STATE_POSTCOPY_ACTIVE) { > + DPRINTF("postcopy end\n"); > + > + qemu_savevm_state_postcopy_complete(s->file); > + DPRINTF("postcopy end after complete\n"); > =20 > - ret =3D vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); > - if (ret >=3D 0) { > - qemu_file_set_rate_limit(s->file, INT64_MAX); > - qemu_savevm_state_complete(s->file); > } > - qemu_mutex_unlock_iothread(); > =20 > - if (ret < 0) { > - migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERR= OR); > - break; > + /* > + * If rp was opened we must clean up the thread before > + * cleaning everything else up. > + * Postcopy opens rp if enabled (even if it's not avtiva= ted) > + */ > + if (migrate_postcopy_ram()) { > + DPRINTF("before rp close"); > + await_outgoing_return_path_close(s); > + DPRINTF("after rp close"); > } > - > if (!qemu_file_get_error(s->file)) { > - migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_COM= PLETED); > + migrate_set_state(s, current_active_type, > + MIG_STATE_COMPLETED); > break; > } > } > } > =20 > if (qemu_file_get_error(s->file)) { > - migrate_set_state(s, MIG_STATE_ACTIVE, MIG_STATE_ERROR); > + migrate_set_state(s, current_active_type, MIG_STATE_ERROR); > + DPRINTF("migration_thread: file is in error state\n"); > break; > } > current_time =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME); > @@ -1006,6 +1155,7 @@ static void *migration_thread(void *opaque) > } > } > =20 > + DPRINTF("migration_thread: After loop"); > qemu_mutex_lock_iothread(); > if (s->state =3D=3D MIG_STATE_COMPLETED) { > int64_t end_time =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME); > @@ -1043,6 +1193,19 @@ void migrate_fd_connect(MigrationState *s) > /* Notify before starting migration thread */ > notifier_list_notify(&migration_state_notifiers, s); > =20 > + /* Open the return path; currently for postcopy but other things mig= ht > + * also want it. > + */ > + if (migrate_postcopy_ram()) { > + if (open_outgoing_return_path(s)) { > + error_report("Unable to open return-path for postcopy"); > + migrate_set_state(s, MIG_STATE_SETUP, MIG_STATE_ERROR); > + migrate_fd_cleanup(s); > + return; > + } > + } > + > qemu_thread_create(&s->thread, "migration", migration_thread, s, > QEMU_THREAD_JOINABLE); > + s->started_migration_thread =3D true; > } --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --AqsLC8rIMeq19msA Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBAgAGBQJUYFWqAAoJEGw4ysog2bOSHYcP/1FMQh4LJVZksAHh10iqZAbO LLMrijDEnRD+1NmU1MtKciI0VqH42A0PY+FMG00C4Ply24DmQeuVgsR+iONWa7tx a0Pua2xUhja3PDWIucknpM7ijwD6xEEPmbs413E6hoBZcOfoChIv94ZGA5MT35mb Q21xDsIsk3EqsoVe8Zd5NbgUeFVCl0Buml6S1Ttx2KGPaN1GyihcmXUTlDb1F+mr akjDuk24BPry0shrRLP4Oeut4fWSWtRdo6WJn69fgHrGd5XGIEl9BRf+CXMoulM0 2jYmtWm6VcWp5uvvy8Gs4p1+9ojhZ787VlScx/xVcYiUzj0Z7v2j83HXxZHmF4vR y/eR/ClCymXw9d2Mz4Bh1d6HK9kudYL9sRCm4IzucSsKsb0e0mIKGZe8WzU19cvs H1xewgGewWQ4L7iPMmB6sUTsSc5Ada8G26uo6TwH/uKtNKBNTz0f91q2a8st4psc AwrWFwlCylgJf41PI3butNrfhh24fdS6Z5H5vuijLRFVqZwPn06LHdzkbKiZTGcU gKfqF3NSHUE4GevofZXhODvLmn7nuIpttKqdVLDpG13ofSRfZX0rAgE5TTBy/1Xi NfKJLOv5cI5ehnsgb/gar/geQO4vh3LrBSdRx/LeAkXCVzRrTVD5mXMHjh5LpCTg M/hdicm8MDYu37Tbz1tQ =6of2 -----END PGP SIGNATURE----- --AqsLC8rIMeq19msA--