aea35460a2e645544434094493c705fd66c09d8d
[sliver-openvswitch.git] / python / ovs / poller.py
1 # Copyright (c) 2010 Nicira Networks
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 logging
17 import select
18 import ovs.timeval
19
20
21 class Poller(object):
22     """High-level wrapper around the "poll" system call.
23
24     Intended usage is for the program's main loop to go about its business
25     servicing whatever events it needs to.  Then, when it runs out of immediate
26     tasks, it calls each subordinate module or object's "wait" function, which
27     in turn calls one (or more) of the functions Poller.fd_wait(),
28     Poller.immediate_wake(), and Poller.timer_wait() to register to be awakened
29     when the appropriate event occurs.  Then the main loop calls
30     Poller.block(), which blocks until one of the registered events happens."""
31
32     def __init__(self):
33         self.__reset()
34
35     def fd_wait(self, fd, events):
36         """Registers 'fd' as waiting for the specified 'events' (which should
37         be select.POLLIN or select.POLLOUT or their bitwise-OR).  The following
38         call to self.block() will wake up when 'fd' becomes ready for one or
39         more of the requested events.
40
41         The event registration is one-shot: only the following call to
42         self.block() is affected.  The event will need to be re-registered
43         after self.block() is called if it is to persist.
44
45         'fd' may be an integer file descriptor or an object with a fileno()
46         method that returns an integer file descriptor."""
47         self.poll.register(fd, events)
48
49     def __timer_wait(self, msec):
50         if self.timeout < 0 or msec < self.timeout:
51             self.timeout = msec
52
53     def timer_wait(self, msec):
54         """Causes the following call to self.block() to block for no more than
55         'msec' milliseconds.  If 'msec' is nonpositive, the following call to
56         self.block() will not block at all.
57
58         The timer registration is one-shot: only the following call to
59         self.block() is affected.  The timer will need to be re-registered
60         after self.block() is called if it is to persist."""
61         if msec <= 0:
62             self.immediate_wake()
63         else:
64             self.__timer_wait(msec)
65
66     def timer_wait_until(self, msec):
67         """Causes the following call to self.block() to wake up when the
68         current time, as returned by ovs.timeval.msec(), reaches 'msec' or
69         later.  If 'msec' is earlier than the current time, the following call
70         to self.block() will not block at all.
71
72         The timer registration is one-shot: only the following call to
73         self.block() is affected.  The timer will need to be re-registered
74         after self.block() is called if it is to persist."""
75         now = ovs.timeval.msec()
76         if msec <= now:
77             self.immediate_wake()
78         else:
79             self.__timer_wait(msec - now)
80
81     def immediate_wake(self):
82         """Causes the following call to self.block() to wake up immediately,
83         without blocking."""
84         self.timeout = 0
85
86     def block(self):
87         """Blocks until one or more of the events registered with
88         self.fd_wait() occurs, or until the minimum duration registered with
89         self.timer_wait() elapses, or not at all if self.immediate_wake() has
90         been called."""
91         try:
92             try:
93                 events = self.poll.poll(self.timeout)
94                 self.__log_wakeup(events)
95             except select.error, e:
96                 # XXX rate-limit
97                 error, msg = e
98                 if error != errno.EINTR:
99                     logging.error("poll: %s" % e[1])
100         finally:
101             self.__reset()
102
103     def __log_wakeup(self, events):
104         if not events:
105             logging.debug("%d-ms timeout" % self.timeout)
106         else:
107             for fd, revents in events:
108                 if revents != 0:
109                     s = ""
110                     if revents & select.POLLIN:
111                         s += "[POLLIN]"
112                     if revents & select.POLLOUT:
113                         s += "[POLLOUT]"
114                     if revents & select.POLLERR:
115                         s += "[POLLERR]"
116                     if revents & select.POLLHUP:
117                         s += "[POLLHUP]"
118                     if revents & select.POLLNVAL:
119                         s += "[POLLNVAL]"
120                     logging.debug("%s on fd %d" % (s, fd))
121
122     def __reset(self):
123         self.poll = select.poll()
124         self.timeout = -1