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