@@ -49,7 +49,8 @@ ovs_pytests = \
python/ovs/tests/test_kv.py \
python/ovs/tests/test_list.py \
python/ovs/tests/test_odp.py \
- python/ovs/tests/test_ofp.py
+ python/ovs/tests/test_ofp.py \
+ python/ovs/tests/test_poller.py
ovs_flowviz = \
python/ovs/flowviz/__init__.py \
@@ -109,10 +109,50 @@ class _SelectSelect(object):
return list(events_dict.items())
+class _PollPoll(object):
+ """ select.poll wrapper that accumulates the events of each fd.
+
+ select.poll.register() replaces the event mask of a file descriptor that
+ is already registered, whereas a fd passed to Poller.fd_wait() more than
+ once must wake up the following Poller.block() on any of the events
+ requested, the way poll_fd_wait() does in lib/poll-loop.c. Otherwise
+ waiting for POLLIN after POLLOUT on the same socket, as a jsonrpc
+ connection with pending output does, would drop POLLOUT.
+ """
+ def __init__(self):
+ self._poll = select.poll()
+ self.events = {}
+
+ def register(self, fd, events):
+ if not isinstance(fd, int):
+ fd = fd.fileno()
+
+ events |= self.events.get(fd, 0)
+ self.events[fd] = events
+ self._poll.register(fd, events)
+
+ def poll(self, timeout):
+ return self._poll.poll(timeout)
+
+
SelectPoll = _SelectSelect
-# If eventlet/gevent isn't used, we can use select.poll by replacing
-# _SelectPoll with select.poll class
-# _SelectPoll = select.poll
+
+
+def _get_poll():
+ """Return a poll object suitable for the current environment.
+
+ Under eventlet or gevent, select.select is replaced by a green
+ implementation that multiplexes through the hub. _SelectSelect is
+ therefore both cooperative and free of the FD_SETSIZE limit there, so
+ keep using it. Everywhere else select.select is the real select(2),
+ which cannot handle a file descriptor numbered FD_SETSIZE or higher, so
+ use poll(2), which has no such limit.
+ """
+ if _using_eventlet_green_select() or (
+ gevent_monkey and
+ gevent_monkey.is_object_patched('select', 'select')):
+ return _SelectSelect()
+ return _PollPoll()
class Poller(object):
@@ -220,7 +260,7 @@ class Poller(object):
vlog.dbg("%s on fd %d" % (s, fd))
def __reset(self):
- self.poll = SelectPoll()
+ self.poll = _get_poll()
self.timeout = -1
new file mode 100644
@@ -0,0 +1,80 @@
+import os
+import resource
+
+from unittest import mock
+
+import pytest
+
+import ovs.poller
+
+
+def test_poller_uses_poll_by_default():
+ """Poller must use poll(2) when select.select is the real select(2).
+
+ select(2) raises ValueError for a file descriptor numbered FD_SETSIZE
+ (1024) or higher, poll(2) has no such limit.
+ """
+ with mock.patch.object(ovs.poller, '_using_eventlet_green_select',
+ return_value=False), \
+ mock.patch.object(ovs.poller, 'gevent_monkey', None):
+ assert isinstance(ovs.poller.Poller().poll, ovs.poller._PollPoll)
+
+
+def test_poller_uses_select_under_eventlet():
+ """Poller must keep emulating poll with select.select under eventlet.
+
+ The green select has no FD_SETSIZE limit either and, unlike the real
+ poll(2), it yields to the hub instead of blocking the interpreter.
+ """
+ with mock.patch.object(ovs.poller, '_using_eventlet_green_select',
+ return_value=True):
+ assert isinstance(ovs.poller.Poller().poll, ovs.poller._SelectSelect)
+
+
+def test_poller_uses_select_under_gevent():
+ """Same as under eventlet, for a gevent patched select.select."""
+ gevent_monkey = mock.Mock()
+ gevent_monkey.is_object_patched.return_value = True
+ with mock.patch.object(ovs.poller, '_using_eventlet_green_select',
+ return_value=False), \
+ mock.patch.object(ovs.poller, 'gevent_monkey', gevent_monkey):
+ assert isinstance(ovs.poller.Poller().poll, ovs.poller._SelectSelect)
+ gevent_monkey.is_object_patched.assert_called_once_with('select',
+ 'select')
+
+
+@pytest.fixture
+def pipe():
+ rfd, wfd = os.pipe()
+ yield rfd, wfd
+ os.close(rfd)
+ os.close(wfd)
+
+
+def test_poll_accumulates_events(pipe):
+ """Registering a fd again must add to its events, not replace them.
+
+ The write end of an empty pipe is writable but never readable, so it
+ only wakes up if POLLOUT survived the later registration for POLLIN.
+ """
+ _, wfd = pipe
+ poll = ovs.poller._PollPoll()
+ poll.register(wfd, ovs.poller.POLLOUT)
+ poll.register(wfd, ovs.poller.POLLIN)
+ assert poll.poll(0) == [(wfd, ovs.poller.POLLOUT)]
+
+
+def test_poll_fd_above_fd_setsize(pipe):
+ """poll(2) must handle a fd numbered FD_SETSIZE (1024) or higher."""
+ fd = 1100
+ soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
+ if soft <= fd:
+ pytest.skip("RLIMIT_NOFILE %d is too low" % soft)
+ _, wfd = pipe
+ os.dup2(wfd, fd)
+ try:
+ poll = ovs.poller._PollPoll()
+ poll.register(fd, ovs.poller.POLLOUT)
+ assert poll.poll(0) == [(fd, ovs.poller.POLLOUT)]
+ finally:
+ os.close(fd)