Skip to content

Instantly share code, notes, and snippets.

@t3rmin4t0r
Created July 20, 2018 21:02
Show Gist options
  • Save t3rmin4t0r/7f60f54851125167613fb4c63ee1145f to your computer and use it in GitHub Desktop.
Save t3rmin4t0r/7f60f54851125167613fb4c63ee1145f to your computer and use it in GitHub Desktop.
Jstack collapsing script
import sys, re, os, math, os.path
from collections import defaultdict
class JStackTrie(object):
def __init__(self):
self.roots = defaultdict(JStackTrie)
self.count = 0
self.name = "root"
def merge(self, calls):
self.count += 1
s = self.roots[calls[0]]
s.name = calls[0]
if len(calls) > 1:
s.merge(calls[1:])
def tree(self, level=0):
v = sorted([ (self.roots[k].count, k, self.roots[k]) for k in self.roots])
v.reverse()
for (_,n, r) in v:
print " "*level, n, r.count
r.tree(level+1)
class JStackSample(object):
def __init__(self, lines):
PATTERN = re.compile("at ([^\(]*)")
calls = map(lambda x : x.group(1), filter(lambda a: a, [PATTERN.search(l) for l in lines]))
calls.reverse()
self.calls = calls
class JStack(object):
def __init__(self, f):
samples = []
lines = []
for l in open(f):
lines.append(l)
if (l.strip() == '--'):
samples.append(JStackSample(lines))
lines = []
self.samples = samples
def __repr__(self):
return "JStack <%d samples>" % len(self.samples)
def main(args):
stacks = [JStack(f) for f in args]
merged = JStackTrie()
for st in stacks:
for sa in st.samples:
merged.merge(sa.calls)
merged.tree()
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment