Skip to contentSkip to conversation

net: epoll support for busy poll

RFC PATCH In review Eliezer Tamir 5 participants 11 messages Aug 21, 2013 – Aug 27, 2013

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 net: epoll support for busy poll

Add busy poll support to epoll.

Background:
The persistent nature of epoll allows us to have a long lasting context that we use to keep track of which device queues we expect traffic to come on for the sockets monitored by an epoll file.

Design:
We tried to make epoll changes as small as possible and to keep networking structures out of eventpoll.c.
All of the epoll changes have nop placeholders for when busy poll is not defined.

Instead of remembering the napi_id for all the sockets in an epoll, we only track the first socket we see with any unique napi_id. The rational for this is that while there may be many thousands of sockets tracked by a single epoll, we expect to only see a handful of unique napi_ids in most cases.

A list of "unique" sockets is linked to struct epoll. Struct busy_poll_id_index serves as the element of this list.

In sk_mark_napi_id() we notice and record the following events:
1. the napid_id of an sk changes
2. the socket receives data through napi poll
in epoll_poll_callback() and ep_table_queue_proc() if these events happened, we check if we need to update the list.

Locking:
ep->busy_list is protected by ep->lock, with irqs disabled. busy_poll indices use RCU.
sk_ll_state uses bitpos.

Performance:
using sockperf, Intel X520 NICs,
Supermicro 6026TT-BTF systems with E5-2690 Xeon CPUs 100 UDP sockets avg. latency 5.756 (std-dev 0.510) 1k UDP sockets avg. latency 5.780 (std-dev 0.536) 10k UDP sockets avg. latency 6.269 (std-dev 0.611)

Open issues:
warn_busy_list_empty() happens under load with memcached. Should we add some hysteresis so the queue list does not change too fast? As usual suggestions about naming, where to put things and overall design are most welcome.

Credit:
Eliel Louzoun and Matthew Wilcox: parts of the event handling algorithm. Willem de Brujin: advice on style, where to put things, RCU.

Special thanks for finding bugs in earlier versions: Julie Cummings.

---

 fs/eventpoll.c          |  131 +++++++++++++++++++++++++++++
 include/linux/poll.h    |    2 
 include/net/busy_poll.h |  216 +++++++++++++++++++++++++++++++++++++++++++++--
 include/net/sock.h      |    1 
 4 files changed, 341 insertions(+), 9 deletions(-)
fs/eventpoll.c +131 -0
index 9ad17b15..f432599 100644
@@ -42,6 +42,7 @@
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
 #include <linux/compat.h>
+#include <net/busy_poll.h>
 
 /*
  * LOCKING:
@@ -215,6 +216,9 @@ struct eventpoll {
 	/* used to optimize loop detection check */
 	int visited;
 	struct list_head visited_list_link;
+
+	/* list of busy poll indices with active unique busy-poll ids */
+	struct busy_list busy_list;
 };
 
 /* Wait structure used by the poll hooks */
@@ -233,6 +237,10 @@ struct eppoll_entry {
 
 	/* The wait queue head that linked the "wait" wait queue item */
 	wait_queue_head_t *whead;
+
+	/* Is this file in the busy wait list for our waiter */
+	struct busy_state busy;
+
 };
 
 /* Wrapper struct used by poll queueing */
@@ -375,6 +383,103 @@ static inline int ep_events_available(struct eventpoll *ep)
 	return !list_empty(&ep->rdllist) || ep->ovflist != EP_UNACTIVE_PTR;
 }
 
