OSDN Git Service

modify gclog to take a procFilter argument, as well as the debug argument
authorWei Huang <weih@google.com>
Wed, 16 Sep 2009 17:11:15 +0000 (10:11 -0700)
committerWei Huang <weih@google.com>
Wed, 16 Sep 2009 23:00:10 +0000 (16:00 -0700)
Change-Id: Id897031b76f41600b7665ae27521167c70092247

tools/gclog.py

index 4696965..4d5b704 100755 (executable)
@@ -15,8 +15,6 @@
 # limitations under the License.
 
 #
-# Version 1.0, 29-Apr-2009
-#
 # Parse event log output, looking for GC events.  Format them for human
 # consumption.
 #
 # The data is generated by dalvik/vm/alloc/HeapDebug.c.
 #
 
+import getopt
+import sys
 import os
 import re
 import time
 
+DEBUG = False       # DEBUG is a global variable
+
+
 def unfloat12(f12):
     """Unpack a float12 value"""
     if f12 < 0:
@@ -128,36 +131,41 @@ def parseExternalStats(value):
     return ( footprint, total, limit, bytes )
 
 
-def handleGcInfo(timestamp, pid, values):
+def handleGcInfo(procFilter, timestamp, pid, values):
     """Handle a single dvm_gc_info event"""
 
     pid = int(pid)
 
     global_info = parseGlobalInfo(values[0])
+
+    if len(procFilter) > 0:
+        if global_info[0] != procFilter:
+            return
+
     heap_stats = parseAggHeapStats(values[1])
     zygote = parseZygoteStats(values[2])
     external = parseExternalStats(values[3])
 
-    debug = False
-    if debug:
-        print "RAW: %s %s (%s,%s,%s,%s)" % \
-                (timestamp, pid, values[0], values[1], values[2], values[3])
+    print "%s %s(%d) softlim=%dKB, extlim=%dKB, extalloc=%dKB" % \
+            (timestamp, global_info[0], pid, zygote[0]/1024, external[2]/1024, external[3]/1024)
 
-        print "> id=\"%s\" time=%d freed=%d" % (global_info[0], global_info[1], global_info[2])
-        print ">  freed=%d foot=%d allow=%d objs=%d bytes=%d" % \
+    if DEBUG:
+        # print "RAW: %s %s (%s,%s,%s,%s)" % \
+        #        (timestamp, pid, values[0], values[1], values[2], values[3])
+
+        print "+ id=\"%s\" time=%d freed=%d" % (global_info[0], global_info[1], global_info[2])
+        print "+  freed=%d foot=%d allow=%d objs=%d bytes=%d" % \
                 (heap_stats[0], heap_stats[1], heap_stats[2], heap_stats[3], heap_stats[4])
-        print ">  soft=%d act=%d allow=%d objs=%d bytes=%d" % \
+        print "+  soft=%d act=%d allow=%d objs=%d bytes=%d" % \
                 (zygote[0], zygote[1], zygote[2], zygote[3], zygote[4])
-        print ">  foot=%d total=%d limit=%d alloc=%d" % \
+        print "+  foot=%d total=%d limit=%d alloc=%d" % \
                 (external[0], external[1], external[2], external[3])
 
-    print "%s %s(%d) softlim=%dKB, extlim=%dKB, extalloc=%dKB" % \
-            (timestamp, global_info[0], pid, zygote[0]/1024, external[2]/1024, external[3]/1024)
     print "  freed %d objects / %d bytes in %dms" % \
             (heap_stats[0], global_info[2], global_info[1])
 
 
-def filterInput(logPipe):
+def filterInput(logPipe, processFilter):
     """Loop until EOF, pulling out GC events"""
 
     # 04-29 20:31:00.334 I/dvm_gc_info(   69): [8320808730292729543,-8916699241518090181,-4006371297196337158,8165229]
@@ -182,18 +190,41 @@ def filterInput(logPipe):
             #print "no match on %s" % line.strip()
             continue
         else:
-            handleGcInfo(match.group(1), match.group(2), ( match.group(3), \
+            handleGcInfo(processFilter, match.group(1), match.group(2), ( match.group(3), \
                     match.group(4), match.group(5), match.group(6) ) )
 
+def PrintUsage():
+  print "usage: %s [-p procFilter] [-d]" % sys.argv[0]
+
+
 def start():
     """Entry point"""
 
+    global DEBUG
+
+    procFilter = ""
+
+    opts, args = getopt.getopt(sys.argv[1:], "hdp:")
+
+    for opt, val in opts:
+        if opt == "-h":
+            PrintUsage()
+            sys.exit(2)
+        elif opt == "-p":
+            procFilter = val
+        elif opt == "-d":
+            DEBUG = True
+
+    print "procfilter = %s" % procFilter
+    print "DEBUG = %s" % DEBUG
+
     # launch a logcat and read from it
     command = 'adb logcat -v time -b events'
     logPipe = os.popen(command)
 
+
     try:
-        filterInput(logPipe)
+        filterInput(logPipe, procFilter)
     except KeyboardInterrupt, err:
         print "Stopping on keyboard interrupt."