import sys, re, collections # malloc_history -callTree: per line " COUNT (SIZE) FUNC (in LIB)". For each allocation leaf path we only have # subtree totals; attribute each subtree to the deepest named raea frame whose own children are all non-raea (alloc libs). size_re = re.compile(r'^([ +!:|]*)(\d+) \(([\d.]+)([KMGB]?)\) (.*?) \(in ([^)]*)\)') mult = {'': 1, 'B': 1, 'K': 1e3, 'M': 1e6, 'G': 1e9} def short(f): ids = re.findall(r'\d+([A-Za-z_][A-Za-z0-9_]{2,})', f) ids = [i for i in ids if not re.match(r'^(Cs|Nt|Nv|Ns|Ny|NC|Ms|Ib|IN|B[0-9])', i)] return '::'.join(ids[-3:]) if ids else f[:80] nodes = [] # (depth, bytes, func, lib) for line in open(sys.argv[1], errors='replace'): m = size_re.match(line) if not m: continue nodes.append((len(m.group(1)), float(m.group(3)) * mult[m.group(4)], m.group(5), m.group(6))) # "self in raea" view: for each raea node, bytes minus bytes of raea children = allocated by it via non-raea callees by = collections.Counter() stack = [] for i, (d, b, f, lib) in enumerate(nodes): while stack and stack[-1][0] >= d: stack.pop() stack.append((d, b, f, lib)) # second pass: compute children sums of raea nodes children = collections.defaultdict(float) st = [] for i, (d, b, f, lib) in enumerate(nodes): while st and nodes[st[-1]][0] >= d: st.pop() if lib == 'raea': # nearest raea ancestor for j in reversed(st): if nodes[j][3] == 'raea': children[j] += b; break st.append(i) want = sys.argv[2] if len(sys.argv) > 2 else None for i, (d, b, f, lib) in enumerate(nodes): if lib == 'raea': own = b - children[i] if own > 0: by[short(f)] += own for f, b in by.most_common(40): print(f"{b/1e6:8.0f} MB {f}")