block/block-copy: add ratelimit to block-copy

We are going to directly use one async block-copy operation for backup
job, so we need rate limiter.

We want to maintain current backup behavior: only background copying is
limited and copy-before-write operations only participate in limit
calculation. Therefore we need one rate limiter for block-copy state
and boolean flag for block-copy call state for actual limitation.

Note, that we can't just calculate each chunk in limiter after
successful copying: it will not save us from starting a lot of async
sub-requests which will exceed limit too much. Instead let's use the
following scheme on sub-request creation:
1. If at the moment limit is not exceeded, create the request and
account it immediately.
2. If at the moment limit is already exceeded, drop create sub-request
and handle limit instead (by sleep).
With this approach we'll never exceed the limit more than by one
sub-request (which pretty much matches current backup behavior).

Note also, that if there is in-flight block-copy async call,
block_copy_kick() should be used after set-speed to apply new setup
faster. For that block_copy_kick() published in this patch.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Message-Id: <20210116214705.822267-7-vsementsov@virtuozzo.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
Vladimir Sementsov-Ogievskiy 2021-01-17 00:46:48 +03:00 committed by Max Reitz
parent 2e099a9d29
commit 7e032df0ea
4 changed files with 51 additions and 4 deletions

View File

@ -61,7 +61,7 @@ static coroutine_fn int backup_top_cbw(BlockDriverState *bs, uint64_t offset,
off = QEMU_ALIGN_DOWN(offset, s->cluster_size);
end = QEMU_ALIGN_UP(offset + bytes, s->cluster_size);
return block_copy(s->bcs, off, end - off, NULL);
return block_copy(s->bcs, off, end - off, true, NULL);
}
static int coroutine_fn backup_top_co_pdiscard(BlockDriverState *bs,

View File

@ -72,7 +72,7 @@ static int coroutine_fn backup_do_cow(BackupBlockJob *job,
trace_backup_do_cow_enter(job, start, offset, bytes);
ret = block_copy(job->bcs, start, end - start, error_is_read);
ret = block_copy(job->bcs, start, end - start, true, error_is_read);
trace_backup_do_cow_return(job, offset, bytes, ret);

View File

@ -26,6 +26,7 @@
#define BLOCK_COPY_MAX_BUFFER (1 * MiB)
#define BLOCK_COPY_MAX_MEM (128 * MiB)
#define BLOCK_COPY_MAX_WORKERS 64
#define BLOCK_COPY_SLICE_TIME 100000000ULL /* ns */
static coroutine_fn int block_copy_task_entry(AioTask *task);
@ -36,6 +37,7 @@ typedef struct BlockCopyCallState {
int64_t bytes;
int max_workers;
int64_t max_chunk;
bool ignore_ratelimit;
BlockCopyAsyncCallbackFunc cb;
void *cb_opaque;
@ -48,6 +50,7 @@ typedef struct BlockCopyCallState {
/* State */
int ret;
bool finished;
QemuCoSleepState *sleep_state;
/* OUT parameters */
bool error_is_read;
@ -111,6 +114,9 @@ typedef struct BlockCopyState {
void *progress_opaque;
SharedResource *mem;
uint64_t speed;
RateLimit rate_limit;
} BlockCopyState;
static BlockCopyTask *find_conflicting_task(BlockCopyState *s,
@ -623,6 +629,21 @@ block_copy_dirty_clusters(BlockCopyCallState *call_state)
}
task->zeroes = ret & BDRV_BLOCK_ZERO;
if (s->speed) {
if (!call_state->ignore_ratelimit) {
uint64_t ns = ratelimit_calculate_delay(&s->rate_limit, 0);
if (ns > 0) {
block_copy_task_end(task, -EAGAIN);
g_free(task);
qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, ns,
&call_state->sleep_state);
continue;
}
}
ratelimit_calculate_delay(&s->rate_limit, task->bytes);
}
trace_block_copy_process(s, task->offset);
co_get_from_shres(s->mem, task->bytes);
@ -661,6 +682,13 @@ out:
return ret < 0 ? ret : found_dirty;
}
void block_copy_kick(BlockCopyCallState *call_state)
{
if (call_state->sleep_state) {
qemu_co_sleep_wake(call_state->sleep_state);
}
}
/*
* block_copy_common
*
@ -707,12 +735,13 @@ static int coroutine_fn block_copy_common(BlockCopyCallState *call_state)
}
int coroutine_fn block_copy(BlockCopyState *s, int64_t start, int64_t bytes,
bool *error_is_read)
bool ignore_ratelimit, bool *error_is_read)
{
BlockCopyCallState call_state = {
.s = s,
.offset = start,
.bytes = bytes,
.ignore_ratelimit = ignore_ratelimit,
.max_workers = BLOCK_COPY_MAX_WORKERS,
};
@ -798,3 +827,18 @@ void block_copy_set_skip_unallocated(BlockCopyState *s, bool skip)
{
s->skip_unallocated = skip;
}
void block_copy_set_speed(BlockCopyState *s, uint64_t speed)
{
s->speed = speed;
if (speed > 0) {
ratelimit_set_speed(&s->rate_limit, speed, BLOCK_COPY_SLICE_TIME);
}
/*
* Note: it's good to kick all call states from here, but it should be done
* only from a coroutine, to not crash if s->calls list changed while
* entering one call. So for now, the only user of this function kicks its
* only one call_state by hand.
*/
}

View File

@ -41,7 +41,7 @@ int64_t block_copy_reset_unallocated(BlockCopyState *s,
int64_t offset, int64_t *count);
int coroutine_fn block_copy(BlockCopyState *s, int64_t offset, int64_t bytes,
bool *error_is_read);
bool ignore_ratelimit, bool *error_is_read);
/*
* Run block-copy in a coroutine, create corresponding BlockCopyCallState
@ -76,6 +76,9 @@ bool block_copy_call_succeeded(BlockCopyCallState *call_state);
bool block_copy_call_failed(BlockCopyCallState *call_state);
int block_copy_call_status(BlockCopyCallState *call_state, bool *error_is_read);
void block_copy_set_speed(BlockCopyState *s, uint64_t speed);
void block_copy_kick(BlockCopyCallState *call_state);
BdrvDirtyBitmap *block_copy_dirty_bitmap(BlockCopyState *s);
void block_copy_set_skip_unallocated(BlockCopyState *s, bool skip);