Source

hack2 / codeviewer / dump.py

Full commit

import json
from pypy.translator.goal import richards
from lib_pypy.disassembler import dis
from pypy.tool.jitlogparser.module_finder import gather_all_code_objs
from pypy.tool.jitlogparser.parser import import_log, parse_log_counts,\
     parse_code_data
from pypy.tool.logparser import extract_category


def load_code(fname):
    res = gather_all_code_objs(fname)
    return res

def line_from_code_spec(data):
    name, _, filename, startline, bytecode_no = parse_code_data(data)
    if filename is not None:
        code = codes[(startline, name)]
        return dis(code).map[bytecode_no].lineno
    return 0

richards_file = richards.__file__
if richards_file.endswith('c'):
    richards_file = richards_file[:-1]
codes = gather_all_code_objs(richards_file)

def count_lines(f):
    lines = open(richards_file).readlines()
    i = len(lines) - 1
    while not lines[i]:
        i -= 1
    return i + 1

def f():
    #lines = {}
    #for code in codes.values():
    #    for opcode in dis(code).opcodes:
    #        lines[opcode.lineno] += 1
    #        lines.setdefault(opcode.lineno, []).append(str(opcode))

    log, loops = import_log('/home/fijal/src/pypy/pypy/translator/goal/log')
    parse_log_counts(extract_category(log, 'jit-backend-count'), loops)
    glob_lines = {}
    glob_traces = {}
    for loop in loops:
        lines_per_loop = {} # make sure we don't count it many times per opcode
        traces_per_loop = {}
        lineno = line_from_code_spec(loop.comment)
        if lineno:
            lines_per_loop[lineno] = loop.count
            traces_per_loop[lineno] = loop.comment
        for op in loop.operations:
            if op.name == 'debug_merge_point':
                lineno = line_from_code_spec(op.args[2])
                if lineno:
                    lines_per_loop[lineno] = loop.count
                    traces_per_loop[lineno] = loop.comment
        for item, val in lines_per_loop.iteritems():
            if item not in glob_lines:
                glob_lines[item] = val
                glob_traces[item] = [traces_per_loop[item]]
            else:
                glob_lines[item] += val
                glob_traces[item].append(traces_per_loop[item])

    data = {'lines': glob_lines,
            'traces': glob_traces,
            'count': count_lines(richards_file),
            'max_value': max(glob_lines.itervalues())}
    json.dump(data, open("info.json", "w"))


f()