???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................ó
fc           @   s¾   d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z e e d ƒ s` e d ƒ ‚ n  e j ƒ  Z	 e j
 ƒ  a d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d d „ Z d S(   s  
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)
iÿÿÿÿNt   getdxpsK   Can't import analyze_dxp: Python built without -DDYNAMIC_EXECUTION_PROFILE.c         C   s#   t  |  ƒ d k o" t |  d t ƒ S(   s[   Returns True if the Python that produced the argument profile
    was built with -DDXPAIRS.i    (   t   lent
   isinstancet   list(   t   profile(    (    s1   /usr/lib64/python2.7/Tools/scripts/analyze_dxp.pyt	   has_pairs(   s    c           C   s'   t   t j ƒ  t j ƒ  a Wd QXd S(   s<   Forgets any execution profile that has been gathered so far.N(   t   _profile_lockt   sysR    t   _cumulative_profile(    (    (    s1   /usr/lib64/python2.7/Tools/scripts/analyze_dxp.pyt   reset_profile/   s    
c          C   s·   t  « t j ƒ  }  t |  ƒ r| x‹ t t t ƒ ƒ D]C } x: t t t | ƒ ƒ D]" } t | | c |  | | 7<qO Wq2 Wn1 x. t t t ƒ ƒ D] } t | c |  | 7<q WWd QXd S(   s‰   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.N(   R   R   R    R   t   rangeR   R   (   t   new_profilet
   first_instt   second_instt   inst(    (    s1   /usr/lib64/python2.7/Tools/scripts/analyze_dxp.pyt   merge_profile7   s    c           C   s%   t   t ƒ  t j t ƒ SWd QXd S(   s9   Returns the cumulative execution profile until this call.N(   R   R   t   copyt   deepcopyR   (    (    (    s1   /usr/lib64/python2.7/Tools/scripts/analyze_dxp.pyt   snapshot_profileH   s    c         C   s‰   t  |  ƒ r |  r |  d } n |  } g  t | ƒ D]. \ } } | d k r2 | t j | | f ^ q2 } | j d t j d ƒ d t ƒ | S(   s    Returns the most common opcodes in order of descending frequency.

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

    iÿÿÿÿi    t   keyi   t   reverse(   R   t	   enumeratet   opcodet   opnamet   sortt   operatort
   itemgettert   True(   R   t	   inst_listt   opt   countt   result(    (    s1   /usr/lib64/python2.7/Tools/scripts/analyze_dxp.pyt   common_instructionsO   s    (c         C   s¤   t  |  ƒ s g  Sg  t |  d  ƒ D]Z \ } } t | ƒ D]A \ } } | d k r7 | | f t j | t j | f | f ^ q7 q! } | j d t j d ƒ d t ƒ | S(   sã   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)

    iÿÿÿÿi    R   i   R   (   R   R   R   R   R   R   R   R   (   R   t   op1t
   op1profilet   op2R   R   (    (    s1   /usr/lib64/python2.7/Tools/scripts/analyze_dxp.pyt   common_pairsa   s    	>c            s7   ˆ  d k r t ƒ  ‰  n  ‡  f d †  } d j | ƒ  ƒ S(   sÃ   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')

    c          3   s3   x, t  ˆ  ƒ D] \ }  } } d | | f Vq Wd  S(   Ns   %s: %s
(   R$   (   t   _t   opsR   (   R   (    s1   /usr/lib64/python2.7/Tools/scripts/analyze_dxp.pyt   seq   s    t    N(   t   NoneR   t   join(   R   R'   (    (   R   s1   /usr/lib64/python2.7/Tools/scripts/analyze_dxp.pyt   render_common_pairsu   s    (   t   __doc__R   R   R   R   t	   threadingt   hasattrt   RuntimeErrort   RLockR   R    R   R   R	   R   R   R    R$   R)   R+   (    (    (    s1   /usr/lib64/python2.7/Tools/scripts/analyze_dxp.pyt   <module>   s    						