From mboxrd@z Thu Jan 1 00:00:00 1970 From: Magnus Carlsson Subject: [PATCH] Fix that makes xend tracing thread safe Date: Thu, 29 Mar 2007 14:52:52 -0700 Message-ID: <460C3534.8020002@galois.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------060704000105010506050606" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org This is a multi-part message in MIME format. --------------060704000105010506050606 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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 --------------060704000105010506050606 Content-Type: text/x-patch; name="SrvDaemon.py-tracing-thread-safe.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="SrvDaemon.py-tracing-thread-safe.patch" # HG changeset patch # User Magnus Carlsson # Date 1175203666 25200 # Node ID c51f65166e062eee00bc99424bbe56f547f3dcfa # Parent ffb9dda429461442b0e727775a810b352ec74c9c Made tracing thread safe, and also print thread name. diff -r ffb9dda42946 -r c51f65166e06 tools/python/xen/xend/server/SrvDaemon.py --- a/tools/python/xen/xend/server/SrvDaemon.py Wed Mar 28 16:52:05 2007 +0100 +++ b/tools/python/xen/xend/server/SrvDaemon.py Thu Mar 29 14:27:46 2007 -0700 @@ -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. --------------060704000105010506050606 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --------------060704000105010506050606--