Commit 547cde90 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 993ef469
...@@ -78,12 +78,18 @@ import thread, threading ...@@ -78,12 +78,18 @@ import thread, threading
# See top-level module description for details. # See top-level module description for details.
class Tracer(object): class Tracer(object):
def __init__(self, ftrace): def __init__(self, ftracetx, ftracerx):
self.ftrace = ftrace # file object to communicate with tracer self.ftx = ftracetx # file object to send data to tracer
self.txlock = threading.Lock() # serializes writes to .ftrace self.txlock = threading.Lock() # serializes writes to .ftx
self.frx = ftracerx # file object to receive data from tracer
self.rxtab = {} # {} tid -> RxQueue self.rxtab = {} # {} tid -> RxQueue
self.rxlock = threading.Lock() # serializes access to .rxtab self.rxlock = threading.Lock() # serializes access to .rxtab
# NOTE 2 separate ftx/frx file objects are used because python for
# stdio objects does locking and this way if it would be only 1 file
# object readline in _serve_recv() would block tx until readline wakes up.
self.trecv = threading.Thread(target=self._serve_recv) self.trecv = threading.Thread(target=self._serve_recv)
self.trecv.daemon = True # XXX better to gracefully stop it? self.trecv.daemon = True # XXX better to gracefully stop it?
self.trecv.start() self.trecv.start()
...@@ -95,18 +101,18 @@ class Tracer(object): ...@@ -95,18 +101,18 @@ class Tracer(object):
tid = thread.get_ident() tid = thread.get_ident()
self.txlock.acquire() self.txlock.acquire()
try: try:
self.ftrace.write(('%d ' % tid) + line + '\n') self.ftx.write(('%d ' % tid) + line + '\n')
self.ftrace.flush() self.ftx.flush()
finally: finally:
self.txlock.release() self.txlock.release()
# _serve_recv receives lines from .ftrace and multiplexes them to # _serve_recv receives lines from .frx and multiplexes them to
# destination threads RX queues. # destination threads RX queues.
# #
# runs in a dedicated thread. # runs in a dedicated thread.
def _serve_recv(self): def _serve_recv(self):
while 1: while 1:
line = self.ftrace.readline() line = self.frx.readline()
# tid SP rest \n # tid SP rest \n
tid, line = line.split(None, 1) tid, line = line.split(None, 1)
line = line.rstrip('\n') line = line.rstrip('\n')
...@@ -240,8 +246,9 @@ def main(): ...@@ -240,8 +246,9 @@ def main():
global gtracer global gtracer
fdtrace = argv[0] fdtrace = argv[0]
fdtrace = int(fdtrace) fdtrace = int(fdtrace)
ftrace = os.fdopen(fdtrace, 'r+') ttx = os.fdopen(fdtrace, 'w')
gtracer = Tracer(ftrace) trx = os.fdopen(fdtrace, 'r')
gtracer = Tracer(ttx, trx)
argv = argv[1:] argv = argv[1:]
# forbid fork (see top-level description about why) # forbid fork (see top-level description about why)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment