Skip to content

Commit a7b4f8f

Browse files
esposemVladimir Sementsov-Ogievskiy
authored and
Vladimir Sementsov-Ogievskiy
committed
progressmeter: protect with a mutex
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 <[email protected]> Reviewed-by: Stefan Hajnoczi <[email protected]> Message-Id: <[email protected]> Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]>
1 parent c02b83e commit a7b4f8f

File tree

7 files changed

+124
-28
lines changed

7 files changed

+124
-28
lines changed

block/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ block_ss.add(files(
1313
'commit.c',
1414
'copy-on-read.c',
1515
'preallocate.c',
16+
'progress_meter.c',
1617
'create.c',
1718
'crypto.c',
1819
'dirty-bitmap.c',

block/progress_meter.c

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Helper functionality for some process progress tracking.
3+
*
4+
* Copyright (c) 2011 IBM Corp.
5+
* Copyright (c) 2012, 2018 Red Hat, Inc.
6+
* Copyright (c) 2020 Virtuozzo International GmbH
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#include "qemu/osdep.h"
27+
#include "qemu/progress_meter.h"
28+
29+
void progress_init(ProgressMeter *pm)
30+
{
31+
qemu_mutex_init(&pm->lock);
32+
}
33+
34+
void progress_destroy(ProgressMeter *pm)
35+
{
36+
qemu_mutex_destroy(&pm->lock);
37+
}
38+
39+
void progress_get_snapshot(ProgressMeter *pm, uint64_t *current,
40+
uint64_t *total)
41+
{
42+
QEMU_LOCK_GUARD(&pm->lock);
43+
44+
*current = pm->current;
45+
*total = pm->total;
46+
}
47+
48+
void progress_work_done(ProgressMeter *pm, uint64_t done)
49+
{
50+
QEMU_LOCK_GUARD(&pm->lock);
51+
pm->current += done;
52+
}
53+
54+
void progress_set_remaining(ProgressMeter *pm, uint64_t remaining)
55+
{
56+
QEMU_LOCK_GUARD(&pm->lock);
57+
pm->total = pm->current + remaining;
58+
}
59+
60+
void progress_increase_remaining(ProgressMeter *pm, uint64_t delta)
61+
{
62+
QEMU_LOCK_GUARD(&pm->lock);
63+
pm->total += delta;
64+
}

blockjob.c

+25-8
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,23 @@ int64_t block_job_ratelimit_get_delay(BlockJob *job, uint64_t n)
306306
BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
307307
{
308308
BlockJobInfo *info;
309+
uint64_t progress_current, progress_total;
309310

310311
if (block_job_is_internal(job)) {
311312
error_setg(errp, "Cannot query QEMU internal jobs");
312313
return NULL;
313314
}
315+
316+
progress_get_snapshot(&job->job.progress, &progress_current,
317+
&progress_total);
318+
314319
info = g_new0(BlockJobInfo, 1);
315320
info->type = g_strdup(job_type_str(&job->job));
316321
info->device = g_strdup(job->job.id);
317322
info->busy = qatomic_read(&job->job.busy);
318323
info->paused = job->job.pause_count > 0;
319-
info->offset = job->job.progress.current;
320-
info->len = job->job.progress.total;
324+
info->offset = progress_current;
325+
info->len = progress_total;
321326
info->speed = job->speed;
322327
info->io_status = job->iostatus;
323328
info->ready = job_is_ready(&job->job),
@@ -344,22 +349,27 @@ static void block_job_iostatus_set_err(BlockJob *job, int error)
344349
static void block_job_event_cancelled(Notifier *n, void *opaque)
345350
{
346351
BlockJob *job = opaque;
352+
uint64_t progress_current, progress_total;
347353

348354
if (block_job_is_internal(job)) {
349355
return;
350356
}
351357

358+
progress_get_snapshot(&job->job.progress, &progress_current,
359+
&progress_total);
360+
352361
qapi_event_send_block_job_cancelled(job_type(&job->job),
353362
job->job.id,
354-
job->job.progress.total,
355-
job->job.progress.current,
363+
progress_total,
364+
progress_current,
356365
job->speed);
357366
}
358367

359368
static void block_job_event_completed(Notifier *n, void *opaque)
360369
{
361370
BlockJob *job = opaque;
362371
const char *msg = NULL;
372+
uint64_t progress_current, progress_total;
363373

364374
if (block_job_is_internal(job)) {
365375
return;
@@ -369,10 +379,13 @@ static void block_job_event_completed(Notifier *n, void *opaque)
369379
msg = error_get_pretty(job->job.err);
370380
}
371381

382+
progress_get_snapshot(&job->job.progress, &progress_current,
383+
&progress_total);
384+
372385
qapi_event_send_block_job_completed(job_type(&job->job),
373386
job->job.id,
374-
job->job.progress.total,
375-
job->job.progress.current,
387+
progress_total,
388+
progress_current,
376389
job->speed,
377390
!!msg,
378391
msg);
@@ -393,15 +406,19 @@ static void block_job_event_pending(Notifier *n, void *opaque)
393406
static void block_job_event_ready(Notifier *n, void *opaque)
394407
{
395408
BlockJob *job = opaque;
409+
uint64_t progress_current, progress_total;
396410

397411
if (block_job_is_internal(job)) {
398412
return;
399413
}
400414

415+
progress_get_snapshot(&job->job.progress, &progress_current,
416+
&progress_total);
417+
401418
qapi_event_send_block_job_ready(job_type(&job->job),
402419
job->job.id,
403-
job->job.progress.total,
404-
job->job.progress.current,
420+
progress_total,
421+
progress_current,
405422
job->speed);
406423
}
407424

include/qemu/progress_meter.h

+19-15
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#ifndef QEMU_PROGRESS_METER_H
2828
#define QEMU_PROGRESS_METER_H
2929

30+
#include "qemu/lockable.h"
31+
3032
typedef struct ProgressMeter {
3133
/**
3234
* Current progress. The unit is arbitrary as long as the ratio between
@@ -37,22 +39,24 @@ typedef struct ProgressMeter {
3739

3840
/** Estimated current value at the completion of the process */
3941
uint64_t total;
42+
43+
QemuMutex lock; /* protects concurrent access to above fields */
4044
} ProgressMeter;
4145

42-
static inline void progress_work_done(ProgressMeter *pm, uint64_t done)
43-
{
44-
pm->current += done;
45-
}
46-
47-
static inline void progress_set_remaining(ProgressMeter *pm, uint64_t remaining)
48-
{
49-
pm->total = pm->current + remaining;
50-
}
51-
52-
static inline void progress_increase_remaining(ProgressMeter *pm,
53-
uint64_t delta)
54-
{
55-
pm->total += delta;
56-
}
46+
void progress_init(ProgressMeter *pm);
47+
void progress_destroy(ProgressMeter *pm);
48+
49+
/* Get a snapshot of internal current and total values */
50+
void progress_get_snapshot(ProgressMeter *pm, uint64_t *current,
51+
uint64_t *total);
52+
53+
/* Increases the amount of work done so far by @done */
54+
void progress_work_done(ProgressMeter *pm, uint64_t done);
55+
56+
/* Sets how much work has to be done to complete to @remaining */
57+
void progress_set_remaining(ProgressMeter *pm, uint64_t remaining);
58+
59+
/* Increases the total work to do by @delta */
60+
void progress_increase_remaining(ProgressMeter *pm, uint64_t delta);
5761

5862
#endif /* QEMU_PROGRESS_METER_H */

job-qmp.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,20 @@ void qmp_job_dismiss(const char *id, Error **errp)
144144
static JobInfo *job_query_single(Job *job, Error **errp)
145145
{
146146
JobInfo *info;
147+
uint64_t progress_current;
148+
uint64_t progress_total;
147149

148150
assert(!job_is_internal(job));
151+
progress_get_snapshot(&job->progress, &progress_current,
152+
&progress_total);
149153

150154
info = g_new(JobInfo, 1);
151155
*info = (JobInfo) {
152156
.id = g_strdup(job->id),
153157
.type = job_type(job),
154158
.status = job->status,
155-
.current_progress = job->progress.current,
156-
.total_progress = job->progress.total,
159+
.current_progress = progress_current,
160+
.total_progress = progress_total,
157161
.has_error = !!job->err,
158162
.error = job->err ? \
159163
g_strdup(error_get_pretty(job->err)) : NULL,

job.c

+3
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn,
339339
job->cb = cb;
340340
job->opaque = opaque;
341341

342+
progress_init(&job->progress);
343+
342344
notifier_list_init(&job->on_finalize_cancelled);
343345
notifier_list_init(&job->on_finalize_completed);
344346
notifier_list_init(&job->on_pending);
@@ -382,6 +384,7 @@ void job_unref(Job *job)
382384

383385
QLIST_REMOVE(job, job_list);
384386

387+
progress_destroy(&job->progress);
385388
error_free(job->err);
386389
g_free(job->id);
387390
g_free(job);

qemu-img.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,7 @@ static void common_block_job_cb(void *opaque, int ret)
900900

901901
static void run_block_job(BlockJob *job, Error **errp)
902902
{
903+
uint64_t progress_current, progress_total;
903904
AioContext *aio_context = blk_get_aio_context(job->blk);
904905
int ret = 0;
905906

@@ -908,9 +909,11 @@ static void run_block_job(BlockJob *job, Error **errp)
908909
do {
909910
float progress = 0.0f;
910911
aio_poll(aio_context, true);
911-
if (job->job.progress.total) {
912-
progress = (float)job->job.progress.current /
913-
job->job.progress.total * 100.f;
912+
913+
progress_get_snapshot(&job->job.progress, &progress_current,
914+
&progress_total);
915+
if (progress_total) {
916+
progress = (float)progress_current / progress_total * 100.f;
914917
}
915918
qemu_progress_print(progress, 0);
916919
} while (!job_is_ready(&job->job) && !job_is_completed(&job->job));

0 commit comments

Comments
 (0)