[PATCH 3 of 4] profiling: Adding support for kcachegrind, using lsprofcalltree
Nicolas Dumazet
nicdumz at gmail.com
Thu Apr 2 09:31:46 UTC 2009
# HG changeset patch
# User Nicolas Dumazet <nicdumz.commits <at> gmail.com>
# Date 1237829437 -32400
# Node ID d008597b912ba658d2044f2cac9b80777a914b98
# Parent 682b8c4edf4cbf285a09c483a0cc48df76c76003
profiling: Adding support for kcachegrind, using lsprofcalltree
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -382,7 +382,7 @@
format = ui.config('profiling', 'format')
output = ui.config('profiling', 'output')
- if format and not format in ['hotshot', 'lsprof']:
+ if format and not format in ['hotshot', 'lsprof', 'kcachegrind']:
ui.warning(_("Unknown profiling format '%s'"
" - Ignored\n") % format)
format = None
@@ -421,7 +421,8 @@
stats.dump_stats(output)
else:
stats.print_stats(40)
- elif format == 'lsprof':
+ else:
+ # kcachegrind or lsprof
try:
from mercurial import lsprof
except ImportError:
@@ -434,13 +435,21 @@
return checkargs()
finally:
p.disable()
- stats = lsprof.Stats(p.getstats())
- stats.sort()
+
if output:
ostream = open(output, 'wb')
else:
ostream = sys.stderr
- stats.pprint(top=10, file=ostream, climit=5)
+
+ if format == 'kcachegrind':
+ import lsprofcalltree
+ calltree = lsprofcalltree.KCacheGrind(p)
+ calltree.output(ostream)
+ else:
+ # lsprof
+ stats = lsprof.Stats(p.getstats())
+ stats.sort()
+ stats.pprint(top=10, file=ostream, climit=5)
if output:
ostream.close()
diff --git a/mercurial/lsprofcalltree.py b/mercurial/lsprofcalltree.py
new file mode 100644
--- /dev/null
+++ b/mercurial/lsprofcalltree.py
@@ -0,0 +1,90 @@
+"""
+lsprofcalltree.py - lsprof output which is readable by kcachegrind
+
+Authors:
+ * David Allouche <david <at> allouche.net>
+ * Jp Calderone & Itamar Shtull-Trauring
+ * Johan Dahlin
+
+This software may be used and distributed according to the terms
+of the GNU General Public License, incorporated herein by reference.
+"""
+
+import optparse
+import os
+import sys
+
+def label(code):
+ if isinstance(code, str):
+ return '~' + code # built-in functions ('~' sorts at the end)
+ else:
+ return '%s %s:%d' % (code.co_name,
+ code.co_filename,
+ code.co_firstlineno)
+
+class KCacheGrind(object):
+ def __init__(self, profiler):
+ self.data = profiler.getstats()
+ self.out_file = None
+
+ def output(self, out_file):
+ self.out_file = out_file
+ print >> out_file, 'events: Ticks'
+ self._print_summary()
+ for entry in self.data:
+ self._entry(entry)
+
+ def _print_summary(self):
+ max_cost = 0
+ for entry in self.data:
+ totaltime = int(entry.totaltime * 1000)
+ max_cost = max(max_cost, totaltime)
+ print >> self.out_file, 'summary: %d' % (max_cost,)
+
+ def _entry(self, entry):
+ out_file = self.out_file
+
+ code = entry.code
+ #print >> out_file, 'ob=%s' % (code.co_filename,)
+ if isinstance(code, str):
+ print >> out_file, 'fi=~'
+ else:
+ print >> out_file, 'fi=%s' % (code.co_filename,)
+ print >> out_file, 'fn=%s' % (label(code),)
+
+ inlinetime = int(entry.inlinetime * 1000)
+ if isinstance(code, str):
+ print >> out_file, '0 ', inlinetime
+ else:
+ print >> out_file, '%d %d' % (code.co_firstlineno, inlinetime)
+
+ # recursive calls are counted in entry.calls
+ if entry.calls:
+ calls = entry.calls
+ else:
+ calls = []
+
+ if isinstance(code, str):
+ lineno = 0
+ else:
+ lineno = code.co_firstlineno
+
+ for subentry in calls:
+ self._subentry(lineno, subentry)
+ print >> out_file
+
+ def _subentry(self, lineno, subentry):
+ out_file = self.out_file
+ code = subentry.code
+ #print >> out_file, 'cob=%s' % (code.co_filename,)
+ print >> out_file, 'cfn=%s' % (label(code),)
+ if isinstance(code, str):
+ print >> out_file, 'cfi=~'
+ print >> out_file, 'calls=%d 0' % (subentry.callcount,)
+ else:
+ print >> out_file, 'cfi=%s' % (code.co_filename,)
+ print >> out_file, 'calls=%d %d' % (
+ subentry.callcount, code.co_firstlineno)
+
+ totaltime = int(subentry.totaltime * 1000)
+ print >> out_file, '%d %d' % (lineno, totaltime)
More information about the Mercurial-devel
mailing list