PageRenderTime 32ms CodeModel.GetById 16ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Tools/scripts/analyze_dxp.py

http://unladen-swallow.googlecode.com/
Python | 165 lines | 143 code | 2 blank | 20 comment | 0 complexity | df24caa7c811b61ffcdb8ab626942d4c MD5 | raw file
  1"""
  2Some helper functions to analyze the output of sys.getdxp() (which is
  3only available if Python was built with -DDYNAMIC_EXECUTION_PROFILE).
  4These will tell you which opcodes have been executed most frequently
  5in the current process, and, if Python was also built with -DDXPAIRS,
  6will tell you which instruction _pairs_ were executed most frequently,
  7which helps with defining superinstructions.
  8
  9If Python was built without -DDYNAMIC_EXECUTION_PROFILE, importing
 10this module will raise a RuntimeError.
 11
 12If you're running a script you want to profile, a simple way to get
 13the common pairs is:
 14
 15$ PYTHONPATH=$PYTHONPATH:<python_srcdir>/Tools/scripts \
 16./python -i -O the_script.py --args
 17...
 18> from analyze_dxp import *
 19> s = RenderCommonPairs()
 20> open('/tmp/some_file', 'w').write(s)
 21"""
 22
 23import copy
 24import opcode
 25import operator
 26import sys
 27import threading
 28import warnings
 29
 30if not hasattr(sys, "getdxp"):
 31    raise RuntimeError("Can't import analyze_dxp: Python built without"
 32                       " -DDYNAMIC_EXECUTION_PROFILE.")
 33
 34
 35_profile_lock = threading.RLock()
 36_cumulative_profile = sys.getdxp()
 37
 38# If Python was built with -DDXPAIRS, sys.getdxp() returns a list of
 39# lists of ints.  Otherwise it returns just a list of ints.
 40def HasPairs(profile):
 41    """Returns True if the Python that produced the argument profile
 42    was built with -DDXPAIRS."""
 43
 44    return len(profile) > 0 and isinstance(profile[0], list)
 45
 46
 47def ResetProfile():
 48    """Forgets any execution profile that has been gathered so far."""
 49    with _profile_lock:
 50        sys.getdxp()  # Resets the internal profile
 51        _cumulative_profile = sys.getdxp()  # 0s out our copy.
 52
 53
 54def MergeProfile():
 55    """Reads sys.getdxp() and merges it into this module's cached copy.
 56
 57    We need this because sys.getdxp() 0s itself every time it's called."""
 58
 59    with _profile_lock:
 60        new_profile = sys.getdxp()
 61        if HasPairs(new_profile):
 62            for first_inst in range(len(_cumulative_profile)):
 63                for second_inst in range(len(_cumulative_profile[first_inst])):
 64                    _cumulative_profile[first_inst][second_inst] += (
 65                        new_profile[first_inst][second_inst])
 66        else:
 67            for inst in range(len(_cumulative_profile)):
 68                _cumulative_profile[inst] += new_profile[inst]
 69
 70
 71def SnapshotProfile():
 72    """Returns an the cumulative execution profile until this call."""
 73    with _profile_lock:
 74        MergeProfile()
 75        return copy.deepcopy(_cumulative_profile)
 76
 77
 78def _Opname(code):
 79    """Returns opcode.opname[code] or <unknown ###> for unknown opcodes."""
 80    if code < len(opcode.opname):
 81        return opcode.opname[code]
 82    else:
 83        return "<unknown %s>" % code
 84
 85
 86def CommonInstructions(profile):
 87    """Returns the most common opcodes in order of descending frequency.
 88
 89    The result is a list of tuples of the form
 90      (opcode, opname, # of occurrences)
 91
 92    """
 93    if HasPairs(profile) and profile:
 94        inst_list = profile[-1]
 95    else:
 96        inst_list = profile
 97    return sorted(((op, _Opname(op), count)
 98                   for op, count in enumerate(inst_list)
 99                   if count > 0),
100                  key=operator.itemgetter(2),
101                  reverse=True)
102
103
104def CommonPairs(profile):
105    """Returns the most common opcode pairs in order of descending frequency.
106
107    The result is a list of tuples of the form
108      ((1st opcode, 2nd opcode),
109       (1st opname, 2nd opname),
110       # of occurrences of the pair)
111
112    """
113    if not HasPairs(profile):
114        return []
115    result = [((op1, op2), (_Opname(op1), _Opname(op2)), count)
116              # Drop the row of single-op profiles with [:-1]
117              for op1, op1profile in enumerate(profile[:-1])
118              for op2, count in enumerate(op1profile)
119              if count > 0]
120    # Add in superinstructions, which are made up of at least pairs of
121    # ordinary instructions.  Include all superinstructions, even if
122    # their count is 0, to identify superinstructions we should
123    # delete.
124    result.extend(((op,), (_Opname(op),), count)
125                  for op, count in enumerate(profile[-1])
126                  if op in opcode.super2prim)
127    result.sort(key=operator.itemgetter(2), reverse=True)
128    return result
129
130
131def RenderCommonPairs(profile=None):
132    """Renders the most common opcode pairs to a string in order of
133    descending frequency.
134
135    The result is a series of lines of the form:
136      # of occurrences: ('1st opname', '2nd opname')
137    or, if the line represents a lone superinstruction:
138      # of occurrences: ('super-name',)
139
140    """
141    if profile is None:
142        profile = SnapshotProfile()
143    def seq():
144        for _, ops, count in CommonPairs(profile):
145            yield "%s: %s\n" % (count, ops)
146    return ''.join(seq())
147
148
149def CommonSequences(profile):
150    """Decompiles the superinstructions in the common pairs to
151    identify longer common sequences."""
152    if not HasPairs(profile):
153        return []
154    result = []
155    for ops, _, count in CommonPairs(profile):
156        supers = list(ops)
157        insts = []
158        while supers:
159            if supers[0] in opcode.super2prim:
160                supers[0:1] = opcode.super2prim[supers[0]]
161            else:
162                insts.append(supers[0])
163                supers = supers[1:]
164        result.append((insts, map(_Opname, insts), count))
165    return result