mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 03:25:20 +00:00
event-test: Simplify debug on/off
Make it easy to change debugging if being used by a client program.
This commit is contained in:
parent
1dd5c7f2df
commit
8f3b6bc2dd
@ -15,6 +15,12 @@ import errno
|
|||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
|
do_debug = False
|
||||||
|
def debug(msg):
|
||||||
|
global do_debug
|
||||||
|
if do_debug:
|
||||||
|
print msg
|
||||||
|
|
||||||
#
|
#
|
||||||
# This general purpose event loop will support waiting for file handle
|
# This general purpose event loop will support waiting for file handle
|
||||||
# I/O and errors events, as well as scheduling repeatable timers with
|
# I/O and errors events, as well as scheduling repeatable timers with
|
||||||
@ -83,8 +89,7 @@ class virEventLoopPure:
|
|||||||
self.opaque[1])
|
self.opaque[1])
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, debug=False):
|
def __init__(self):
|
||||||
self.debugOn = debug
|
|
||||||
self.poll = select.poll()
|
self.poll = select.poll()
|
||||||
self.pipetrick = os.pipe()
|
self.pipetrick = os.pipe()
|
||||||
self.nextHandleID = 1
|
self.nextHandleID = 1
|
||||||
@ -106,13 +111,9 @@ class virEventLoopPure:
|
|||||||
# with the event loop for input events. When we need to force
|
# with the event loop for input events. When we need to force
|
||||||
# the main thread out of a poll() sleep, we simple write a
|
# the main thread out of a poll() sleep, we simple write a
|
||||||
# single byte of data to the other end of the pipe.
|
# single byte of data to the other end of the pipe.
|
||||||
self.debug("Self pipe watch %d write %d" %(self.pipetrick[0], self.pipetrick[1]))
|
debug("Self pipe watch %d write %d" %(self.pipetrick[0], self.pipetrick[1]))
|
||||||
self.poll.register(self.pipetrick[0], select.POLLIN)
|
self.poll.register(self.pipetrick[0], select.POLLIN)
|
||||||
|
|
||||||
def debug(self, msg):
|
|
||||||
if self.debugOn:
|
|
||||||
print msg
|
|
||||||
|
|
||||||
|
|
||||||
# Calculate when the next timeout is due to occurr, returning
|
# Calculate when the next timeout is due to occurr, returning
|
||||||
# the absolute timestamp for the next timeout, or 0 if there is
|
# the absolute timestamp for the next timeout, or 0 if there is
|
||||||
@ -166,7 +167,7 @@ class virEventLoopPure:
|
|||||||
def run_once(self):
|
def run_once(self):
|
||||||
sleep = -1
|
sleep = -1
|
||||||
next = self.next_timeout()
|
next = self.next_timeout()
|
||||||
self.debug("Next timeout due at %d" % next)
|
debug("Next timeout due at %d" % next)
|
||||||
if next > 0:
|
if next > 0:
|
||||||
now = int(time.time() * 1000)
|
now = int(time.time() * 1000)
|
||||||
if now >= next:
|
if now >= next:
|
||||||
@ -174,7 +175,7 @@ class virEventLoopPure:
|
|||||||
else:
|
else:
|
||||||
sleep = (next - now) / 1000.0
|
sleep = (next - now) / 1000.0
|
||||||
|
|
||||||
self.debug("Poll with a sleep of %d" % sleep)
|
debug("Poll with a sleep of %d" % sleep)
|
||||||
events = self.poll.poll(sleep)
|
events = self.poll.poll(sleep)
|
||||||
|
|
||||||
# Dispatch any file handle events that occurred
|
# Dispatch any file handle events that occurred
|
||||||
@ -188,7 +189,7 @@ class virEventLoopPure:
|
|||||||
|
|
||||||
h = self.get_handle_by_fd(fd)
|
h = self.get_handle_by_fd(fd)
|
||||||
if h:
|
if h:
|
||||||
self.debug("Dispatch fd %d handle %d events %d" % (fd, h.get_id(), revents))
|
debug("Dispatch fd %d handle %d events %d" % (fd, h.get_id(), revents))
|
||||||
h.dispatch(self.events_from_poll(revents))
|
h.dispatch(self.events_from_poll(revents))
|
||||||
|
|
||||||
now = int(time.time() * 1000)
|
now = int(time.time() * 1000)
|
||||||
@ -201,7 +202,7 @@ class virEventLoopPure:
|
|||||||
# Deduct 20ms, since schedular timeslice
|
# Deduct 20ms, since schedular timeslice
|
||||||
# means we could be ever so slightly early
|
# means we could be ever so slightly early
|
||||||
if now >= (want-20):
|
if now >= (want-20):
|
||||||
self.debug("Dispatch timer %d now %s want %s" % (t.get_id(), str(now), str(want)))
|
debug("Dispatch timer %d now %s want %s" % (t.get_id(), str(now), str(want)))
|
||||||
t.set_last_fired(now)
|
t.set_last_fired(now)
|
||||||
t.dispatch()
|
t.dispatch()
|
||||||
|
|
||||||
@ -230,7 +231,7 @@ class virEventLoopPure:
|
|||||||
self.poll.register(fd, self.events_to_poll(events))
|
self.poll.register(fd, self.events_to_poll(events))
|
||||||
self.interrupt()
|
self.interrupt()
|
||||||
|
|
||||||
self.debug("Add handle %d fd %d events %d" % (handleID, fd, events))
|
debug("Add handle %d fd %d events %d" % (handleID, fd, events))
|
||||||
|
|
||||||
return handleID
|
return handleID
|
||||||
|
|
||||||
@ -247,7 +248,7 @@ class virEventLoopPure:
|
|||||||
self.timers.append(h)
|
self.timers.append(h)
|
||||||
self.interrupt()
|
self.interrupt()
|
||||||
|
|
||||||
self.debug("Add timer %d interval %d" % (timerID, interval))
|
debug("Add timer %d interval %d" % (timerID, interval))
|
||||||
|
|
||||||
return timerID
|
return timerID
|
||||||
|
|
||||||
@ -260,7 +261,7 @@ class virEventLoopPure:
|
|||||||
self.poll.register(h.get_fd(), self.events_to_poll(events))
|
self.poll.register(h.get_fd(), self.events_to_poll(events))
|
||||||
self.interrupt()
|
self.interrupt()
|
||||||
|
|
||||||
self.debug("Update handle %d fd %d events %d" % (handleID, h.get_fd(), events))
|
debug("Update handle %d fd %d events %d" % (handleID, h.get_fd(), events))
|
||||||
|
|
||||||
# Change the periodic frequency of the timer
|
# Change the periodic frequency of the timer
|
||||||
def update_timer(self, timerID, interval):
|
def update_timer(self, timerID, interval):
|
||||||
@ -269,7 +270,7 @@ class virEventLoopPure:
|
|||||||
h.set_interval(interval);
|
h.set_interval(interval);
|
||||||
self.interrupt()
|
self.interrupt()
|
||||||
|
|
||||||
self.debug("Update timer %d interval %d" % (timerID, interval))
|
debug("Update timer %d interval %d" % (timerID, interval))
|
||||||
break
|
break
|
||||||
|
|
||||||
# Stop monitoring for events on the file handle
|
# Stop monitoring for events on the file handle
|
||||||
@ -278,7 +279,7 @@ class virEventLoopPure:
|
|||||||
for h in self.handles:
|
for h in self.handles:
|
||||||
if h.get_id() == handleID:
|
if h.get_id() == handleID:
|
||||||
self.poll.unregister(h.get_fd())
|
self.poll.unregister(h.get_fd())
|
||||||
self.debug("Remove handle %d fd %d" % (handleID, h.get_fd()))
|
debug("Remove handle %d fd %d" % (handleID, h.get_fd()))
|
||||||
else:
|
else:
|
||||||
handles.append(h)
|
handles.append(h)
|
||||||
self.handles = handles
|
self.handles = handles
|
||||||
@ -290,7 +291,7 @@ class virEventLoopPure:
|
|||||||
for h in self.timers:
|
for h in self.timers:
|
||||||
if h.get_id() != timerID:
|
if h.get_id() != timerID:
|
||||||
timers.append(h)
|
timers.append(h)
|
||||||
self.debug("Remove timer %d" % timerID)
|
debug("Remove timer %d" % timerID)
|
||||||
self.timers = timers
|
self.timers = timers
|
||||||
self.interrupt()
|
self.interrupt()
|
||||||
|
|
||||||
@ -329,7 +330,7 @@ class virEventLoopPure:
|
|||||||
|
|
||||||
# This single global instance of the event loop wil be used for
|
# This single global instance of the event loop wil be used for
|
||||||
# monitoring libvirt events
|
# monitoring libvirt events
|
||||||
eventLoop = virEventLoopPure(debug=False)
|
eventLoop = virEventLoopPure()
|
||||||
|
|
||||||
# This keeps track of what thread is running the event loop,
|
# This keeps track of what thread is running the event loop,
|
||||||
# (if it is run in a background thread)
|
# (if it is run in a background thread)
|
||||||
|
Loading…
Reference in New Issue
Block a user