您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

258 行
9.3KB

  1. # Copyright (c) 2010, CCP Games
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. # * Neither the name of CCP Games nor the
  12. # names of its contributors may be used to endorse or promote products
  13. # derived from this software without specific prior written permission.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY CCP GAMES ``AS IS'' AND ANY
  16. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. # DISCLAIMED. IN NO EVENT SHALL CCP GAMES BE LIABLE FOR ANY
  19. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. """This module is API-equivalent to the standard library :mod:`profile` module
  26. lbut it is greenthread-aware as well as thread-aware. Use this module
  27. to profile Eventlet-based applications in preference to either :mod:`profile` or :mod:`cProfile`.
  28. FIXME: No testcases for this module.
  29. """
  30. profile_orig = __import__('profile')
  31. __all__ = profile_orig.__all__
  32. from eventlet.patcher import slurp_properties
  33. slurp_properties(profile_orig, globals(), srckeys=dir(profile_orig))
  34. import sys
  35. import functools
  36. from eventlet import greenthread
  37. from eventlet import patcher
  38. import six
  39. thread = patcher.original(six.moves._thread.__name__) # non-monkeypatched module needed
  40. # This class provides the start() and stop() functions
  41. class Profile(profile_orig.Profile):
  42. base = profile_orig.Profile
  43. def __init__(self, timer=None, bias=None):
  44. self.current_tasklet = greenthread.getcurrent()
  45. self.thread_id = thread.get_ident()
  46. self.base.__init__(self, timer, bias)
  47. self.sleeping = {}
  48. def __call__(self, *args):
  49. """make callable, allowing an instance to be the profiler"""
  50. self.dispatcher(*args)
  51. def _setup(self):
  52. self._has_setup = True
  53. self.cur = None
  54. self.timings = {}
  55. self.current_tasklet = greenthread.getcurrent()
  56. self.thread_id = thread.get_ident()
  57. self.simulate_call("profiler")
  58. def start(self, name="start"):
  59. if getattr(self, "running", False):
  60. return
  61. self._setup()
  62. self.simulate_call("start")
  63. self.running = True
  64. sys.setprofile(self.dispatcher)
  65. def stop(self):
  66. sys.setprofile(None)
  67. self.running = False
  68. self.TallyTimings()
  69. # special cases for the original run commands, makin sure to
  70. # clear the timer context.
  71. def runctx(self, cmd, globals, locals):
  72. if not getattr(self, "_has_setup", False):
  73. self._setup()
  74. try:
  75. return profile_orig.Profile.runctx(self, cmd, globals, locals)
  76. finally:
  77. self.TallyTimings()
  78. def runcall(self, func, *args, **kw):
  79. if not getattr(self, "_has_setup", False):
  80. self._setup()
  81. try:
  82. return profile_orig.Profile.runcall(self, func, *args, **kw)
  83. finally:
  84. self.TallyTimings()
  85. def trace_dispatch_return_extend_back(self, frame, t):
  86. """A hack function to override error checking in parent class. It
  87. allows invalid returns (where frames weren't preveiously entered into
  88. the profiler) which can happen for all the tasklets that suddenly start
  89. to get monitored. This means that the time will eventually be attributed
  90. to a call high in the chain, when there is a tasklet switch
  91. """
  92. if isinstance(self.cur[-2], Profile.fake_frame):
  93. return False
  94. self.trace_dispatch_call(frame, 0)
  95. return self.trace_dispatch_return(frame, t)
  96. def trace_dispatch_c_return_extend_back(self, frame, t):
  97. # same for c return
  98. if isinstance(self.cur[-2], Profile.fake_frame):
  99. return False # ignore bogus returns
  100. self.trace_dispatch_c_call(frame, 0)
  101. return self.trace_dispatch_return(frame, t)
  102. def SwitchTasklet(self, t0, t1, t):
  103. # tally the time spent in the old tasklet
  104. pt, it, et, fn, frame, rcur = self.cur
  105. cur = (pt, it + t, et, fn, frame, rcur)
  106. # we are switching to a new tasklet, store the old
  107. self.sleeping[t0] = cur, self.timings
  108. self.current_tasklet = t1
  109. # find the new one
  110. try:
  111. self.cur, self.timings = self.sleeping.pop(t1)
  112. except KeyError:
  113. self.cur, self.timings = None, {}
  114. self.simulate_call("profiler")
  115. self.simulate_call("new_tasklet")
  116. def TallyTimings(self):
  117. oldtimings = self.sleeping
  118. self.sleeping = {}
  119. # first, unwind the main "cur"
  120. self.cur = self.Unwind(self.cur, self.timings)
  121. # we must keep the timings dicts separate for each tasklet, since it contains
  122. # the 'ns' item, recursion count of each function in that tasklet. This is
  123. # used in the Unwind dude.
  124. for tasklet, (cur, timings) in six.iteritems(oldtimings):
  125. self.Unwind(cur, timings)
  126. for k, v in six.iteritems(timings):
  127. if k not in self.timings:
  128. self.timings[k] = v
  129. else:
  130. # accumulate all to the self.timings
  131. cc, ns, tt, ct, callers = self.timings[k]
  132. # ns should be 0 after unwinding
  133. cc += v[0]
  134. tt += v[2]
  135. ct += v[3]
  136. for k1, v1 in six.iteritems(v[4]):
  137. callers[k1] = callers.get(k1, 0) + v1
  138. self.timings[k] = cc, ns, tt, ct, callers
  139. def Unwind(self, cur, timings):
  140. "A function to unwind a 'cur' frame and tally the results"
  141. "see profile.trace_dispatch_return() for details"
  142. # also see simulate_cmd_complete()
  143. while(cur[-1]):
  144. rpt, rit, ret, rfn, frame, rcur = cur
  145. frame_total = rit + ret
  146. if rfn in timings:
  147. cc, ns, tt, ct, callers = timings[rfn]
  148. else:
  149. cc, ns, tt, ct, callers = 0, 0, 0, 0, {}
  150. if not ns:
  151. ct = ct + frame_total
  152. cc = cc + 1
  153. if rcur:
  154. ppt, pit, pet, pfn, pframe, pcur = rcur
  155. else:
  156. pfn = None
  157. if pfn in callers:
  158. callers[pfn] = callers[pfn] + 1 # hack: gather more
  159. elif pfn:
  160. callers[pfn] = 1
  161. timings[rfn] = cc, ns - 1, tt + rit, ct, callers
  162. ppt, pit, pet, pfn, pframe, pcur = rcur
  163. rcur = ppt, pit + rpt, pet + frame_total, pfn, pframe, pcur
  164. cur = rcur
  165. return cur
  166. def ContextWrap(f):
  167. @functools.wraps(f)
  168. def ContextWrapper(self, arg, t):
  169. current = greenthread.getcurrent()
  170. if current != self.current_tasklet:
  171. self.SwitchTasklet(self.current_tasklet, current, t)
  172. t = 0.0 # the time was billed to the previous tasklet
  173. return f(self, arg, t)
  174. return ContextWrapper
  175. # Add "return safety" to the dispatchers
  176. Profile.dispatch = dict(profile_orig.Profile.dispatch, **{
  177. 'return': Profile.trace_dispatch_return_extend_back,
  178. 'c_return': Profile.trace_dispatch_c_return_extend_back,
  179. })
  180. # Add automatic tasklet detection to the callbacks.
  181. Profile.dispatch = dict((k, ContextWrap(v)) for k, v in six.viewitems(Profile.dispatch))
  182. # run statements shamelessly stolen from profile.py
  183. def run(statement, filename=None, sort=-1):
  184. """Run statement under profiler optionally saving results in filename
  185. This function takes a single argument that can be passed to the
  186. "exec" statement, and an optional file name. In all cases this
  187. routine attempts to "exec" its first argument and gather profiling
  188. statistics from the execution. If no file name is present, then this
  189. function automatically prints a simple profiling report, sorted by the
  190. standard name string (file/line/function-name) that is presented in
  191. each line.
  192. """
  193. prof = Profile()
  194. try:
  195. prof = prof.run(statement)
  196. except SystemExit:
  197. pass
  198. if filename is not None:
  199. prof.dump_stats(filename)
  200. else:
  201. return prof.print_stats(sort)
  202. def runctx(statement, globals, locals, filename=None):
  203. """Run statement under profiler, supplying your own globals and locals,
  204. optionally saving results in filename.
  205. statement and filename have the same semantics as profile.run
  206. """
  207. prof = Profile()
  208. try:
  209. prof = prof.runctx(statement, globals, locals)
  210. except SystemExit:
  211. pass
  212. if filename is not None:
  213. prof.dump_stats(filename)
  214. else:
  215. return prof.print_stats()