[pkg-fso-commits] [SCM] FSO frameworkd Debian packaging branch, master, updated. milestone3-274-gd9a0e57

Michael 'Mickey' Lauer mickey at vanille-media.de
Tue Nov 11 17:09:21 UTC 2008


The following commit has been merged in the master branch:
commit e8c255cd774e6a5fbdcc7cdb31fd7fce8dac1c96
Author: Michael 'Mickey' Lauer <mickey at vanille-media.de>
Date:   Sun Nov 2 14:53:00 2008 +0100

    use cProfile to profile and import http://www.gnome.org/~johan/lsprofcalltree.py,
    that way we can view our profile output in kCacheGrind

diff --git a/ChangeLog b/ChangeLog
index c35f7e7..86f8c71 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2008-11-02	Michael Lauer	<mickey at openmoko.org>
+
+	* python-hotshot is deprecated and give bogus results. Switch to cProfile and
+	  import http://www.gnome.org/~johan/lsprofcalltree.py, that way we can view
+	  the profile results in kcachegrind.
+
 2008-11-01	Michael Lauer	<mickey at openmoko.org>
 
 	* Add command line argument for profiling with the python-hotshot profiler
diff --git a/framework/frameworkd b/framework/frameworkd
index f9638ef..6af7645 100755
--- a/framework/frameworkd
+++ b/framework/frameworkd
@@ -8,7 +8,7 @@ The Open Device Daemon - Python Implementation
 GPLv2 or later
 """
 
-__version__ = "1.1.0"
+__version__ = "1.2.0"
 
 import sys, os
 from optparse import OptionParser
@@ -40,7 +40,7 @@ class TheOptionParser( OptionParser ):
         self.add_option( "-p", "--profile",
             metavar = "<filename>",
             dest = "profile",
-            help = "launch in profile mode (needs python-hotshot)",
+            help = "launch in profile mode (needs python-profile)",
             action = "store",
         )
 
@@ -52,9 +52,9 @@ if __name__ == "__main__":
 
     if options.values.profile:
         try:
-            import hotshot
+            import cProfile
         except ImportError:
-            print "python-hotshot not found. Can't profile"
+            print "Can't import cProfile; python-profile not installed? Can't profile."
             sys.exit( -1 )
         else:
             print "WARNING: profiling mode. profiling to %s" % options.values.profile
@@ -68,11 +68,18 @@ if __name__ == "__main__":
 
     try:
         if options.values.profile:
-            profile = hotshot.Profile( options.values.profile )
-            profile.runcall( c.launch )
+            p = cProfile.Profile()
+            p.run( "c.launch()" )
         else:
             c.launch()
     except KeyboardInterrupt:
         print >>sys.stderr, "ctrl-c: exiting."
         c.shutdown()
         del c
+
+    if options.values.profile:
+        import lsprofcalltree
+        k = lsprofcalltree.KCacheGrind(p)
+        data = open( options.values.profile, "w" )
+        k.output( data )
+        data.close()
diff --git a/framework/lsprofcalltree.py b/framework/lsprofcalltree.py
new file mode 100644
index 0000000..efedc5a
--- /dev/null
+++ b/framework/lsprofcalltree.py
@@ -0,0 +1,119 @@
+# lsprofcalltree.py: lsprof output which is readable by kcachegrind
+# David Allouche
+# Jp Calderone & Itamar Shtull-Trauring
+# Johan Dahlin
+
+import optparse
+import os
+import sys
+
+try:
+    import cProfile
+except ImportError:
+    raise SystemExit("This script requires cProfile from Python 2.5")
+
+def label(code):
+    if isinstance(code, str):
+        return ('~', 0, 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)
+
+def main(args):
+    usage = "%s [-o output_file_path] scriptfile [arg] ..."
+    parser = optparse.OptionParser(usage=usage % sys.argv[0])
+    parser.allow_interspersed_args = False
+    parser.add_option('-o', '--outfile', dest="outfile",
+                      help="Save stats to <outfile>", default=None)
+
+    if not sys.argv[1:]:
+        parser.print_usage()
+        sys.exit(2)
+
+    options, args = parser.parse_args()
+
+    if not options.outfile:
+        options.outfile = '%s.log' % os.path.basename(args[0])
+
+    sys.argv[:] = args
+
+    prof = cProfile.Profile()
+    try:
+        try:
+            prof = prof.run('execfile(%r)' % (sys.argv[0],))
+        except SystemExit:
+            pass
+    finally:
+        kg = KCacheGrind(prof)
+        kg.output(file(options.outfile, 'w'))
+
+if __name__ == '__main__':
+    sys.exit(main(sys.argv))

-- 
FSO frameworkd Debian packaging



More information about the pkg-fso-commits mailing list