+#ifdef CONFIG_NET_RX_BUSY_POLL
+/*
+ * if busy polling is on and the file for this pwq is a socket,
+ * return a pointer to struct socket
+ */
+static inline struct socket *ep_sock_from_pwq(struct eppoll_entry *pwq)
+{
+	struct file *file = pwq->base->ffd.file;
+	struct inode *inode = file_inode(file);
+
+	if (!net_busy_loop_on())
+		return NULL;
+
+	if (!S_ISSOCK(inode->i_mode))
+		return NULL;
+
+	return file->private_data;
+}
+
+/*
+ * check if a socket might need to be added or removed from busy poll list
+ * must be called with ep->lock taken, irqs disabled
+ */
+static inline void eppoll_check_busy_poll(struct eventpoll *ep,
+					  struct eppoll_entry *pwq)
+{
+	struct socket *sock = ep_sock_from_pwq(pwq);
+
+	if (!sock)
+		return;
+
+	busy_poll_update_socket(sock, &ep->busy_list, &pwq->busy.listed);
+}
+
+/*
+ * attempt to delete on removal even if !net_busy_loop_on because it may have
+ * been turned off while we have sockets on the list
+ */
+static inline void eppoll_del_busy_poll(struct eventpoll *ep,
+					struct eppoll_entry *pwq)
+{
+	struct socket *sock = pwq->busy.listed ? ep_sock_from_pwq(pwq) : NULL;
+	unsigned long flags;
+
+	if (sock) {
+		spin_lock_irqsave(&ep->lock, flags);
+		busy_poll_delete_socket(&ep->busy_list, sock);
+		pwq->busy.listed = false;
+		spin_unlock_irqrestore(&ep->lock, flags);
+	}
+}
+
+/*
+ * called from ep_poll() when there is nothing for the user
+ * and busy polling is on
+ */
+static inline void epoll_busy_loop(struct eventpoll *ep, unsigned long end)
+{
+	struct busy_poll_id_index *idx;
+
+	rcu_read_lock_bh();
+
+	while (!busy_loop_timeout(end) && !need_resched()) {
+		list_for_each_entry_rcu(idx, &ep->busy_list.head, list) {
+			if (napi_id_busy_loop(idx->napi_id) &&
+			    ep_events_available(ep))
+				goto out;
+		}
+	}
+out:
+	rcu_read_unlock_bh();
+}
+
+#else /* CONFIG_NET_RX_BUSY_POLL */
+
+static inline struct socket *ep_sock_from_pwq(struct eppoll_entry *pwq)
+{
+	return NULL;
+}
+
+static inline void eppoll_check_busy_poll(struct eventpoll *ep,
+					  struct eppoll_entry *pwq)
+{
+}
+
+static inline void eppoll_del_busy_poll(struct eventpoll *ep,
+					struct eppoll_entry *pwq)
+{
+}
+
+static inline void epoll_busy_loop(struct eventpoll *ep, unsigned long end)
+{
+}
+#endif /* CONFIG_NET_RX_BUSY_POLL */
+
+
+
 /**
  * ep_call_nested - Perform a bound (possibly) nested call, by checking
  *                  that the recursion limit is not exceeded, and that
@@ -536,6 +641,7 @@ static void ep_unregister_pollwait(struct eventpoll *ep, struct epitem *epi)
 
 		list_del(&pwq->llink);
 		ep_remove_wait_queue(pwq);
+		eppoll_del_busy_poll(ep, pwq);
 		kmem_cache_free(pwq_cache, pwq);
 	}
 }
@@ -755,6 +861,10 @@ static void ep_free(struct eventpoll *ep)
 		epi = rb_entry(rbp, struct epitem, rbn);
 		ep_remove(ep, epi);
 	}
+
+	/* at this stage ep->ll_list should be empty */
+	warn_busy_list_empty(&ep->busy_list);
+
 	mutex_unlock(&ep->mtx);
 
 	mutex_unlock(&epmutex);
@@ -920,6 +1030,7 @@ static int ep_alloc(struct eventpoll **pep)
 	init_waitqueue_head(&ep->wq);
 	init_waitqueue_head(&ep->poll_wait);
 	INIT_LIST_HEAD(&ep->rdllist);
+	init_busy_list(&ep->busy_list);
 	ep->rbr = RB_ROOT;
 	ep->ovflist = EP_UNACTIVE_PTR;
 	ep->user = user;
