3 Some helper functions to analyze the output of sys.getdxp() (which is
4 only available if Python was built with -DDYNAMIC_EXECUTION_PROFILE).
5 These will tell you which opcodes have been executed most frequently
6 in the current process, and, if Python was also built with -DDXPAIRS,
7 will tell you which instruction _pairs_ were executed most frequently,
8 which may help in choosing new instructions.
10 If Python was built without -DDYNAMIC_EXECUTION_PROFILE, importing
11 this module will raise a RuntimeError.
13 If you're running a script you want to profile, a simple way to get
16 $ PYTHONPATH=$PYTHONPATH:<python_srcdir>/Tools/scripts \
17 ./python -i -O the_script.py --args
19 > from analyze_dxp import *
20 > s = render_common_pairs()
21 > open('/tmp/some_file', 'w').write(s)
30 if not hasattr(sys, "getdxp"):
31 raise RuntimeError("Can't import analyze_dxp: Python built without"
32 " -DDYNAMIC_EXECUTION_PROFILE.")
35 _profile_lock = threading.RLock()
36 _cumulative_profile = sys.getdxp()
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.
40 def has_pairs(profile):
41 """Returns True if the Python that produced the argument profile
42 was built with -DDXPAIRS."""
44 return len(profile) > 0 and isinstance(profile[0], list)
48 """Forgets any execution profile that has been gathered so far."""
50 sys.getdxp() # Resets the internal profile
51 global _cumulative_profile
52 _cumulative_profile = sys.getdxp() # 0s out our copy.
56 """Reads sys.getdxp() and merges it into this module's cached copy.
58 We need this because sys.getdxp() 0s itself every time it's called."""
61 new_profile = sys.getdxp()
62 if has_pairs(new_profile):
63 for first_inst in range(len(_cumulative_profile)):
64 for second_inst in range(len(_cumulative_profile[first_inst])):
65 _cumulative_profile[first_inst][second_inst] += (
66 new_profile[first_inst][second_inst])
68 for inst in range(len(_cumulative_profile)):
69 _cumulative_profile[inst] += new_profile[inst]
72 def snapshot_profile():
73 """Returns the cumulative execution profile until this call."""
76 return copy.deepcopy(_cumulative_profile)
79 def common_instructions(profile):
80 """Returns the most common opcodes in order of descending frequency.
82 The result is a list of tuples of the form
83 (opcode, opname, # of occurrences)
86 if has_pairs(profile) and profile:
87 inst_list = profile[-1]
90 result = [(op, opcode.opname[op], count)
91 for op, count in enumerate(inst_list)
93 result.sort(key=operator.itemgetter(2), reverse=True)
97 def common_pairs(profile):
98 """Returns the most common opcode pairs in order of descending frequency.
100 The result is a list of tuples of the form
101 ((1st opcode, 2nd opcode),
102 (1st opname, 2nd opname),
103 # of occurrences of the pair)
106 if not has_pairs(profile):
108 result = [((op1, op2), (opcode.opname[op1], opcode.opname[op2]), count)
109 # Drop the row of single-op profiles with [:-1]
110 for op1, op1profile in enumerate(profile[:-1])
111 for op2, count in enumerate(op1profile)
113 result.sort(key=operator.itemgetter(2), reverse=True)
117 def render_common_pairs(profile=None):
118 """Renders the most common opcode pairs to a string in order of
119 descending frequency.
121 The result is a series of lines of the form:
122 # of occurrences: ('1st opname', '2nd opname')
126 profile = snapshot_profile()
128 for _, ops, count in common_pairs(profile):
129 yield "%s: %s\n" % (count, ops)
130 return ''.join(seq())