Back to index

python3.2  3.2.2
Functions | Variables
analyze_dxp Namespace Reference

Functions

def has_pairs
def reset_profile
def merge_profile
def snapshot_profile
def common_instructions
def common_pairs
def render_common_pairs

Variables

tuple _profile_lock = threading.RLock()
tuple _cumulative_profile = sys.getdxp()

Detailed Description

Some helper functions to analyze the output of sys.getdxp() (which is
only available if Python was built with -DDYNAMIC_EXECUTION_PROFILE).
These will tell you which opcodes have been executed most frequently
in the current process, and, if Python was also built with -DDXPAIRS,
will tell you which instruction _pairs_ were executed most frequently,
which may help in choosing new instructions.

If Python was built without -DDYNAMIC_EXECUTION_PROFILE, importing
this module will raise a RuntimeError.

If you're running a script you want to profile, a simple way to get
the common pairs is:

$ PYTHONPATH=$PYTHONPATH:<python_srcdir>/Tools/scripts \
./python -i -O the_script.py --args
...
> from analyze_dxp import *
> s = render_common_pairs()
> open('/tmp/some_file', 'w').write(s)

Function Documentation

Returns the most common opcodes in order of descending frequency.

The result is a list of tuples of the form
  (opcode, opname, # of occurrences)

Definition at line 78 of file analyze_dxp.py.

00078 
00079 def common_instructions(profile):
00080     """Returns the most common opcodes in order of descending frequency.
00081 
00082     The result is a list of tuples of the form
00083       (opcode, opname, # of occurrences)
00084 
00085     """
00086     if has_pairs(profile) and profile:
00087         inst_list = profile[-1]
00088     else:
00089         inst_list = profile
00090     result = [(op, opcode.opname[op], count)
00091               for op, count in enumerate(inst_list)
00092               if count > 0]
00093     result.sort(key=operator.itemgetter(2), reverse=True)
00094     return result
00095 

Here is the call graph for this function:

def analyze_dxp.common_pairs (   profile)
Returns the most common opcode pairs in order of descending frequency.

The result is a list of tuples of the form
  ((1st opcode, 2nd opcode),
   (1st opname, 2nd opname),
   # of occurrences of the pair)

Definition at line 96 of file analyze_dxp.py.

00096 
00097 def common_pairs(profile):
00098     """Returns the most common opcode pairs in order of descending frequency.
00099 
00100     The result is a list of tuples of the form
00101       ((1st opcode, 2nd opcode),
00102        (1st opname, 2nd opname),
00103        # of occurrences of the pair)
00104 
00105     """
00106     if not has_pairs(profile):
00107         return []
00108     result = [((op1, op2), (opcode.opname[op1], opcode.opname[op2]), count)
00109               # Drop the row of single-op profiles with [:-1]
00110               for op1, op1profile in enumerate(profile[:-1])
00111               for op2, count in enumerate(op1profile)
00112               if count > 0]
00113     result.sort(key=operator.itemgetter(2), reverse=True)
00114     return result
00115 

Here is the call graph for this function:

Here is the caller graph for this function:

def analyze_dxp.has_pairs (   profile)
Returns True if the Python that produced the argument profile
was built with -DDXPAIRS.

Definition at line 39 of file analyze_dxp.py.

00039 
00040 def has_pairs(profile):
00041     """Returns True if the Python that produced the argument profile
00042     was built with -DDXPAIRS."""
00043 
00044     return len(profile) > 0 and isinstance(profile[0], list)
00045 

Here is the caller graph for this function:

Reads sys.getdxp() and merges it into this module's cached copy.

We need this because sys.getdxp() 0s itself every time it's called.

Definition at line 54 of file analyze_dxp.py.

00054 
00055 def merge_profile():
00056     """Reads sys.getdxp() and merges it into this module's cached copy.
00057 
00058     We need this because sys.getdxp() 0s itself every time it's called."""
00059 
00060     with _profile_lock:
00061         new_profile = sys.getdxp()
00062         if has_pairs(new_profile):
00063             for first_inst in range(len(_cumulative_profile)):
00064                 for second_inst in range(len(_cumulative_profile[first_inst])):
00065                     _cumulative_profile[first_inst][second_inst] += (
00066                         new_profile[first_inst][second_inst])
00067         else:
00068             for inst in range(len(_cumulative_profile)):
00069                 _cumulative_profile[inst] += new_profile[inst]
00070 

Here is the call graph for this function:

Here is the caller graph for this function:

def analyze_dxp.render_common_pairs (   profile = None)
Renders the most common opcode pairs to a string in order of
descending frequency.

The result is a series of lines of the form:
  # of occurrences: ('1st opname', '2nd opname')

Definition at line 116 of file analyze_dxp.py.

00116 
00117 def render_common_pairs(profile=None):
00118     """Renders the most common opcode pairs to a string in order of
00119     descending frequency.
00120 
00121     The result is a series of lines of the form:
00122       # of occurrences: ('1st opname', '2nd opname')
00123 
00124     """
00125     if profile is None:
00126         profile = snapshot_profile()
00127     def seq():
00128         for _, ops, count in common_pairs(profile):
00129             yield "%s: %s\n" % (count, ops)
00130     return ''.join(seq())

Here is the call graph for this function:

Forgets any execution profile that has been gathered so far.

Definition at line 46 of file analyze_dxp.py.

00046 
00047 def reset_profile():
00048     """Forgets any execution profile that has been gathered so far."""
00049     with _profile_lock:
00050         sys.getdxp()  # Resets the internal profile
00051         global _cumulative_profile
00052         _cumulative_profile = sys.getdxp()  # 0s out our copy.
00053 

Returns the cumulative execution profile until this call.

Definition at line 71 of file analyze_dxp.py.

00071 
00072 def snapshot_profile():
00073     """Returns the cumulative execution profile until this call."""
00074     with _profile_lock:
00075         merge_profile()
00076         return copy.deepcopy(_cumulative_profile)
00077 

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

tuple analyze_dxp._cumulative_profile = sys.getdxp()

Definition at line 35 of file analyze_dxp.py.

Definition at line 34 of file analyze_dxp.py.