@@ -987,6 +1098,8 @@ static int ep_poll_callback(wait_queue_t *wait, unsigned mode, int sync, void *k
 
 	spin_lock_irqsave(&ep->lock, flags);
 
+	eppoll_check_busy_poll(ep, ep_pwq_from_wait(wait));
+
 	/*
 	 * If the event mask does not contain any poll(2) event, we consider the
 	 * descriptor to be disabled. This condition is likely the effect of the
@@ -1066,9 +1179,11 @@ static void ep_ptable_queue_proc(struct file *file, wait_queue_head_t *whead,
 		init_waitqueue_func_entry(&pwq->wait, ep_poll_callback);
 		pwq->whead = whead;
 		pwq->base = epi;
+		init_busy_state(&pwq->busy);
 		add_wait_queue(whead, &pwq->wait);
 		list_add_tail(&pwq->llink, &epi->pwqlist);
 		epi->nwait++;
+		eppoll_check_busy_poll(epi->ep, pwq);
 	} else {
 		/* We have to signal that an error occurred */
 		epi->nwait = -1;
@@ -1559,6 +1674,7 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
 	long slack = 0;
 	wait_queue_t wait;
 	ktime_t expires, *to = NULL;
+	unsigned long busy_loop_end = 0;
 
 	if (timeout > 0) {
 		struct timespec end_time = ep_set_mstimeout(timeout);
@@ -1577,6 +1693,21 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
 	}
 
 fetch_events:
+
+	/*
+	 * Busy poll if globally on and supporting sockets found && no events,
+	 * busy loop will return if need_resched or ep_events_available.
+	 *
+	 * we must do our busy polling with irqs enabled
+	 */
+
+	if (epoll_can_busy_loop(&ep->busy_list) && !ep_events_available(ep)) {
+		if (!busy_loop_end)
+			busy_loop_end = busy_loop_end_time();
+
+		epoll_busy_loop(ep, busy_loop_end);
+	}
+
 	spin_lock_irqsave(&ep->lock, flags);
 
 	if (!ep_events_available(ep)) {
include/linux/poll.h +1 -1
index c08386f..8de6133 100644
@@ -69,7 +69,7 @@ static inline unsigned long poll_requested_events(const poll_table *p)
 static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
 {
 	pt->_qproc = qproc;
-	pt->_key   = ~0UL; /* all events enabled */
+	pt->_key   = ~(unsigned long) POLL_BUSY_LOOP;
 }
 
 struct poll_table_entry {
include/net/busy_poll.h +208 -8

266 lines not on this page · raw message

include/net/sock.h +1 -0
index e4bbcbf..0537109 100644
@@ -330,6 +330,7 @@ struct sock {
 #ifdef CONFIG_NET_RX_BUSY_POLL
 	unsigned int		sk_napi_id;
 	unsigned int		sk_ll_usec;
+	unsigned long		sk_ll_state;
 #endif
 	atomic_t		sk_drops;
 	int			sk_rcvbuf;
#1 lore raw
Quoted 6 lines · Eliezer Tamir

Instead of remembering the napi_id for all the sockets in an epoll, we only track the first socket we see with any unique napi_id. The rational for this is that while there may be many thousands of sockets tracked by a single epoll, we expect to only see a handful of unique napi_ids in most cases.

This looks buggy assumption to me.

We use epoll() with in the order of millions of fds per epoller, and typically one napi_id per cpu.

With your model, we would have to use nr_cpu epollers and make sure that sockets are properly placed on those epollers by their napi_ids.

#2 lore raw
Quoted 9 lines · Eric Dumazet
Quoted 6 lines · Eliezer Tamir

Instead of remembering the napi_id for all the sockets in an epoll, we only track the first socket we see with any unique napi_id. The rational for this is that while there may be many thousands of sockets tracked by a single epoll, we expect to only see a handful of unique napi_ids in most cases.

Quoted 7 lines

This looks buggy assumption to me.

We use epoll() with in the order of millions of fds per epoller, and typically one napi_id per cpu.

With your model, we would have to use nr_cpu epollers and make sure that sockets are properly placed on those epollers by their napi_ids.

There is no assumption here on how the user configures epoll. All we are doing is reducing the list of sockets monitored by an epoll instance to a smaller subset where each NAPI_ID appears only once.

The user is free to have an epoll monitor sockets from queues associated to any CPU.

#3 lore raw
In reply to Eliezer Tamir
Quoted 6 lines · Eliezer Tamir

Performance:
using sockperf, Intel X520 NICs,
Supermicro 6026TT-BTF systems with E5-2690 Xeon CPUs 100 UDP sockets avg. latency 5.756 (std-dev 0.510) 1k UDP sockets avg. latency 5.780 (std-dev 0.536) 10k UDP sockets avg. latency 6.269 (std-dev 0.611)

How does this compare to with normal poll on this system?

In other words, what advantage is there to using epoll instead of poll when busy looping?

epoll and busy_poll seem to be opposites. epoll inherently has higher latency than normal poll, but provides stable performance with many more FDs.

#4 lore raw
Quoted 8 lines · Eric Wong
Quoted 6 lines · Eliezer Tamir

Performance:
using sockperf, Intel X520 NICs,
Supermicro 6026TT-BTF systems with E5-2690 Xeon CPUs 100 UDP sockets avg. latency 5.756 (std-dev 0.510) 1k UDP sockets avg. latency 5.780 (std-dev 0.536) 10k UDP sockets avg. latency 6.269 (std-dev 0.611)

Quoted 8 lines

How does this compare to with normal poll on this system?

In other words, what advantage is there to using epoll instead of poll when busy looping?

epoll and busy_poll seem to be opposites. epoll inherently has higher latency than normal poll, but provides stable performance with many more FDs.

I'm not sure I understand your claim that epoll has a higher latency than poll.

Some quick (and very unscientific) testing seem to indicate that while poll is slightly faster than epoll when polling one socket, when polling on three sockets epoll is already slightly faster.

(what's the point of using poll if you only need to monitor one socket?)

With a larger socket set epoll is a clear winner.

Both select and poll on 1k UDP sockets take over 120us on a similar system.
Poll on 10 sockets takes about 6.8us, on 100 socket about 16.5us.

Note that I'm using a slightly old NIC that has a high HW latency. For comparison, a busy poll on a socket read (using netperf) is about 5.7us.

I expect Amir is seeing better numbers, but he's not telling. ;)

#5 lore raw
Quoted 2 lines · Eliezer Tamir

I'm not sure I understand your claim that epoll has a higher latency than poll.

Nevermind, I usually live in an EPOLLONESHOT bubble so I use more epoll_ctl than "normal" epoll apps :x

Anyways, good to know epoll wins over poll. Thanks.

#6 lore raw
In reply to Eliezer Tamir

I'm on vacation, will test and have some inputs later this week when I be back.

Amir

Quoted 38 lines · Eliezer Tamir
Quoted 8 lines · Eric Wong
Quoted 6 lines · Eliezer Tamir

Performance:
using sockperf, Intel X520 NICs,
Supermicro 6026TT-BTF systems with E5-2690 Xeon CPUs 100 UDP sockets avg. latency 5.756 (std-dev 0.510) 1k UDP sockets avg. latency 5.780 (std-dev 0.536) 10k UDP sockets avg. latency 6.269 (std-dev 0.611)

Quoted 8 lines

How does this compare to with normal poll on this system?

In other words, what advantage is there to using epoll instead of poll when busy looping?

epoll and busy_poll seem to be opposites. epoll inherently has higher latency than normal poll, but provides stable performance with many more FDs.

I'm not sure I understand your claim that epoll has a higher latency than poll.

Some quick (and very unscientific) testing seem to indicate that while poll is slightly faster than epoll when polling one socket, when polling on three sockets epoll is already slightly faster.

(what's the point of using poll if you only need to monitor one socket?)

With a larger socket set epoll is a clear winner.

Both select and poll on 1k UDP sockets take over 120us on a similar system.
Poll on 10 sockets takes about 6.8us, on 100 socket about 16.5us.

Note that I'm using a slightly old NIC that has a high HW latency. For comparison, a busy poll on a socket read (using netperf) is about 5.7us.

Quoted 6 lines

I expect Amir is seeing better numbers, but he's not telling. ;) --
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

------------------------------------------------------------------------------
Introducing Performance Central, a new site from SourceForge and AppDynamics. Performance Central is your source for news, insights, analysis and resources for efficient Application Performance Management. Visit us today!
http://pubads.g.doubleclick.net/gampad/clk?id=48897511&iu=/4140/ostg.clktrk

_______________________________________________
E1000-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/e1000-devel
To learn more about Intel&#174; Ethernet, visit http://communities.intel.com/community/wired

#7 lore raw
Quoted 3 lines · Amir Vadai

I'm on vacation, will test and have some inputs later this week when I be back.

Hello Amir,

Ping me when you get back and I will send you my latest so you can play with it.

#8 lore raw
Quoted 9 lines · Eliezer Tamir
Quoted 3 lines · Amir Vadai

I'm on vacation, will test and have some inputs later this week when I be back.

Hello Amir,

Ping me when you get back and I will send you my latest so you can play with it.

Hi Eliezer,

You can send the latest version, so I can play with it and get some numbers.

Thanks,
Amir

maintainer
#9 lore raw
In reply to Eliezer Tamir

From: Eliezer Tamir <[email protected]>
Date: Wed, 21 Aug 2013 13:39:54 +0300

include/net/busy_poll.h extern unsigned int sysctl_net_busy_poll __read_mostly; · line 43

Quoted 1 line
include/net/busy_poll.h +1 -0
+	SK_LL_STATE_MISS_2,	/* data came through napi poll twice in a row */

Please rename this to SK_LL_STATE_MISS_MULTI or something like that.

Thanks.

#10 lore raw
Quoted 8 lines · David Miller

From: Eliezer Tamir <[email protected]>
Date: Wed, 21 Aug 2013 13:39:54 +0300

include/net/busy_poll.h extern unsigned int sysctl_net_busy_poll __read_mostly; · line 43

Quoted 1 line
include/net/busy_poll.h +1 -0
+	SK_LL_STATE_MISS_2,	/* data came through napi poll twice in a row */

Please rename this to SK_LL_STATE_MISS_MULTI or something like that.

Thanks.

OK