1 | n/a | """ |
---|
2 | n/a | Some helper functions to analyze the output of sys.getdxp() (which is |
---|
3 | n/a | only available if Python was built with -DDYNAMIC_EXECUTION_PROFILE). |
---|
4 | n/a | These will tell you which opcodes have been executed most frequently |
---|
5 | n/a | in the current process, and, if Python was also built with -DDXPAIRS, |
---|
6 | n/a | will tell you which instruction _pairs_ were executed most frequently, |
---|
7 | n/a | which may help in choosing new instructions. |
---|
8 | n/a | |
---|
9 | n/a | If Python was built without -DDYNAMIC_EXECUTION_PROFILE, importing |
---|
10 | n/a | this module will raise a RuntimeError. |
---|
11 | n/a | |
---|
12 | n/a | If you're running a script you want to profile, a simple way to get |
---|
13 | n/a | the common pairs is: |
---|
14 | n/a | |
---|
15 | n/a | $ PYTHONPATH=$PYTHONPATH:<python_srcdir>/Tools/scripts \ |
---|
16 | n/a | ./python -i -O the_script.py --args |
---|
17 | n/a | ... |
---|
18 | n/a | > from analyze_dxp import * |
---|
19 | n/a | > s = render_common_pairs() |
---|
20 | n/a | > open('/tmp/some_file', 'w').write(s) |
---|
21 | n/a | """ |
---|
22 | n/a | |
---|
23 | n/a | import copy |
---|
24 | n/a | import opcode |
---|
25 | n/a | import operator |
---|
26 | n/a | import sys |
---|
27 | n/a | import threading |
---|
28 | n/a | |
---|
29 | n/a | if not hasattr(sys, "getdxp"): |
---|
30 | n/a | raise RuntimeError("Can't import analyze_dxp: Python built without" |
---|
31 | n/a | " -DDYNAMIC_EXECUTION_PROFILE.") |
---|
32 | n/a | |
---|
33 | n/a | |
---|
34 | n/a | _profile_lock = threading.RLock() |
---|
35 | n/a | _cumulative_profile = sys.getdxp() |
---|
36 | n/a | |
---|
37 | n/a | # If Python was built with -DDXPAIRS, sys.getdxp() returns a list of |
---|
38 | n/a | # lists of ints. Otherwise it returns just a list of ints. |
---|
39 | n/a | def has_pairs(profile): |
---|
40 | n/a | """Returns True if the Python that produced the argument profile |
---|
41 | n/a | was built with -DDXPAIRS.""" |
---|
42 | n/a | |
---|
43 | n/a | return len(profile) > 0 and isinstance(profile[0], list) |
---|
44 | n/a | |
---|
45 | n/a | |
---|
46 | n/a | def reset_profile(): |
---|
47 | n/a | """Forgets any execution profile that has been gathered so far.""" |
---|
48 | n/a | with _profile_lock: |
---|
49 | n/a | sys.getdxp() # Resets the internal profile |
---|
50 | n/a | global _cumulative_profile |
---|
51 | n/a | _cumulative_profile = sys.getdxp() # 0s out our copy. |
---|
52 | n/a | |
---|
53 | n/a | |
---|
54 | n/a | def merge_profile(): |
---|
55 | n/a | """Reads sys.getdxp() and merges it into this module's cached copy. |
---|
56 | n/a | |
---|
57 | n/a | We need this because sys.getdxp() 0s itself every time it's called.""" |
---|
58 | n/a | |
---|
59 | n/a | with _profile_lock: |
---|
60 | n/a | new_profile = sys.getdxp() |
---|
61 | n/a | if has_pairs(new_profile): |
---|
62 | n/a | for first_inst in range(len(_cumulative_profile)): |
---|
63 | n/a | for second_inst in range(len(_cumulative_profile[first_inst])): |
---|
64 | n/a | _cumulative_profile[first_inst][second_inst] += ( |
---|
65 | n/a | new_profile[first_inst][second_inst]) |
---|
66 | n/a | else: |
---|
67 | n/a | for inst in range(len(_cumulative_profile)): |
---|
68 | n/a | _cumulative_profile[inst] += new_profile[inst] |
---|
69 | n/a | |
---|
70 | n/a | |
---|
71 | n/a | def snapshot_profile(): |
---|
72 | n/a | """Returns the cumulative execution profile until this call.""" |
---|
73 | n/a | with _profile_lock: |
---|
74 | n/a | merge_profile() |
---|
75 | n/a | return copy.deepcopy(_cumulative_profile) |
---|
76 | n/a | |
---|
77 | n/a | |
---|
78 | n/a | def common_instructions(profile): |
---|
79 | n/a | """Returns the most common opcodes in order of descending frequency. |
---|
80 | n/a | |
---|
81 | n/a | The result is a list of tuples of the form |
---|
82 | n/a | (opcode, opname, # of occurrences) |
---|
83 | n/a | |
---|
84 | n/a | """ |
---|
85 | n/a | if has_pairs(profile) and profile: |
---|
86 | n/a | inst_list = profile[-1] |
---|
87 | n/a | else: |
---|
88 | n/a | inst_list = profile |
---|
89 | n/a | result = [(op, opcode.opname[op], count) |
---|
90 | n/a | for op, count in enumerate(inst_list) |
---|
91 | n/a | if count > 0] |
---|
92 | n/a | result.sort(key=operator.itemgetter(2), reverse=True) |
---|
93 | n/a | return result |
---|
94 | n/a | |
---|
95 | n/a | |
---|
96 | n/a | def common_pairs(profile): |
---|
97 | n/a | """Returns the most common opcode pairs in order of descending frequency. |
---|
98 | n/a | |
---|
99 | n/a | The result is a list of tuples of the form |
---|
100 | n/a | ((1st opcode, 2nd opcode), |
---|
101 | n/a | (1st opname, 2nd opname), |
---|
102 | n/a | # of occurrences of the pair) |
---|
103 | n/a | |
---|
104 | n/a | """ |
---|
105 | n/a | if not has_pairs(profile): |
---|
106 | n/a | return [] |
---|
107 | n/a | result = [((op1, op2), (opcode.opname[op1], opcode.opname[op2]), count) |
---|
108 | n/a | # Drop the row of single-op profiles with [:-1] |
---|
109 | n/a | for op1, op1profile in enumerate(profile[:-1]) |
---|
110 | n/a | for op2, count in enumerate(op1profile) |
---|
111 | n/a | if count > 0] |
---|
112 | n/a | result.sort(key=operator.itemgetter(2), reverse=True) |
---|
113 | n/a | return result |
---|
114 | n/a | |
---|
115 | n/a | |
---|
116 | n/a | def render_common_pairs(profile=None): |
---|
117 | n/a | """Renders the most common opcode pairs to a string in order of |
---|
118 | n/a | descending frequency. |
---|
119 | n/a | |
---|
120 | n/a | The result is a series of lines of the form: |
---|
121 | n/a | # of occurrences: ('1st opname', '2nd opname') |
---|
122 | n/a | |
---|
123 | n/a | """ |
---|
124 | n/a | if profile is None: |
---|
125 | n/a | profile = snapshot_profile() |
---|
126 | n/a | def seq(): |
---|
127 | n/a | for _, ops, count in common_pairs(profile): |
---|
128 | n/a | yield "%s: %s\n" % (count, ops) |
---|
129 | n/a | return ''.join(seq()) |
---|