All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Dumazet <eric.dumazet@gmail.com>
To: David Miller <davem@davemloft.net>
Cc: Julian Anastasov <ja@ssi.bg>, Yuchung Cheng <ycheng@google.com>,
	netdev <netdev@vger.kernel.org>,
	Neal Cardwell <ncardwell@google.com>,
	Larry Brakmo <brakmo@google.com>,
	Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH v7 net-next 1/2] net: add skb_mstamp infrastructure
Date: Wed, 26 Feb 2014 14:02:11 -0800	[thread overview]
Message-ID: <1393452131.26794.7.camel@edumazet-glaptop2.roam.corp.google.com> (raw)
In-Reply-To: <1393309324.2316.119.camel@edumazet-glaptop2.roam.corp.google.com>

From: Eric Dumazet <edumazet@google.com>

ktime_get() is too expensive on some cases, and we'd like to get
usec resolution timestamps in TCP stack.

This patch adds a light weight facility using a combination of
local_clock() and jiffies samples.

Instead of :

        u64 t0, t1;

        t0 = ktime_get();
        // stuff
        t1 = ktime_get();
        delta_us = ktime_us_delta(t1, t0);

use :
        struct skb_mstamp t0, t1;

        skb_mstamp_get(&t0);
        // stuff
        skb_mstamp_get(&t1);
        delta_us = skb_mstamp_us_delta(&t1, &t0);

Note : local_clock() might have a (bounded) drift between cpus.

Do not use this infra in place of ktime_get() without understanding the
issues.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Stephen Hemminger <stephen@networkplumber.org>
Cc: Yuchung Cheng <ycheng@google.com>
Cc: Neal Cardwell <ncardwell@google.com>
Cc: Larry Brakmo <brakmo@google.com>
Cc: Julian Anastasov <ja@ssi.bg>
---
v7: fix kerneldoc

 include/linux/skbuff.h |   59 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 57 insertions(+), 2 deletions(-)

diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 11b6925f0e96..6d8ed22accb4 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -32,6 +32,7 @@
 #include <linux/hrtimer.h>
 #include <linux/dma-mapping.h>
 #include <linux/netdev_features.h>
