DPDK patches and discussions
 help / color / mirror / Atom feed
From: Pablo de Lara <pablo.de.lara.guarch@intel.com>
To: dev@dpdk.org
Subject: [dpdk-dev] [PATCH v6 2/8] eal: add common time structures and functions
Date: Thu, 12 Nov 2015 12:55:32 +0000	[thread overview]
Message-ID: <1447332938-201120-3-git-send-email-pablo.de.lara.guarch@intel.com> (raw)
In-Reply-To: <1447332938-201120-1-git-send-email-pablo.de.lara.guarch@intel.com>

From: Daniel Mrzyglod <danielx.t.mrzyglod@intel.com>

Add common functions and structures to handle time, and cycle counts
which will be used for PTP processing.

Signed-off-by: Daniel Mrzyglod <danielx.t.mrzyglod@intel.com>
Signed-off-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
Reviewed-by: John McNamara <john.mcnamara@intel.com>
---
 lib/librte_eal/common/Makefile           |   2 +-
 lib/librte_eal/common/include/rte_time.h | 210 +++++++++++++++++++++++++++++++
 2 files changed, 211 insertions(+), 1 deletion(-)
 create mode 100644 lib/librte_eal/common/include/rte_time.h

diff --git a/lib/librte_eal/common/Makefile b/lib/librte_eal/common/Makefile
index 0c43d6a..8508473 100644
--- a/lib/librte_eal/common/Makefile
+++ b/lib/librte_eal/common/Makefile
@@ -40,7 +40,7 @@ INC += rte_string_fns.h rte_version.h
 INC += rte_eal_memconfig.h rte_malloc_heap.h
 INC += rte_hexdump.h rte_devargs.h rte_dev.h
 INC += rte_pci_dev_feature_defs.h rte_pci_dev_features.h
-INC += rte_malloc.h
+INC += rte_malloc.h rte_time.h
 
 ifeq ($(CONFIG_RTE_INSECURE_FUNCTION_WARNING),y)
 INC += rte_warnings.h
