From patchwork Fri May 20 07:13:23 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulrich Obergfell X-Patchwork-Id: 96547 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id D0D91B71D9 for ; Fri, 20 May 2011 17:13:46 +1000 (EST) Received: from localhost ([::1]:53183 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QNJu3-0002Bz-UY for incoming@patchwork.ozlabs.org; Fri, 20 May 2011 03:13:43 -0400 Received: from eggs.gnu.org ([140.186.70.92]:59057) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QNJtf-000275-Av for qemu-devel@nongnu.org; Fri, 20 May 2011 03:13:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QNJte-0005RZ-1p for qemu-devel@nongnu.org; Fri, 20 May 2011 03:13:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58625) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QNJtd-0005RS-Pm for qemu-devel@nongnu.org; Fri, 20 May 2011 03:13:18 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p4K7DHsZ017570 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 20 May 2011 03:13:17 -0400 Received: from localhost.localdomain (vpn1-7-144.ams2.redhat.com [10.36.7.144]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p4K7Cxq6003713; Fri, 20 May 2011 03:13:14 -0400 From: Ulrich Obergfell To: qemu-devel@nongnu.org Date: Fri, 20 May 2011 09:13:23 +0200 Message-Id: <1305875603-3104-6-git-send-email-uobergfe@redhat.com> In-Reply-To: <1305875603-3104-1-git-send-email-uobergfe@redhat.com> References: <1305875603-3104-1-git-send-email-uobergfe@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: aliguori@us.ibm.com, mtosatti@redhat.com, kvm@vger.kernel.org, jan.kiszka@siemens.com, zamsden@redhat.com, uobergfe@redhat.com, gcosta@redhat.com, avi@redhat.com Subject: [Qemu-devel] [PATCH v5 5/5] hpet 'driftfix': add code in hpet_timer() to compensate delayed callbacks and coalesced interrupts 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 Loss of periodic timer interrupts caused by delayed callbacks and by interrupt coalescing is compensated by gradually injecting additional interrupts during subsequent timer intervals, starting at a rate of one additional interrupt per interval. The injection of additional interrupts is based on a backlog of unaccounted HPET clock periods (new HPETTimer field 'ticks_not_accounted'). The backlog increases due to delayed callbacks and coalesced interrupts, and it decreases if an interrupt was injected successfully. If the backlog increases while compensation is still in progress, the rate at which additional interrupts are injected is increased too. A limit is imposed on the backlog and on the rate. Injecting additional timer interrupts to compensate lost interrupts can alleviate long term time drift. However, on a short time scale, this method can have the side effect of making virtual machine time intermittently pass slower and faster than real time (depending on the guest's time keeping algorithm). Compensation is disabled by default and can be enabled for guests where this behaviour may be acceptable. Signed-off-by: Ulrich Obergfell --- hw/hpet.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 118 insertions(+), 2 deletions(-) diff --git a/hw/hpet.c b/hw/hpet.c index 0428290..bc2a21a 100644 --- a/hw/hpet.c +++ b/hw/hpet.c @@ -31,6 +31,7 @@ #include "hpet_emul.h" #include "sysbus.h" #include "mc146818rtc.h" +#include //#define HPET_DEBUG #ifdef HPET_DEBUG @@ -41,6 +42,9 @@ #define HPET_MSI_SUPPORT 0 +#define MAX_TICKS_NOT_ACCOUNTED (uint64_t)500000000 /* 5 sec */ +#define MAX_IRQ_RATE (uint32_t)10 + struct HPETState; typedef struct HPETTimer { /* timers */ uint8_t tn; /*timer number*/ @@ -334,13 +338,68 @@ static const VMStateDescription vmstate_hpet = { }; /* + * This function resets the driftfix state in the following situations. + * + * - When the guest o/s changes the 'CFG_ENABLE' bit (overall enable) + * in the General Configuration Register from 0 to 1. + * + * - When the guest o/s changes the 'TN_ENABLE' bit (timer N interrupt enable) + * in the Timer N Configuration and Capabilities Register from 0 to 1. + */ +static void hpet_timer_driftfix_reset(HPETTimer *t) +{ + if (t->state->driftfix && timer_is_periodic(t)) { + t->ticks_not_accounted = t->prev_period = t->period; + t->irq_rate = 1; + t->divisor = 1; + } +} + +/* + * This function determines whether there is a backlog of ticks for which + * no interrupts have been delivered to the guest o/s yet. If the backlog + * is equal to or greater than the current period length, then additional + * interrupts will be delivered to the guest o/s inside of the subsequent + * period interval to compensate missed interrupts. + * + * 'ticks_not_accounted' increases by 'N * period' when the comparator is + * being advanced, and it decreases by 'prev_period' when an interrupt is + * delivered to the guest o/s. Normally 'prev_period' is equal to 'period' + * and 'N' is 1. 'prev_period' is different from 'period' if a guest o/s + * has changed the comparator value during the previous period interval. + * 'N' is greater than 1 if the callback was delayed by 'N - 1' periods, + * and 'N' is zero while additional interrupts are delivered inside of an + * interval. + * + * This function is called after the comparator has been advanced but before + * the interrupt is delivered to the guest o/s. Hence, 'ticks_not_accounted' + * is equal to 'prev_period' plus 'period' if there is no backlog. + */ +static bool hpet_timer_has_tick_backlog(HPETTimer *t) +{ + uint64_t backlog = 0; + + if (t->ticks_not_accounted >= t->period + t->prev_period) { + backlog = t->ticks_not_accounted - (t->period + t->prev_period); + } + return (backlog >= t->period); +} + +/* * timer expiration callback */ static void hpet_timer(void *opaque) { HPETTimer *t = opaque; + HPETState *s = t->state; uint64_t diff; - + int irq_delivered = 0; + uint32_t period_count = 0; /* elapsed periods since last callback + * 1: normal case + * > 1: missed 'period_count - 1' interrupts + * due to delayed callback + * 0: callback inside of an interval + * to deliver additional interrupts */ uint64_t period = t->period; uint64_t cur_tick = hpet_get_ticks(t->state); @@ -348,13 +407,48 @@ static void hpet_timer(void *opaque) if (t->config & HPET_TN_32BIT) { while (hpet_time_after(cur_tick, t->cmp)) { t->cmp = (uint32_t)(t->cmp + t->period); + t->ticks_not_accounted += t->period; + period_count++; } } else { while (hpet_time_after64(cur_tick, t->cmp)) { t->cmp += period; + t->ticks_not_accounted += period; + period_count++; } } diff = hpet_calculate_diff(t, cur_tick); + if (s->driftfix) { + if (t->ticks_not_accounted > MAX_TICKS_NOT_ACCOUNTED) { + t->ticks_not_accounted = t->period + t->prev_period; + } + if (hpet_timer_has_tick_backlog(t)) { + if (t->irq_rate == 1 || period_count > 1) { + /* Increase the interrupt delivery rate if compensation + * was not already in progress or if this callback was + * delayed. */ + t->irq_rate++; + t->irq_rate = MIN(t->irq_rate, MAX_IRQ_RATE); + } + if (t->divisor == 0) { + assert(period_count); + } + if (period_count) { + /* A non-zero 'period_count' marks the start of a new + * period interval. Initialize the divisor now. */ + t->divisor = t->irq_rate; + } + /* 'diff' is the amount of ticks between the current callback + * and the start of the next period interval. 'diff' becomes + * smaller at each additional callback inside of an interval + * while compensation is in progress. Decrement the divisor + * here, so that the entire interval is divided into parts + * of similar size. */ + diff /= t->divisor--; + } else { + t->irq_rate = 1; + } + } qemu_mod_timer(t->qemu_timer, qemu_get_clock_ns(vm_clock) + (int64_t)ticks_to_ns(diff)); } else if (t->config & HPET_TN_32BIT && !timer_is_periodic(t)) { @@ -365,7 +459,27 @@ static void hpet_timer(void *opaque) t->wrap_flag = 0; } } - update_irq(t, 1); + if (s->driftfix && timer_is_periodic(t) && period != 0) { + if (t->ticks_not_accounted >= t->period + t->prev_period) { + irq_delivered = update_irq(t, 1); + if (irq_delivered) { + t->ticks_not_accounted -= t->prev_period; + t->prev_period = t->period; + } else { + if (period_count) { + /* If the interrupt was not delivered to the guest o/s, + * increase the delivery rate only if this happened at + * the start of a period interval (a failure to deliver + * additional interrupts inside of an interval does not + * cause an increase). */ + t->irq_rate++; + t->irq_rate = MIN(t->irq_rate, MAX_IRQ_RATE); + } + } + } + } else { + update_irq(t, 1); + } } static void hpet_set_timer(HPETTimer *t) @@ -534,6 +648,7 @@ static void hpet_ram_writel(void *opaque, target_phys_addr_t addr, timer->period = (uint32_t)timer->period; } if (activating_bit(old_val, new_val, HPET_TN_ENABLE)) { + hpet_timer_driftfix_reset(timer); hpet_set_timer(timer); } else if (deactivating_bit(old_val, new_val, HPET_TN_ENABLE)) { hpet_del_timer(timer); @@ -608,6 +723,7 @@ static void hpet_ram_writel(void *opaque, target_phys_addr_t addr, ticks_to_ns(s->hpet_counter) - qemu_get_clock_ns(vm_clock); for (i = 0; i < s->num_timers; i++) { if ((&s->timer[i])->cmp != ~0ULL) { + hpet_timer_driftfix_reset(&s->timer[i]); hpet_set_timer(&s->timer[i]); } }