python: Resolve pep8 blank line errors.
[cascardo/ovs.git] / python / ovs / poller.py
1 # Copyright (c) 2010, 2015 Nicira, Inc.
2 #
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:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import errno
16 import ovs.timeval
17 import ovs.vlog
18 import select
19 import socket
20
21 try:
22     import eventlet.patcher
23
24     def _using_eventlet_green_select():
25         return eventlet.patcher.is_monkey_patched(select)
26 except:
27     def _using_eventlet_green_select():
28         return False
29
30 vlog = ovs.vlog.Vlog("poller")
31
32 POLLIN = 0x001
33 POLLOUT = 0x004
34 POLLERR = 0x008
35 POLLHUP = 0x010
36 POLLNVAL = 0x020
37
38
39 # eventlet/gevent doesn't support select.poll. If select.poll is used,
40 # python interpreter is blocked as a whole instead of switching from the
41 # current thread that is about to block to other runnable thread.
42 # So emulate select.poll by select.select because using python means that
43 # performance isn't so important.
44 class _SelectSelect(object):
45     """ select.poll emulation by using select.select.
46     Only register and poll are needed at the moment.
47     """
48     def __init__(self):
49         self.rlist = []
50         self.wlist = []
51         self.xlist = []
52
53     def register(self, fd, events):
54         if isinstance(fd, socket.socket):
55             fd = fd.fileno()
56         assert isinstance(fd, int)
57         if events & POLLIN:
58             self.rlist.append(fd)
59             events &= ~POLLIN
60         if events & POLLOUT:
61             self.wlist.append(fd)
62             events &= ~POLLOUT
63         if events:
64             self.xlist.append(fd)
65
66     def poll(self, timeout):
67         if timeout == -1:
68             # epoll uses -1 for infinite timeout, select uses None.
69             timeout = None
70         else:
71             timeout = float(timeout) / 1000
72         # XXX workaround a bug in eventlet
73         # see https://github.com/eventlet/eventlet/pull/25
74         if timeout == 0 and _using_eventlet_green_select():
75             timeout = 0.1
76
77         rlist, wlist, xlist = select.select(self.rlist, self.wlist, self.xlist,
78                                             timeout)
79         events_dict = {}
80         for fd in rlist:
81             events_dict[fd] = events_dict.get(fd, 0) | POLLIN
82         for fd in wlist:
83             events_dict[fd] = events_dict.get(fd, 0) | POLLOUT
84         for fd in xlist:
85             events_dict[fd] = events_dict.get(fd, 0) | (POLLERR |
86                                                         POLLHUP |
87                                                         POLLNVAL)
88         return events_dict.items()
89
90
91 SelectPoll = _SelectSelect
92 # If eventlet/gevent isn't used, we can use select.poll by replacing
93 # _SelectPoll with select.poll class
94 # _SelectPoll = select.poll
95
96
97 class Poller(object):
98     """High-level wrapper around the "poll" system call.
99
100     Intended usage is for the program's main loop to go about its business
101     servicing whatever events it needs to.  Then, when it runs out of immediate
102     tasks, it calls each subordinate module or object's "wait" function, which
103     in turn calls one (or more) of the functions Poller.fd_wait(),
104     Poller.immediate_wake(), and Poller.timer_wait() to register to be awakened
105     when the appropriate event occurs.  Then the main loop calls
106     Poller.block(), which blocks until one of the registered events happens."""
107
108     def __init__(self):
109         self.__reset()
110
111     def fd_wait(self, fd, events):
112         """Registers 'fd' as waiting for the specified 'events' (which should
113         be select.POLLIN or select.POLLOUT or their bitwise-OR).  The following
114         call to self.block() will wake up when 'fd' becomes ready for one or
115         more of the requested events.
116
117         The event registration is one-shot: only the following call to
118         self.block() is affected.  The event will need to be re-registered
119         after self.block() is called if it is to persist.
120
121         'fd' may be an integer file descriptor or an object with a fileno()
122         method that returns an integer file descriptor."""
123         self.poll.register(fd, events)
124
125     def __timer_wait(self, msec):
126         if self.timeout < 0 or msec < self.timeout:
127             self.timeout = msec
128
129     def timer_wait(self, msec):
130         """Causes the following call to self.block() to block for no more than
131         'msec' milliseconds.  If 'msec' is nonpositive, the following call to
132         self.block() will not block at all.
133
134         The timer registration is one-shot: only the following call to
135         self.block() is affected.  The timer will need to be re-registered
136         after self.block() is called if it is to persist."""
137         if msec <= 0:
138             self.immediate_wake()
139         else:
140             self.__timer_wait(msec)
141
142     def timer_wait_until(self, msec):
143         """Causes the following call to self.block() to wake up when the
144         current time, as returned by ovs.timeval.msec(), reaches 'msec' or
145         later.  If 'msec' is earlier than the current time, the following call
146         to self.block() will not block at all.
147
148         The timer registration is one-shot: only the following call to
149         self.block() is affected.  The timer will need to be re-registered
150         after self.block() is called if it is to persist."""
151         now = ovs.timeval.msec()
152         if msec <= now:
153             self.immediate_wake()
154         else:
155             self.__timer_wait(msec - now)
156
157     def immediate_wake(self):
158         """Causes the following call to self.block() to wake up immediately,
159         without blocking."""
160         self.timeout = 0
161
162     def block(self):
163         """Blocks until one or more of the events registered with
164         self.fd_wait() occurs, or until the minimum duration registered with
165         self.timer_wait() elapses, or not at all if self.immediate_wake() has
166         been called."""
167         try:
168             try:
169                 events = self.poll.poll(self.timeout)
170                 self.__log_wakeup(events)
171             except select.error, e:
172                 # XXX rate-limit
173                 error, msg = e
174                 if error != errno.EINTR:
175                     vlog.err("poll: %s" % e[1])
176         finally:
177             self.__reset()
178
179     def __log_wakeup(self, events):
180         if not events:
181             vlog.dbg("%d-ms timeout" % self.timeout)
182         else:
183             for fd, revents in events:
184                 if revents != 0:
185                     s = ""
186                     if revents & POLLIN:
187                         s += "[POLLIN]"
188                     if revents & POLLOUT:
189                         s += "[POLLOUT]"
190                     if revents & POLLERR:
191                         s += "[POLLERR]"
192                     if revents & POLLHUP:
193                         s += "[POLLHUP]"
194                     if revents & POLLNVAL:
195                         s += "[POLLNVAL]"
196                     vlog.dbg("%s on fd %d" % (s, fd))
197
198     def __reset(self):
199         self.poll = SelectPoll()
200         self.timeout = -1