blob: 5d0e9685a4f1aaf7a057eb58d27bd5122b2f6150 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
Guido van Rossum81762581992-04-21 15:36:23 +00002#
Guido van Rossumb6775db1994-08-01 11:34:53 +00003# Class for profiling python code. rev 1.0 6/2/94
Guido van Rossum81762581992-04-21 15:36:23 +00004#
Benjamin Peterson8d770692011-06-27 09:14:34 -05005# Written by James Roskind
Guido van Rossumb6775db1994-08-01 11:34:53 +00006# Based on prior profile module by Sjoerd Mullender...
7# which was hacked somewhat by: Guido van Rossum
Guido van Rossumb6775db1994-08-01 11:34:53 +00008
Guido van Rossum54f22ed2000-02-04 15:10:34 +00009"""Class for profiling Python code."""
Guido van Rossumb6775db1994-08-01 11:34:53 +000010
Benjamin Peterson8d770692011-06-27 09:14:34 -050011# Copyright Disney Enterprises, Inc. All Rights Reserved.
12# Licensed to PSF under a Contributor Agreement
Benjamin Peterson0f93d3d2011-06-27 09:18:46 -050013#
Benjamin Peterson8d770692011-06-27 09:14:34 -050014# Licensed under the Apache License, Version 2.0 (the "License");
15# you may not use this file except in compliance with the License.
16# You may obtain a copy of the License at
Benjamin Peterson0f93d3d2011-06-27 09:18:46 -050017#
Benjamin Peterson8d770692011-06-27 09:14:34 -050018# http://www.apache.org/licenses/LICENSE-2.0
Benjamin Peterson0f93d3d2011-06-27 09:18:46 -050019#
Benjamin Peterson8d770692011-06-27 09:14:34 -050020# Unless required by applicable law or agreed to in writing, software
21# distributed under the License is distributed on an "AS IS" BASIS,
22# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
23# either express or implied. See the License for the specific language
24# governing permissions and limitations under the License.
Guido van Rossumb6775db1994-08-01 11:34:53 +000025
Guido van Rossum81762581992-04-21 15:36:23 +000026
27import sys
Guido van Rossum4e160981992-09-02 20:43:20 +000028import os
Guido van Rossumb6775db1994-08-01 11:34:53 +000029import time
Guido van Rossum4e160981992-09-02 20:43:20 +000030import marshal
Nicholas Bastin824b1b22004-03-23 18:44:39 +000031from optparse import OptionParser
Guido van Rossum81762581992-04-21 15:36:23 +000032
Georg Brandlb6b13242009-09-04 17:15:16 +000033__all__ = ["run", "runctx", "Profile"]
Guido van Rossum81762581992-04-21 15:36:23 +000034
Tim Peters2344fae2001-01-15 00:50:52 +000035# Sample timer for use with
Guido van Rossumb6775db1994-08-01 11:34:53 +000036#i_count = 0
37#def integer_timer():
Tim Peters2344fae2001-01-15 00:50:52 +000038# global i_count
39# i_count = i_count + 1
40# return i_count
Guido van Rossumb6775db1994-08-01 11:34:53 +000041#itimes = integer_timer # replace with C coded timer returning integers
Guido van Rossum81762581992-04-21 15:36:23 +000042
Giampaolo Rodola'fca677a2013-02-25 11:36:40 +010043class _Utils:
44 """Support class for utility functions which are shared by
45 profile.py and cProfile.py modules.
46 Not supposed to be used directly.
47 """
48
49 def __init__(self, profiler):
50 self.profiler = profiler
51
52 def run(self, statement, filename, sort):
53 prof = self.profiler()
54 try:
55 prof.run(statement)
56 except SystemExit:
57 pass
58 finally:
59 self._show(prof, filename, sort)
60
61 def runctx(self, statement, globals, locals, filename, sort):
62 prof = self.profiler()
63 try:
64 prof.runctx(statement, globals, locals)
65 except SystemExit:
66 pass
67 finally:
68 self._show(prof, filename, sort)
69
70 def _show(self, prof, filename, sort):
71 if filename is not None:
72 prof.dump_stats(filename)
73 else:
74 prof.print_stats(sort)
75
76
Guido van Rossumb6775db1994-08-01 11:34:53 +000077#**************************************************************************
78# The following are the static member functions for the profiler class
79# Note that an instance of Profile() is *not* needed to call them.
80#**************************************************************************
Guido van Rossum81762581992-04-21 15:36:23 +000081
Nicholas Bastin824b1b22004-03-23 18:44:39 +000082def run(statement, filename=None, sort=-1):
Jeremy Hyltonadcf8a02001-03-14 20:01:19 +000083 """Run statement under profiler optionally saving results in filename
Guido van Rossum4e160981992-09-02 20:43:20 +000084
Jeremy Hyltonadcf8a02001-03-14 20:01:19 +000085 This function takes a single argument that can be passed to the
86 "exec" statement, and an optional file name. In all cases this
87 routine attempts to "exec" its first argument and gather profiling
88 statistics from the execution. If no file name is present, then this
89 function automatically prints a simple profiling report, sorted by the
90 standard name string (file/line/function-name) that is presented in
91 each line.
92 """
Giampaolo Rodola'fca677a2013-02-25 11:36:40 +010093 return _Utils(Profile).run(statement, filename, sort)
Guido van Rossume61fa0a1993-10-22 13:56:35 +000094
Georg Brandl8e43fbf2010-08-02 12:20:23 +000095def runctx(statement, globals, locals, filename=None, sort=-1):
Nicholas Bastin1eb4bfc2004-03-22 20:12:56 +000096 """Run statement under profiler, supplying your own globals and locals,
97 optionally saving results in filename.
98
99 statement and filename have the same semantics as profile.run
100 """
Giampaolo Rodola'fca677a2013-02-25 11:36:40 +0100101 return _Utils(Profile).runctx(statement, globals, locals, filename, sort)
Nicholas Bastin1eb4bfc2004-03-22 20:12:56 +0000102
Nicholas Bastin1eb4bfc2004-03-22 20:12:56 +0000103
Guido van Rossumb6775db1994-08-01 11:34:53 +0000104class Profile:
Tim Peters2344fae2001-01-15 00:50:52 +0000105 """Profiler class.
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000106
Tim Peters2344fae2001-01-15 00:50:52 +0000107 self.cur is always a tuple. Each such tuple corresponds to a stack
108 frame that is currently active (self.cur[-2]). The following are the
109 definitions of its members. We use this external "parallel stack" to
110 avoid contaminating the program that we are profiling. (old profiler
111 used to write into the frames local dictionary!!) Derived classes
112 can change the definition of some entries, as long as they leave
Tim Petersdf5cfd82001-10-05 23:15:10 +0000113 [-2:] intact (frame and previous tuple). In case an internal error is
114 detected, the -3 element is used as the function name.
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000115
Tim Petersdf5cfd82001-10-05 23:15:10 +0000116 [ 0] = Time that needs to be charged to the parent frame's function.
117 It is used so that a function call will not have to access the
118 timing data for the parent frame.
119 [ 1] = Total time spent in this frame's function, excluding time in
Tim Peters8d061ed2001-10-07 08:35:44 +0000120 subfunctions (this latter is tallied in cur[2]).
Tim Petersfb163782001-10-07 08:49:02 +0000121 [ 2] = Total time spent in subfunctions, excluding time executing the
Tim Peters8d061ed2001-10-07 08:35:44 +0000122 frame's function (this latter is tallied in cur[1]).
Tim Petersdf5cfd82001-10-05 23:15:10 +0000123 [-3] = Name of the function that corresponds to this frame.
Tim Peters8d061ed2001-10-07 08:35:44 +0000124 [-2] = Actual frame that we correspond to (used to sync exception handling).
125 [-1] = Our parent 6-tuple (corresponds to frame.f_back).
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000126
Tim Peters2344fae2001-01-15 00:50:52 +0000127 Timing data for each function is stored as a 5-tuple in the dictionary
Tim Petersdf5cfd82001-10-05 23:15:10 +0000128 self.timings[]. The index is always the name stored in self.cur[-3].
Tim Peters2344fae2001-01-15 00:50:52 +0000129 The following are the definitions of the members:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000130
Tim Peters2344fae2001-01-15 00:50:52 +0000131 [0] = The number of times this function was called, not counting direct
132 or indirect recursion,
133 [1] = Number of times this function appears on the stack, minus one
134 [2] = Total time spent internal to this function
135 [3] = Cumulative time that this function was present on the stack. In
136 non-recursive functions, this is the total execution time from start
137 to finish of each invocation of a function, including time spent in
138 all subfunctions.
Tim Peters6e221492001-10-07 04:02:36 +0000139 [4] = A dictionary indicating for each function name, the number of times
Tim Peters2344fae2001-01-15 00:50:52 +0000140 it was called by us.
141 """
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142
Tim Peters659a6032001-10-09 20:51:19 +0000143 bias = 0 # calibration constant
144
145 def __init__(self, timer=None, bias=None):
Tim Peters2344fae2001-01-15 00:50:52 +0000146 self.timings = {}
147 self.cur = None
148 self.cmd = ""
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000149 self.c_func_name = ""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000150
Tim Peters659a6032001-10-09 20:51:19 +0000151 if bias is None:
152 bias = self.bias
153 self.bias = bias # Materialize in local dict for lookup speed.
154
Martin v. Löwisa4dac402005-03-03 11:39:45 +0000155 if not timer:
Victor Stinnerfe98e2f2012-04-29 03:01:20 +0200156 self.timer = self.get_time = time.process_time
157 self.dispatcher = self.trace_dispatch_i
Tim Peters2344fae2001-01-15 00:50:52 +0000158 else:
159 self.timer = timer
160 t = self.timer() # test out timer function
161 try:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000162 length = len(t)
163 except TypeError:
164 self.get_time = timer
165 self.dispatcher = self.trace_dispatch_i
166 else:
167 if length == 2:
Tim Peters2344fae2001-01-15 00:50:52 +0000168 self.dispatcher = self.trace_dispatch
169 else:
170 self.dispatcher = self.trace_dispatch_l
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000171 # This get_time() implementation needs to be defined
172 # here to capture the passed-in timer in the parameter
173 # list (for performance). Note that we can't assume
174 # the timer() result contains two values in all
175 # cases.
Raymond Hettinger97aa32b2003-10-22 16:49:01 +0000176 def get_time_timer(timer=timer, sum=sum):
177 return sum(timer())
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000178 self.get_time = get_time_timer
Tim Peters2344fae2001-01-15 00:50:52 +0000179 self.t = self.get_time()
180 self.simulate_call('profiler')
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181
Tim Peters2344fae2001-01-15 00:50:52 +0000182 # Heavily optimized dispatch routine for os.times() timer
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183
Tim Peters2344fae2001-01-15 00:50:52 +0000184 def trace_dispatch(self, frame, event, arg):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000185 timer = self.timer
186 t = timer()
Tim Peters659a6032001-10-09 20:51:19 +0000187 t = t[0] + t[1] - self.t - self.bias
Tim Peters2344fae2001-01-15 00:50:52 +0000188
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000189 if event == "c_call":
Nicholas Bastin12ac3e12004-07-12 23:38:02 +0000190 self.c_func_name = arg.__name__
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000191
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000192 if self.dispatch[event](self, frame,t):
193 t = timer()
Tim Peters2344fae2001-01-15 00:50:52 +0000194 self.t = t[0] + t[1]
195 else:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000196 r = timer()
Tim Peters2344fae2001-01-15 00:50:52 +0000197 self.t = r[0] + r[1] - t # put back unrecorded delta
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198
Tim Peters6e221492001-10-07 04:02:36 +0000199 # Dispatch routine for best timer program (return = scalar, fastest if
200 # an integer but float works too -- and time.clock() relies on that).
Guido van Rossumb6775db1994-08-01 11:34:53 +0000201
Tim Peters2344fae2001-01-15 00:50:52 +0000202 def trace_dispatch_i(self, frame, event, arg):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000203 timer = self.timer
Tim Peters659a6032001-10-09 20:51:19 +0000204 t = timer() - self.t - self.bias
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000205
206 if event == "c_call":
Nicholas Bastin12ac3e12004-07-12 23:38:02 +0000207 self.c_func_name = arg.__name__
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000208
209 if self.dispatch[event](self, frame, t):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000210 self.t = timer()
Tim Peters2344fae2001-01-15 00:50:52 +0000211 else:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000212 self.t = timer() - t # put back unrecorded delta
Guido van Rossumcbf3dd51997-10-08 15:23:02 +0000213
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000214 # Dispatch routine for macintosh (timer returns time in ticks of
215 # 1/60th second)
Tim Peters2344fae2001-01-15 00:50:52 +0000216
217 def trace_dispatch_mac(self, frame, event, arg):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000218 timer = self.timer
Tim Peters659a6032001-10-09 20:51:19 +0000219 t = timer()/60.0 - self.t - self.bias
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000220
221 if event == "c_call":
Nicholas Bastin12ac3e12004-07-12 23:38:02 +0000222 self.c_func_name = arg.__name__
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000223
Tim Peters659a6032001-10-09 20:51:19 +0000224 if self.dispatch[event](self, frame, t):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000225 self.t = timer()/60.0
Tim Peters2344fae2001-01-15 00:50:52 +0000226 else:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000227 self.t = timer()/60.0 - t # put back unrecorded delta
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
Tim Peters2344fae2001-01-15 00:50:52 +0000229 # SLOW generic dispatch routine for timer returning lists of numbers
Guido van Rossumb6775db1994-08-01 11:34:53 +0000230
Tim Peters2344fae2001-01-15 00:50:52 +0000231 def trace_dispatch_l(self, frame, event, arg):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000232 get_time = self.get_time
Tim Peters659a6032001-10-09 20:51:19 +0000233 t = get_time() - self.t - self.bias
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000235 if event == "c_call":
Nicholas Bastin12ac3e12004-07-12 23:38:02 +0000236 self.c_func_name = arg.__name__
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000237
Tim Peters659a6032001-10-09 20:51:19 +0000238 if self.dispatch[event](self, frame, t):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000239 self.t = get_time()
Tim Peters2344fae2001-01-15 00:50:52 +0000240 else:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000241 self.t = get_time() - t # put back unrecorded delta
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242
Tim Peters8d061ed2001-10-07 08:35:44 +0000243 # In the event handlers, the first 3 elements of self.cur are unpacked
244 # into vrbls w/ 3-letter names. The last two characters are meant to be
245 # mnemonic:
246 # _pt self.cur[0] "parent time" time to be charged to parent frame
247 # _it self.cur[1] "internal time" time spent directly in the function
248 # _et self.cur[2] "external time" time spent in subfunctions
Guido van Rossumb6775db1994-08-01 11:34:53 +0000249
Tim Peters2344fae2001-01-15 00:50:52 +0000250 def trace_dispatch_exception(self, frame, t):
Tim Peters8d061ed2001-10-07 08:35:44 +0000251 rpt, rit, ret, rfn, rframe, rcur = self.cur
Fred Drakea0bc9992001-10-03 21:12:32 +0000252 if (rframe is not frame) and rcur:
Tim Peters2344fae2001-01-15 00:50:52 +0000253 return self.trace_dispatch_return(rframe, t)
Tim Peters8d061ed2001-10-07 08:35:44 +0000254 self.cur = rpt, rit+t, ret, rfn, rframe, rcur
Guido van Rossumf137f752001-10-04 00:58:24 +0000255 return 1
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256
257
Tim Peters2344fae2001-01-15 00:50:52 +0000258 def trace_dispatch_call(self, frame, t):
Tim Petersdf5cfd82001-10-05 23:15:10 +0000259 if self.cur and frame.f_back is not self.cur[-2]:
Tim Peters8d061ed2001-10-07 08:35:44 +0000260 rpt, rit, ret, rfn, rframe, rcur = self.cur
Guido van Rossumf137f752001-10-04 00:58:24 +0000261 if not isinstance(rframe, Profile.fake_frame):
Tim Petersdb1ed2a2001-10-07 04:30:53 +0000262 assert rframe.f_back is frame.f_back, ("Bad call", rfn,
263 rframe, rframe.f_back,
264 frame, frame.f_back)
Guido van Rossumf137f752001-10-04 00:58:24 +0000265 self.trace_dispatch_return(rframe, 0)
Tim Petersdb1ed2a2001-10-07 04:30:53 +0000266 assert (self.cur is None or \
267 frame.f_back is self.cur[-2]), ("Bad call",
268 self.cur[-3])
Tim Peters2344fae2001-01-15 00:50:52 +0000269 fcode = frame.f_code
270 fn = (fcode.co_filename, fcode.co_firstlineno, fcode.co_name)
271 self.cur = (t, 0, 0, fn, frame, self.cur)
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000272 timings = self.timings
Raymond Hettinger54f02222002-06-01 14:18:47 +0000273 if fn in timings:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000274 cc, ns, tt, ct, callers = timings[fn]
275 timings[fn] = cc, ns + 1, tt, ct, callers
Tim Peters2344fae2001-01-15 00:50:52 +0000276 else:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000277 timings[fn] = 0, 0, 0, 0, {}
Tim Peters2344fae2001-01-15 00:50:52 +0000278 return 1
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000280 def trace_dispatch_c_call (self, frame, t):
281 fn = ("", 0, self.c_func_name)
282 self.cur = (t, 0, 0, fn, frame, self.cur)
283 timings = self.timings
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000284 if fn in timings:
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000285 cc, ns, tt, ct, callers = timings[fn]
286 timings[fn] = cc, ns+1, tt, ct, callers
287 else:
288 timings[fn] = 0, 0, 0, 0, {}
289 return 1
290
Tim Peters2344fae2001-01-15 00:50:52 +0000291 def trace_dispatch_return(self, frame, t):
Tim Petersdf5cfd82001-10-05 23:15:10 +0000292 if frame is not self.cur[-2]:
Tim Petersdb1ed2a2001-10-07 04:30:53 +0000293 assert frame is self.cur[-2].f_back, ("Bad return", self.cur[-3])
294 self.trace_dispatch_return(self.cur[-2], 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000295
Tim Peters8d061ed2001-10-07 08:35:44 +0000296 # Prefix "r" means part of the Returning or exiting frame.
297 # Prefix "p" means part of the Previous or Parent or older frame.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000298
Tim Peters8d061ed2001-10-07 08:35:44 +0000299 rpt, rit, ret, rfn, frame, rcur = self.cur
300 rit = rit + t
301 frame_total = rit + ret
Guido van Rossumb6775db1994-08-01 11:34:53 +0000302
Tim Peters8d061ed2001-10-07 08:35:44 +0000303 ppt, pit, pet, pfn, pframe, pcur = rcur
304 self.cur = ppt, pit + rpt, pet + frame_total, pfn, pframe, pcur
Guido van Rossumb6775db1994-08-01 11:34:53 +0000305
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000306 timings = self.timings
307 cc, ns, tt, ct, callers = timings[rfn]
Tim Peters2344fae2001-01-15 00:50:52 +0000308 if not ns:
Tim Peters8d061ed2001-10-07 08:35:44 +0000309 # This is the only occurrence of the function on the stack.
310 # Else this is a (directly or indirectly) recursive call, and
311 # its cumulative time will get updated when the topmost call to
312 # it returns.
313 ct = ct + frame_total
Tim Peters2344fae2001-01-15 00:50:52 +0000314 cc = cc + 1
Tim Peters8d061ed2001-10-07 08:35:44 +0000315
Raymond Hettinger54f02222002-06-01 14:18:47 +0000316 if pfn in callers:
Tim Peters2344fae2001-01-15 00:50:52 +0000317 callers[pfn] = callers[pfn] + 1 # hack: gather more
318 # stats such as the amount of time added to ct courtesy
319 # of this specific call, and the contribution to cc
320 # courtesy of this call.
321 else:
322 callers[pfn] = 1
Tim Peters8d061ed2001-10-07 08:35:44 +0000323
324 timings[rfn] = cc, ns - 1, tt + rit, ct, callers
Guido van Rossumb6775db1994-08-01 11:34:53 +0000325
Tim Peters2344fae2001-01-15 00:50:52 +0000326 return 1
Guido van Rossumb6775db1994-08-01 11:34:53 +0000327
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000328
329 dispatch = {
330 "call": trace_dispatch_call,
331 "exception": trace_dispatch_exception,
332 "return": trace_dispatch_return,
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000333 "c_call": trace_dispatch_c_call,
Armin Rigof8790242005-09-20 18:50:13 +0000334 "c_exception": trace_dispatch_return, # the C function returned
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000335 "c_return": trace_dispatch_return,
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000336 }
337
338
Tim Petersdb1ed2a2001-10-07 04:30:53 +0000339 # The next few functions play with self.cmd. By carefully preloading
Tim Peters2344fae2001-01-15 00:50:52 +0000340 # our parallel stack, we can force the profiled result to include
341 # an arbitrary string as the name of the calling function.
342 # We use self.cmd as that string, and the resulting stats look
343 # very nice :-).
Guido van Rossumb6775db1994-08-01 11:34:53 +0000344
Tim Peters2344fae2001-01-15 00:50:52 +0000345 def set_cmd(self, cmd):
Tim Petersdf5cfd82001-10-05 23:15:10 +0000346 if self.cur[-1]: return # already set
Tim Peters2344fae2001-01-15 00:50:52 +0000347 self.cmd = cmd
348 self.simulate_call(cmd)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000349
Tim Peters2344fae2001-01-15 00:50:52 +0000350 class fake_code:
351 def __init__(self, filename, line, name):
352 self.co_filename = filename
353 self.co_line = line
354 self.co_name = name
355 self.co_firstlineno = 0
Guido van Rossumb6775db1994-08-01 11:34:53 +0000356
Tim Peters2344fae2001-01-15 00:50:52 +0000357 def __repr__(self):
358 return repr((self.co_filename, self.co_line, self.co_name))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000359
Tim Peters2344fae2001-01-15 00:50:52 +0000360 class fake_frame:
361 def __init__(self, code, prior):
362 self.f_code = code
363 self.f_back = prior
Guido van Rossumb6775db1994-08-01 11:34:53 +0000364
Tim Peters2344fae2001-01-15 00:50:52 +0000365 def simulate_call(self, name):
366 code = self.fake_code('profile', 0, name)
367 if self.cur:
Tim Petersdf5cfd82001-10-05 23:15:10 +0000368 pframe = self.cur[-2]
Tim Peters2344fae2001-01-15 00:50:52 +0000369 else:
370 pframe = None
371 frame = self.fake_frame(code, pframe)
Fred Draked10ed8b2001-10-17 01:49:50 +0000372 self.dispatch['call'](self, frame, 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000373
Tim Peters2344fae2001-01-15 00:50:52 +0000374 # collect stats from pending stack, including getting final
375 # timings for self.cmd frame.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000376
Tim Peters2344fae2001-01-15 00:50:52 +0000377 def simulate_cmd_complete(self):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000378 get_time = self.get_time
379 t = get_time() - self.t
Tim Petersdf5cfd82001-10-05 23:15:10 +0000380 while self.cur[-1]:
Tim Peters2344fae2001-01-15 00:50:52 +0000381 # We *can* cause assertion errors here if
382 # dispatch_trace_return checks for a frame match!
Fred Draked10ed8b2001-10-17 01:49:50 +0000383 self.dispatch['return'](self, self.cur[-2], t)
Tim Peters2344fae2001-01-15 00:50:52 +0000384 t = 0
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000385 self.t = get_time() - t
Guido van Rossumb6775db1994-08-01 11:34:53 +0000386
387
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000388 def print_stats(self, sort=-1):
Tim Peters2344fae2001-01-15 00:50:52 +0000389 import pstats
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000390 pstats.Stats(self).strip_dirs().sort_stats(sort). \
Tim Peters2344fae2001-01-15 00:50:52 +0000391 print_stats()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000392
Tim Peters2344fae2001-01-15 00:50:52 +0000393 def dump_stats(self, file):
Giampaolo Rodola'2f50aaf2013-02-12 02:04:27 +0100394 with open(file, 'wb') as f:
395 self.create_stats()
396 marshal.dump(self.stats, f)
Tim Peters2344fae2001-01-15 00:50:52 +0000397
398 def create_stats(self):
399 self.simulate_cmd_complete()
400 self.snapshot_stats()
401
402 def snapshot_stats(self):
403 self.stats = {}
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000404 for func, (cc, ns, tt, ct, callers) in self.timings.items():
Tim Peters2344fae2001-01-15 00:50:52 +0000405 callers = callers.copy()
406 nc = 0
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000407 for callcnt in callers.values():
Raymond Hettingere0d49722002-06-02 18:55:56 +0000408 nc += callcnt
Tim Peters2344fae2001-01-15 00:50:52 +0000409 self.stats[func] = cc, nc, tt, ct, callers
Guido van Rossumb6775db1994-08-01 11:34:53 +0000410
411
Tim Peters2344fae2001-01-15 00:50:52 +0000412 # The following two methods can be called by clients to use
413 # a profiler to profile a statement, given as a string.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000414
Tim Peters2344fae2001-01-15 00:50:52 +0000415 def run(self, cmd):
416 import __main__
417 dict = __main__.__dict__
418 return self.runctx(cmd, dict, dict)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000419
Tim Peters2344fae2001-01-15 00:50:52 +0000420 def runctx(self, cmd, globals, locals):
421 self.set_cmd(cmd)
422 sys.setprofile(self.dispatcher)
423 try:
Georg Brandl7cae87c2006-09-06 06:51:57 +0000424 exec(cmd, globals, locals)
Tim Peters2344fae2001-01-15 00:50:52 +0000425 finally:
426 sys.setprofile(None)
427 return self
Guido van Rossumb6775db1994-08-01 11:34:53 +0000428
Tim Peters2344fae2001-01-15 00:50:52 +0000429 # This method is more useful to profile a single function call.
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000430 def runcall(self, func, *args, **kw):
Walter Dörwald70a6b492004-02-12 17:35:32 +0000431 self.set_cmd(repr(func))
Tim Peters2344fae2001-01-15 00:50:52 +0000432 sys.setprofile(self.dispatcher)
433 try:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000434 return func(*args, **kw)
Tim Peters2344fae2001-01-15 00:50:52 +0000435 finally:
436 sys.setprofile(None)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000437
Tim Peters2344fae2001-01-15 00:50:52 +0000438
439 #******************************************************************
440 # The following calculates the overhead for using a profiler. The
441 # problem is that it takes a fair amount of time for the profiler
442 # to stop the stopwatch (from the time it receives an event).
443 # Similarly, there is a delay from the time that the profiler
444 # re-starts the stopwatch before the user's code really gets to
445 # continue. The following code tries to measure the difference on
Tim Peters659a6032001-10-09 20:51:19 +0000446 # a per-event basis.
447 #
448 # Note that this difference is only significant if there are a lot of
Tim Peters2344fae2001-01-15 00:50:52 +0000449 # events, and relatively little user code per event. For example,
450 # code with small functions will typically benefit from having the
451 # profiler calibrated for the current platform. This *could* be
452 # done on the fly during init() time, but it is not worth the
453 # effort. Also note that if too large a value specified, then
454 # execution time on some functions will actually appear as a
455 # negative number. It is *normal* for some functions (with very
456 # low call counts) to have such negative stats, even if the
457 # calibration figure is "correct."
458 #
459 # One alternative to profile-time calibration adjustments (i.e.,
460 # adding in the magic little delta during each event) is to track
461 # more carefully the number of events (and cumulatively, the number
462 # of events during sub functions) that are seen. If this were
463 # done, then the arithmetic could be done after the fact (i.e., at
464 # display time). Currently, we track only call/return events.
465 # These values can be deduced by examining the callees and callers
466 # vectors for each functions. Hence we *can* almost correct the
467 # internal time figure at print time (note that we currently don't
468 # track exception event processing counts). Unfortunately, there
469 # is currently no similar information for cumulative sub-function
470 # time. It would not be hard to "get all this info" at profiler
471 # time. Specifically, we would have to extend the tuples to keep
472 # counts of this in each frame, and then extend the defs of timing
473 # tuples to include the significant two figures. I'm a bit fearful
474 # that this additional feature will slow the heavily optimized
475 # event/time ratio (i.e., the profiler would run slower, fur a very
476 # low "value added" feature.)
Tim Peters2344fae2001-01-15 00:50:52 +0000477 #**************************************************************
478
Tim Peterscce092d2001-10-09 05:31:56 +0000479 def calibrate(self, m, verbose=0):
Tim Peters659a6032001-10-09 20:51:19 +0000480 if self.__class__ is not Profile:
481 raise TypeError("Subclasses must override .calibrate().")
482
483 saved_bias = self.bias
484 self.bias = 0
485 try:
Tim Peterse13cc922001-10-09 21:01:31 +0000486 return self._calibrate_inner(m, verbose)
Tim Peters659a6032001-10-09 20:51:19 +0000487 finally:
488 self.bias = saved_bias
489
Tim Peterse13cc922001-10-09 21:01:31 +0000490 def _calibrate_inner(self, m, verbose):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000491 get_time = self.get_time
Tim Peters2344fae2001-01-15 00:50:52 +0000492
Tim Peterscce092d2001-10-09 05:31:56 +0000493 # Set up a test case to be run with and without profiling. Include
494 # lots of calls, because we're trying to quantify stopwatch overhead.
495 # Do not raise any exceptions, though, because we want to know
496 # exactly how many profile events are generated (one call event, +
497 # one return event, per Python-level call).
Tim Peters2344fae2001-01-15 00:50:52 +0000498
Tim Peterscce092d2001-10-09 05:31:56 +0000499 def f1(n):
500 for i in range(n):
501 x = 1
Tim Peters2344fae2001-01-15 00:50:52 +0000502
Tim Peterscce092d2001-10-09 05:31:56 +0000503 def f(m, f1=f1):
504 for i in range(m):
505 f1(100)
Tim Peters2344fae2001-01-15 00:50:52 +0000506
Tim Peterscce092d2001-10-09 05:31:56 +0000507 f(m) # warm up the cache
508
509 # elapsed_noprofile <- time f(m) takes without profiling.
510 t0 = get_time()
511 f(m)
512 t1 = get_time()
513 elapsed_noprofile = t1 - t0
514 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000515 print("elapsed time without profiling =", elapsed_noprofile)
Tim Peterscce092d2001-10-09 05:31:56 +0000516
517 # elapsed_profile <- time f(m) takes with profiling. The difference
518 # is profiling overhead, only some of which the profiler subtracts
519 # out on its own.
520 p = Profile()
521 t0 = get_time()
522 p.runctx('f(m)', globals(), locals())
523 t1 = get_time()
524 elapsed_profile = t1 - t0
525 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000526 print("elapsed time with profiling =", elapsed_profile)
Tim Peterscce092d2001-10-09 05:31:56 +0000527
528 # reported_time <- "CPU seconds" the profiler charged to f and f1.
529 total_calls = 0.0
530 reported_time = 0.0
531 for (filename, line, funcname), (cc, ns, tt, ct, callers) in \
532 p.timings.items():
533 if funcname in ("f", "f1"):
534 total_calls += cc
535 reported_time += tt
536
537 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000538 print("'CPU seconds' profiler reported =", reported_time)
539 print("total # calls =", total_calls)
Tim Peterscce092d2001-10-09 05:31:56 +0000540 if total_calls != m + 1:
541 raise ValueError("internal error: total calls = %d" % total_calls)
542
543 # reported_time - elapsed_noprofile = overhead the profiler wasn't
544 # able to measure. Divide by twice the number of calls (since there
545 # are two profiler events per call in this test) to get the hidden
546 # overhead per event.
547 mean = (reported_time - elapsed_noprofile) / 2.0 / total_calls
548 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000549 print("mean stopwatch overhead per profile event =", mean)
Tim Peterscce092d2001-10-09 05:31:56 +0000550 return mean
Guido van Rossumb6775db1994-08-01 11:34:53 +0000551
Guido van Rossumb6775db1994-08-01 11:34:53 +0000552#****************************************************************************
Guido van Rossumcc778eb1996-10-01 02:55:54 +0000553
Johannes Gijsbers2abe7852005-01-09 01:58:02 +0000554def main():
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000555 usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
Johannes Gijsbers2abe7852005-01-09 01:58:02 +0000556 parser = OptionParser(usage=usage)
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000557 parser.allow_interspersed_args = False
Tim Peters4e0e1b62004-07-07 20:54:48 +0000558 parser.add_option('-o', '--outfile', dest="outfile",
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000559 help="Save stats to <outfile>", default=None)
560 parser.add_option('-s', '--sort', dest="sort",
Georg Brandl8e43fbf2010-08-02 12:20:23 +0000561 help="Sort order when printing to stdout, based on pstats.Stats class",
562 default=-1)
Tim Petersb497c102005-01-10 16:48:37 +0000563
Johannes Gijsbers2abe7852005-01-09 01:58:02 +0000564 if not sys.argv[1:]:
565 parser.print_usage()
566 sys.exit(2)
Tim Petersb497c102005-01-10 16:48:37 +0000567
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000568 (options, args) = parser.parse_args()
Georg Brandl8e43fbf2010-08-02 12:20:23 +0000569 sys.argv[:] = args
Tim Petersb497c102005-01-10 16:48:37 +0000570
Georg Brandl8e43fbf2010-08-02 12:20:23 +0000571 if len(args) > 0:
572 progname = args[0]
573 sys.path.insert(0, os.path.dirname(progname))
574 with open(progname, 'rb') as fp:
575 code = compile(fp.read(), progname, 'exec')
576 globs = {
577 '__file__': progname,
578 '__name__': '__main__',
579 '__package__': None,
580 '__cached__': None,
581 }
582 runctx(code, globs, None, options.outfile, options.sort)
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000583 else:
Johannes Gijsbers2abe7852005-01-09 01:58:02 +0000584 parser.print_usage()
585 return parser
586
587# When invoked as main program, invoke the profiler on a script
588if __name__ == '__main__':
589 main()