analyze_dxp.py 4.1 KB

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