diff --git a/lib/librte_eal/common/include/rte_time.h b/lib/librte_eal/common/include/rte_time.h
new file mode 100644
index 0000000..33f3038
--- /dev/null
+++ b/lib/librte_eal/common/include/rte_time.h
@@ -0,0 +1,210 @@
+/*-
+ *   BSD LICENSE
+ *
+ *   Copyright(c) 2015 Intel Corporation. All rights reserved.
+ *   All rights reserved.
+ *
+ *   Redistribution and use in source and binary forms, with or without
+ *   modification, are permitted provided that the following conditions
+ *   are met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in
+ *       the documentation and/or other materials provided with the
+ *       distribution.
+ *     * Neither the name of Intel Corporation nor the names of its
+ *       contributors may be used to endorse or promote products derived
+ *       from this software without specific prior written permission.
+ *
+ *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define NSEC_PER_SEC 1000000000L
+
+/**
+ * @internal
+ *
+ * Structure to hold the parameters of a running cycle counter to assist
+ * in converting cycles to nanoseconds.
+ */
+struct rte_timecounter {
+	/** Last cycle counter value read. */
+	uint64_t cycle_last;
+	/** Nanoseconds count. */
+	uint64_t nsec;
+	/** Bitmask separating nanosecond and sub-nanoseconds. */
+	uint64_t nsec_mask;
+	/** Sub-nanoseconds count. */
+	uint64_t nsec_frac;
+	/** Reads the current cycle counter value. */
+	uint64_t (*read)(void *arg);
+	/** Bitmask for two's complement subtraction of non-64 bit counters. */
+	uint64_t cc_mask;
+	/** Cycle to nanosecond divisor (power of two). */
+	uint32_t cc_shift;
+	/** Argument of read() function pointer. */
+	void *arg;
+};
+
+/**
+ * @internal
+ *
+ * Initialize the rte_timecounter structure.
+ */
+static inline void
+rte_timecounter_init(struct rte_timecounter *tc, uint64_t start_time)
+{
+	tc->cycle_last = tc->read(tc->arg);
+	tc->nsec = start_time;
+	tc->nsec_mask = (1ULL << tc->cc_shift) - 1;
+	tc->nsec_frac = 0;
+}
+
+/**
+ * @internal
+ *
+ * Converts cyclecounter cycles to nanoseconds.
+ */
+static inline uint64_t
+rte_cyclecounter_cycles_to_ns(uint64_t cycles, uint64_t *frac,
+			      uint32_t shift, uint64_t mask)
+{
+	uint64_t ns;
+
+	/* Add fractional nanoseconds. */
+	ns = cycles + *frac;
+	*frac = ns & mask;
+
+	/* Shift to get only nanoseconds. */
+	return ns >> shift;
+}
+
+/**
+ * @internal
+ *
+ * Similar to rte_cyclecounter_cycles_to_ns(), but this is used when computing
+ * a time previous to the time stored in the cycle counter.
+ */
+static inline uint64_t
+rte_cyclecounter_cycles_to_ns_previous(uint64_t cycles, uint64_t frac,
+				       uint32_t shift)
+{
+	return ((cycles - frac) >> shift);
+}
+
+/**
+ * @internal
+ *
+ * Converts cycle units into nanoseconds and adds to the previous time stored.
+ */
+static inline uint64_t
+rte_timecounter_cycles_to_ns_time(struct rte_timecounter *tc,
+				  uint64_t cycles)
+{
+	uint64_t delta;
+	uint64_t nsec = tc->nsec;
+
+	delta = (cycles - tc->cycle_last) & tc->cc_mask;
+
+	if (delta > (tc->cc_mask / 2)) {
+		/* Handle cycle counts that have wrapped around . */
+		delta = (tc->cycle_last - cycles) & tc->cc_mask;
+		nsec -= rte_cyclecounter_cycles_to_ns_previous(delta,
+							       tc->nsec_frac,
+							       tc->cc_shift);
+	} else {
+		nsec += rte_cyclecounter_cycles_to_ns(delta,
+						      &tc->nsec_frac,
+						      tc->cc_shift,
+						      tc->nsec_mask);
+	}
+
+	return nsec;
+}
+
+/**
+ * @internal
+ *
+ * Get nanoseconds since the last call to this function.
+ */
+static inline uint64_t
+rte_timecounter_read_ns_delta(struct rte_timecounter *tc)
+{
+	uint64_t cycle_now, cycle_delta;
+	uint64_t ns_offset;
+
+	/* Read cycle counter. */
+	cycle_now = tc->read(tc->arg);
+
+	/* Calculate the delta since the last call. */
+	cycle_delta = (cycle_now - tc->cycle_last) & tc->cc_mask;
+
+	/* Convert to nanoseconds. */
+	ns_offset = rte_cyclecounter_cycles_to_ns(cycle_delta, &tc->nsec_frac,
+						  tc->cc_shift, tc->nsec_mask);
+
+	/* Store current cycle counter for next call. */
+	tc->cycle_last = cycle_now;
+
+	return ns_offset;
+}
+
+/**
+ * @internal
+ *
+ * Read timecounter and update the internal nanosecond count in the structure.
+ */
+static inline uint64_t
+rte_timecounter_read(struct rte_timecounter *tc)
+{
+	uint64_t nsec;
+
+	/* Increment time by nanoseconds since last call. */
+	nsec = rte_timecounter_read_ns_delta(tc);
+	nsec += tc->nsec;
+	tc->nsec = nsec;
+
+	return nsec;
+}
+
+/**
+ * @internal
+ *
+ * Convert from timespec structure into nanosecond units.
+ */
+static inline uint64_t
+rte_timespec_to_ns(const struct timespec *ts)
+{
+	return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
+}
+
+/**
+ * @internal
+ *
+ * Convert from nanosecond units into timespec structure.
+ */
+static inline struct timespec
+rte_ns_to_timespec(uint64_t nsec)
+{
+	struct timespec ts = {0, 0};
+
+	if (nsec == 0)
+		return ts;
+
+	ts.tv_sec = nsec / NSEC_PER_SEC;
+	ts.tv_nsec = nsec % NSEC_PER_SEC;
+
+	return ts;
+}
-- 
1.8.1.4

  parent reply	other threads:[~2015-11-12 12:55 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-02 15:20 [dpdk-dev] [PATCH 0/3] add sample ptp slave application Daniel Mrzyglod
2015-10-02 15:20 ` [dpdk-dev] [PATCH 1/3] ethdev: add additional ieee1588 support functions Daniel Mrzyglod
2015-10-02 15:20 ` [dpdk-dev] [PATCH 2/3] ixgbe: " Daniel Mrzyglod
2015-10-02 15:20 ` [dpdk-dev] [PATCH 3/3] example: PTP client slave minimal implementation Daniel Mrzyglod
2015-10-30  9:43 ` [dpdk-dev] [PATCH v2 0/6] add sample ptp slave application Daniel Mrzyglod
2015-10-30  9:43   ` [dpdk-dev] [PATCH v2 1/6] ethdev: add additional ieee1588 support functions Daniel Mrzyglod
2015-10-30  9:43   ` [dpdk-dev] [PATCH v2 2/6] ixgbe: " Daniel Mrzyglod
2015-10-30  9:43   ` [dpdk-dev] [PATCH v2 3/6] igb: " Daniel Mrzyglod
2015-10-30  9:43   ` [dpdk-dev] [PATCH v2 4/6] i40e: " Daniel Mrzyglod
2015-10-30 11:19     ` Ananyev, Konstantin
2015-10-30 11:33       ` De Lara Guarch, Pablo
2015-10-30 11:36         ` Thomas Monjalon
2015-10-30 11:38         ` Ananyev, Konstantin
2015-10-30  9:43   ` [dpdk-dev] [PATCH v2 5/6] example: PTP client slave minimal implementation Daniel Mrzyglod
2015-10-30  9:43   ` [dpdk-dev] [PATCH v2 6/6] doc: add a PTPCLIENT sample guide Daniel Mrzyglod
2015-10-30 11:23   ` [dpdk-dev] [PATCH v2 0/6] add sample ptp slave application Mcnamara, John
2015-11-03 16:38 ` [dpdk-dev] [PATCH v3 0/7] " Daniel Mrzyglod
2015-11-03 16:38   ` [dpdk-dev] [PATCH v3 1/7] ethdev: add additional ieee1588 support functions Daniel Mrzyglod
2015-11-03 16:38   ` [dpdk-dev] [PATCH v3 2/7] net: Add common PTP structures and functions Daniel Mrzyglod
2015-11-03 16:38   ` [dpdk-dev] [PATCH v3 3/7] ixgbe: add additional ieee1588 support functions Daniel Mrzyglod
2015-11-03 16:38   ` [dpdk-dev] [PATCH v3 4/7] igb: " Daniel Mrzyglod
2015-11-03 16:38   ` [dpdk-dev] [PATCH v3 5/7] i40e: " Daniel Mrzyglod
2015-11-03 16:38   ` [dpdk-dev] [PATCH v3 6/7] example: PTP client slave minimal implementation Daniel Mrzyglod
2015-11-03 20:06     ` De Lara Guarch, Pablo
2015-11-03 16:38   ` [dpdk-dev] [PATCH v3 7/7] doc: add a PTPCLIENT sample guide Daniel Mrzyglod
2015-11-04 10:06 ` [dpdk-dev] [PATCH v4 0/7] add sample ptp slave application Daniel Mrzyglod
2015-11-04 10:06   ` [dpdk-dev] [PATCH v4 1/7] ethdev: add additional ieee1588 support functions Daniel Mrzyglod
2015-11-04 10:06   ` [dpdk-dev] [PATCH v4 2/7] net: Add common PTP structures and functions Daniel Mrzyglod
2015-11-04 10:06   ` [dpdk-dev] [PATCH v4 3/7] ixgbe: add additional ieee1588 support functions Daniel Mrzyglod
2015-11-04 10:06   ` [dpdk-dev] [PATCH v4 4/7] igb: " Daniel Mrzyglod
2015-11-04 10:06   ` [dpdk-dev] [PATCH v4 5/7] i40e: " Daniel Mrzyglod
2015-11-04 10:06   ` [dpdk-dev] [PATCH v4 6/7] example: PTP client slave minimal implementation Daniel Mrzyglod
2015-11-04 10:06   ` [dpdk-dev] [PATCH v4 7/7] doc: add a PTPCLIENT sample guide Daniel Mrzyglod
2015-11-05 12:46   ` [dpdk-dev] [PATCH v4 0/7] add sample ptp slave application Mcnamara, John
2015-11-05 15:17     ` Thomas Monjalon
2015-11-05 16:08       ` Mrzyglod, DanielX T
2015-11-05 13:37   ` Mcnamara, John
2015-11-05 14:05 ` [dpdk-dev] [PATCH v5 " Daniel Mrzyglod
2015-11-05 14:06   ` [dpdk-dev] [PATCH v5 1/7] ethdev: add additional ieee1588 support functions Daniel Mrzyglod
2015-11-10 11:03     ` Thomas Monjalon
2015-11-10 11:36       ` Mcnamara, John
2015-11-10 11:58         ` Thomas Monjalon
2015-11-10 14:12           ` Mcnamara, John
2015-11-10 14:16             ` Thomas Monjalon
2015-11-10 15:18             ` Liu, Yong
2015-11-11  1:40               ` Cao, Waterman
2015-11-05 14:06   ` [dpdk-dev] [PATCH v5 2/7] net: Add common PTP structures and functions Daniel Mrzyglod
2015-11-10 11:25     ` Thomas Monjalon
2015-11-11 10:45       ` Mcnamara, John
2015-11-11 11:24         ` Thomas Monjalon
2015-11-05 14:06   ` [dpdk-dev] [PATCH v5 3/7] ixgbe: add additional ieee1588 support functions Daniel Mrzyglod
2015-11-05 14:06   ` [dpdk-dev] [PATCH v5 4/7] igb: " Daniel Mrzyglod
2015-11-05 14:06   ` [dpdk-dev] [PATCH v5 5/7] i40e: " Daniel Mrzyglod
2015-11-05 14:06   ` [dpdk-dev] [PATCH v5 6/7] example: PTP client slave minimal implementation Daniel Mrzyglod
2015-11-05 14:06   ` [dpdk-dev] [PATCH v5 7/7] doc: add a PTPCLIENT sample guide Daniel Mrzyglod
2015-11-05 14:10   ` [dpdk-dev] [PATCH v5 0/7] add sample ptp slave application Mrzyglod, DanielX T
2015-11-05 14:30     ` Mcnamara, John
2015-11-12 12:55   ` [dpdk-dev] [PATCH v6 0/8] " Pablo de Lara
2015-11-12 12:55     ` [dpdk-dev] [PATCH v6 1/8] ethdev: add additional ieee1588 support functions Pablo de Lara
2015-11-12 12:55     ` Pablo de Lara [this message]
2015-11-12 12:55     ` [dpdk-dev] [PATCH v6 3/8] ixgbe: " Pablo de Lara
2015-11-12 12:55     ` [dpdk-dev] [PATCH v6 4/8] igb: " Pablo de Lara
2015-11-12 12:55     ` [dpdk-dev] [PATCH v6 5/8] i40e: " Pablo de Lara
2015-11-12 12:55     ` [dpdk-dev] [PATCH v6 6/8] testpmd: add nanosecond output for ieee1588 fwd Pablo de Lara
2015-11-12 12:55     ` [dpdk-dev] [PATCH v6 7/8] example: minimal ptp client implementation Pablo de Lara
2015-11-12 12:55     ` [dpdk-dev] [PATCH v6 8/8] doc: add a ptpclient sample guide Pablo de Lara
2015-11-13 14:38       ` Thomas Monjalon
2015-11-13 14:58         ` De Lara Guarch, Pablo
2015-11-13 15:10           ` Thomas Monjalon
2015-11-13 15:15             ` De Lara Guarch, Pablo
2015-11-13 15:19               ` Thomas Monjalon
2015-11-12 13:20     ` [dpdk-dev] [PATCH v6 0/8] add sample ptp slave application Mcnamara, John
2015-11-13 16:09     ` [dpdk-dev] [PATCH v7 " Pablo de Lara
2015-11-13 16:09       ` [dpdk-dev] [PATCH v7 1/8] ethdev: add ieee1588 functions for device clock time Pablo de Lara
2015-11-13 16:09       ` [dpdk-dev] [PATCH v7 2/8] eal: add helpers for time conversions Pablo de Lara
2015-11-13 16:09       ` [dpdk-dev] [PATCH v7 3/8] ixgbe: support ieee1588 functions for device time Pablo de Lara
2015-11-13 16:09       ` [dpdk-dev] [PATCH v7 4/8] igb: " Pablo de Lara
2015-11-13 16:09       ` [dpdk-dev] [PATCH v7 5/8] i40e: " Pablo de Lara
2015-11-13 16:09       ` [dpdk-dev] [PATCH v7 6/8] testpmd: add nanosecond output for ieee1588 Pablo de Lara
2015-11-13 16:09       ` [dpdk-dev] [PATCH v7 7/8] example: add minimal PTP client Pablo de Lara
2015-11-13 16:09       ` [dpdk-dev] [PATCH v7 8/8] doc: add a ptpclient sample guide Pablo de Lara
2015-11-13 16:28       ` [dpdk-dev] [PATCH v7 0/8] add sample ptp slave application Thomas Monjalon
2015-11-13 16:38         ` De Lara Guarch, Pablo
2015-11-13 16:49       ` Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1447332938-201120-3-git-send-email-pablo.de.lara.guarch@intel.com \
    --to=pablo.de.lara.guarch@intel.com \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).