From patchwork Sat May 17 00:56:43 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 30348 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-vc0-f198.google.com (mail-vc0-f198.google.com [209.85.220.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 55A8320C81 for ; Sat, 17 May 2014 00:57:28 +0000 (UTC) Received: by mail-vc0-f198.google.com with SMTP id ij19sf19710891vcb.9 for ; Fri, 16 May 2014 17:57:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=jROTKhpphHRIyIlRtzBY+OzIXTcxnsdxkj4jLwV2mSk=; b=XZGuvexXJmvJvXLs4ihxgIcRh14U8Mvb3W2tjqPeu4Ns2kKLFqbfkb5dtY5wAtc5Y4 jau3XidU/DvO1nEECYa5QYOB6Yrlt+ghrZ8udDX4o58+1oebxjq1nUpBW7EmbfYwO2Gw c8DJF4qmCP36jByoHvDUxXE9kSfexBICVgg3wu71pzJftwsL0M/XOepmwq7cTeOjk94S IlpkJ9dmcwVD2toR+HvoUUOO7FJKIEV1UG4fTsdFaZe8lU5jk25t0xLN+IcLMkCo4kUo tsyefzmxktSPtgH1MqMufaHhEEcUiwLMlek8O7Vbmov8J0qypvvUAvH4sr0NZPKc9heT 9Eug== X-Gm-Message-State: ALoCoQkADbOKk8JeWuq5es6rP0tM7/FjiXfrY/JHnBWb2Oan+pianVl0LP6n0Kh3iHLz0t/lC7Pv X-Received: by 10.236.159.39 with SMTP id r27mr9624964yhk.7.1400288248125; Fri, 16 May 2014 17:57:28 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.86.163 with SMTP id p32ls956996qgd.42.gmail; Fri, 16 May 2014 17:57:28 -0700 (PDT) X-Received: by 10.220.81.194 with SMTP id y2mr44081vck.29.1400288248050; Fri, 16 May 2014 17:57:28 -0700 (PDT) Received: from mail-vc0-f175.google.com (mail-vc0-f175.google.com [209.85.220.175]) by mx.google.com with ESMTPS id wj8si2058987vcb.200.2014.05.16.17.57.28 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 16 May 2014 17:57:28 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.175 as permitted sender) client-ip=209.85.220.175; Received: by mail-vc0-f175.google.com with SMTP id hu19so7069740vcb.20 for ; Fri, 16 May 2014 17:57:28 -0700 (PDT) X-Received: by 10.58.134.101 with SMTP id pj5mr3668113veb.38.1400288247944; Fri, 16 May 2014 17:57:27 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.221.72 with SMTP id ib8csp95082vcb; Fri, 16 May 2014 17:57:27 -0700 (PDT) X-Received: by 10.66.226.145 with SMTP id rs17mr25018622pac.144.1400288246920; Fri, 16 May 2014 17:57:26 -0700 (PDT) Received: from mail-pb0-f47.google.com (mail-pb0-f47.google.com [209.85.160.47]) by mx.google.com with ESMTPS id dh1si5457844pbc.327.2014.05.16.17.57.26 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 16 May 2014 17:57:26 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.160.47 as permitted sender) client-ip=209.85.160.47; Received: by mail-pb0-f47.google.com with SMTP id rp16so3249575pbb.6 for ; Fri, 16 May 2014 17:57:26 -0700 (PDT) X-Received: by 10.66.148.98 with SMTP id tr2mr25143720pab.33.1400288246494; Fri, 16 May 2014 17:57:26 -0700 (PDT) Received: from localhost.localdomain (c-67-170-153-23.hsd1.or.comcast.net. [67.170.153.23]) by mx.google.com with ESMTPSA id ph1sm40567436pac.14.2014.05.16.17.57.25 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 16 May 2014 17:57:25 -0700 (PDT) From: John Stultz To: LKML Cc: John Stultz , Miroslav Lichvar , Richard Cochran , Prarit Bhargava Subject: [PATCH 2/3] [RFC] timekeeping: Use cached ntp_tick_length when accumulating error Date: Fri, 16 May 2014 17:56:43 -0700 Message-Id: <1400288204-414-3-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1400288204-414-1-git-send-email-john.stultz@linaro.org> References: <1400288204-414-1-git-send-email-john.stultz@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: john.stultz@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.175 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , By caching the ntp_tick_length() when we correct the frequency error, and then using that cached value to accumulate error, we avoid large initial errors when the tick length is changed. This makes convergence happen much faster in the simulator, since the initial error doesn't have to be slowly whittled away. This initially seems like an accounting error, but Miroslav pointed out that ntp_tick_length() can change mid-tick, so when we apply it in the error accumulation, we are applying any recent change to the entire tick. This approach chooses to apply changes in the ntp_tick_length() only to the next tick, which allows us to calculate the freq correction before using the new tick length, which avoids accumulating error. Credit to Miroslav for pointing this out and providing the original patch this functionality has been pulled out from, along with the rational. Cc: Miroslav Lichvar Cc: Richard Cochran Cc: Prarit Bhargava Reported-by: Miroslav Lichvar Signed-off-by: John Stultz --- include/linux/timekeeper_internal.h | 7 +++++++ kernel/time/timekeeping.c | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/linux/timekeeper_internal.h b/include/linux/timekeeper_internal.h index 4d398f1..4fddf6c 100644 --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -34,6 +34,13 @@ struct timekeeper { /* Clock shifted nano seconds */ u64 xtime_nsec; + /* The ntp_tick_length() value currently being used. + * This cached copy ensures we consistently apply the tick + * length for an entire tick, as ntp_tick_length may change + * mid-tick, and we don't want to apply that new value to + * the tick in progress. + */ + u64 ntp_tick; /* Difference between accumulated time and NTP time in ntp * shifted nano seconds. */ s64 ntp_error; diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index d46bda2..846603e 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -141,6 +141,7 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) tk->ntp_error = 0; tk->ntp_error_shift = NTP_SCALE_SHIFT - clock->shift; + tk->ntp_tick = ntpinterval << tk->ntp_error_shift; /* * The timekeeper keeps its own mult values for the currently @@ -1144,6 +1145,8 @@ static __always_inline void timekeeping_freqadjust(struct timekeeper *tk, if (tk->ntp_err_mult) xinterval -= tk->cycle_interval; + tk->ntp_tick = ntp_tick_length(); + /* Calculate current error per tick */ tick_error = ntp_tick_length() >> tk->ntp_error_shift; tick_error -= (xinterval + tk->xtime_remainder); @@ -1289,7 +1292,7 @@ static cycle_t logarithmic_accumulation(struct timekeeper *tk, cycle_t offset, tk->raw_time.tv_nsec = raw_nsecs; /* Accumulate error between NTP and clock interval */ - tk->ntp_error += ntp_tick_length() << shift; + tk->ntp_error += tk->ntp_tick << shift; tk->ntp_error -= (tk->xtime_interval + tk->xtime_remainder) << (tk->ntp_error_shift + shift);