Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 8176258 | 1992-04-21 15:36:23 +0000 | [diff] [blame] | 2 | # |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3 | # Class for profiling python code. rev 1.0 6/2/94 |
Guido van Rossum | 8176258 | 1992-04-21 15:36:23 +0000 | [diff] [blame] | 4 | # |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5 | # Based on prior profile module by Sjoerd Mullender... |
| 6 | # which was hacked somewhat by: Guido van Rossum |
| 7 | # |
| 8 | # See profile.doc for more information |
| 9 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 10 | """Class for profiling Python code.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 11 | |
| 12 | # Copyright 1994, by InfoSeek Corporation, all rights reserved. |
| 13 | # Written by James Roskind |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 14 | # |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 15 | # Permission to use, copy, modify, and distribute this Python software |
| 16 | # and its associated documentation for any purpose (subject to the |
| 17 | # restriction in the following sentence) without fee is hereby granted, |
| 18 | # provided that the above copyright notice appears in all copies, and |
| 19 | # that both that copyright notice and this permission notice appear in |
| 20 | # supporting documentation, and that the name of InfoSeek not be used in |
| 21 | # advertising or publicity pertaining to distribution of the software |
| 22 | # without specific, written prior permission. This permission is |
| 23 | # explicitly restricted to the copying and modification of the software |
| 24 | # to remain in Python, compiled Python, or other languages (such as C) |
| 25 | # wherein the modified or derived code is exclusively imported into a |
| 26 | # Python module. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 27 | # |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 28 | # INFOSEEK CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS |
| 29 | # SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 30 | # FITNESS. IN NO EVENT SHALL INFOSEEK CORPORATION BE LIABLE FOR ANY |
| 31 | # SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 32 | # RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 33 | # CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 34 | # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 35 | |
| 36 | |
Guido van Rossum | 8176258 | 1992-04-21 15:36:23 +0000 | [diff] [blame] | 37 | |
| 38 | import sys |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 39 | import os |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 40 | import time |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 41 | import marshal |
Guido van Rossum | 8176258 | 1992-04-21 15:36:23 +0000 | [diff] [blame] | 42 | |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 43 | __all__ = ["run","help","Profile"] |
Guido van Rossum | 8176258 | 1992-04-21 15:36:23 +0000 | [diff] [blame] | 44 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 45 | # Sample timer for use with |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 46 | #i_count = 0 |
| 47 | #def integer_timer(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 48 | # global i_count |
| 49 | # i_count = i_count + 1 |
| 50 | # return i_count |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 51 | #itimes = integer_timer # replace with C coded timer returning integers |
Guido van Rossum | 8176258 | 1992-04-21 15:36:23 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 53 | #************************************************************************** |
| 54 | # The following are the static member functions for the profiler class |
| 55 | # Note that an instance of Profile() is *not* needed to call them. |
| 56 | #************************************************************************** |
Guido van Rossum | 8176258 | 1992-04-21 15:36:23 +0000 | [diff] [blame] | 57 | |
Jeremy Hylton | adcf8a0 | 2001-03-14 20:01:19 +0000 | [diff] [blame] | 58 | def run(statement, filename=None): |
| 59 | """Run statement under profiler optionally saving results in filename |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 60 | |
Jeremy Hylton | adcf8a0 | 2001-03-14 20:01:19 +0000 | [diff] [blame] | 61 | This function takes a single argument that can be passed to the |
| 62 | "exec" statement, and an optional file name. In all cases this |
| 63 | routine attempts to "exec" its first argument and gather profiling |
| 64 | statistics from the execution. If no file name is present, then this |
| 65 | function automatically prints a simple profiling report, sorted by the |
| 66 | standard name string (file/line/function-name) that is presented in |
| 67 | each line. |
| 68 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 69 | prof = Profile() |
| 70 | try: |
| 71 | prof = prof.run(statement) |
| 72 | except SystemExit: |
| 73 | pass |
Jeremy Hylton | adcf8a0 | 2001-03-14 20:01:19 +0000 | [diff] [blame] | 74 | if filename is not None: |
| 75 | prof.dump_stats(filename) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 76 | else: |
| 77 | return prof.print_stats() |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 78 | |
| 79 | # print help |
| 80 | def help(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 81 | for dirname in sys.path: |
| 82 | fullname = os.path.join(dirname, 'profile.doc') |
| 83 | if os.path.exists(fullname): |
| 84 | sts = os.system('${PAGER-more} '+fullname) |
| 85 | if sts: print '*** Pager exit status:', sts |
| 86 | break |
| 87 | else: |
| 88 | print 'Sorry, can\'t find the help file "profile.doc"', |
| 89 | print 'along the Python search path' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 90 | |
| 91 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 92 | if os.name == "mac": |
Jack Jansen | 1bdcadd | 2001-06-19 20:11:36 +0000 | [diff] [blame] | 93 | import MacOS |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 94 | def _get_time_mac(timer=MacOS.GetTicks): |
| 95 | return timer() / 60.0 |
| 96 | |
| 97 | if hasattr(os, "times"): |
| 98 | def _get_time_times(timer=os.times): |
| 99 | t = timer() |
| 100 | return t[0] + t[1] |
| 101 | |
| 102 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 103 | class Profile: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 104 | """Profiler class. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 105 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 106 | self.cur is always a tuple. Each such tuple corresponds to a stack |
| 107 | frame that is currently active (self.cur[-2]). The following are the |
| 108 | definitions of its members. We use this external "parallel stack" to |
| 109 | avoid contaminating the program that we are profiling. (old profiler |
| 110 | used to write into the frames local dictionary!!) Derived classes |
| 111 | can change the definition of some entries, as long as they leave |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame^] | 112 | [3:] intact. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame^] | 114 | [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 |
| 118 | subfunctions |
| 119 | [2] = Cumulative time spent in this frame's function, including time in |
| 120 | all subfunctions to this frame (but excluding this frame!). |
| 121 | [3] = Name of the function that corresponds to this frame. |
| 122 | [4] = Actual frame that we correspond to (used to sync exception handling) |
| 123 | [5] = Our parent 6-tuple (corresponds to frame.f_back) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 124 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 125 | Timing data for each function is stored as a 5-tuple in the dictionary |
| 126 | self.timings[]. The index is always the name stored in self.cur[4]. |
| 127 | The following are the definitions of the members: |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 128 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 129 | [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. |
| 137 | [5] = A dictionary indicating for each function name, the number of times |
| 138 | it was called by us. |
| 139 | """ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 140 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 141 | def __init__(self, timer=None): |
| 142 | self.timings = {} |
| 143 | self.cur = None |
| 144 | self.cmd = "" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 145 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 146 | if not timer: |
| 147 | if os.name == 'mac': |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 148 | self.timer = MacOS.GetTicks |
| 149 | self.dispatcher = self.trace_dispatch_mac |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 150 | self.get_time = _get_time_mac |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 151 | elif hasattr(time, 'clock'): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 152 | self.timer = self.get_time = time.clock |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 153 | self.dispatcher = self.trace_dispatch_i |
| 154 | elif hasattr(os, 'times'): |
| 155 | self.timer = os.times |
| 156 | self.dispatcher = self.trace_dispatch |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 157 | self.get_time = _get_time_times |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 158 | else: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 159 | self.timer = self.get_time = time.time |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 160 | self.dispatcher = self.trace_dispatch_i |
| 161 | else: |
| 162 | self.timer = timer |
| 163 | t = self.timer() # test out timer function |
| 164 | try: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 165 | length = len(t) |
| 166 | except TypeError: |
| 167 | self.get_time = timer |
| 168 | self.dispatcher = self.trace_dispatch_i |
| 169 | else: |
| 170 | if length == 2: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 171 | self.dispatcher = self.trace_dispatch |
| 172 | else: |
| 173 | self.dispatcher = self.trace_dispatch_l |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 174 | # This get_time() implementation needs to be defined |
| 175 | # here to capture the passed-in timer in the parameter |
| 176 | # list (for performance). Note that we can't assume |
| 177 | # the timer() result contains two values in all |
| 178 | # cases. |
Guido van Rossum | e4deb95 | 2001-08-09 21:22:15 +0000 | [diff] [blame] | 179 | import operator |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 180 | def get_time_timer(timer=timer, |
| 181 | reduce=reduce, reducer=operator.add): |
| 182 | return reduce(reducer, timer(), 0) |
| 183 | self.get_time = get_time_timer |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 184 | self.t = self.get_time() |
| 185 | self.simulate_call('profiler') |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 186 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 187 | # Heavily optimized dispatch routine for os.times() timer |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 188 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 189 | def trace_dispatch(self, frame, event, arg): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 190 | timer = self.timer |
| 191 | t = timer() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 192 | t = t[0] + t[1] - self.t # No Calibration constant |
| 193 | # t = t[0] + t[1] - self.t - .00053 # Calibration constant |
| 194 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 195 | if self.dispatch[event](self, frame,t): |
| 196 | t = timer() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 197 | self.t = t[0] + t[1] |
| 198 | else: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 199 | r = timer() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 200 | self.t = r[0] + r[1] - t # put back unrecorded delta |
| 201 | return |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | |
| 203 | |
| 204 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 205 | # Dispatch routine for best timer program (return = scalar integer) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 206 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 207 | def trace_dispatch_i(self, frame, event, arg): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 208 | timer = self.timer |
| 209 | t = timer() - self.t # - 1 # Integer calibration constant |
| 210 | if self.dispatch[event](self, frame,t): |
| 211 | self.t = timer() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 212 | else: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 213 | self.t = timer() - t # put back unrecorded delta |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 214 | return |
Guido van Rossum | cbf3dd5 | 1997-10-08 15:23:02 +0000 | [diff] [blame] | 215 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 216 | # Dispatch routine for macintosh (timer returns time in ticks of |
| 217 | # 1/60th second) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 218 | |
| 219 | def trace_dispatch_mac(self, frame, event, arg): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 220 | timer = self.timer |
| 221 | t = timer()/60.0 - self.t # - 1 # Integer calibration constant |
| 222 | if self.dispatch[event](self, frame,t): |
| 223 | self.t = timer()/60.0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 224 | else: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 225 | self.t = timer()/60.0 - t # put back unrecorded delta |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 226 | return |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | |
| 228 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 229 | # SLOW generic dispatch routine for timer returning lists of numbers |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 230 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 231 | def trace_dispatch_l(self, frame, event, arg): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 232 | get_time = self.get_time |
| 233 | t = get_time() - self.t |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 235 | if self.dispatch[event](self, frame,t): |
| 236 | self.t = get_time() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 237 | else: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 238 | self.t = get_time() - t # put back unrecorded delta |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 239 | return |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 240 | |
| 241 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 242 | def trace_dispatch_exception(self, frame, t): |
| 243 | rt, rtt, rct, rfn, rframe, rcur = self.cur |
Fred Drake | a0bc999 | 2001-10-03 21:12:32 +0000 | [diff] [blame] | 244 | if (rframe is not frame) and rcur: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 245 | return self.trace_dispatch_return(rframe, t) |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame^] | 246 | self.cur = rt, rtt+t, rct, rfn, rframe, rcur |
| 247 | return 1 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 248 | |
| 249 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 250 | def trace_dispatch_call(self, frame, t): |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame^] | 251 | if self.cur and frame.f_back is not self.cur[4]: |
| 252 | rt, rtt, rct, rfn, rframe, rcur = self.cur |
| 253 | if not isinstance(rframe, Profile.fake_frame): |
| 254 | if rframe.f_back is not frame.f_back: |
| 255 | print rframe, rframe.f_back |
| 256 | print frame, frame.f_back |
| 257 | raise "Bad call", self.cur[3] |
| 258 | self.trace_dispatch_return(rframe, 0) |
| 259 | if self.cur and frame.f_back is not self.cur[4]: |
| 260 | raise "Bad call[2]", self.cur[3] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 261 | fcode = frame.f_code |
| 262 | fn = (fcode.co_filename, fcode.co_firstlineno, fcode.co_name) |
| 263 | self.cur = (t, 0, 0, fn, frame, self.cur) |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 264 | timings = self.timings |
| 265 | if timings.has_key(fn): |
| 266 | cc, ns, tt, ct, callers = timings[fn] |
| 267 | timings[fn] = cc, ns + 1, tt, ct, callers |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 268 | else: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 269 | timings[fn] = 0, 0, 0, 0, {} |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 270 | return 1 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 271 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 272 | def trace_dispatch_return(self, frame, t): |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame^] | 273 | if frame is not self.cur[4]: |
| 274 | if frame is self.cur[4].f_back: |
| 275 | self.trace_dispatch_return(self.cur[4], 0) |
| 276 | else: |
| 277 | raise "Bad return", self.cur[3] |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 278 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 279 | # Prefix "r" means part of the Returning or exiting frame |
| 280 | # Prefix "p" means part of the Previous or older frame |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 281 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 282 | rt, rtt, rct, rfn, frame, rcur = self.cur |
| 283 | rtt = rtt + t |
| 284 | sft = rtt + rct |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 285 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 286 | pt, ptt, pct, pfn, pframe, pcur = rcur |
| 287 | self.cur = pt, ptt+rt, pct+sft, pfn, pframe, pcur |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 288 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 289 | timings = self.timings |
| 290 | cc, ns, tt, ct, callers = timings[rfn] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 291 | if not ns: |
| 292 | ct = ct + sft |
| 293 | cc = cc + 1 |
| 294 | if callers.has_key(pfn): |
| 295 | callers[pfn] = callers[pfn] + 1 # hack: gather more |
| 296 | # stats such as the amount of time added to ct courtesy |
| 297 | # of this specific call, and the contribution to cc |
| 298 | # courtesy of this call. |
| 299 | else: |
| 300 | callers[pfn] = 1 |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 301 | timings[rfn] = cc, ns - 1, tt+rtt, ct, callers |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 302 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 303 | return 1 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 304 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 305 | |
| 306 | dispatch = { |
| 307 | "call": trace_dispatch_call, |
| 308 | "exception": trace_dispatch_exception, |
| 309 | "return": trace_dispatch_return, |
| 310 | } |
| 311 | |
| 312 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 313 | # The next few function play with self.cmd. By carefully preloading |
| 314 | # our parallel stack, we can force the profiled result to include |
| 315 | # an arbitrary string as the name of the calling function. |
| 316 | # We use self.cmd as that string, and the resulting stats look |
| 317 | # very nice :-). |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 318 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 319 | def set_cmd(self, cmd): |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame^] | 320 | if self.cur[5]: return # already set |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 321 | self.cmd = cmd |
| 322 | self.simulate_call(cmd) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 323 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 324 | class fake_code: |
| 325 | def __init__(self, filename, line, name): |
| 326 | self.co_filename = filename |
| 327 | self.co_line = line |
| 328 | self.co_name = name |
| 329 | self.co_firstlineno = 0 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 330 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 331 | def __repr__(self): |
| 332 | return repr((self.co_filename, self.co_line, self.co_name)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 333 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 334 | class fake_frame: |
| 335 | def __init__(self, code, prior): |
| 336 | self.f_code = code |
| 337 | self.f_back = prior |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 338 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 339 | def simulate_call(self, name): |
| 340 | code = self.fake_code('profile', 0, name) |
| 341 | if self.cur: |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame^] | 342 | pframe = self.cur[4] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 343 | else: |
| 344 | pframe = None |
| 345 | frame = self.fake_frame(code, pframe) |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 346 | a = self.dispatch['call'](self, frame, 0) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 347 | return |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 348 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 349 | # collect stats from pending stack, including getting final |
| 350 | # timings for self.cmd frame. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 351 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 352 | def simulate_cmd_complete(self): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 353 | get_time = self.get_time |
| 354 | t = get_time() - self.t |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame^] | 355 | while self.cur[5]: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 356 | # We *can* cause assertion errors here if |
| 357 | # dispatch_trace_return checks for a frame match! |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame^] | 358 | a = self.dispatch['return'](self, self.cur[4], t) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 359 | t = 0 |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 360 | self.t = get_time() - t |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 361 | |
| 362 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 363 | def print_stats(self): |
| 364 | import pstats |
| 365 | pstats.Stats(self).strip_dirs().sort_stats(-1). \ |
| 366 | print_stats() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 367 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 368 | def dump_stats(self, file): |
| 369 | f = open(file, 'wb') |
| 370 | self.create_stats() |
| 371 | marshal.dump(self.stats, f) |
| 372 | f.close() |
| 373 | |
| 374 | def create_stats(self): |
| 375 | self.simulate_cmd_complete() |
| 376 | self.snapshot_stats() |
| 377 | |
| 378 | def snapshot_stats(self): |
| 379 | self.stats = {} |
| 380 | for func in self.timings.keys(): |
| 381 | cc, ns, tt, ct, callers = self.timings[func] |
| 382 | callers = callers.copy() |
| 383 | nc = 0 |
| 384 | for func_caller in callers.keys(): |
| 385 | nc = nc + callers[func_caller] |
| 386 | self.stats[func] = cc, nc, tt, ct, callers |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 387 | |
| 388 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 389 | # The following two methods can be called by clients to use |
| 390 | # a profiler to profile a statement, given as a string. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 391 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 392 | def run(self, cmd): |
| 393 | import __main__ |
| 394 | dict = __main__.__dict__ |
| 395 | return self.runctx(cmd, dict, dict) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 396 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 397 | def runctx(self, cmd, globals, locals): |
| 398 | self.set_cmd(cmd) |
| 399 | sys.setprofile(self.dispatcher) |
| 400 | try: |
| 401 | exec cmd in globals, locals |
| 402 | finally: |
| 403 | sys.setprofile(None) |
| 404 | return self |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 405 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 406 | # This method is more useful to profile a single function call. |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 407 | def runcall(self, func, *args, **kw): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 408 | self.set_cmd(`func`) |
| 409 | sys.setprofile(self.dispatcher) |
| 410 | try: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 411 | return apply(func, args, kw) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 412 | finally: |
| 413 | sys.setprofile(None) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 414 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 415 | |
| 416 | #****************************************************************** |
| 417 | # The following calculates the overhead for using a profiler. The |
| 418 | # problem is that it takes a fair amount of time for the profiler |
| 419 | # to stop the stopwatch (from the time it receives an event). |
| 420 | # Similarly, there is a delay from the time that the profiler |
| 421 | # re-starts the stopwatch before the user's code really gets to |
| 422 | # continue. The following code tries to measure the difference on |
| 423 | # a per-event basis. The result can the be placed in the |
| 424 | # Profile.dispatch_event() routine for the given platform. Note |
| 425 | # that this difference is only significant if there are a lot of |
| 426 | # events, and relatively little user code per event. For example, |
| 427 | # code with small functions will typically benefit from having the |
| 428 | # profiler calibrated for the current platform. This *could* be |
| 429 | # done on the fly during init() time, but it is not worth the |
| 430 | # effort. Also note that if too large a value specified, then |
| 431 | # execution time on some functions will actually appear as a |
| 432 | # negative number. It is *normal* for some functions (with very |
| 433 | # low call counts) to have such negative stats, even if the |
| 434 | # calibration figure is "correct." |
| 435 | # |
| 436 | # One alternative to profile-time calibration adjustments (i.e., |
| 437 | # adding in the magic little delta during each event) is to track |
| 438 | # more carefully the number of events (and cumulatively, the number |
| 439 | # of events during sub functions) that are seen. If this were |
| 440 | # done, then the arithmetic could be done after the fact (i.e., at |
| 441 | # display time). Currently, we track only call/return events. |
| 442 | # These values can be deduced by examining the callees and callers |
| 443 | # vectors for each functions. Hence we *can* almost correct the |
| 444 | # internal time figure at print time (note that we currently don't |
| 445 | # track exception event processing counts). Unfortunately, there |
| 446 | # is currently no similar information for cumulative sub-function |
| 447 | # time. It would not be hard to "get all this info" at profiler |
| 448 | # time. Specifically, we would have to extend the tuples to keep |
| 449 | # counts of this in each frame, and then extend the defs of timing |
| 450 | # tuples to include the significant two figures. I'm a bit fearful |
| 451 | # that this additional feature will slow the heavily optimized |
| 452 | # event/time ratio (i.e., the profiler would run slower, fur a very |
| 453 | # low "value added" feature.) |
| 454 | # |
| 455 | # Plugging in the calibration constant doesn't slow down the |
| 456 | # profiler very much, and the accuracy goes way up. |
| 457 | #************************************************************** |
| 458 | |
| 459 | def calibrate(self, m): |
| 460 | # Modified by Tim Peters |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 461 | get_time = self.get_time |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 462 | n = m |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 463 | s = get_time() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 464 | while n: |
| 465 | self.simple() |
| 466 | n = n - 1 |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 467 | f = get_time() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 468 | my_simple = f - s |
| 469 | #print "Simple =", my_simple, |
| 470 | |
| 471 | n = m |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 472 | s = get_time() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 473 | while n: |
| 474 | self.instrumented() |
| 475 | n = n - 1 |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 476 | f = get_time() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 477 | my_inst = f - s |
| 478 | # print "Instrumented =", my_inst |
| 479 | avg_cost = (my_inst - my_simple)/m |
| 480 | #print "Delta/call =", avg_cost, "(profiler fixup constant)" |
| 481 | return avg_cost |
| 482 | |
| 483 | # simulate a program with no profiler activity |
| 484 | def simple(self): |
| 485 | a = 1 |
| 486 | pass |
| 487 | |
| 488 | # simulate a program with call/return event processing |
| 489 | def instrumented(self): |
| 490 | a = 1 |
| 491 | self.profiler_simulation(a, a, a) |
| 492 | |
| 493 | # simulate an event processing activity (from user's perspective) |
| 494 | def profiler_simulation(self, x, y, z): |
| 495 | t = self.timer() |
| 496 | ## t = t[0] + t[1] |
| 497 | self.ut = t |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 498 | |
| 499 | |
| 500 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 501 | class OldProfile(Profile): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 502 | """A derived profiler that simulates the old style profile, providing |
| 503 | errant results on recursive functions. The reason for the usefulness of |
| 504 | this profiler is that it runs faster (i.e., less overhead). It still |
| 505 | creates all the caller stats, and is quite useful when there is *no* |
| 506 | recursion in the user's code. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 507 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 508 | This code also shows how easy it is to create a modified profiler. |
| 509 | """ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 510 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 511 | def trace_dispatch_exception(self, frame, t): |
| 512 | rt, rtt, rct, rfn, rframe, rcur = self.cur |
| 513 | if rcur and not rframe is frame: |
| 514 | return self.trace_dispatch_return(rframe, t) |
| 515 | return 0 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 516 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 517 | def trace_dispatch_call(self, frame, t): |
| 518 | fn = `frame.f_code` |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 519 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 520 | self.cur = (t, 0, 0, fn, frame, self.cur) |
| 521 | if self.timings.has_key(fn): |
| 522 | tt, ct, callers = self.timings[fn] |
| 523 | self.timings[fn] = tt, ct, callers |
| 524 | else: |
| 525 | self.timings[fn] = 0, 0, {} |
| 526 | return 1 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 527 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 528 | def trace_dispatch_return(self, frame, t): |
| 529 | rt, rtt, rct, rfn, frame, rcur = self.cur |
| 530 | rtt = rtt + t |
| 531 | sft = rtt + rct |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 532 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 533 | pt, ptt, pct, pfn, pframe, pcur = rcur |
| 534 | self.cur = pt, ptt+rt, pct+sft, pfn, pframe, pcur |
| 535 | |
| 536 | tt, ct, callers = self.timings[rfn] |
| 537 | if callers.has_key(pfn): |
| 538 | callers[pfn] = callers[pfn] + 1 |
| 539 | else: |
| 540 | callers[pfn] = 1 |
| 541 | self.timings[rfn] = tt+rtt, ct + sft, callers |
| 542 | |
| 543 | return 1 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 544 | |
| 545 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 546 | dispatch = { |
| 547 | "call": trace_dispatch_call, |
| 548 | "exception": trace_dispatch_exception, |
| 549 | "return": trace_dispatch_return, |
| 550 | } |
| 551 | |
| 552 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 553 | def snapshot_stats(self): |
| 554 | self.stats = {} |
| 555 | for func in self.timings.keys(): |
| 556 | tt, ct, callers = self.timings[func] |
| 557 | callers = callers.copy() |
| 558 | nc = 0 |
| 559 | for func_caller in callers.keys(): |
| 560 | nc = nc + callers[func_caller] |
| 561 | self.stats[func] = nc, nc, tt, ct, callers |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 562 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 563 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 564 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 565 | class HotProfile(Profile): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 566 | """The fastest derived profile example. It does not calculate |
| 567 | caller-callee relationships, and does not calculate cumulative |
| 568 | time under a function. It only calculates time spent in a |
| 569 | function, so it runs very quickly due to its very low overhead. |
| 570 | """ |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 571 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 572 | def trace_dispatch_exception(self, frame, t): |
| 573 | rt, rtt, rfn, rframe, rcur = self.cur |
| 574 | if rcur and not rframe is frame: |
| 575 | return self.trace_dispatch_return(rframe, t) |
| 576 | return 0 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 577 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 578 | def trace_dispatch_call(self, frame, t): |
| 579 | self.cur = (t, 0, frame, self.cur) |
| 580 | return 1 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 581 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 582 | def trace_dispatch_return(self, frame, t): |
| 583 | rt, rtt, frame, rcur = self.cur |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 584 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 585 | rfn = `frame.f_code` |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 586 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 587 | pt, ptt, pframe, pcur = rcur |
| 588 | self.cur = pt, ptt+rt, pframe, pcur |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 589 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 590 | if self.timings.has_key(rfn): |
| 591 | nc, tt = self.timings[rfn] |
| 592 | self.timings[rfn] = nc + 1, rt + rtt + tt |
| 593 | else: |
| 594 | self.timings[rfn] = 1, rt + rtt |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 595 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 596 | return 1 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 597 | |
| 598 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 599 | dispatch = { |
| 600 | "call": trace_dispatch_call, |
| 601 | "exception": trace_dispatch_exception, |
| 602 | "return": trace_dispatch_return, |
| 603 | } |
| 604 | |
| 605 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 606 | def snapshot_stats(self): |
| 607 | self.stats = {} |
| 608 | for func in self.timings.keys(): |
| 609 | nc, tt = self.timings[func] |
| 610 | self.stats[func] = nc, nc, tt, 0, {} |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 611 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 612 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 613 | |
| 614 | #**************************************************************************** |
| 615 | def Stats(*args): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 616 | print 'Report generating functions are in the "pstats" module\a' |
Guido van Rossum | cc778eb | 1996-10-01 02:55:54 +0000 | [diff] [blame] | 617 | |
| 618 | |
| 619 | # When invoked as main program, invoke the profiler on a script |
| 620 | if __name__ == '__main__': |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 621 | if not sys.argv[1:]: |
| 622 | print "usage: profile.py scriptfile [arg] ..." |
| 623 | sys.exit(2) |
Guido van Rossum | cc778eb | 1996-10-01 02:55:54 +0000 | [diff] [blame] | 624 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 625 | filename = sys.argv[1] # Get script filename |
Guido van Rossum | cc778eb | 1996-10-01 02:55:54 +0000 | [diff] [blame] | 626 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 627 | del sys.argv[0] # Hide "profile.py" from argument list |
Guido van Rossum | cc778eb | 1996-10-01 02:55:54 +0000 | [diff] [blame] | 628 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 629 | # Insert script directory in front of module search path |
| 630 | sys.path.insert(0, os.path.dirname(filename)) |
Guido van Rossum | cc778eb | 1996-10-01 02:55:54 +0000 | [diff] [blame] | 631 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 632 | run('execfile(' + `filename` + ')') |