From patchwork Fri Mar 13 09:25:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 11436433 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4DD2E921 for ; Fri, 13 Mar 2020 09:26:44 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 31433206FA for ; Fri, 13 Mar 2020 09:26:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 31433206FA Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1jCgZm-0002W1-CC; Fri, 13 Mar 2020 09:25:54 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1jCgZl-0002Vm-E3 for xen-devel@lists.xenproject.org; Fri, 13 Mar 2020 09:25:53 +0000 X-Inumbo-ID: a243c916-650c-11ea-a6c1-bc764e2007e4 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id a243c916-650c-11ea-a6c1-bc764e2007e4; Fri, 13 Mar 2020 09:25:52 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 34A50B0BA; Fri, 13 Mar 2020 09:25:52 +0000 (UTC) From: Jan Beulich To: "xen-devel@lists.xenproject.org" References: <60130f14-3fc5-e40d-fec6-2448fefa6fc4@suse.com> Message-ID: <9460a5f8-5b6e-bba9-79fc-dae54cc6b348@suse.com> Date: Fri, 13 Mar 2020 10:25:55 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <60130f14-3fc5-e40d-fec6-2448fefa6fc4@suse.com> Content-Language: en-US Subject: [Xen-devel] [PATCH 2/4] x86/time: reduce rounding errors in calculations X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Wei Liu , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Plain (unsigned) integer division simply truncates the results. The overall errors are smaller though if we use proper rounding. (Extend this to the purely cosmetic aspect of time.c's freq_string(), which before this change I've frequently observed to report e.g. NN.999MHz HPET clock speeds.) Signed-off-by: Jan Beulich --- We could switch at least the first div/rem pair in calibrate_APIC_clock() to use do_div(), but this would imply switching bus_freq (and then also result) to unsigned int (as the divisor has to be 32-bit). While I think there's pretty little risk for bus frequencies to go beyond 4GHz in the next so many years, I still wasn't certain enough this would be a welcome change. --- a/xen/arch/x86/apic.c +++ b/xen/arch/x86/apic.c @@ -1261,7 +1261,9 @@ static int __init calibrate_APIC_clock(v /* set up multipliers for accurate timer code */ bus_freq = result*HZ; bus_cycle = (u32) (1000000000000LL/bus_freq); /* in pico seconds */ + bus_cycle += (1000000000000UL % bus_freq) * 2 > bus_freq; bus_scale = (1000*262144)/bus_cycle; + bus_scale += ((1000 * 262144) % bus_cycle) * 2 > bus_cycle; apic_printk(APIC_VERBOSE, "..... bus_scale = %#x\n", bus_scale); /* reset APIC to zero timeout value */ --- a/xen/arch/x86/hpet.c +++ b/xen/arch/x86/hpet.c @@ -799,9 +799,9 @@ u64 __init hpet_setup(void) hpet_resume(hpet_boot_cfg); hpet_rate = 1000000000000000ULL; /* 10^15 */ - (void)do_div(hpet_rate, hpet_period); + last = do_div(hpet_rate, hpet_period); - return hpet_rate; + return hpet_rate + (last * 2 > hpet_period); } void hpet_resume(u32 *boot_cfg) --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -275,7 +275,10 @@ static char *freq_string(u64 freq) { static char s[20]; unsigned int x, y; - y = (unsigned int)do_div(freq, 1000000) / 1000; + + if ( do_div(freq, 1000) > 500 ) + ++freq; + y = (unsigned int)do_div(freq, 1000); x = (unsigned int)freq; snprintf(s, sizeof(s), "%u.%03uMHz", x, y); return s;