mirror of
https://github.com/izzy2lost/xemu.git
synced 2026-07-06 00:20:22 -07:00
Merge remote-tracking branch 'remotes/vsementsov/tags/pull-jobs-2021-06-25' into staging
block: Make block-copy API thread-safe # gpg: Signature made Fri 25 Jun 2021 13:40:24 BST # gpg: using RSA key 8B9C26CDB2FD147C880E86A1561F24C1F19F79FB # gpg: Good signature from "Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>" [unknown] # gpg: WARNING: This key is not certified with a trusted signature! # gpg: There is no indication that the signature belongs to the owner. # Primary key fingerprint: 8B9C 26CD B2FD 147C 880E 86A1 561F 24C1 F19F 79FB * remotes/vsementsov/tags/pull-jobs-2021-06-25: block-copy: atomic .cancelled and .finished fields in BlockCopyCallState block-copy: add CoMutex lock block-copy: move progress_set_remaining in block_copy_task_end block-copy: streamline choice of copy_range vs. read/write block-copy: small refactor in block_copy_task_entry and block_copy_common co-shared-resource: protect with a mutex progressmeter: protect with a mutex blockjob: let ratelimit handle a speed of 0 block-copy: let ratelimit handle a speed of 0 ratelimit: treat zero speed as unlimited Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
+248
-164
File diff suppressed because it is too large
Load Diff
@@ -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',
|
||||
|
||||
@@ -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"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* 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 OTHER
|
||||
* 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 = pm->current;
|
||||
*total = pm->total;
|
||||
}
|
||||
|
||||
void progress_work_done(ProgressMeter *pm, uint64_t done)
|
||||
{
|
||||
QEMU_LOCK_GUARD(&pm->lock);
|
||||
pm->current += done;
|
||||
}
|
||||
|
||||
void progress_set_remaining(ProgressMeter *pm, uint64_t remaining)
|
||||
{
|
||||
QEMU_LOCK_GUARD(&pm->lock);
|
||||
pm->total = pm->current + remaining;
|
||||
}
|
||||
|
||||
void progress_increase_remaining(ProgressMeter *pm, uint64_t delta)
|
||||
{
|
||||
QEMU_LOCK_GUARD(&pm->lock);
|
||||
pm->total += delta;
|
||||
}
|
||||
+28
-18
@@ -300,28 +300,29 @@ bool block_job_set_speed(BlockJob *job, int64_t speed, Error **errp)
|
||||
|
||||
int64_t block_job_ratelimit_get_delay(BlockJob *job, uint64_t n)
|
||||
{
|
||||
if (!job->speed) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ratelimit_calculate_delay(&job->limit, n);
|
||||
}
|
||||
|
||||
BlockJobInfo *block_job_query(BlockJob *job, Error **errp)
|
||||
{
|
||||
BlockJobInfo *info;
|
||||
uint64_t progress_current, progress_total;
|
||||
|
||||
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 = g_new0(BlockJobInfo, 1);
|
||||
info->type = g_strdup(job_type_str(&job->job));
|
||||
info->device = g_strdup(job->job.id);
|
||||
info->busy = qatomic_read(&job->job.busy);
|
||||
info->paused = job->job.pause_count > 0;
|
||||
info->offset = job->job.progress.current;
|
||||
info->len = job->job.progress.total;
|
||||
info->offset = progress_current;
|
||||
info->len = progress_total;
|
||||
info->speed = job->speed;
|
||||
info->io_status = job->iostatus;
|
||||
info->ready = job_is_ready(&job->job),
|
||||
@@ -348,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 = opaque;
|
||||
uint64_t progress_current, progress_total;
|
||||
|
||||
if (block_job_is_internal(job)) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -364,6 +369,7 @@ static void block_job_event_completed(Notifier *n, void *opaque)
|
||||
{
|
||||
BlockJob *job = opaque;
|
||||
const char *msg = NULL;
|
||||
uint64_t progress_current, progress_total;
|
||||
|
||||
if (block_job_is_internal(job)) {
|
||||
return;
|
||||
@@ -373,10 +379,13 @@ static void block_job_event_completed(Notifier *n, void *opaque)
|
||||
msg = error_get_pretty(job->job.err);
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -397,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 = opaque;
|
||||
uint64_t progress_current, progress_total;
|
||||
|
||||
if (block_job_is_internal(job)) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -472,12 +485,9 @@ void *block_job_create(const char *job_id, const BlockJobDriver *driver,
|
||||
blk_set_disable_request_queuing(blk, true);
|
||||
blk_set_allow_aio_context_change(blk, true);
|
||||
|
||||
/* Only set speed when necessary to avoid NotSupported error */
|
||||
if (speed != 0) {
|
||||
if (!block_job_set_speed(job, speed, errp)) {
|
||||
job_early_fail(&job->job);
|
||||
return NULL;
|
||||
}
|
||||
if (!block_job_set_speed(job, speed, errp)) {
|
||||
job_early_fail(&job->job);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return job;
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#include "block/block.h"
|
||||
#include "qemu/co-shared-resource.h"
|
||||
|
||||
/* All APIs are thread-safe */
|
||||
|
||||
typedef void (*BlockCopyAsyncCallbackFunc)(void *opaque);
|
||||
typedef struct BlockCopyState BlockCopyState;
|
||||
typedef struct BlockCopyCallState BlockCopyCallState;
|
||||
|
||||
@@ -26,15 +26,13 @@
|
||||
#ifndef QEMU_CO_SHARED_RESOURCE_H
|
||||
#define QEMU_CO_SHARED_RESOURCE_H
|
||||
|
||||
|
||||
/* Accesses to co-shared-resource API are thread-safe */
|
||||
typedef struct SharedResource SharedResource;
|
||||
|
||||
/*
|
||||
* Create SharedResource structure
|
||||
*
|
||||
* @total: total amount of some resource to be shared between clients
|
||||
*
|
||||
* Note: this API is not thread-safe.
|
||||
*/
|
||||
SharedResource *shres_create(uint64_t total);
|
||||
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
#ifndef QEMU_PROGRESS_METER_H
|
||||
#define QEMU_PROGRESS_METER_H
|
||||
|
||||
#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 {
|
||||
|
||||
/** Estimated current value at the completion of the process */
|
||||
uint64_t total;
|
||||
|
||||
QemuMutex lock; /* protects concurrent access to above fields */
|
||||
} ProgressMeter;
|
||||
|
||||
static inline void progress_work_done(ProgressMeter *pm, uint64_t done)
|
||||
{
|
||||
pm->current += done;
|
||||
}
|
||||
void progress_init(ProgressMeter *pm);
|
||||
void progress_destroy(ProgressMeter *pm);
|
||||
|
||||
static inline void progress_set_remaining(ProgressMeter *pm, uint64_t remaining)
|
||||
{
|
||||
pm->total = pm->current + remaining;
|
||||
}
|
||||
/* Get a snapshot of internal current and total values */
|
||||
void progress_get_snapshot(ProgressMeter *pm, uint64_t *current,
|
||||
uint64_t *total);
|
||||
|
||||
static inline void progress_increase_remaining(ProgressMeter *pm,
|
||||
uint64_t delta)
|
||||
{
|
||||
pm->total += delta;
|
||||
}
|
||||
/* 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);
|
||||
|
||||
#endif /* QEMU_PROGRESS_METER_H */
|
||||
|
||||
@@ -43,7 +43,11 @@ static inline int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t n)
|
||||
double delay_slices;
|
||||
|
||||
QEMU_LOCK_GUARD(&limit->lock);
|
||||
assert(limit->slice_quota && limit->slice_ns);
|
||||
if (!limit->slice_quota) {
|
||||
/* Throttling disabled. */
|
||||
return 0;
|
||||
}
|
||||
assert(limit->slice_ns);
|
||||
|
||||
if (limit->slice_end_time < now) {
|
||||
/* Previous, possibly extended, time slice finished; reset the
|
||||
@@ -83,7 +87,11 @@ static inline void ratelimit_set_speed(RateLimit *limit, uint64_t speed,
|
||||
{
|
||||
QEMU_LOCK_GUARD(&limit->lock);
|
||||
limit->slice_ns = slice_ns;
|
||||
limit->slice_quota = MAX(((double)speed * slice_ns) / 1000000000ULL, 1);
|
||||
if (speed == 0) {
|
||||
limit->slice_quota = 0;
|
||||
} else {
|
||||
limit->slice_quota = MAX(((double)speed * slice_ns) / 1000000000ULL, 1);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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;
|
||||
|
||||
assert(!job_is_internal(job));
|
||||
progress_get_snapshot(&job->progress, &progress_current,
|
||||
&progress_total);
|
||||
|
||||
info = g_new(JobInfo, 1);
|
||||
*info = (JobInfo) {
|
||||
.id = g_strdup(job->id),
|
||||
.type = job_type(job),
|
||||
.status = job->status,
|
||||
.current_progress = job->progress.current,
|
||||
.total_progress = job->progress.total,
|
||||
.current_progress = progress_current,
|
||||
.total_progress = progress_total,
|
||||
.has_error = !!job->err,
|
||||
.error = job->err ? \
|
||||
g_strdup(error_get_pretty(job->err)) : NULL,
|
||||
|
||||
@@ -339,6 +339,8 @@ void *job_create(const char *job_id, const JobDriver *driver, JobTxn *txn,
|
||||
job->cb = cb;
|
||||
job->opaque = opaque;
|
||||
|
||||
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)
|
||||
|
||||
QLIST_REMOVE(job, job_list);
|
||||
|
||||
progress_destroy(&job->progress);
|
||||
error_free(job->err);
|
||||
g_free(job->id);
|
||||
g_free(job);
|
||||
|
||||
+6
-3
@@ -900,6 +900,7 @@ static void common_block_job_cb(void *opaque, int ret)
|
||||
|
||||
static void run_block_job(BlockJob *job, Error **errp)
|
||||
{
|
||||
uint64_t progress_current, progress_total;
|
||||
AioContext *aio_context = blk_get_aio_context(job->blk);
|
||||
int ret = 0;
|
||||
|
||||
@@ -908,9 +909,11 @@ static void run_block_job(BlockJob *job, Error **errp)
|
||||
do {
|
||||
float progress = 0.0f;
|
||||
aio_poll(aio_context, true);
|
||||
if (job->job.progress.total) {
|
||||
progress = (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 = (float)progress_current / progress_total * 100.f;
|
||||
}
|
||||
qemu_progress_print(progress, 0);
|
||||
} while (!job_is_ready(&job->job) && !job_is_completed(&job->job));
|
||||
|
||||
@@ -28,10 +28,13 @@
|
||||
#include "qemu/co-shared-resource.h"
|
||||
|
||||
struct SharedResource {
|
||||
uint64_t total;
|
||||
uint64_t available;
|
||||
uint64_t total; /* Set in shres_create() and not changed anymore */
|
||||
|
||||
/* State fields protected by lock */
|
||||
uint64_t available;
|
||||
CoQueue queue;
|
||||
|
||||
QemuMutex lock;
|
||||
};
|
||||
|
||||
SharedResource *shres_create(uint64_t total)
|
||||
@@ -40,6 +43,7 @@ SharedResource *shres_create(uint64_t total)
|
||||
|
||||
s->total = s->available = total;
|
||||
qemu_co_queue_init(&s->queue);
|
||||
qemu_mutex_init(&s->lock);
|
||||
|
||||
return s;
|
||||
}
|
||||
@@ -47,10 +51,12 @@ SharedResource *shres_create(uint64_t total)
|
||||
void shres_destroy(SharedResource *s)
|
||||
{
|
||||
assert(s->available == s->total);
|
||||
qemu_mutex_destroy(&s->lock);
|
||||
g_free(s);
|
||||
}
|
||||
|
||||
bool co_try_get_from_shres(SharedResource *s, uint64_t n)
|
||||
/* Called with lock held. */
|
||||
static bool co_try_get_from_shres_locked(SharedResource *s, uint64_t n)
|
||||
{
|
||||
if (s->available >= n) {
|
||||
s->available -= n;
|
||||
@@ -60,16 +66,24 @@ bool co_try_get_from_shres(SharedResource *s, uint64_t n)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool co_try_get_from_shres(SharedResource *s, uint64_t n)
|
||||
{
|
||||
QEMU_LOCK_GUARD(&s->lock);
|
||||
return co_try_get_from_shres_locked(s, n);
|
||||
}
|
||||
|
||||
void coroutine_fn co_get_from_shres(SharedResource *s, uint64_t n)
|
||||
{
|
||||
assert(n <= s->total);
|
||||
while (!co_try_get_from_shres(s, n)) {
|
||||
qemu_co_queue_wait(&s->queue, NULL);
|
||||
QEMU_LOCK_GUARD(&s->lock);
|
||||
while (!co_try_get_from_shres_locked(s, n)) {
|
||||
qemu_co_queue_wait(&s->queue, &s->lock);
|
||||
}
|
||||
}
|
||||
|
||||
void coroutine_fn co_put_to_shres(SharedResource *s, uint64_t n)
|
||||
{
|
||||
QEMU_LOCK_GUARD(&s->lock);
|
||||
assert(s->total - s->available >= n);
|
||||
s->available += n;
|
||||
qemu_co_queue_restart_all(&s->queue);
|
||||
|
||||
Reference in New Issue
Block a user