blob: 0340a7907bfd47981d191bb90317295ff4398616 [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 Rossumb6775db1994-08-01 11:34:53 +000028import time
Guido van Rossum4e160981992-09-02 20:43:20 +000029import marshal
Guido van Rossum81762581992-04-21 15:36:23 +000030
Georg Brandlb6b13242009-09-04 17:15:16 +000031__all__ = ["run", "runctx", "Profile"]
Guido van Rossum81762581992-04-21 15:36:23 +000032
Tim Peters2344fae2001-01-15 00:50:52 +000033# Sample timer for use with
Guido van Rossumb6775db1994-08-01 11:34:53 +000034#i_count = 0
35#def integer_timer():
Tim Peters2344fae2001-01-15 00:50:52 +000036# global i_count
37# i_count = i_count + 1
38# return i_count
Guido van Rossumb6775db1994-08-01 11:34:53 +000039#itimes = integer_timer # replace with C coded timer returning integers
Guido van Rossum81762581992-04-21 15:36:23 +000040
Giampaolo Rodola'fca677a2013-02-25 11:36:40 +010041class _Utils:
42 """Support class for utility functions which are shared by
43 profile.py and cProfile.py modules.
44 Not supposed to be used directly.
45 """
46
47 def __init__(self, profiler):
48 self.profiler = profiler
49
50 def run(self, statement, filename, sort):
51 prof = self.profiler()
52 try:
53 prof.run(statement)
54 except SystemExit:
55 pass
56 finally:
57 self._show(prof, filename, sort)
58
59 def runctx(self, statement, globals, locals, filename, sort):
60 prof = self.profiler()
61 try:
62 prof.runctx(statement, globals, locals)
63 except SystemExit:
64 pass
65 finally:
66 self._show(prof, filename, sort)
67
68 def _show(self, prof, filename, sort):
69 if filename is not None:
70 prof.dump_stats(filename)
71 else:
72 prof.print_stats(sort)
73
74
Guido van Rossumb6775db1994-08-01 11:34:53 +000075#**************************************************************************
76# The following are the static member functions for the profiler class
77# Note that an instance of Profile() is *not* needed to call them.
78#**************************************************************************
Guido van Rossum81762581992-04-21 15:36:23 +000079
Nicholas Bastin824b1b22004-03-23 18:44:39 +000080def run(statement, filename=None, sort=-1):
Jeremy Hyltonadcf8a02001-03-14 20:01:19 +000081 """Run statement under profiler optionally saving results in filename
Guido van Rossum4e160981992-09-02 20:43:20 +000082
Jeremy Hyltonadcf8a02001-03-14 20:01:19 +000083 This function takes a single argument that can be passed to the
84 "exec" statement, and an optional file name. In all cases this
85 routine attempts to "exec" its first argument and gather profiling
86 statistics from the execution. If no file name is present, then this
87 function automatically prints a simple profiling report, sorted by the
88 standard name string (file/line/function-name) that is presented in
89 each line.
90 """
Giampaolo Rodola'fca677a2013-02-25 11:36:40 +010091 return _Utils(Profile).run(statement, filename, sort)
Guido van Rossume61fa0a1993-10-22 13:56:35 +000092
Georg Brandl8e43fbf2010-08-02 12:20:23 +000093def runctx(statement, globals, locals, filename=None, sort=-1):
Nicholas Bastin1eb4bfc2004-03-22 20:12:56 +000094 """Run statement under profiler, supplying your own globals and locals,
95 optionally saving results in filename.
96
97 statement and filename have the same semantics as profile.run
98 """
Giampaolo Rodola'fca677a2013-02-25 11:36:40 +010099 return _Utils(Profile).runctx(statement, globals, locals, filename, sort)
Nicholas Bastin1eb4bfc2004-03-22 20:12:56 +0000100
Nicholas Bastin1eb4bfc2004-03-22 20:12:56 +0000101
Guido van Rossumb6775db1994-08-01 11:34:53 +0000102class Profile:
Tim Peters2344fae2001-01-15 00:50:52 +0000103 """Profiler class.
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000104
Tim Peters2344fae2001-01-15 00:50:52 +0000105 self.cur is always a tuple. Each such tuple corresponds to a stack
106 frame that is currently active (self.cur[-2]). The following are the
107 definitions of its members. We use this external "parallel stack" to
108 avoid contaminating the program that we are profiling. (old profiler
109 used to write into the frames local dictionary!!) Derived classes
110 can change the definition of some entries, as long as they leave
Tim Petersdf5cfd82001-10-05 23:15:10 +0000111 [-2:] intact (frame and previous tuple). In case an internal error is
112 detected, the -3 element is used as the function name.
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000113
Tim Petersdf5cfd82001-10-05 23:15:10 +0000114 [ 0] = Time that needs to be charged to the parent frame's function.
115 It is used so that a function call will not have to access the
116 timing data for the parent frame.
117 [ 1] = Total time spent in this frame's function, excluding time in
Tim Peters8d061ed2001-10-07 08:35:44 +0000118 subfunctions (this latter is tallied in cur[2]).
Tim Petersfb163782001-10-07 08:49:02 +0000119 [ 2] = Total time spent in subfunctions, excluding time executing the
Tim Peters8d061ed2001-10-07 08:35:44 +0000120 frame's function (this latter is tallied in cur[1]).
Tim Petersdf5cfd82001-10-05 23:15:10 +0000121 [-3] = Name of the function that corresponds to this frame.
Tim Peters8d061ed2001-10-07 08:35:44 +0000122 [-2] = Actual frame that we correspond to (used to sync exception handling).
123 [-1] = Our parent 6-tuple (corresponds to frame.f_back).
Guido van Rossum54f22ed2000-02-04 15:10:34 +0000124
Tim Peters2344fae2001-01-15 00:50:52 +0000125 Timing data for each function is stored as a 5-tuple in the dictionary
Tim Petersdf5cfd82001-10-05 23:15:10 +0000126 self.timings[]. The index is always the name stored in self.cur[-3].
Tim Peters2344fae2001-01-15 00:50:52 +0000127 The following are the definitions of the members:
Guido van Rossumb6775db1994-08-01 11:34:53 +0000128
Tim Peters2344fae2001-01-15 00:50:52 +0000129 [0] = The number of times this function was called, not counting direct
130 or indirect recursion,
131 [1] = Number of times this function appears on the stack, minus one
132 [2] = Total time spent internal to this function
133 [3] = Cumulative time that this function was present on the stack. In
134 non-recursive functions, this is the total execution time from start
135 to finish of each invocation of a function, including time spent in
136 all subfunctions.
Tim Peters6e221492001-10-07 04:02:36 +0000137 [4] = A dictionary indicating for each function name, the number of times
Tim Peters2344fae2001-01-15 00:50:52 +0000138 it was called by us.
139 """
Guido van Rossumb6775db1994-08-01 11:34:53 +0000140
Tim Peters659a6032001-10-09 20:51:19 +0000141 bias = 0 # calibration constant
142
143 def __init__(self, timer=None, bias=None):
Tim Peters2344fae2001-01-15 00:50:52 +0000144 self.timings = {}
145 self.cur = None
146 self.cmd = ""
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000147 self.c_func_name = ""
Guido van Rossumb6775db1994-08-01 11:34:53 +0000148
Tim Peters659a6032001-10-09 20:51:19 +0000149 if bias is None:
150 bias = self.bias
151 self.bias = bias # Materialize in local dict for lookup speed.
152
Martin v. Löwisa4dac402005-03-03 11:39:45 +0000153 if not timer:
Victor Stinnerfe98e2f2012-04-29 03:01:20 +0200154 self.timer = self.get_time = time.process_time
155 self.dispatcher = self.trace_dispatch_i
Tim Peters2344fae2001-01-15 00:50:52 +0000156 else:
157 self.timer = timer
158 t = self.timer() # test out timer function
159 try:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000160 length = len(t)
161 except TypeError:
162 self.get_time = timer
163 self.dispatcher = self.trace_dispatch_i
164 else:
165 if length == 2:
Tim Peters2344fae2001-01-15 00:50:52 +0000166 self.dispatcher = self.trace_dispatch
167 else:
168 self.dispatcher = self.trace_dispatch_l
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000169 # This get_time() implementation needs to be defined
170 # here to capture the passed-in timer in the parameter
171 # list (for performance). Note that we can't assume
172 # the timer() result contains two values in all
173 # cases.
Raymond Hettinger97aa32b2003-10-22 16:49:01 +0000174 def get_time_timer(timer=timer, sum=sum):
175 return sum(timer())
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000176 self.get_time = get_time_timer
Tim Peters2344fae2001-01-15 00:50:52 +0000177 self.t = self.get_time()
178 self.simulate_call('profiler')
Guido van Rossumb6775db1994-08-01 11:34:53 +0000179
Serhiy Storchaka7e4db2f2017-05-04 08:17:47 +0300180 # Heavily optimized dispatch routine for time.process_time() timer
Guido van Rossumb6775db1994-08-01 11:34:53 +0000181
Tim Peters2344fae2001-01-15 00:50:52 +0000182 def trace_dispatch(self, frame, event, arg):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000183 timer = self.timer
184 t = timer()
Tim Peters659a6032001-10-09 20:51:19 +0000185 t = t[0] + t[1] - self.t - self.bias
Tim Peters2344fae2001-01-15 00:50:52 +0000186
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000187 if event == "c_call":
Nicholas Bastin12ac3e12004-07-12 23:38:02 +0000188 self.c_func_name = arg.__name__
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000189
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000190 if self.dispatch[event](self, frame,t):
191 t = timer()
Tim Peters2344fae2001-01-15 00:50:52 +0000192 self.t = t[0] + t[1]
193 else:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000194 r = timer()
Tim Peters2344fae2001-01-15 00:50:52 +0000195 self.t = r[0] + r[1] - t # put back unrecorded delta
Guido van Rossumb6775db1994-08-01 11:34:53 +0000196
Tim Peters6e221492001-10-07 04:02:36 +0000197 # Dispatch routine for best timer program (return = scalar, fastest if
Victor Stinner884d13a2017-10-17 14:46:45 -0700198 # an integer but float works too -- and time.process_time() relies on that).
Guido van Rossumb6775db1994-08-01 11:34:53 +0000199
Tim Peters2344fae2001-01-15 00:50:52 +0000200 def trace_dispatch_i(self, frame, event, arg):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000201 timer = self.timer
Tim Peters659a6032001-10-09 20:51:19 +0000202 t = timer() - self.t - self.bias
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000203
204 if event == "c_call":
Nicholas Bastin12ac3e12004-07-12 23:38:02 +0000205 self.c_func_name = arg.__name__
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000206
207 if self.dispatch[event](self, frame, t):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000208 self.t = timer()
Tim Peters2344fae2001-01-15 00:50:52 +0000209 else:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000210 self.t = timer() - t # put back unrecorded delta
Guido van Rossumcbf3dd51997-10-08 15:23:02 +0000211
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000212 # Dispatch routine for macintosh (timer returns time in ticks of
213 # 1/60th second)
Tim Peters2344fae2001-01-15 00:50:52 +0000214
215 def trace_dispatch_mac(self, frame, event, arg):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000216 timer = self.timer
Tim Peters659a6032001-10-09 20:51:19 +0000217 t = timer()/60.0 - self.t - self.bias
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000218
219 if event == "c_call":
Nicholas Bastin12ac3e12004-07-12 23:38:02 +0000220 self.c_func_name = arg.__name__
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000221
Tim Peters659a6032001-10-09 20:51:19 +0000222 if self.dispatch[event](self, frame, t):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000223 self.t = timer()/60.0
Tim Peters2344fae2001-01-15 00:50:52 +0000224 else:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000225 self.t = timer()/60.0 - t # put back unrecorded delta
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226
Tim Peters2344fae2001-01-15 00:50:52 +0000227 # SLOW generic dispatch routine for timer returning lists of numbers
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228
Tim Peters2344fae2001-01-15 00:50:52 +0000229 def trace_dispatch_l(self, frame, event, arg):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000230 get_time = self.get_time
Tim Peters659a6032001-10-09 20:51:19 +0000231 t = get_time() - self.t - self.bias
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000233 if event == "c_call":
Nicholas Bastin12ac3e12004-07-12 23:38:02 +0000234 self.c_func_name = arg.__name__
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000235
Tim Peters659a6032001-10-09 20:51:19 +0000236 if self.dispatch[event](self, frame, t):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000237 self.t = get_time()
Tim Peters2344fae2001-01-15 00:50:52 +0000238 else:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000239 self.t = get_time() - t # put back unrecorded delta
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240
Tim Peters8d061ed2001-10-07 08:35:44 +0000241 # In the event handlers, the first 3 elements of self.cur are unpacked
242 # into vrbls w/ 3-letter names. The last two characters are meant to be
243 # mnemonic:
244 # _pt self.cur[0] "parent time" time to be charged to parent frame
245 # _it self.cur[1] "internal time" time spent directly in the function
246 # _et self.cur[2] "external time" time spent in subfunctions
Guido van Rossumb6775db1994-08-01 11:34:53 +0000247
Tim Peters2344fae2001-01-15 00:50:52 +0000248 def trace_dispatch_exception(self, frame, t):
Tim Peters8d061ed2001-10-07 08:35:44 +0000249 rpt, rit, ret, rfn, rframe, rcur = self.cur
Fred Drakea0bc9992001-10-03 21:12:32 +0000250 if (rframe is not frame) and rcur:
Tim Peters2344fae2001-01-15 00:50:52 +0000251 return self.trace_dispatch_return(rframe, t)
Tim Peters8d061ed2001-10-07 08:35:44 +0000252 self.cur = rpt, rit+t, ret, rfn, rframe, rcur
Guido van Rossumf137f752001-10-04 00:58:24 +0000253 return 1
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
255
Tim Peters2344fae2001-01-15 00:50:52 +0000256 def trace_dispatch_call(self, frame, t):
Tim Petersdf5cfd82001-10-05 23:15:10 +0000257 if self.cur and frame.f_back is not self.cur[-2]:
Tim Peters8d061ed2001-10-07 08:35:44 +0000258 rpt, rit, ret, rfn, rframe, rcur = self.cur
Guido van Rossumf137f752001-10-04 00:58:24 +0000259 if not isinstance(rframe, Profile.fake_frame):
Tim Petersdb1ed2a2001-10-07 04:30:53 +0000260 assert rframe.f_back is frame.f_back, ("Bad call", rfn,
261 rframe, rframe.f_back,
262 frame, frame.f_back)
Guido van Rossumf137f752001-10-04 00:58:24 +0000263 self.trace_dispatch_return(rframe, 0)
Tim Petersdb1ed2a2001-10-07 04:30:53 +0000264 assert (self.cur is None or \
265 frame.f_back is self.cur[-2]), ("Bad call",
266 self.cur[-3])
Tim Peters2344fae2001-01-15 00:50:52 +0000267 fcode = frame.f_code
268 fn = (fcode.co_filename, fcode.co_firstlineno, fcode.co_name)
269 self.cur = (t, 0, 0, fn, frame, self.cur)
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000270 timings = self.timings
Raymond Hettinger54f02222002-06-01 14:18:47 +0000271 if fn in timings:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000272 cc, ns, tt, ct, callers = timings[fn]
273 timings[fn] = cc, ns + 1, tt, ct, callers
Tim Peters2344fae2001-01-15 00:50:52 +0000274 else:
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000275 timings[fn] = 0, 0, 0, 0, {}
Tim Peters2344fae2001-01-15 00:50:52 +0000276 return 1
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000278 def trace_dispatch_c_call (self, frame, t):
279 fn = ("", 0, self.c_func_name)
280 self.cur = (t, 0, 0, fn, frame, self.cur)
281 timings = self.timings
Guido van Rossume2b70bc2006-08-18 22:13:04 +0000282 if fn in timings:
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000283 cc, ns, tt, ct, callers = timings[fn]
284 timings[fn] = cc, ns+1, tt, ct, callers
285 else:
286 timings[fn] = 0, 0, 0, 0, {}
287 return 1
288
Tim Peters2344fae2001-01-15 00:50:52 +0000289 def trace_dispatch_return(self, frame, t):
Tim Petersdf5cfd82001-10-05 23:15:10 +0000290 if frame is not self.cur[-2]:
Tim Petersdb1ed2a2001-10-07 04:30:53 +0000291 assert frame is self.cur[-2].f_back, ("Bad return", self.cur[-3])
292 self.trace_dispatch_return(self.cur[-2], 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000293
Tim Peters8d061ed2001-10-07 08:35:44 +0000294 # Prefix "r" means part of the Returning or exiting frame.
295 # Prefix "p" means part of the Previous or Parent or older frame.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000296
Tim Peters8d061ed2001-10-07 08:35:44 +0000297 rpt, rit, ret, rfn, frame, rcur = self.cur
298 rit = rit + t
299 frame_total = rit + ret
Guido van Rossumb6775db1994-08-01 11:34:53 +0000300
Tim Peters8d061ed2001-10-07 08:35:44 +0000301 ppt, pit, pet, pfn, pframe, pcur = rcur
302 self.cur = ppt, pit + rpt, pet + frame_total, pfn, pframe, pcur
Guido van Rossumb6775db1994-08-01 11:34:53 +0000303
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000304 timings = self.timings
305 cc, ns, tt, ct, callers = timings[rfn]
Tim Peters2344fae2001-01-15 00:50:52 +0000306 if not ns:
Tim Peters8d061ed2001-10-07 08:35:44 +0000307 # This is the only occurrence of the function on the stack.
308 # Else this is a (directly or indirectly) recursive call, and
309 # its cumulative time will get updated when the topmost call to
310 # it returns.
311 ct = ct + frame_total
Tim Peters2344fae2001-01-15 00:50:52 +0000312 cc = cc + 1
Tim Peters8d061ed2001-10-07 08:35:44 +0000313
Raymond Hettinger54f02222002-06-01 14:18:47 +0000314 if pfn in callers:
Tim Peters2344fae2001-01-15 00:50:52 +0000315 callers[pfn] = callers[pfn] + 1 # hack: gather more
316 # stats such as the amount of time added to ct courtesy
317 # of this specific call, and the contribution to cc
318 # courtesy of this call.
319 else:
320 callers[pfn] = 1
Tim Peters8d061ed2001-10-07 08:35:44 +0000321
322 timings[rfn] = cc, ns - 1, tt + rit, ct, callers
Guido van Rossumb6775db1994-08-01 11:34:53 +0000323
Tim Peters2344fae2001-01-15 00:50:52 +0000324 return 1
Guido van Rossumb6775db1994-08-01 11:34:53 +0000325
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000326
327 dispatch = {
328 "call": trace_dispatch_call,
329 "exception": trace_dispatch_exception,
330 "return": trace_dispatch_return,
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000331 "c_call": trace_dispatch_c_call,
Armin Rigof8790242005-09-20 18:50:13 +0000332 "c_exception": trace_dispatch_return, # the C function returned
Nicholas Bastinc69ebe82004-03-24 21:57:10 +0000333 "c_return": trace_dispatch_return,
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000334 }
335
336
Tim Petersdb1ed2a2001-10-07 04:30:53 +0000337 # The next few functions play with self.cmd. By carefully preloading
Tim Peters2344fae2001-01-15 00:50:52 +0000338 # our parallel stack, we can force the profiled result to include
339 # an arbitrary string as the name of the calling function.
340 # We use self.cmd as that string, and the resulting stats look
341 # very nice :-).
Guido van Rossumb6775db1994-08-01 11:34:53 +0000342
Tim Peters2344fae2001-01-15 00:50:52 +0000343 def set_cmd(self, cmd):
Tim Petersdf5cfd82001-10-05 23:15:10 +0000344 if self.cur[-1]: return # already set
Tim Peters2344fae2001-01-15 00:50:52 +0000345 self.cmd = cmd
346 self.simulate_call(cmd)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000347
Tim Peters2344fae2001-01-15 00:50:52 +0000348 class fake_code:
349 def __init__(self, filename, line, name):
350 self.co_filename = filename
351 self.co_line = line
352 self.co_name = name
353 self.co_firstlineno = 0
Guido van Rossumb6775db1994-08-01 11:34:53 +0000354
Tim Peters2344fae2001-01-15 00:50:52 +0000355 def __repr__(self):
356 return repr((self.co_filename, self.co_line, self.co_name))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000357
Tim Peters2344fae2001-01-15 00:50:52 +0000358 class fake_frame:
359 def __init__(self, code, prior):
360 self.f_code = code
361 self.f_back = prior
Guido van Rossumb6775db1994-08-01 11:34:53 +0000362
Tim Peters2344fae2001-01-15 00:50:52 +0000363 def simulate_call(self, name):
364 code = self.fake_code('profile', 0, name)
365 if self.cur:
Tim Petersdf5cfd82001-10-05 23:15:10 +0000366 pframe = self.cur[-2]
Tim Peters2344fae2001-01-15 00:50:52 +0000367 else:
368 pframe = None
369 frame = self.fake_frame(code, pframe)
Fred Draked10ed8b2001-10-17 01:49:50 +0000370 self.dispatch['call'](self, frame, 0)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000371
Tim Peters2344fae2001-01-15 00:50:52 +0000372 # collect stats from pending stack, including getting final
373 # timings for self.cmd frame.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000374
Tim Peters2344fae2001-01-15 00:50:52 +0000375 def simulate_cmd_complete(self):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000376 get_time = self.get_time
377 t = get_time() - self.t
Tim Petersdf5cfd82001-10-05 23:15:10 +0000378 while self.cur[-1]:
Tim Peters2344fae2001-01-15 00:50:52 +0000379 # We *can* cause assertion errors here if
380 # dispatch_trace_return checks for a frame match!
Fred Draked10ed8b2001-10-17 01:49:50 +0000381 self.dispatch['return'](self, self.cur[-2], t)
Tim Peters2344fae2001-01-15 00:50:52 +0000382 t = 0
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000383 self.t = get_time() - t
Guido van Rossumb6775db1994-08-01 11:34:53 +0000384
385
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000386 def print_stats(self, sort=-1):
Tim Peters2344fae2001-01-15 00:50:52 +0000387 import pstats
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000388 pstats.Stats(self).strip_dirs().sort_stats(sort). \
Tim Peters2344fae2001-01-15 00:50:52 +0000389 print_stats()
Guido van Rossumb6775db1994-08-01 11:34:53 +0000390
Tim Peters2344fae2001-01-15 00:50:52 +0000391 def dump_stats(self, file):
Giampaolo Rodola'2f50aaf2013-02-12 02:04:27 +0100392 with open(file, 'wb') as f:
393 self.create_stats()
394 marshal.dump(self.stats, f)
Tim Peters2344fae2001-01-15 00:50:52 +0000395
396 def create_stats(self):
397 self.simulate_cmd_complete()
398 self.snapshot_stats()
399
400 def snapshot_stats(self):
401 self.stats = {}
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000402 for func, (cc, ns, tt, ct, callers) in self.timings.items():
Tim Peters2344fae2001-01-15 00:50:52 +0000403 callers = callers.copy()
404 nc = 0
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000405 for callcnt in callers.values():
Raymond Hettingere0d49722002-06-02 18:55:56 +0000406 nc += callcnt
Tim Peters2344fae2001-01-15 00:50:52 +0000407 self.stats[func] = cc, nc, tt, ct, callers
Guido van Rossumb6775db1994-08-01 11:34:53 +0000408
409
Tim Peters2344fae2001-01-15 00:50:52 +0000410 # The following two methods can be called by clients to use
411 # a profiler to profile a statement, given as a string.
Guido van Rossumb6775db1994-08-01 11:34:53 +0000412
Tim Peters2344fae2001-01-15 00:50:52 +0000413 def run(self, cmd):
414 import __main__
415 dict = __main__.__dict__
416 return self.runctx(cmd, dict, dict)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000417
Tim Peters2344fae2001-01-15 00:50:52 +0000418 def runctx(self, cmd, globals, locals):
419 self.set_cmd(cmd)
420 sys.setprofile(self.dispatcher)
421 try:
Georg Brandl7cae87c2006-09-06 06:51:57 +0000422 exec(cmd, globals, locals)
Tim Peters2344fae2001-01-15 00:50:52 +0000423 finally:
424 sys.setprofile(None)
425 return self
Guido van Rossumb6775db1994-08-01 11:34:53 +0000426
Tim Peters2344fae2001-01-15 00:50:52 +0000427 # This method is more useful to profile a single function call.
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000428 def runcall(self, func, *args, **kw):
Walter Dörwald70a6b492004-02-12 17:35:32 +0000429 self.set_cmd(repr(func))
Tim Peters2344fae2001-01-15 00:50:52 +0000430 sys.setprofile(self.dispatcher)
431 try:
Guido van Rossum68468eb2003-02-27 20:14:51 +0000432 return func(*args, **kw)
Tim Peters2344fae2001-01-15 00:50:52 +0000433 finally:
434 sys.setprofile(None)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000435
Tim Peters2344fae2001-01-15 00:50:52 +0000436
437 #******************************************************************
438 # The following calculates the overhead for using a profiler. The
439 # problem is that it takes a fair amount of time for the profiler
440 # to stop the stopwatch (from the time it receives an event).
441 # Similarly, there is a delay from the time that the profiler
442 # re-starts the stopwatch before the user's code really gets to
443 # continue. The following code tries to measure the difference on
Tim Peters659a6032001-10-09 20:51:19 +0000444 # a per-event basis.
445 #
446 # Note that this difference is only significant if there are a lot of
Tim Peters2344fae2001-01-15 00:50:52 +0000447 # events, and relatively little user code per event. For example,
448 # code with small functions will typically benefit from having the
449 # profiler calibrated for the current platform. This *could* be
450 # done on the fly during init() time, but it is not worth the
451 # effort. Also note that if too large a value specified, then
452 # execution time on some functions will actually appear as a
453 # negative number. It is *normal* for some functions (with very
454 # low call counts) to have such negative stats, even if the
455 # calibration figure is "correct."
456 #
457 # One alternative to profile-time calibration adjustments (i.e.,
458 # adding in the magic little delta during each event) is to track
459 # more carefully the number of events (and cumulatively, the number
460 # of events during sub functions) that are seen. If this were
461 # done, then the arithmetic could be done after the fact (i.e., at
462 # display time). Currently, we track only call/return events.
463 # These values can be deduced by examining the callees and callers
464 # vectors for each functions. Hence we *can* almost correct the
465 # internal time figure at print time (note that we currently don't
466 # track exception event processing counts). Unfortunately, there
467 # is currently no similar information for cumulative sub-function
468 # time. It would not be hard to "get all this info" at profiler
469 # time. Specifically, we would have to extend the tuples to keep
470 # counts of this in each frame, and then extend the defs of timing
471 # tuples to include the significant two figures. I'm a bit fearful
472 # that this additional feature will slow the heavily optimized
473 # event/time ratio (i.e., the profiler would run slower, fur a very
474 # low "value added" feature.)
Tim Peters2344fae2001-01-15 00:50:52 +0000475 #**************************************************************
476
Tim Peterscce092d2001-10-09 05:31:56 +0000477 def calibrate(self, m, verbose=0):
Tim Peters659a6032001-10-09 20:51:19 +0000478 if self.__class__ is not Profile:
479 raise TypeError("Subclasses must override .calibrate().")
480
481 saved_bias = self.bias
482 self.bias = 0
483 try:
Tim Peterse13cc922001-10-09 21:01:31 +0000484 return self._calibrate_inner(m, verbose)
Tim Peters659a6032001-10-09 20:51:19 +0000485 finally:
486 self.bias = saved_bias
487
Tim Peterse13cc922001-10-09 21:01:31 +0000488 def _calibrate_inner(self, m, verbose):
Fred Drakeedb5ffb2001-06-08 04:25:24 +0000489 get_time = self.get_time
Tim Peters2344fae2001-01-15 00:50:52 +0000490
Tim Peterscce092d2001-10-09 05:31:56 +0000491 # Set up a test case to be run with and without profiling. Include
492 # lots of calls, because we're trying to quantify stopwatch overhead.
493 # Do not raise any exceptions, though, because we want to know
494 # exactly how many profile events are generated (one call event, +
495 # one return event, per Python-level call).
Tim Peters2344fae2001-01-15 00:50:52 +0000496
Tim Peterscce092d2001-10-09 05:31:56 +0000497 def f1(n):
498 for i in range(n):
499 x = 1
Tim Peters2344fae2001-01-15 00:50:52 +0000500
Tim Peterscce092d2001-10-09 05:31:56 +0000501 def f(m, f1=f1):
502 for i in range(m):
503 f1(100)
Tim Peters2344fae2001-01-15 00:50:52 +0000504
Tim Peterscce092d2001-10-09 05:31:56 +0000505 f(m) # warm up the cache
506
507 # elapsed_noprofile <- time f(m) takes without profiling.
508 t0 = get_time()
509 f(m)
510 t1 = get_time()
511 elapsed_noprofile = t1 - t0
512 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000513 print("elapsed time without profiling =", elapsed_noprofile)
Tim Peterscce092d2001-10-09 05:31:56 +0000514
515 # elapsed_profile <- time f(m) takes with profiling. The difference
516 # is profiling overhead, only some of which the profiler subtracts
517 # out on its own.
518 p = Profile()
519 t0 = get_time()
520 p.runctx('f(m)', globals(), locals())
521 t1 = get_time()
522 elapsed_profile = t1 - t0
523 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000524 print("elapsed time with profiling =", elapsed_profile)
Tim Peterscce092d2001-10-09 05:31:56 +0000525
526 # reported_time <- "CPU seconds" the profiler charged to f and f1.
527 total_calls = 0.0
528 reported_time = 0.0
529 for (filename, line, funcname), (cc, ns, tt, ct, callers) in \
530 p.timings.items():
531 if funcname in ("f", "f1"):
532 total_calls += cc
533 reported_time += tt
534
535 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000536 print("'CPU seconds' profiler reported =", reported_time)
537 print("total # calls =", total_calls)
Tim Peterscce092d2001-10-09 05:31:56 +0000538 if total_calls != m + 1:
539 raise ValueError("internal error: total calls = %d" % total_calls)
540
541 # reported_time - elapsed_noprofile = overhead the profiler wasn't
542 # able to measure. Divide by twice the number of calls (since there
543 # are two profiler events per call in this test) to get the hidden
544 # overhead per event.
545 mean = (reported_time - elapsed_noprofile) / 2.0 / total_calls
546 if verbose:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000547 print("mean stopwatch overhead per profile event =", mean)
Tim Peterscce092d2001-10-09 05:31:56 +0000548 return mean
Guido van Rossumb6775db1994-08-01 11:34:53 +0000549
Guido van Rossumb6775db1994-08-01 11:34:53 +0000550#****************************************************************************
Guido van Rossumcc778eb1996-10-01 02:55:54 +0000551
Johannes Gijsbers2abe7852005-01-09 01:58:02 +0000552def main():
Serhiy Storchaka7e4db2f2017-05-04 08:17:47 +0300553 import os
554 from optparse import OptionParser
555
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000556 usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
Johannes Gijsbers2abe7852005-01-09 01:58:02 +0000557 parser = OptionParser(usage=usage)
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000558 parser.allow_interspersed_args = False
Tim Peters4e0e1b62004-07-07 20:54:48 +0000559 parser.add_option('-o', '--outfile', dest="outfile",
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000560 help="Save stats to <outfile>", default=None)
561 parser.add_option('-s', '--sort', dest="sort",
Georg Brandl8e43fbf2010-08-02 12:20:23 +0000562 help="Sort order when printing to stdout, based on pstats.Stats class",
563 default=-1)
Tim Petersb497c102005-01-10 16:48:37 +0000564
Johannes Gijsbers2abe7852005-01-09 01:58:02 +0000565 if not sys.argv[1:]:
566 parser.print_usage()
567 sys.exit(2)
Tim Petersb497c102005-01-10 16:48:37 +0000568
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000569 (options, args) = parser.parse_args()
Georg Brandl8e43fbf2010-08-02 12:20:23 +0000570 sys.argv[:] = args
Tim Petersb497c102005-01-10 16:48:37 +0000571
Georg Brandl8e43fbf2010-08-02 12:20:23 +0000572 if len(args) > 0:
573 progname = args[0]
574 sys.path.insert(0, os.path.dirname(progname))
575 with open(progname, 'rb') as fp:
576 code = compile(fp.read(), progname, 'exec')
577 globs = {
578 '__file__': progname,
579 '__name__': '__main__',
580 '__package__': None,
581 '__cached__': None,
582 }
583 runctx(code, globs, None, options.outfile, options.sort)
Nicholas Bastin824b1b22004-03-23 18:44:39 +0000584 else:
Johannes Gijsbers2abe7852005-01-09 01:58:02 +0000585 parser.print_usage()
586 return parser
587
588# When invoked as main program, invoke the profiler on a script
589if __name__ == '__main__':
590 main()