From nobody Sun Oct 26 03:53:42 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 1514958461797950.4012734195478; Tue, 2 Jan 2018 21:47:41 -0800 (PST) Received: from localhost ([::1]:40654 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eWbts-0000Jy-T3 for importer@patchew.org; Wed, 03 Jan 2018 00:47:40 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48072) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eWbnf-0003FE-8y for qemu-devel@nongnu.org; Wed, 03 Jan 2018 00:41:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eWbne-00018Y-40 for qemu-devel@nongnu.org; Wed, 03 Jan 2018 00:41:15 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55924) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eWbnd-00017t-Qv for qemu-devel@nongnu.org; Wed, 03 Jan 2018 00:41:14 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 03A475F7AC for ; Wed, 3 Jan 2018 05:41:13 +0000 (UTC) Received: from xz-mi.nay.redhat.com (dhcp-14-131.nay.redhat.com [10.66.14.131]) by smtp.corp.redhat.com (Postfix) with ESMTP id B54716199F; Wed, 3 Jan 2018 05:41:10 +0000 (UTC) From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 3 Jan 2018 13:40:41 +0800 Message-Id: <20180103054043.25719-10-peterx@redhat.com> In-Reply-To: <20180103054043.25719-1-peterx@redhat.com> References: <20180103054043.25719-1-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 03 Jan 2018 05:41:13 +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 09/11] migration: cleanup stats update into function 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: Laurent Vivier , "Dr . David Alan Gilbert" , peterx@redhat.com, Juan Quintela 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 have quite a few lines in migration_thread() that calculates some statistics for the migration interations. Isolate it into a single function to improve readability. Signed-off-by: Peter Xu --- migration/migration.c | 82 +++++++++++++++++++++++++++++------------------= ---- migration/migration.h | 13 ++++++++ 2 files changed, 59 insertions(+), 36 deletions(-) diff --git a/migration/migration.c b/migration/migration.c index bfcba24caa..2629f907e9 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -1273,6 +1273,8 @@ MigrationState *migrate_init(void) s->mig_start_time =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME); s->mig_total_time =3D 0; s->old_vm_running =3D false; + s->initial_bytes =3D 0; + s->threshold_size =3D 0; return s; } =20 @@ -2164,6 +2166,39 @@ static void migration_calculate_complete(MigrationSt= ate *s) } } =20 +static void migration_update_statistics(MigrationState *s, + int64_t current_time) +{ + uint64_t transferred =3D qemu_ftell(s->to_dst_file) - s->initial_bytes; + uint64_t time_spent =3D current_time - s->initial_time; + double bandwidth =3D (double)transferred / time_spent; + int64_t threshold_size =3D bandwidth * s->parameters.downtime_limit; + + if (current_time < s->initial_time + BUFFER_DELAY) { + return; + } + + s->mbps =3D (((double) transferred * 8.0) / + ((double) time_spent / 1000.0)) / 1000.0 / 1000.0; + + /* + * if we haven't sent anything, we don't want to + * recalculate. 10000 is a small enough number for our purposes + */ + if (ram_counters.dirty_pages_rate && transferred > 10000) { + s->expected_downtime =3D ram_counters.dirty_pages_rate * + qemu_target_page_size() / bandwidth; + } + + qemu_file_reset_rate_limit(s->to_dst_file); + + s->initial_time =3D current_time; + s->initial_bytes =3D qemu_ftell(s->to_dst_file); + + trace_migrate_transferred(transferred, time_spent, + bandwidth, threshold_size); +} + /* * Master migration thread on the source VM. * It drives the migration and pumps the data down the outgoing channel. @@ -2171,22 +2206,15 @@ static void migration_calculate_complete(MigrationS= tate *s) 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; - /* - * The final stage happens when the remaining data is smaller than - * this threshold; it's calculated from the requested downtime and - * measured bandwidth - */ - int64_t threshold_size =3D 0; bool entered_postcopy =3D false; /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */ enum MigrationStatus current_active_state =3D MIGRATION_STATUS_ACTIVE; =20 rcu_register_thread(); =20 + s->initial_time =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME); + qemu_savevm_state_header(s->to_dst_file); =20 /* @@ -2226,17 +2254,17 @@ static void *migration_thread(void *opaque) if (!qemu_file_rate_limit(s->to_dst_file)) { uint64_t pend_post, pend_nonpost; =20 - qemu_savevm_state_pending(s->to_dst_file, threshold_size, + qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_nonpost, &pend_post); pending_size =3D pend_nonpost + pend_post; - trace_migrate_pending(pending_size, threshold_size, + trace_migrate_pending(pending_size, s->threshold_size, pend_post, pend_nonpost); - if (pending_size && pending_size >=3D threshold_size) { + if (pending_size && pending_size >=3D s->threshold_size) { /* Still a significant amount to transfer */ =20 if (migrate_postcopy() && s->state !=3D MIGRATION_STATUS_POSTCOPY_ACTIVE && - pend_nonpost <=3D threshold_size && + pend_nonpost <=3D s->threshold_size && atomic_read(&s->start_postcopy)) { =20 if (!postcopy_start(s)) { @@ -2261,33 +2289,15 @@ static void *migration_thread(void *opaque) trace_migration_thread_file_err(); break; } + current_time =3D qemu_clock_get_ms(QEMU_CLOCK_REALTIME); - if (current_time >=3D initial_time + BUFFER_DELAY) { - uint64_t transferred_bytes =3D qemu_ftell(s->to_dst_file) - - initial_bytes; - uint64_t time_spent =3D current_time - initial_time; - double bandwidth =3D (double)transferred_bytes / time_spent; - threshold_size =3D bandwidth * s->parameters.downtime_limit; - - s->mbps =3D (((double) transferred_bytes * 8.0) / - ((double) time_spent / 1000.0)) / 1000.0 / 1000.0; - - trace_migrate_transferred(transferred_bytes, time_spent, - bandwidth, threshold_size); - /* if we haven't sent anything, we don't want to recalculate - 10000 is a small enough number for our purposes */ - if (ram_counters.dirty_pages_rate && transferred_bytes > 10000= ) { - s->expected_downtime =3D ram_counters.dirty_pages_rate * - qemu_target_page_size() / bandwidth; - } =20 - qemu_file_reset_rate_limit(s->to_dst_file); - initial_time =3D current_time; - initial_bytes =3D qemu_ftell(s->to_dst_file); - } + /* Conditionally update statistics */ + migration_update_statistics(s, current_time); + if (qemu_file_rate_limit(s->to_dst_file)) { /* usleep expects microseconds */ - g_usleep((initial_time + BUFFER_DELAY - current_time)*1000); + g_usleep((s->initial_time + BUFFER_DELAY - current_time) * 100= 0); } } =20 diff --git a/migration/migration.h b/migration/migration.h index 3ab5506233..248f7d9a5c 100644 --- a/migration/migration.h +++ b/migration/migration.h @@ -90,6 +90,19 @@ struct MigrationState QEMUBH *cleanup_bh; QEMUFile *to_dst_file; =20 + /* + * Migration thread statistic variables, mostly used in + * migration_thread() iterations only. + */ + uint64_t initial_bytes; + int64_t initial_time; + /* + * The final stage happens when the remaining data is smaller than + * this threshold; it's calculated from the requested downtime and + * measured bandwidth + */ + int64_t threshold_size; + /* params from 'migrate-set-parameters' */ MigrationParameters parameters; =20 --=20 2.14.3