Created
June 30, 2014 02:28
-
-
Save aoriani/737b1307f12196128bb6 to your computer and use it in GitHub Desktop.
Parses GC entry in logcat to generate GC metrics for a process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import json | |
import re | |
import sys | |
#TODO use raw strings | |
DALVIK_VM_LINE = re.compile("^.*/dalvikvm\(\s*(\d+)\)\s*:\s*(.*)$") | |
DALVIK_HEAP_GROW_LINE = re.compile("^.*/dalvikvm-heap\(\s*(\d+)\).*Grow\sheap.*$") | |
class GCStats: | |
GC_CONCURRENT_MSG = re.compile("^GC_CONCURRENT\s.*paused\s(\d+)ms\+(\d+)ms,\stotal\s(\d+)ms.*$") | |
GC_EXPLICT_MSG = re.compile("^GC_EXPLICIT\s.*paused\s(\d+)ms\+(\d+)ms,\stotal\s(\d+)ms.*$") | |
GC_ALLOC_MSG = re.compile("^GC_FOR_ALLOC\s.*paused\s(\d+)ms,\stotal\s(\d+)ms.*$") | |
class GCMetric: | |
def __init__(self): | |
self.events = 0 | |
self.pausedTime = 0 | |
self.totalTime = 0 | |
def add(self, paused, total): | |
self.events += 1 | |
self.pausedTime += paused | |
self.totalTime += total | |
def __init__(self): | |
self.concurrent = self.GCMetric() | |
self.explicit = self.GCMetric() | |
self.alloc = self.GCMetric() | |
self.heapGrow = 0 | |
def addHeapGrow(self): | |
self.heapGrow += 1 | |
def parseGCMessage(self, msg): | |
match = GCStats.GC_CONCURRENT_MSG.match(msg) | |
if match: | |
self.concurrent.add(int(match.group(1)) + int(match.group(2)), int(match.group(3))) | |
match = GCStats.GC_EXPLICT_MSG.match(msg) | |
if match: | |
self.explicit.add(int(match.group(1)) + int(match.group(2)), int(match.group(3))) | |
match = GCStats.GC_ALLOC_MSG.match(msg) | |
if match: | |
self.alloc.add(int(match.group(1)), int(match.group(2))) | |
def getGCMessage(pid, line): | |
match = DALVIK_VM_LINE.match(line) | |
if match: | |
linePid = int(match.group(1)) | |
if linePid == pid: | |
return match.group(2) | |
return None | |
def isHeapGrow(pid, line): | |
match = DALVIK_HEAP_GROW_LINE.match(line) | |
return match and (int(match.group(1)) == pid) | |
if __name__ == "__main__": | |
if len(sys.argv) == 3: | |
pid = int(sys.argv[1]) | |
logcatFile = sys.argv[2] | |
gcStats = GCStats() | |
with open(logcatFile) as logcat: | |
for line in logcat: | |
if isHeapGrow(pid, line): | |
gcStats.addHeapGrow() | |
msg = getGCMessage(pid, line) | |
if msg: | |
gcStats.parseGCMessage(msg) | |
print(json.dumps(vars(gcStats), default=lambda o: o.__dict__, indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment