1 # Copyright (c) 2010, 2015 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")
38 # eventlet/gevent doesn't support select.poll. If select.poll is used,
39 # python interpreter is blocked as a whole instead of switching from the
40 # current thread that is about to block to other runnable thread.
41 # So emulate select.poll by select.select because using python means that
42 # performance isn't so important.
43 class _SelectSelect(object):
44 """ select.poll emulation by using select.select.
45 Only register and poll are needed at the moment.
52 def register(self, fd, events):
53 if isinstance(fd, socket.socket):
55 assert isinstance(fd, int)
65 def poll(self, timeout):
67 # epoll uses -1 for infinite timeout, select uses None.
70 timeout = float(timeout) / 1000
71 # XXX workaround a bug in eventlet
72 # see https://github.com/eventlet/eventlet/pull/25
73 if timeout == 0 and _using_eventlet_green_select():
76 rlist, wlist, xlist = select.select(self.rlist, self.wlist, self.xlist,
80 events_dict[fd] = events_dict.get(fd, 0) | POLLIN
82 events_dict[fd] = events_dict.get(fd, 0) | POLLOUT
84 events_dict[fd] = events_dict.get(fd, 0) | (POLLERR |
87 return events_dict.items()
90 SelectPoll = _SelectSelect
91 # If eventlet/gevent isn't used, we can use select.poll by replacing
92 # _SelectPoll with select.poll class
93 # _SelectPoll = select.poll
97 """High-level wrapper around the "poll" system call.
99 Intended usage is for the program's main loop to go about its business
100 servicing whatever events it needs to. Then, when it runs out of immediate
101 tasks, it calls each subordinate module or object's "wait" function, which
102 in turn calls one (or more) of the functions Poller.fd_wait(),
103 Poller.immediate_wake(), and Poller.timer_wait() to register to be awakened
104 when the appropriate event occurs. Then the main loop calls
105 Poller.block(), which blocks until one of the registered events happens."""
110 def fd_wait(self, fd, events):
111 """Registers 'fd' as waiting for the specified 'events' (which should
112 be select.POLLIN or select.POLLOUT or their bitwise-OR). The following
113 call to self.block() will wake up when 'fd' becomes ready for one or
114 more of the requested events.
116 The event registration is one-shot: only the following call to
117 self.block() is affected. The event will need to be re-registered
118 after self.block() is called if it is to persist.
120 'fd' may be an integer file descriptor or an object with a fileno()
121 method that returns an integer file descriptor."""
122 self.poll.register(fd, events)
124 def __timer_wait(self, msec):
125 if self.timeout < 0 or msec < self.timeout:
128 def timer_wait(self, msec):
129 """Causes the following call to self.block() to block for no more than
130 'msec' milliseconds. If 'msec' is nonpositive, the following call to
131 self.block() will not block at all.
133 The timer registration is one-shot: only the following call to
134 self.block() is affected. The timer will need to be re-registered
135 after self.block() is called if it is to persist."""
137 self.immediate_wake()
139 self.__timer_wait(msec)
141 def timer_wait_until(self, msec):
142 """Causes the following call to self.block() to wake up when the
143 current time, as returned by ovs.timeval.msec(), reaches 'msec' or
144 later. If 'msec' is earlier than the current time, the following call
145 to self.block() will not block at all.
147 The timer registration is one-shot: only the following call to
148 self.block() is affected. The timer will need to be re-registered
149 after self.block() is called if it is to persist."""
150 now = ovs.timeval.msec()
152 self.immediate_wake()
154 self.__timer_wait(msec - now)
156 def immediate_wake(self):
157 """Causes the following call to self.block() to wake up immediately,
162 """Blocks until one or more of the events registered with
163 self.fd_wait() occurs, or until the minimum duration registered with
164 self.timer_wait() elapses, or not at all if self.immediate_wake() has
168 events = self.poll.poll(self.timeout)
169 self.__log_wakeup(events)
170 except select.error, e:
173 if error != errno.EINTR:
174 vlog.err("poll: %s" % e[1])
178 def __log_wakeup(self, events):
180 vlog.dbg("%d-ms timeout" % self.timeout)
182 for fd, revents in events:
187 if revents & POLLOUT:
189 if revents & POLLERR:
191 if revents & POLLHUP:
193 if revents & POLLNVAL:
195 vlog.dbg("%s on fd %d" % (s, fd))
198 self.poll = SelectPoll()