def show_results(mallocs, node_filters, sorter, group_by, symbol_resolver, src_addr_formatter=debug.SourceAddress.__str__, max_levels=None, show_backtrace=True, printer=prof.default_printer): root = tree.TreeNode(TreeKey('All', None)) lost = 0 unfreed = 0 for buf in mallocs: for desc in mallocs[buf]: node = root for gr in group_by: node = node.get_or_add(groups[gr](desc)) if desc.backtrace and show_backtrace: frames = list( debug.resolve_all(symbol_resolver, (addr - 1 for addr in desc.backtrace))) frames = prof.strip_garbage(frames) frames = strip_malloc(frames) level = max_levels bt = node for frame in frames: src_addr = src_addr_formatter(frame) bt = bt.get_or_add(TreeBacktrace(src_addr, node)) bt.key.hit() if max_levels: level -= 1 if not level: break node.key.alloc += 1 node.key.total_bytes += desc.alloc_len node.key.lost_bytes += desc.alloc_len - desc.req_len def flatten(node): for child in node.children: flatten(child) if node.has_only_one_child(): node.key.backtrace += '\n' + next(node.children).key.backtrace node.remove_all() def propagate(parent, node): if isinstance(node.key, TreeBacktrace): flatten(node) return for child in node.children: propagate(node, child) if parent: parent.key.alloc += node.key.alloc parent.key.total_bytes += node.key.total_bytes parent.key.lost_bytes += node.key.lost_bytes propagate(None, root) def formatter(node): return node.key.__str__() def node_filter(*args): for filter in node_filters: if not list(filter(*args)): return False return True def order_by(node): if isinstance(node.key, TreeBacktrace): return -node.key.count return sorters[sorter](node) tree.print_tree(root, formatter, printer=printer, order_by=order_by, node_filter=node_filter)
def show_results(mallocs, node_filters, sorter, group_by, symbol_resolver, src_addr_formatter=debug.SourceAddress.__str__, max_levels=None, show_backtrace=True, printer=prof.default_printer): root = tree.TreeNode(TreeKey('All', None)) lost = 0 unfreed = 0 for buf in mallocs: for desc in mallocs[buf]: node = root for gr in group_by: node = node.get_or_add(groups[gr](desc)) if desc.backtrace and show_backtrace: frames = list(debug.resolve_all(symbol_resolver, (addr - 1 for addr in desc.backtrace))) frames = prof.strip_garbage(frames) frames = strip_malloc(frames) level = max_levels bt = node for frame in frames: src_addr = src_addr_formatter(frame) bt = bt.get_or_add(TreeBacktrace(src_addr, node)) bt.key.hit() if max_levels: level -= 1 if not level: break node.key.alloc += 1 node.key.total_bytes += desc.alloc_len node.key.lost_bytes += desc.alloc_len - desc.req_len def flatten(node): for child in node.children: flatten(child) if node.has_only_one_child(): node.key.backtrace += '\n' + next(node.children).key.backtrace node.remove_all() def propagate(parent, node): if isinstance(node.key, TreeBacktrace): flatten(node) return for child in node.children: propagate(node, child) if parent: parent.key.alloc += node.key.alloc parent.key.total_bytes += node.key.total_bytes parent.key.lost_bytes += node.key.lost_bytes propagate(None, root) def formatter(node): return node.key.__str__() def node_filter(*args): for filter in node_filters: if not filter(*args): return False return True def order_by(node): if isinstance(node.key, TreeBacktrace): return -node.key.count return sorters[sorter](node) tree.print_tree(root, formatter, printer=printer, order_by=order_by, node_filter=node_filter)