+#include <linux/sched.h>
 #include <net/flow_keys.h>
 
 /* A. Checksumming of received packets by device.
@@ -356,11 +357,62 @@ typedef unsigned int sk_buff_data_t;
 typedef unsigned char *sk_buff_data_t;
 #endif
 
+/**
+ * struct skb_mstamp - multi resolution time stamps
+ * @stamp_us: timestamp in us resolution
+ * @stamp_jiffies: timestamp in jiffies
+ */
+struct skb_mstamp {
+	union {
+		u64		v64;
+		struct {
+			u32	stamp_us;
+			u32	stamp_jiffies;
+		};
+	};
+};
+
+/**
+ * skb_mstamp_get - get current timestamp
+ * @cl: place to store timestamps
+ */
+static inline void skb_mstamp_get(struct skb_mstamp *cl)
+{
+	u64 val = local_clock();
+
+	do_div(val, NSEC_PER_USEC);
+	cl->stamp_us = (u32)val;
+	cl->stamp_jiffies = (u32)jiffies;
+}
+
+/**
+ * skb_mstamp_delta - compute the difference in usec between two skb_mstamp
+ * @t1: pointer to newest sample
+ * @t0: pointer to oldest sample
+ */
+static inline u32 skb_mstamp_us_delta(const struct skb_mstamp *t1,
+				      const struct skb_mstamp *t0)
+{
+	s32 delta_us = t1->stamp_us - t0->stamp_us;
+	u32 delta_jiffies = t1->stamp_jiffies - t0->stamp_jiffies;
+
+	/* If delta_us is negative, this might be because interval is too big,
+	 * or local_clock() drift is too big : fallback using jiffies.
+	 */
+	if (delta_us <= 0 ||
+	    delta_jiffies >= (INT_MAX / (USEC_PER_SEC / HZ)))
+
+		delta_us = jiffies_to_usecs(delta_jiffies);
+
+	return delta_us;
+}
+
+
 /** 
  *	struct sk_buff - socket buffer
  *	@next: Next buffer in list
  *	@prev: Previous buffer in list
- *	@tstamp: Time we arrived
+ *	@tstamp: Time we arrived/left
  *	@sk: Socket we are owned by
  *	@dev: Device we arrived on/are leaving by
  *	@cb: Control buffer. Free for use by every layer. Put private vars here
@@ -429,7 +481,10 @@ struct sk_buff {
 	struct sk_buff		*next;
 	struct sk_buff		*prev;
 
-	ktime_t			tstamp;
+	union {
+		ktime_t		tstamp;
+		struct skb_mstamp skb_mstamp;
+	};
 
 	struct sock		*sk;
 	struct net_device	*dev;

  parent reply	other threads:[~2014-02-26 22:02 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-23  6:38 [PATCH net-next] tcp: switch rtt estimations to usec resolution Eric Dumazet
2014-02-23  7:36 ` Julian Anastasov
2014-02-23 17:55   ` Eric Dumazet
2014-02-23 18:50     ` [PATCH v2 " Eric Dumazet
2014-02-23 19:19       ` Yuchung Cheng
2014-02-23 21:41         ` Eric Dumazet
2014-02-24  4:42       ` [PATCH v3 " Eric Dumazet
2014-02-24 17:58         ` Eric Dumazet
2014-02-24 18:24         ` [PATCH v4 " Eric Dumazet
2014-02-24 20:47           ` Julian Anastasov
2014-02-24 22:13             ` Eric Dumazet
2014-02-24 23:01               ` Julian Anastasov
2014-02-24 23:31                 ` [PATCH v5 " Eric Dumazet
2014-02-24 23:51                   ` Stephen Hemminger
2014-02-25  0:15                     ` David Miller
2014-02-25  1:11                     ` Eric Dumazet
2014-02-25  1:38                       ` Eric Dumazet
2014-02-25  6:22                         ` [PATCH v6 net-next 1/2] net: add skb_mstamp infrastructure Eric Dumazet
2014-02-25  9:51                           ` David Laight
2014-02-25 12:21                             ` Eric Dumazet
2014-02-26 20:02                           ` David Miller
2014-02-26 20:29                             ` Eric Dumazet
2014-02-26 22:02                           ` Eric Dumazet [this message]
2014-02-26 22:04                             ` [PATCH v7 " David Miller
2014-02-25  6:22                         ` [PATCH v6 net-next 2/2] tcp: switch rtt estimations to usec resolution Eric Dumazet
2014-02-25 17:46                           ` Neal Cardwell
2014-02-25 18:17                             ` Eric Dumazet
2014-02-25 19:13                           ` Neal Cardwell
2014-02-26 22:02                           ` [PATCH v7 " Eric Dumazet
2014-02-26 22:04                             ` David Miller
2014-02-27  1:09                             ` Stephen Hemminger
2014-02-27  1:21                               ` Rick Jones
2014-02-27  9:57                                 ` David Laight
2014-02-27  2:25                               ` Eric Dumazet
2014-02-24 22:19           ` [PATCH v4 net-next] " Eric Dumazet
2014-02-28  5:20       ` [PATCH v2 " Andi Kleen
2014-02-28  6:14         ` Eric Dumazet
2014-02-28 13:31           ` Andi Kleen
2014-02-28 13:51             ` Eric Dumazet
2014-02-23 19:11     ` [PATCH " Julian Anastasov

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=1393452131.26794.7.camel@edumazet-glaptop2.roam.corp.google.com \
    --to=eric.dumazet@gmail.com \
    --cc=brakmo@google.com \
    --cc=davem@davemloft.net \
    --cc=ja@ssi.bg \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    --cc=ycheng@google.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.