From patchwork Sat May 17 00:56:44 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 30349 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pa0-f69.google.com (mail-pa0-f69.google.com [209.85.220.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id C1571202E4 for ; Sat, 17 May 2014 00:57:29 +0000 (UTC) Received: by mail-pa0-f69.google.com with SMTP id ey11sf16853977pad.4 for ; Fri, 16 May 2014 17:57:29 -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=hjdzYZzeNkOmIqS1QRibqrQjCCk9krri76i78Z2Fsic=; b=DRstraq5H8UQ22EDus7STj2SnH4VF8jn5sT1lRvBjO9/S04dD6QYj9QOnTGb+9gry4 mX74Zhr7RWDcTW7NUdETiN29G1w3eVxvKMFyrG1oydxDaiaYSt7Me70vYYbQIUgYTQGc mon+iQb1Am3yQ0N1NPTEM2T4oYGGWACiOwJqG6JL8++B4Iy0+BYFZ3zO4NNfN69GRk+7 LTQvVk/scXJuxpFsPOQ3mbT7rJ9757z0NkIYw8guzdwR6Lc0+iqJSTBsze/CGNL+LTZR P1sX6tfpio7Cs5yZaev9pY6KpLrKy8zLtEquWRlE/1sk7yAzVhf8iaAYDOpJ9NiaygY0 p0ZA== X-Gm-Message-State: ALoCoQm3VUKU1VWpDSNxQovPcD0ao9hMna5DToGyv8JA85xbLmJld3eFW8KZ3ruQtEvFvvBGF44G X-Received: by 10.68.230.193 with SMTP id ta1mr10262511pbc.6.1400288249047; Fri, 16 May 2014 17:57:29 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.48.210 with SMTP id o76ls918496qga.66.gmail; Fri, 16 May 2014 17:57:28 -0700 (PDT) X-Received: by 10.220.12.66 with SMTP id w2mr47450vcw.15.1400288248940; Fri, 16 May 2014 17:57:28 -0700 (PDT) Received: from mail-vc0-f174.google.com (mail-vc0-f174.google.com [209.85.220.174]) by mx.google.com with ESMTPS id tj4si2062720vdc.15.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.174 as permitted sender) client-ip=209.85.220.174; Received: by mail-vc0-f174.google.com with SMTP id lh14so7052269vcb.5 for ; Fri, 16 May 2014 17:57:28 -0700 (PDT) X-Received: by 10.58.245.2 with SMTP id xk2mr291112vec.9.1400288248832; Fri, 16 May 2014 17:57:28 -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 ib8csp95084vcb; Fri, 16 May 2014 17:57:28 -0700 (PDT) X-Received: by 10.68.194.229 with SMTP id hz5mr24932989pbc.91.1400288248005; Fri, 16 May 2014 17:57:28 -0700 (PDT) Received: from mail-pa0-f50.google.com (mail-pa0-f50.google.com [209.85.220.50]) by mx.google.com with ESMTPS id ai10si10987191pad.94.2014.05.16.17.57.27 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 16 May 2014 17:57:27 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.50 as permitted sender) client-ip=209.85.220.50; Received: by mail-pa0-f50.google.com with SMTP id fb1so3208457pad.23 for ; Fri, 16 May 2014 17:57:27 -0700 (PDT) X-Received: by 10.66.236.5 with SMTP id uq5mr619103pac.122.1400288247669; Fri, 16 May 2014 17:57:27 -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.26 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 16 May 2014 17:57:27 -0700 (PDT) From: John Stultz To: LKML Cc: John Stultz , Miroslav Lichvar , Richard Cochran , Prarit Bhargava Subject: [PATCH 3/3] [RFC] timekeeping: Calculate freq adjustment directly Date: Fri, 16 May 2014 17:56:44 -0700 Message-Id: <1400288204-414-4-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.174 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: , After working with Mirolsav's simulator and his initial patch, I've grown more comfortable with his approach of calculating the freq adjustment directly using a division, rather then trying to aproximate it over a number of ticks. Part of the rational here is that now the error adjustment is very small (only a 0 or 1 unit adjustment to the multiplier) it can take quite some time to reduce any accumulated error. The approximation method strains this, since it takes log(adjustment) number of updates to approximate, and we can accumulate quite a bit of error in that period. The downside with this approach is it requires doing a 64bit divide and a few multiplies to properly make and apply the calculation, which will occur about every second or whenever the ntp_tick_length() value is adjusted. The positive side of this approach is that we see very small error accumulation. I do have some concern that compared with the simulator, the scale of the error without this patch in the real world will be hard to ever observe, the extra overhead of the computation (particularly on 32bit systems) won't actually provide a benefit. Cc: Miroslav Lichvar Cc: Richard Cochran Cc: Prarit Bhargava Signed-off-by: John Stultz --- include/linux/timekeeper_internal.h | 2 ++ kernel/time/timekeeping.c | 67 +++++++++++++------------------------ 2 files changed, 25 insertions(+), 44 deletions(-) diff --git a/include/linux/timekeeper_internal.h b/include/linux/timekeeper_internal.h index 4fddf6c..03e5dff 100644 --- a/include/linux/timekeeper_internal.h +++ b/include/linux/timekeeper_internal.h @@ -48,6 +48,8 @@ struct timekeeper { * ntp shifted nano seconds. */ u32 ntp_error_shift; + /* Mult adjustment being applied to correct freq error */ + u32 ntp_freq_mult; /* Mult adjustment being applied to correct ntp_error */ u32 ntp_err_mult; diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 846603e..27c54a6 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -149,6 +149,7 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) * to counteract clock drifting. */ tk->mult = clock->mult; + tk->ntp_freq_mult = clock->mult; tk->ntp_err_mult = 0; } @@ -1058,20 +1059,20 @@ device_initcall(timekeeping_init_ops); */ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, s64 offset, - bool negative, - int adj_scale) + int adj) { s64 interval = tk->cycle_interval; - s32 mult_adj = 1; - if (negative) { - mult_adj = -mult_adj; + if (!adj) + return; + + if (unlikely(abs(adj) > 1)) { + interval *= adj; + offset *= adj; + } else if (adj < 0) { interval = -interval; offset = -offset; } - mult_adj <<= adj_scale; - interval <<= adj_scale; - offset <<= adj_scale; /* * So the following can be confusing. @@ -1079,7 +1080,7 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, * To keep things simple, lets assume mult_adj == 1 for now. * * When mult_adj != 1, remember that the interval and offset values - * have been appropriately scaled so the math is the same. + * have been appropriately multiplied so the math is the same. * * The basic idea here is that we're increasing the multiplier * by one, this causes the xtime_interval to be incremented by @@ -1122,7 +1123,7 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, * * XXX - TODO: Doc ntp_error calculation. */ - tk->mult += mult_adj; + tk->mult += adj; tk->xtime_interval += interval; tk->xtime_nsec -= offset; tk->ntp_error -= (interval - offset) << tk->ntp_error_shift; @@ -1135,36 +1136,13 @@ static __always_inline void timekeeping_apply_adjustment(struct timekeeper *tk, static __always_inline void timekeeping_freqadjust(struct timekeeper *tk, s64 offset) { - s64 interval = tk->cycle_interval; - s64 xinterval = tk->xtime_interval; - s64 tick_error; - bool negative; - u32 adj; - - /* Remove any current error adj from freq calculation */ - 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); + s64 tick_ns; - /* Don't worry about correcting it if its small */ - if (likely((tick_error >= 0) && (tick_error <= interval))) + if (likely(tk->ntp_tick == ntp_tick_length())) return; - - /* preserve the direction of correction */ - negative = (tick_error < 0); - - /* Sort out the magnitude of the correction */ - tick_error = abs(tick_error); - for (adj = 0; tick_error > interval; adj++) - tick_error >>= 1; - - /* scale the corrections */ - timekeeping_apply_adjustment(tk, offset, negative, adj); + tk->ntp_tick = ntp_tick_length(); + tick_ns = (tk->ntp_tick >> tk->ntp_error_shift) - tk->xtime_remainder; + tk->ntp_freq_mult = div64_u64(tick_ns, tk->cycle_interval); } /* @@ -1173,18 +1151,19 @@ static __always_inline void timekeeping_freqadjust(struct timekeeper *tk, */ static void timekeeping_adjust(struct timekeeper *tk, s64 offset) { + u32 new_mult; + /* Correct for the current frequency error */ timekeeping_freqadjust(tk, offset); /* Next make a small adjustment to fix any cumulative error */ - if (!tk->ntp_err_mult && (tk->ntp_error > 0)) { + if (tk->ntp_error > 0) tk->ntp_err_mult = 1; - timekeeping_apply_adjustment(tk, offset, 0, 0); - } else if (tk->ntp_err_mult && (tk->ntp_error <= 0)) { - /* Undo any existing error adjustment */ - timekeeping_apply_adjustment(tk, offset, 1, 0); + else tk->ntp_err_mult = 0; - } + + new_mult = tk->ntp_freq_mult + tk->ntp_err_mult; + timekeeping_apply_adjustment(tk, offset, new_mult - tk->mult); if (unlikely(tk->clock->maxadj && (tk->mult > tk->clock->mult + tk->clock->maxadj))) {