[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 15/30] block: add block_job_sleep_ns
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PATCH 15/30] block: add block_job_sleep_ns |
Date: |
Thu, 10 May 2012 13:49:19 +0200 |
From: Paolo Bonzini <address@hidden>
This function abstracts the pretty complex semantics of the "busy"
member of BlockJob.
Signed-off-by: Paolo Bonzini <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
block.c | 11 +++++++++++
block/stream.c | 23 +++++++++--------------
block_int.h | 22 +++++++++++++++-------
3 files changed, 35 insertions(+), 21 deletions(-)
diff --git a/block.c b/block.c
index 6154c3f..f9a11aa 100644
--- a/block.c
+++ b/block.c
@@ -4188,6 +4188,7 @@ void *block_job_create(const BlockJobType *job_type,
BlockDriverState *bs,
job->bs = bs;
job->cb = cb;
job->opaque = opaque;
+ job->busy = true;
bs->job = job;
/* Only set speed when necessary to avoid NotSupported error */
@@ -4254,3 +4255,13 @@ void block_job_cancel_sync(BlockJob *job)
qemu_aio_wait();
}
}
+
+void block_job_sleep_ns(BlockJob *job, QEMUClock *clock, int64_t ns)
+{
+ /* Check cancellation *before* setting busy = false, too! */
+ if (!block_job_is_cancelled(job)) {
+ job->busy = false;
+ co_sleep_ns(clock, ns);
+ job->busy = true;
+ }
+}
diff --git a/block/stream.c b/block/stream.c
index b7e1ad4..b279acd 100644
--- a/block/stream.c
+++ b/block/stream.c
@@ -192,12 +192,17 @@ static void coroutine_fn stream_run(void *opaque)
}
for (sector_num = 0; sector_num < end; sector_num += n) {
-retry:
+ uint64_t delay_ns = 0;
+
+wait:
+ /* Note that even when no rate limit is applied we need to yield
+ * with no pending I/O here so that qemu_aio_flush() returns.
+ */
+ block_job_sleep_ns(&s->common, rt_clock, delay_ns);
if (block_job_is_cancelled(&s->common)) {
break;
}
- s->common.busy = true;
if (base) {
ret = is_allocated_base(bs, base, sector_num,
STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
@@ -209,13 +214,9 @@ retry:
trace_stream_one_iteration(s, sector_num, n, ret);
if (ret == 0) {
if (s->common.speed) {
- uint64_t delay_ns = ratelimit_calculate_delay(&s->limit, n);
+ delay_ns = ratelimit_calculate_delay(&s->limit, n);
if (delay_ns > 0) {
- s->common.busy = false;
- co_sleep_ns(rt_clock, delay_ns);
-
- /* Recheck cancellation and that sectors are unallocated */
- goto retry;
+ goto wait;
}
}
ret = stream_populate(bs, sector_num, n, buf);
@@ -227,12 +228,6 @@ retry:
/* Publish progress */
s->common.offset += n * BDRV_SECTOR_SIZE;
-
- /* Note that even when no rate limit is applied we need to yield
- * with no pending I/O here so that qemu_aio_flush() returns.
- */
- s->common.busy = false;
- co_sleep_ns(rt_clock, 0);
}
if (!base) {
diff --git a/block_int.h b/block_int.h
index fbbc7ae..3bf2367 100644
--- a/block_int.h
+++ b/block_int.h
@@ -97,18 +97,15 @@ struct BlockJob {
/**
* Set to true if the job should cancel itself. The flag must
* always be tested just before toggling the busy flag from false
- * to true. After a job has detected that the cancelled flag is
- * true, it should not anymore issue any I/O operation to the
- * block device.
+ * to true. After a job has been cancelled, it should only yield
+ * if #qemu_aio_wait will ("sooner or later") reenter the coroutine.
*/
bool cancelled;
/**
* Set to false by the job while it is in a quiescent state, where
- * no I/O is pending and cancellation can be processed without
- * issuing new I/O. The busy flag must be set to false when the
- * job goes to sleep on any condition that is not detected by
- * #qemu_aio_wait, such as a timer.
+ * no I/O is pending and the job has yielded on any condition
+ * that is not detected by #qemu_aio_wait, such as a timer.
*/
bool busy;
@@ -364,6 +361,17 @@ void *block_job_create(const BlockJobType *job_type,
BlockDriverState *bs,
void *opaque, Error **errp);
/**
+ * block_job_sleep_ns:
+ * @job: The job that calls the function.
+ * @clock: The clock to sleep on.
+ * @ns: How many nanoseconds to stop for.
+ *
+ * Put the job to sleep (assuming that it wasn't canceled) for @ns
+ * nanoseconds. Canceling the job will interrupt the wait immediately.
+ */
+void block_job_sleep_ns(BlockJob *job, QEMUClock *clock, int64_t ns);
+
+/**
* block_job_complete:
* @job: The job being completed.
* @ret: The status code.
--
1.7.6.5
- [Qemu-devel] [PATCH 04/30] block: push bdrv_change_backing_file error checking up from drivers, (continued)
- [Qemu-devel] [PATCH 04/30] block: push bdrv_change_backing_file error checking up from drivers, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 10/30] qtest: Add floppy test, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 06/30] block: open backing file as read-only when probing for size, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 09/30] qtest: Add function to send QMP commands, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 12/30] block: another bdrv_append fix, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 07/30] block: fix allocation size for dirty bitmap, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 02/30] block: add mode argument to blockdev-snapshot-sync, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 05/30] block: update in-memory backing file and format, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 18/30] block: protect path_has_protocol from filenames with colons, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 11/30] block: fix snapshot on QED, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 15/30] block: add block_job_sleep_ns,
Kevin Wolf <=
- [Qemu-devel] [PATCH 16/30] block: wait for job callback in block_job_cancel_sync, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 23/30] stream: fix sectors not allocated test, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 19/30] block: move field reset from bdrv_open_common to bdrv_close, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 26/30] stream: fix HMP block_job_set_speed, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 27/30] stream: fix ratelimiting corner case, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 22/30] qemu-io: fix the alloc command, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 24/30] stream: add testcase for partial streaming, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 25/30] stream: pass new base image format to bdrv_change_backing_file, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 28/30] stream: do not copy unallocated sectors from the base, Kevin Wolf, 2012/05/10
- [Qemu-devel] [PATCH 29/30] tests/Makefile: Add missing $(EXESUF), Kevin Wolf, 2012/05/10