1 # Copyright (c) 2010 Nicira, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
22 import eventlet.patcher
24 def _using_eventlet_green_select():
25 return eventlet.patcher.is_monkey_patched(select)
27 def _using_eventlet_green_select():
30 vlog = ovs.vlog.Vlog("poller")
33 # eventlet/gevent doesn't support select.poll. If select.poll is used,
34 # python interpreter is blocked as a whole instead of switching from the
35 # current thread that is about to block to other runnable thread.
36 # So emulate select.poll by select.select because using python means that
37 # performance isn't so important.
38 class _SelectSelect(object):
39 """ select.poll emulation by using select.select.
40 Only register and poll are needed at the moment.
47 def register(self, fd, events):
48 if isinstance(fd, socket.socket):
50 assert isinstance(fd, int)
51 if events & select.POLLIN:
53 events &= ~select.POLLIN
54 if events & select.POLLOUT:
56 events &= ~select.POLLOUT
60 def poll(self, timeout):
62 # epoll uses -1 for infinite timeout, select uses None.
65 timeout = float(timeout) / 1000
66 # XXX workaround a bug in eventlet
67 # see https://github.com/eventlet/eventlet/pull/25
68 if timeout == 0 and _using_eventlet_green_select():
71 rlist, wlist, xlist = select.select(self.rlist, self.wlist, self.xlist,
73 # collections.defaultdict is introduced by python 2.5 and
74 # XenServer uses python 2.4. We don't use it for XenServer.
75 # events_dict = collections.defaultdict(int)
76 # events_dict[fd] |= event
79 events_dict[fd] = events_dict.get(fd, 0) | select.POLLIN
81 events_dict[fd] = events_dict.get(fd, 0) | select.POLLOUT
83 events_dict[fd] = events_dict.get(fd, 0) | (select.POLLERR |
86 return events_dict.items()
89 SelectPoll = _SelectSelect
90 # If eventlet/gevent isn't used, we can use select.poll by replacing
91 # _SelectPoll with select.poll class
92 # _SelectPoll = select.poll
96 """High-level wrapper around the "poll" system call.
98 Intended usage is for the program's main loop to go about its business
99 servicing whatever events it needs to. Then, when it runs out of immediate
100 tasks, it calls each subordinate module or object's "wait" function, which
101 in turn calls one (or more) of the functions Poller.fd_wait(),
102 Poller.immediate_wake(), and Poller.timer_wait() to register to be awakened
103 when the appropriate event occurs. Then the main loop calls
104 Poller.block(), which blocks until one of the registered events happens."""
109 def fd_wait(self, fd, events):
110 """Registers 'fd' as waiting for the specified 'events' (which should
111 be select.POLLIN or select.POLLOUT or their bitwise-OR). The following
112 call to self.block() will wake up when 'fd' becomes ready for one or
113 more of the requested events.
115 The event registration is one-shot: only the following call to
116 self.block() is affected. The event will need to be re-registered
117 after self.block() is called if it is to persist.
119 'fd' may be an integer file descriptor or an object with a fileno()
120 method that returns an integer file descriptor."""
121 self.poll.register(fd, events)
123 def __timer_wait(self, msec):
124 if self.timeout < 0 or msec < self.timeout:
127 def timer_wait(self, msec):
128 """Causes the following call to self.block() to block for no more than
129 'msec' milliseconds. If 'msec' is nonpositive, the following call to
130 self.block() will not block at all.
132 The timer registration is one-shot: only the following call to
133 self.block() is affected. The timer will need to be re-registered
134 after self.block() is called if it is to persist."""
136 self.immediate_wake()
138 self.__timer_wait(msec)
140 def timer_wait_until(self, msec):
141 """Causes the following call to self.block() to wake up when the
142 current time, as returned by ovs.timeval.msec(), reaches 'msec' or
143 later. If 'msec' is earlier than the current time, the following call
144 to self.block() will not block at all.
146 The timer registration is one-shot: only the following call to
147 self.block() is affected. The timer will need to be re-registered
148 after self.block() is called if it is to persist."""
149 now = ovs.timeval.msec()
151 self.immediate_wake()
153 self.__timer_wait(msec - now)
155 def immediate_wake(self):
156 """Causes the following call to self.block() to wake up immediately,
161 """Blocks until one or more of the events registered with
162 self.fd_wait() occurs, or until the minimum duration registered with
163 self.timer_wait() elapses, or not at all if self.immediate_wake() has
167 events = self.poll.poll(self.timeout)
168 self.__log_wakeup(events)
169 except select.error, e:
172 if error != errno.EINTR:
173 vlog.err("poll: %s" % e[1])
177 def __log_wakeup(self, events):
179 vlog.dbg("%d-ms timeout" % self.timeout)
181 for fd, revents in events:
184 if revents & select.POLLIN:
186 if revents & select.POLLOUT:
188 if revents & select.POLLERR:
190 if revents & select.POLLHUP:
192 if revents & select.POLLNVAL:
194 vlog.dbg("%s on fd %d" % (s, fd))
197 self.poll = SelectPoll()