From patchwork Mon Feb 21 08:51:25 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 83800 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id BEFBAB6EE8 for ; Mon, 21 Feb 2011 20:29:28 +1100 (EST) Received: from localhost ([127.0.0.1]:51052 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PrS57-0003QW-Vg for incoming@patchwork.ozlabs.org; Mon, 21 Feb 2011 04:29:26 -0500 Received: from [140.186.70.92] (port=33310 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PrRUY-0007Lv-N3 for qemu-devel@nongnu.org; Mon, 21 Feb 2011 03:51:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PrRUV-0001XT-Ru for qemu-devel@nongnu.org; Mon, 21 Feb 2011 03:51:37 -0500 Received: from mail-ww0-f53.google.com ([74.125.82.53]:62999) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PrRUV-0001WX-M7 for qemu-devel@nongnu.org; Mon, 21 Feb 2011 03:51:35 -0500 Received: by mail-ww0-f53.google.com with SMTP id 18so5375392wwi.10 for ; Mon, 21 Feb 2011 00:51:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:sender:from:to:cc:subject:date:message-id :x-mailer:in-reply-to:references; bh=gfFLtiChn4smULGpN4jS5V3aTeQvQp9vfadB95gQ+fQ=; b=cVTUXuWlMeRARh4dleaSbhYbB5t0NmrcYg71SVLZua8YGRCsXhhxIWRGAw38gIj8Ml nw9xFfGGYEesBmA1MCSU0/CzJdX6INu4FpkcBANmn7vtSiYP7ZLSFYrrdvRr/Xk5bOyc b7mYm3/2DIxRq0N9hGOPPq+p//5i2P95AfNVI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; b=o5yXyqreyMePwNNNFwVlJmWsbDwkJSn9KtU0eo246gOe1ZK/8Sg+Xw/I1jvrFpli6L wBbneQU+f08kTe5gfCYmE7HWP6qhe7bAZ9rWcdMgg4PxFU1/ya9sscdJ7pai8jDbMJsE Ae2YhCn4DcN5Ugb0z+rMZGhx9zw2WGFgolOJc= Received: by 10.227.72.199 with SMTP id n7mr910599wbj.61.1298278294857; Mon, 21 Feb 2011 00:51:34 -0800 (PST) Received: from localhost.localdomain (93-34-149-100.ip50.fastwebnet.it [93.34.149.100]) by mx.google.com with ESMTPS id t5sm1948579wes.9.2011.02.21.00.51.33 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 21 Feb 2011 00:51:34 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 21 Feb 2011 09:51:25 +0100 Message-Id: <1298278286-9158-4-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.3.5 In-Reply-To: <1298278286-9158-1-git-send-email-pbonzini@redhat.com> References: <1298278286-9158-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 74.125.82.53 Cc: edgar.iglesias@gmail.com Subject: [Qemu-devel] [PATCH 3/4] rewrite accounting of wait time to the vm_clock X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The current code is advancing qemu_icount before waiting for I/O. Instead, after the patch qemu_icount is left aside (it is a pure instruction counter) and qemu_icount_bias is changed according to the actual amount of time spent in the wait. This is more accurate, and actually works in the iothread case as well. Signed-off-by: Paolo Bonzini --- qemu-timer.c | 78 +++++++++++++++++++++++++++++----------------------------- 1 files changed, 39 insertions(+), 39 deletions(-) diff --git a/qemu-timer.c b/qemu-timer.c index 06fa507..163ec69 100644 --- a/qemu-timer.c +++ b/qemu-timer.c @@ -614,6 +614,39 @@ void configure_icount(const char *option) qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10); } +static int64_t cpu_clock_last_read; + +int qemu_calculate_timeout(void) +{ + int64_t delta; + + /* When using icount, vm_clock timers are handled outside of the alarm + timer. So, wait for I/O in "small bits" to ensure forward progress of + vm_clock when the guest CPU is idle. When not using icount, though, we + just wait for a fixed amount of time (it might as well be infinite). */ + if (!use_icount || !vm_running) { + return 5000; + } + + delta = qemu_icount_delta(); + if (delta > 0) { + /* Virtual time is ahead of real time, wait for it to sync. Time + spent waiting for I/O will not be counted. */ + cpu_clock_last_read = -1; + } else { + /* Wait until the next virtual time event, and account the wait + as virtual time. */ + delta = qemu_next_deadline(); + cpu_clock_last_read = cpu_get_clock(); + } + + if (delta > 0) { + return (delta + 999999) / 1000000; + } else { + return 0; + } +} + void qemu_run_all_timers(void) { alarm_timer->pending = 0; @@ -626,6 +659,12 @@ void qemu_run_all_timers(void) /* vm time timers */ if (vm_running) { + if (use_icount && cpu_clock_last_read != -1) { + /* Virtual time passed without executing instructions. Increase + the bias between instruction count and virtual time. */ + qemu_icount_bias += cpu_get_clock() - cpu_clock_last_read; + cpu_clock_last_read = -1; + } qemu_run_timers(vm_clock); } @@ -1066,42 +1105,3 @@ void quit_timers(void) alarm_timer = NULL; t->stop(t); } - -int qemu_calculate_timeout(void) -{ - int timeout; - int64_t add; - int64_t delta; - - /* When using icount, making forward progress with qemu_icount when the - guest CPU is idle is critical. We only use the static io-thread timeout - for non icount runs. */ - if (!use_icount || !vm_running) { - return 5000; - } - - /* Advance virtual time to the next event. */ - delta = qemu_icount_delta(); - if (delta > 0) { - /* If virtual time is ahead of real time then just - wait for IO. */ - timeout = (delta + 999999) / 1000000; - } else { - /* Wait for either IO to occur or the next - timer event. */ - add = qemu_next_deadline(); - /* We advance the timer before checking for IO. - Limit the amount we advance so that early IO - activity won't get the guest too far ahead. */ - if (add > 10000000) - add = 10000000; - delta += add; - qemu_icount += qemu_icount_round (add); - timeout = delta / 1000000; - if (timeout < 0) - timeout = 0; - } - - return timeout; -} -