ยปCore Development>Code coverage>Tools/scripts/analyze_dxp.py

Python code coverage for Tools/scripts/analyze_dxp.py

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