| 1 | from __future__ import print_function |
| 2 | |
| 3 | from rpython.translator.simplify import get_graph |
| 4 | from hashlib import md5 |
| 5 | |
| 6 | def get_statistics(graph, translator, save_per_graph_details=None, ignore_stack_checks=False): |
| 7 | seen_graphs = {} |
| 8 | stack = [graph] |
| 9 | num_graphs = 0 |
| 10 | num_blocks = 0 |
| 11 | num_ops = 0 |
| 12 | num_mallocs = 0 |
| 13 | per_graph = {} |
| 14 | while stack: |
| 15 | graph = stack.pop() |
| 16 | if graph in seen_graphs: |
| 17 | continue |
| 18 | seen_graphs[graph] = True |
| 19 | num_graphs += 1 |
| 20 | old_num_blocks = num_blocks |
| 21 | old_num_ops = num_ops |
| 22 | old_num_mallocs = num_mallocs |
| 23 | for block in graph.iterblocks(): |
| 24 | num_blocks += 1 |
| 25 | for op in block.operations: |
| 26 | if op.opname == "direct_call": |
| 27 | called_graph = get_graph(op.args[0], translator) |
| 28 | if called_graph is not None and ignore_stack_checks: |
| 29 | if called_graph.name.startswith('ll_stack_check'): |
| 30 | continue |
| 31 | if called_graph is not None: |
| 32 | stack.append(called_graph) |
| 33 | elif op.opname == "indirect_call": |
| 34 | called_graphs = op.args[-1].value |
| 35 | if called_graphs is not None: |
| 36 | stack.extend(called_graphs) |
| 37 | elif op.opname.startswith("malloc"): |
| 38 | num_mallocs += 1 |
| 39 | num_ops += 1 |
| 40 | per_graph[graph] = (num_blocks-old_num_blocks, num_ops-old_num_ops, num_mallocs-old_num_mallocs) |
| 41 | if save_per_graph_details: |
| 42 | details = [] |
| 43 | for graph, (nblocks, nops, nmallocs) in per_graph.iteritems(): |
| 44 | try: |
| 45 | code = graph.func.__code__.co_code |
| 46 | except AttributeError: |
| 47 | code = "None" |
| 48 | hash = md5(code).hexdigest() |
| 49 | details.append((hash, graph.name, nblocks, nops, nmallocs)) |
| 50 | details.sort() |
| 51 | f = open(save_per_graph_details, "w") |
| 52 | try: |
| 53 | for hash, name, nblocks, nops, nmallocs in details: |
| 54 | print(hash, name, nblocks, nops, nmallocs, file=f) |
| 55 | finally: |
| 56 | f.close() |
| 57 | return num_graphs, num_blocks, num_ops, num_mallocs |
| 58 | |
| 59 | def print_statistics(graph, translator, save_per_graph_details=None, ignore_stack_checks=False): |
| 60 | num_graphs, num_blocks, num_ops, num_mallocs = get_statistics( |
| 61 | graph, translator, save_per_graph_details, |
| 62 | ignore_stack_checks=ignore_stack_checks) |
| 63 | print(("Statistics:\nnumber of graphs %s\n" |
| 64 | "number of blocks %s\n" |
| 65 | "number of operations %s\n" |
| 66 | "number of mallocs %s\n" |
| 67 | ) % (num_graphs, num_blocks, num_ops, num_mallocs)) |