Skip to contentSkip to conversation

Fix that makes xend tracing thread safe

PATCH Applied Magnus Carlsson 2 participants 2 messages Mar 29, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix that makes xend tracing thread safe

Currently, the output in xend.trace is sometimes garbled since multiple threads write to it simultaneously. The following patch fixes that.

It also adds an initial field with the thread name on each traced line, which makes it easier to follow the trace. (If this is considered a new feature which shouldn't be included due to the feature freeze, it can be disabled by removing the first line in print_trace.)

Cheers,
Magnus

# HG changeset patch
# User Magnus Carlsson <[email protected]>
# Date 1175203666 25200
# Node ID c51f65166e062eee00bc99424bbe56f547f3dcfa # Parent ffb9dda429461442b0e727775a810b352ec74c9c Made tracing thread safe, and also print thread name.

tools/python/xen/xend/server/SrvDaemon.py +50 -44
diff -r ffb9dda42946 -r c51f65166e06 tools/python/xen/xend/server/SrvDaemon.py
@@ -38,7 +38,8 @@ class Daemon:
         self.traceon = False
         self.tracefile = None
         self.traceindent = 0
-        self.child = 0 
+        self.child = 0
+        self.traceLock = threading.Lock()
 
 
     def cleanup_xend(self, kill):
@@ -253,6 +254,7 @@ class Daemon:
                 pass
 
     def print_trace(self, string):
+        self.tracefile.write("%s: "% threading.currentThread().getName())
         for i in range(self.traceindent):
             ch = " "
             if (i % 5):
@@ -263,50 +265,54 @@ class Daemon:
         self.tracefile.write(string)
             
     def trace(self, frame, event, arg):
-        if not self.traceon:
-            print >>self.tracefile
-            print >>self.tracefile, '-' * 20, 'TRACE OFF', '-' * 20
-            self.tracefile.close()
-            self.tracefile = None
-            return None
-        if event == 'call':
-            code = frame.f_code
-            filename = code.co_filename
-            m = re.search('.*xend/(.*)', filename)
-            if not m:
+        self.traceLock.acquire()
+        try:
+            if not self.traceon:
+                print >>self.tracefile
+                print >>self.tracefile, '-' * 20, 'TRACE OFF', '-' * 20
+                self.tracefile.close()
+                self.tracefile = None
                 return None
-            modulename = m.group(1)
-            if modulename.endswith('.pyc'):
-                modulename = modulename[:-1]
-            if modulename == 'sxp.py' or \
-               modulename == 'XendLogging.py' or \
-               modulename == 'XendMonitor.py' or \
-               modulename == 'server/SrvServer.py':
-                return None
-            self.traceindent += 1
-            self.print_trace("> %s:%s\n"
-                             % (modulename, code.co_name))
-        elif event == 'line':
-            filename = frame.f_code.co_filename
-            lineno = frame.f_lineno
-            self.print_trace("%4d %s" %
-                             (lineno, linecache.getline(filename, lineno)))
-        elif event == 'return':
-            code = frame.f_code
-            filename = code.co_filename
-            m = re.search('.*xend/(.*)', filename)
-            if not m:
-                return None
-            modulename = m.group(1)
-            self.print_trace("< %s:%s\n"
-                             % (modulename, code.co_name))
-            self.traceindent -= 1
-        elif event == 'exception':
-            self.print_trace("! Exception:\n")
-            (ex, val, tb) = arg
-            traceback.print_exception(ex, val, tb, 10, self.tracefile)
-            #del tb
-        return self.trace
+            if event == 'call':
+                code = frame.f_code
+                filename = code.co_filename
+                m = re.search('.*xend/(.*)', filename)
+                if not m:
+                    return None
+                modulename = m.group(1)
+                if modulename.endswith('.pyc'):
+                    modulename = modulename[:-1]
+                if modulename == 'sxp.py' or \
+                   modulename == 'XendLogging.py' or \
+                   modulename == 'XendMonitor.py' or \
+                   modulename == 'server/SrvServer.py':
+                    return None
+                self.traceindent += 1
+                self.print_trace("> %s:%s\n"
+                                 % (modulename, code.co_name))
+            elif event == 'line':
+                filename = frame.f_code.co_filename
+                lineno = frame.f_lineno
+                self.print_trace("%4d %s" %
+                                 (lineno, linecache.getline(filename, lineno)))
+            elif event == 'return':
+                code = frame.f_code
+                filename = code.co_filename
+                m = re.search('.*xend/(.*)', filename)
+                if not m:
+                    return None
+                modulename = m.group(1)
+                self.print_trace("< %s:%s\n"
+                                 % (modulename, code.co_name))
+                self.traceindent -= 1
+            elif event == 'exception':
+                self.print_trace("! Exception:\n")
+                (ex, val, tb) = arg
+                traceback.print_exception(ex, val, tb, 10, self.tracefile)
+                #del tb
+            return self.trace
+        finally:
+            self.traceLock.release()
 
     def set_user(self):
         # Set the UID.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

Applied
#1 lore raw
Quoted 7 lines · Magnus Carlsson

Currently, the output in xend.trace is sometimes garbled since multiple threads write to it simultaneously. The following patch fixes that.

It also adds an initial field with the thread name on each traced line, which makes it easier to follow the trace. (If this is considered a new feature which shouldn't be included due to the feature freeze, it can be disabled by removing the first line in print_trace.)

Applied, thanks Magnus.

Ewan.