From nobody Mon Feb 9 08:10:19 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1623658695594919.6707631925909; Mon, 14 Jun 2021 01:18:15 -0700 (PDT) Received: from localhost ([::1]:49342 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lshnS-0003y7-GK for importer@patchew.org; Mon, 14 Jun 2021 04:18:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40892) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lshhL-00023O-RX for qemu-devel@nongnu.org; Mon, 14 Jun 2021 04:11:55 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:43635) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lshhI-0000V5-KJ for qemu-devel@nongnu.org; Mon, 14 Jun 2021 04:11:55 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-566-kdNnAyyrMrubVbfrg5m9RQ-1; Mon, 14 Jun 2021 04:11:48 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 35473A0CD9; Mon, 14 Jun 2021 08:11:47 +0000 (UTC) Received: from localhost.localdomain (ovpn-113-49.ams2.redhat.com [10.36.113.49]) by smtp.corp.redhat.com (Postfix) with ESMTP id E360C5D6A8; Mon, 14 Jun 2021 08:11:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623658311; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=P5h8obBUwkbowRvrbRyioEvWf7sFSHOHr5u+oUJjy+A=; b=APRrmQKlj0OkdqXWSWMZFKwQb7/T7Rd6APYaw+hq7pAAaHGJ5jdcaCl+Dc1Se8tK71xdYC ndfnts4eut81zUjLpmQp2o2t7rmaZrV9vBRb/qq4AwX41fEm3PAmt69rVcPPslP7XOyOcS Nb6gQ2EIK39ota58QsvspJAd7UusE4I= X-MC-Unique: kdNnAyyrMrubVbfrg5m9RQ-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v3 4/5] progressmeter: protect with a mutex Date: Mon, 14 Jun 2021 10:11:29 +0200 Message-Id: <20210614081130.22134-5-eesposit@redhat.com> In-Reply-To: <20210614081130.22134-1-eesposit@redhat.com> References: <20210614081130.22134-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eesposit@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.199, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Emanuele Giuseppe Esposito , Vladimir Sementsov-Ogievskiy , qemu-devel@nongnu.org, Max Reitz , Stefan Hajnoczi , Paolo Bonzini , John Snow Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Progressmeter is protected by the AioContext mutex, which is taken by the block jobs and their caller (like blockdev). We would like to remove the dependency of block layer code on the AioContext mutex, since most drivers and the core I/O code are already not relying on it. Create a new C file to implement the ProgressMeter API, but keep the struct as public, to avoid forcing allocation on the heap. Also add a mutex to be able to provide an accurate snapshot of the progress values to the caller. Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Stefan Hajnoczi Reviewed-by: Vladimir Sementsov-Ogievskiy --- block/meson.build | 1 + block/progress_meter.c | 64 +++++++++++++++++++++++++++++++++++ blockjob.c | 33 +++++++++++++----- include/qemu/progress_meter.h | 34 +++++++++++-------- job-qmp.c | 8 +++-- job.c | 3 ++ qemu-img.c | 9 +++-- 7 files changed, 124 insertions(+), 28 deletions(-) create mode 100644 block/progress_meter.c diff --git a/block/meson.build b/block/meson.build index 01861e1545..ef1ba3d973 100644 --- a/block/meson.build +++ b/block/meson.build @@ -13,6 +13,7 @@ block_ss.add(files( 'commit.c', 'copy-on-read.c', 'preallocate.c', + 'progress_meter.c', 'create.c', 'crypto.c', 'dirty-bitmap.c', diff --git a/block/progress_meter.c b/block/progress_meter.c new file mode 100644 index 0000000000..aa2e60248c --- /dev/null +++ b/block/progress_meter.c @@ -0,0 +1,64 @@ +/* + * Helper functionality for some process progress tracking. + * + * Copyright (c) 2011 IBM Corp. + * Copyright (c) 2012, 2018 Red Hat, Inc. + * Copyright (c) 2020 Virtuozzo International GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ +#include "qemu/osdep.h" +#include "qemu/progress_meter.h" + +void progress_init(ProgressMeter *pm) +{ + qemu_mutex_init(&pm->lock); +} + +void progress_destroy(ProgressMeter *pm) +{ + qemu_mutex_destroy(&pm->lock); +} + +void progress_get_snapshot(ProgressMeter *pm, uint64_t *current, + uint64_t *total) +{ + QEMU_LOCK_GUARD(&pm->lock); + + *current =3D pm->current; + *total =3D pm->total; +} + +void progress_work_done(ProgressMeter *pm, uint64_t done) +{ + QEMU_LOCK_GUARD(&pm->lock); + pm->current +=3D done; +} + +void progress_set_remaining(ProgressMeter *pm, uint64_t remaining) +{ + QEMU_LOCK_GUARD(&pm->lock); + pm->total =3D pm->current + remaining; +} + +void progress_increase_remaining(ProgressMeter *pm, uint64_t delta) +{ + QEMU_LOCK_GUARD(&pm->lock); + pm->total +=3D delta; +} diff --git a/blockjob.c b/blockjob.c index 22e5bb9b1f..4bad1408cb 100644 --- a/blockjob.c +++ b/blockjob.c @@ -306,18 +306,23 @@ int64_t block_job_ratelimit_get_delay(BlockJob *job, = uint64_t n) BlockJobInfo *block_job_query(BlockJob *job, Error **errp) { BlockJobInfo *info; + uint64_t progress_current, progress_total; =20 if (block_job_is_internal(job)) { error_setg(errp, "Cannot query QEMU internal jobs"); return NULL; } + + progress_get_snapshot(&job->job.progress, &progress_current, + &progress_total); + info =3D g_new0(BlockJobInfo, 1); info->type =3D g_strdup(job_type_str(&job->job)); info->device =3D g_strdup(job->job.id); info->busy =3D qatomic_read(&job->job.busy); info->paused =3D job->job.pause_count > 0; - info->offset =3D job->job.progress.current; - info->len =3D job->job.progress.total; + info->offset =3D progress_current; + info->len =3D progress_total; info->speed =3D job->speed; info->io_status =3D job->iostatus; info->ready =3D job_is_ready(&job->job), @@ -344,15 +349,19 @@ static void block_job_iostatus_set_err(BlockJob *job,= int error) static void block_job_event_cancelled(Notifier *n, void *opaque) { BlockJob *job =3D opaque; + uint64_t progress_current, progress_total; =20 if (block_job_is_internal(job)) { return; } =20 + progress_get_snapshot(&job->job.progress, &progress_current, + &progress_total); + qapi_event_send_block_job_cancelled(job_type(&job->job), job->job.id, - job->job.progress.total, - job->job.progress.current, + progress_total, + progress_current, job->speed); } =20 @@ -360,6 +369,7 @@ static void block_job_event_completed(Notifier *n, void= *opaque) { BlockJob *job =3D opaque; const char *msg =3D NULL; + uint64_t progress_current, progress_total; =20 if (block_job_is_internal(job)) { return; @@ -369,10 +379,13 @@ static void block_job_event_completed(Notifier *n, vo= id *opaque) msg =3D error_get_pretty(job->job.err); } =20 + progress_get_snapshot(&job->job.progress, &progress_current, + &progress_total); + qapi_event_send_block_job_completed(job_type(&job->job), job->job.id, - job->job.progress.total, - job->job.progress.current, + progress_total, + progress_current, job->speed, !!msg, msg); @@ -393,15 +406,19 @@ static void block_job_event_pending(Notifier *n, void= *opaque) static void block_job_event_ready(Notifier *n, void *opaque) { BlockJob *job =3D opaque; + uint64_t progress_current, progress_total; =20 if (block_job_is_internal(job)) { return; } =20 + progress_get_snapshot(&job->job.progress, &progress_current, + &progress_total); + qapi_event_send_block_job_ready(job_type(&job->job), job->job.id, - job->job.progress.total, - job->job.progress.current, + progress_total, + progress_current, job->speed); } =20 diff --git a/include/qemu/progress_meter.h b/include/qemu/progress_meter.h index 9a23ff071c..dadf822bbf 100644 --- a/include/qemu/progress_meter.h +++ b/include/qemu/progress_meter.h @@ -27,6 +27,8 @@ #ifndef QEMU_PROGRESS_METER_H #define QEMU_PROGRESS_METER_H =20 +#include "qemu/lockable.h" + typedef struct ProgressMeter { /** * Current progress. The unit is arbitrary as long as the ratio between @@ -37,22 +39,24 @@ typedef struct ProgressMeter { =20 /** Estimated current value at the completion of the process */ uint64_t total; + + QemuMutex lock; /* protects concurrent access to above fields */ } ProgressMeter; =20 -static inline void progress_work_done(ProgressMeter *pm, uint64_t done) -{ - pm->current +=3D done; -} - -static inline void progress_set_remaining(ProgressMeter *pm, uint64_t rema= ining) -{ - pm->total =3D pm->current + remaining; -} - -static inline void progress_increase_remaining(ProgressMeter *pm, - uint64_t delta) -{ - pm->total +=3D delta; -} +void progress_init(ProgressMeter *pm); +void progress_destroy(ProgressMeter *pm); + +/* Get a snapshot of internal current and total values */ +void progress_get_snapshot(ProgressMeter *pm, uint64_t *current, + uint64_t *total); + +/* Increases the amount of work done so far by @done */ +void progress_work_done(ProgressMeter *pm, uint64_t done); + +/* Sets how much work has to be done to complete to @remaining */ +void progress_set_remaining(ProgressMeter *pm, uint64_t remaining); + +/* Increases the total work to do by @delta */ +void progress_increase_remaining(ProgressMeter *pm, uint64_t delta); =20 #endif /* QEMU_PROGRESS_METER_H */ diff --git a/job-qmp.c b/job-qmp.c index 34c4da094f..829a28aa70 100644 --- a/job-qmp.c +++ b/job-qmp.c @@ -144,16 +144,20 @@ void qmp_job_dismiss(const char *id, Error **errp) static JobInfo *job_query_single(Job *job, Error **errp) { JobInfo *info; + uint64_t progress_current; + uint64_t progress_total; =20 assert(!job_is_internal(job)); + progress_get_snapshot(&job->progress, &progress_current, + &progress_total); =20 info =3D g_new(JobInfo, 1); *info =3D (JobInfo) { .id =3D g_strdup(job->id), .type =3D job_type(job), .status =3D job->status, - .current_progress =3D job->progress.current, - .total_progress =3D job->progress.total, + .current_progress =3D progress_current, + .total_progress =3D progress_total, .has_error =3D !!job->err, .error =3D job->err ? \ g_strdup(error_get_pretty(job->err)) : NULL, diff --git a/job.c b/job.c index 8775c1803b..e7a5d28854 100644 --- a/job.c +++ b/job.c @@ -339,6 +339,8 @@ void *job_create(const char *job_id, const JobDriver *d= river, JobTxn *txn, job->cb =3D cb; job->opaque =3D opaque; =20 + progress_init(&job->progress); + notifier_list_init(&job->on_finalize_cancelled); notifier_list_init(&job->on_finalize_completed); notifier_list_init(&job->on_pending); @@ -382,6 +384,7 @@ void job_unref(Job *job) =20 QLIST_REMOVE(job, job_list); =20 + progress_destroy(&job->progress); error_free(job->err); g_free(job->id); g_free(job); diff --git a/qemu-img.c b/qemu-img.c index a5993682aa..7956a89965 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -900,6 +900,7 @@ static void common_block_job_cb(void *opaque, int ret) =20 static void run_block_job(BlockJob *job, Error **errp) { + uint64_t progress_current, progress_total; AioContext *aio_context =3D blk_get_aio_context(job->blk); int ret =3D 0; =20 @@ -908,9 +909,11 @@ static void run_block_job(BlockJob *job, Error **errp) do { float progress =3D 0.0f; aio_poll(aio_context, true); - if (job->job.progress.total) { - progress =3D (float)job->job.progress.current / - job->job.progress.total * 100.f; + + progress_get_snapshot(&job->job.progress, &progress_current, + &progress_total); + if (progress_total) { + progress =3D (float)progress_current / progress_total * 100.f; } qemu_progress_print(progress, 0); } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); --=20 2.31.1