From patchwork Tue Apr 24 13:26:55 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 154677 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E6919B6F6E for ; Tue, 24 Apr 2012 23:27:43 +1000 (EST) Received: from localhost ([::1]:45787 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SMfmP-000189-Od for incoming@patchwork.ozlabs.org; Tue, 24 Apr 2012 09:27:41 -0400 Received: from eggs.gnu.org ([208.118.235.92]:46707) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SMfm5-0000XY-HP for qemu-devel@nongnu.org; Tue, 24 Apr 2012 09:27:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SMflv-0006bL-DE for qemu-devel@nongnu.org; Tue, 24 Apr 2012 09:27:21 -0400 Received: from mail-pb0-f45.google.com ([209.85.160.45]:38207) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SMflv-0006b5-2U for qemu-devel@nongnu.org; Tue, 24 Apr 2012 09:27:11 -0400 Received: by pbcuo5 with SMTP id uo5so124618pbc.4 for ; Tue, 24 Apr 2012 06:27:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=FhIwzwLJYXpwLudYH+nLs2wnl3//SD8ypn5qRcfj6KY=; b=ImyuabJPyuvPXVy19Mo9F/KYKAAmAbyyOxVASLNd5CeJIAcE7uVOfElzusbkQIkfTn d3AFxt9wzxNtF3FDRS07iqF5XD2zfcWePrnRwsgWDy8xZ279dHJM6fzPcjYyHqrJcIqO Sas26bMXxLuRJAlcNTUA92GQMJQWSUeWVk66aLK7VjnJO4wrZiHWBhI88FCGUX+6dz3q A/8Lqz0WBJ8PvstFfX78909U4eO7rWVGprgKuaVr3ijlVwsOrJMq4RDhoapbd89437xc hdOucEf+P/xztkPJNzaPt6klN3+2er14QPOzTVivIf6gzdHuu7vojQuKGDk7EyjPRUh8 xhlw== Received: by 10.68.236.73 with SMTP id us9mr3866482pbc.153.1335274029095; Tue, 24 Apr 2012 06:27:09 -0700 (PDT) Received: from yakj.usersys.redhat.com (93-34-182-16.ip50.fastwebnet.it. [93.34.182.16]) by mx.google.com with ESMTPS id qe7sm796778pbc.56.2012.04.24.06.27.05 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 24 Apr 2012 06:27:08 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 24 Apr 2012 15:26:55 +0200 Message-Id: <1335274016-9052-2-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.9.3 In-Reply-To: <1335274016-9052-1-git-send-email-pbonzini@redhat.com> References: <1335274016-9052-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.160.45 Cc: kwolf@redhat.com, stefanha@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH v2 1/2] block: add block_job_sleep_ns X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This function abstracts the pretty complex semantics of the "busy" member of BlockJob. Signed-off-by: Paolo Bonzini --- block.c | 11 +++++++++++ block/stream.c | 22 ++++++++-------------- block_int.h | 22 +++++++++++++++------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/block.c b/block.c index 3fe12b6..60820bf 100644 --- a/block.c +++ b/block.c @@ -4195,6 +4195,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; return job; } @@ -4244,3 +4245,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 0116450..a2bb854 100644 --- a/block/stream.c +++ b/block/stream.c @@ -100,14 +100,18 @@ static void coroutine_fn stream_run(void *opaque) } for (sector_num = 0; sector_num < end; sector_num += n) { + uint64_t delay_ns = 0; bool copy; -retry: +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; ret = bdrv_co_is_allocated(bs, sector_num, STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n); if (ret == 1) { @@ -123,13 +127,9 @@ retry: trace_stream_one_iteration(s, sector_num, n, ret); if (ret >= 0 && copy) { 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); @@ -141,12 +141,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 12fba69..c1f8649 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; @@ -361,6 +358,17 @@ void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs, BlockDriverCompletionFunc *cb, void *opaque); /** + * 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.