From patchwork Thu Mar 3 04:21:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 591231 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 635CF14010F for ; Thu, 3 Mar 2016 15:25:04 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b=FDaNOy0k; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756876AbcCCEYg (ORCPT ); Wed, 2 Mar 2016 23:24:36 -0500 Received: from mail-pa0-f44.google.com ([209.85.220.44]:33958 "EHLO mail-pa0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754938AbcCCEWN (ORCPT ); Wed, 2 Mar 2016 23:22:13 -0500 Received: by mail-pa0-f44.google.com with SMTP id fy10so7044013pac.1 for ; Wed, 02 Mar 2016 20:22:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wUEX5ZNKJdCYE5c3HqjvQP35/5J7FGl9kYg8Gd9PCXc=; b=FDaNOy0kv5rOoi+x0WOirmrgyv0dJWNNmjB9y1Q6G0mZ2dkx2lULPLUWm3635gjQKW PL5naIu9UUouInUmVhF0nygN/4VUbdIXr1mczwBN7mFnMqVeOLsIPlMKC3NRVlApQc7E mTKWMwq/9v5lLTnxdCoI4e8KnpjQJ1FXuHDNU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=wUEX5ZNKJdCYE5c3HqjvQP35/5J7FGl9kYg8Gd9PCXc=; b=RNK+KsCSh8jdUqa/OV824uXTqidUdWa82PNHh6V45cufE7zqDAkWDLYvqHrMOJMGKG 2iuY41Uku/JzkiMnDpujeIruRYG+sD5ZQ9sffxbA/0GWk7uNSB9WKXbr6rF1P5ie0e7Z T8pI8A1FyJf1z06h6fczGaNa/nI9t7MpE2CpP20xfX41690GX6KUtFIJv7wQ3n/cViig /UR9fX4oGJ3z78OuInp1+WE4lAFhrmVPGGkGsORwNIItmrgxAAF14NRdbU44gp1n3Gzw Sq2WJcHnVvKUlOxHc+U2vCQSFD6zpvp6aQM9PyaL/BpIjRQ+HBi1VfkwmNd81WT1QjX9 xCew== X-Gm-Message-State: AD7BkJJOwMqwKVeTrGTGchJfeig3GEQ7CEyh9cJKgXe1BEIeVYHmgr+Kqs7vOcXdwvkn1J7m X-Received: by 10.66.132.73 with SMTP id os9mr745349pab.27.1456978932553; Wed, 02 Mar 2016 20:22:12 -0800 (PST) Received: from localhost.localdomain (c-76-115-103-22.hsd1.or.comcast.net. [76.115.103.22]) by smtp.gmail.com with ESMTPSA id xu1sm56468698pab.31.2016.03.02.20.22.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 02 Mar 2016 20:22:11 -0800 (PST) From: John Stultz To: lkml Cc: "Christopher S. Hall" , Prarit Bhargava , Richard Cochran , Thomas Gleixner , Ingo Molnar , Andy Lutomirski , kevin.b.stanton@intel.com, kevin.j.clarke@intel.com, hpa@zytor.com, jeffrey.t.kirsher@intel.com, netdev@vger.kernel.org, John Stultz Subject: [PATCH 2/8] time: Add timekeeping snapshot code capturing system time and counter Date: Wed, 2 Mar 2016 20:21:53 -0800 Message-Id: <1456978919-30076-3-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1456978919-30076-1-git-send-email-john.stultz@linaro.org> References: <1456978919-30076-1-git-send-email-john.stultz@linaro.org> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: "Christopher S. Hall" In the current timekeeping code there isn't any interface to atomically capture the current relationship between the system counter and system time. ktime_get_snapshot() returns this triple (counter, monotonic raw, realtime) in the system_time_snapshot struct. Cc: Prarit Bhargava Cc: Richard Cochran Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Andy Lutomirski Cc: kevin.b.stanton@intel.com Cc: kevin.j.clarke@intel.com Cc: hpa@zytor.com Cc: jeffrey.t.kirsher@intel.com Cc: netdev@vger.kernel.org Reviewed-by: Thomas Gleixner Signed-off-by: Christopher S. Hall [jstultz: Moved structure definitions around to clean things up, fixed cycles_t/cycle_t confusion.] Signed-off-by: John Stultz --- include/linux/timekeeping.h | 18 ++++++++++++++++++ kernel/time/timekeeping.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h index ec89d84..7817591 100644 --- a/include/linux/timekeeping.h +++ b/include/linux/timekeeping.h @@ -267,6 +267,24 @@ extern void ktime_get_raw_and_real_ts64(struct timespec64 *ts_raw, struct timespec64 *ts_real); /* + * struct system_time_snapshot - simultaneous raw/real time capture with + * counter value + * @cycles: Clocksource counter value to produce the system times + * @real: Realtime system time + * @raw: Monotonic raw system time + */ +struct system_time_snapshot { + cycle_t cycles; + ktime_t real; + ktime_t raw; +}; + +/* + * Simultaneously snapshot realtime and monotonic raw clocks + */ +extern void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot); + +/* * Persistent clock related interfaces */ extern int persistent_clock_is_local; diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 4243d28..89b4695 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -874,6 +874,36 @@ time64_t __ktime_get_real_seconds(void) return tk->xtime_sec; } +/** + * ktime_get_snapshot - snapshots the realtime/monotonic raw clocks with counter + * @systime_snapshot: pointer to struct receiving the system time snapshot + */ +void ktime_get_snapshot(struct system_time_snapshot *systime_snapshot) +{ + struct timekeeper *tk = &tk_core.timekeeper; + unsigned long seq; + ktime_t base_raw; + ktime_t base_real; + s64 nsec_raw; + s64 nsec_real; + cycle_t now; + + do { + seq = read_seqcount_begin(&tk_core.seq); + + now = tk->tkr_mono.read(tk->tkr_mono.clock); + base_real = ktime_add(tk->tkr_mono.base, + tk_core.timekeeper.offs_real); + base_raw = tk->tkr_raw.base; + nsec_real = timekeeping_cycles_to_ns(&tk->tkr_mono, now); + nsec_raw = timekeeping_cycles_to_ns(&tk->tkr_raw, now); + } while (read_seqcount_retry(&tk_core.seq, seq)); + + systime_snapshot->cycles = now; + systime_snapshot->real = ktime_add_ns(base_real, nsec_real); + systime_snapshot->raw = ktime_add_ns(base_raw, nsec_raw); +} +EXPORT_SYMBOL_GPL(ktime_get_snapshot); #ifdef CONFIG_NTP_PPS