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 | # |
Benjamin Peterson | 7e6b3aa | 2011-06-27 09:14:34 -0500 | [diff] [blame] | 5 | # Written by James Roskind |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 6 | # Based on prior profile module by Sjoerd Mullender... |
| 7 | # which was hacked somewhat by: Guido van Rossum |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 9 | """Class for profiling Python code.""" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10 | |
Benjamin Peterson | 7e6b3aa | 2011-06-27 09:14:34 -0500 | [diff] [blame] | 11 | # Copyright Disney Enterprises, Inc. All Rights Reserved. |
| 12 | # Licensed to PSF under a Contributor Agreement |
Benjamin Peterson | cfb7731 | 2011-06-27 09:18:46 -0500 | [diff] [blame] | 13 | # |
Benjamin Peterson | 7e6b3aa | 2011-06-27 09:14:34 -0500 | [diff] [blame] | 14 | # 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 Peterson | cfb7731 | 2011-06-27 09:18:46 -0500 | [diff] [blame] | 17 | # |
Benjamin Peterson | 7e6b3aa | 2011-06-27 09:14:34 -0500 | [diff] [blame] | 18 | # http://www.apache.org/licenses/LICENSE-2.0 |
Benjamin Peterson | cfb7731 | 2011-06-27 09:18:46 -0500 | [diff] [blame] | 19 | # |
Benjamin Peterson | 7e6b3aa | 2011-06-27 09:14:34 -0500 | [diff] [blame] | 20 | # 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 Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 8176258 | 1992-04-21 15:36:23 +0000 | [diff] [blame] | 26 | |
| 27 | import sys |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 28 | import os |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 29 | import time |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 30 | import marshal |
Nicholas Bastin | 824b1b2 | 2004-03-23 18:44:39 +0000 | [diff] [blame] | 31 | from optparse import OptionParser |
Guido van Rossum | 8176258 | 1992-04-21 15:36:23 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 48713e8 | 2004-03-23 19:19:21 +0000 | [diff] [blame] | 33 | __all__ = ["run", "runctx", "help", "Profile"] |
Guido van Rossum | 8176258 | 1992-04-21 15:36:23 +0000 | [diff] [blame] | 34 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 35 | # Sample timer for use with |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 36 | #i_count = 0 |
| 37 | #def integer_timer(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 38 | # global i_count |
| 39 | # i_count = i_count + 1 |
| 40 | # return i_count |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 41 | #itimes = integer_timer # replace with C coded timer returning integers |
Guido van Rossum | 8176258 | 1992-04-21 15:36:23 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 43 | #************************************************************************** |
| 44 | # The following are the static member functions for the profiler class |
| 45 | # Note that an instance of Profile() is *not* needed to call them. |
| 46 | #************************************************************************** |
Guido van Rossum | 8176258 | 1992-04-21 15:36:23 +0000 | [diff] [blame] | 47 | |
Nicholas Bastin | 824b1b2 | 2004-03-23 18:44:39 +0000 | [diff] [blame] | 48 | def run(statement, filename=None, sort=-1): |
Jeremy Hylton | adcf8a0 | 2001-03-14 20:01:19 +0000 | [diff] [blame] | 49 | """Run statement under profiler optionally saving results in filename |
Guido van Rossum | 4e16098 | 1992-09-02 20:43:20 +0000 | [diff] [blame] | 50 | |
Jeremy Hylton | adcf8a0 | 2001-03-14 20:01:19 +0000 | [diff] [blame] | 51 | This function takes a single argument that can be passed to the |
| 52 | "exec" statement, and an optional file name. In all cases this |
| 53 | routine attempts to "exec" its first argument and gather profiling |
| 54 | statistics from the execution. If no file name is present, then this |
| 55 | function automatically prints a simple profiling report, sorted by the |
| 56 | standard name string (file/line/function-name) that is presented in |
| 57 | each line. |
| 58 | """ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 59 | prof = Profile() |
| 60 | try: |
| 61 | prof = prof.run(statement) |
| 62 | except SystemExit: |
| 63 | pass |
Jeremy Hylton | adcf8a0 | 2001-03-14 20:01:19 +0000 | [diff] [blame] | 64 | if filename is not None: |
| 65 | prof.dump_stats(filename) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 66 | else: |
Nicholas Bastin | 824b1b2 | 2004-03-23 18:44:39 +0000 | [diff] [blame] | 67 | return prof.print_stats(sort) |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 68 | |
Florent Xicluna | 9284745 | 2010-09-13 17:36:36 +0000 | [diff] [blame] | 69 | def runctx(statement, globals, locals, filename=None, sort=-1): |
Nicholas Bastin | 1eb4bfc | 2004-03-22 20:12:56 +0000 | [diff] [blame] | 70 | """Run statement under profiler, supplying your own globals and locals, |
| 71 | optionally saving results in filename. |
| 72 | |
| 73 | statement and filename have the same semantics as profile.run |
| 74 | """ |
| 75 | prof = Profile() |
| 76 | try: |
| 77 | prof = prof.runctx(statement, globals, locals) |
| 78 | except SystemExit: |
| 79 | pass |
| 80 | |
| 81 | if filename is not None: |
| 82 | prof.dump_stats(filename) |
| 83 | else: |
Florent Xicluna | 9284745 | 2010-09-13 17:36:36 +0000 | [diff] [blame] | 84 | return prof.print_stats(sort) |
Nicholas Bastin | 1eb4bfc | 2004-03-22 20:12:56 +0000 | [diff] [blame] | 85 | |
Johannes Gijsbers | c0b194a | 2005-01-10 09:07:22 +0000 | [diff] [blame] | 86 | # Backwards compatibility. |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 87 | def help(): |
Johannes Gijsbers | c0b194a | 2005-01-10 09:07:22 +0000 | [diff] [blame] | 88 | print "Documentation for the profile module can be found " |
| 89 | print "in the Python Library Reference, section 'The Python Profiler'." |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 90 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 91 | if hasattr(os, "times"): |
| 92 | def _get_time_times(timer=os.times): |
| 93 | t = timer() |
| 94 | return t[0] + t[1] |
| 95 | |
Martin v. Löwis | a4dac40 | 2005-03-03 11:39:45 +0000 | [diff] [blame] | 96 | # Using getrusage(3) is better than clock(3) if available: |
| 97 | # on some systems (e.g. FreeBSD), getrusage has a higher resolution |
| 98 | # Furthermore, on a POSIX system, returns microseconds, which |
| 99 | # wrap around after 36min. |
| 100 | _has_res = 0 |
| 101 | try: |
| 102 | import resource |
| 103 | resgetrusage = lambda: resource.getrusage(resource.RUSAGE_SELF) |
| 104 | def _get_time_resource(timer=resgetrusage): |
| 105 | t = timer() |
| 106 | return t[0] + t[1] |
| 107 | _has_res = 1 |
| 108 | except ImportError: |
| 109 | pass |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 111 | class Profile: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 112 | """Profiler class. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 113 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 114 | self.cur is always a tuple. Each such tuple corresponds to a stack |
| 115 | frame that is currently active (self.cur[-2]). The following are the |
| 116 | definitions of its members. We use this external "parallel stack" to |
| 117 | avoid contaminating the program that we are profiling. (old profiler |
| 118 | used to write into the frames local dictionary!!) Derived classes |
| 119 | can change the definition of some entries, as long as they leave |
Tim Peters | df5cfd8 | 2001-10-05 23:15:10 +0000 | [diff] [blame] | 120 | [-2:] intact (frame and previous tuple). In case an internal error is |
| 121 | detected, the -3 element is used as the function name. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 122 | |
Tim Peters | df5cfd8 | 2001-10-05 23:15:10 +0000 | [diff] [blame] | 123 | [ 0] = Time that needs to be charged to the parent frame's function. |
| 124 | It is used so that a function call will not have to access the |
| 125 | timing data for the parent frame. |
| 126 | [ 1] = Total time spent in this frame's function, excluding time in |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 127 | subfunctions (this latter is tallied in cur[2]). |
Tim Peters | fb16378 | 2001-10-07 08:49:02 +0000 | [diff] [blame] | 128 | [ 2] = Total time spent in subfunctions, excluding time executing the |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 129 | frame's function (this latter is tallied in cur[1]). |
Tim Peters | df5cfd8 | 2001-10-05 23:15:10 +0000 | [diff] [blame] | 130 | [-3] = Name of the function that corresponds to this frame. |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 131 | [-2] = Actual frame that we correspond to (used to sync exception handling). |
| 132 | [-1] = Our parent 6-tuple (corresponds to frame.f_back). |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 133 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 134 | Timing data for each function is stored as a 5-tuple in the dictionary |
Tim Peters | df5cfd8 | 2001-10-05 23:15:10 +0000 | [diff] [blame] | 135 | self.timings[]. The index is always the name stored in self.cur[-3]. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 136 | The following are the definitions of the members: |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 137 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 138 | [0] = The number of times this function was called, not counting direct |
| 139 | or indirect recursion, |
| 140 | [1] = Number of times this function appears on the stack, minus one |
| 141 | [2] = Total time spent internal to this function |
| 142 | [3] = Cumulative time that this function was present on the stack. In |
| 143 | non-recursive functions, this is the total execution time from start |
| 144 | to finish of each invocation of a function, including time spent in |
| 145 | all subfunctions. |
Tim Peters | 6e22149 | 2001-10-07 04:02:36 +0000 | [diff] [blame] | 146 | [4] = A dictionary indicating for each function name, the number of times |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 147 | it was called by us. |
| 148 | """ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 149 | |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 150 | bias = 0 # calibration constant |
| 151 | |
| 152 | def __init__(self, timer=None, bias=None): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 153 | self.timings = {} |
| 154 | self.cur = None |
| 155 | self.cmd = "" |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 156 | self.c_func_name = "" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 157 | |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 158 | if bias is None: |
| 159 | bias = self.bias |
| 160 | self.bias = bias # Materialize in local dict for lookup speed. |
| 161 | |
Martin v. Löwis | a4dac40 | 2005-03-03 11:39:45 +0000 | [diff] [blame] | 162 | if not timer: |
| 163 | if _has_res: |
| 164 | self.timer = resgetrusage |
| 165 | self.dispatcher = self.trace_dispatch |
| 166 | self.get_time = _get_time_resource |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 167 | elif hasattr(time, 'clock'): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 168 | self.timer = self.get_time = time.clock |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 169 | self.dispatcher = self.trace_dispatch_i |
| 170 | elif hasattr(os, 'times'): |
| 171 | self.timer = os.times |
| 172 | self.dispatcher = self.trace_dispatch |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 173 | self.get_time = _get_time_times |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 174 | else: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 175 | self.timer = self.get_time = time.time |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 176 | self.dispatcher = self.trace_dispatch_i |
| 177 | else: |
| 178 | self.timer = timer |
| 179 | t = self.timer() # test out timer function |
| 180 | try: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 181 | length = len(t) |
| 182 | except TypeError: |
| 183 | self.get_time = timer |
| 184 | self.dispatcher = self.trace_dispatch_i |
| 185 | else: |
| 186 | if length == 2: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 187 | self.dispatcher = self.trace_dispatch |
| 188 | else: |
| 189 | self.dispatcher = self.trace_dispatch_l |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 190 | # This get_time() implementation needs to be defined |
| 191 | # here to capture the passed-in timer in the parameter |
| 192 | # list (for performance). Note that we can't assume |
| 193 | # the timer() result contains two values in all |
| 194 | # cases. |
Raymond Hettinger | 97aa32b | 2003-10-22 16:49:01 +0000 | [diff] [blame] | 195 | def get_time_timer(timer=timer, sum=sum): |
| 196 | return sum(timer()) |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 197 | self.get_time = get_time_timer |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 198 | self.t = self.get_time() |
| 199 | self.simulate_call('profiler') |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 200 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 201 | # Heavily optimized dispatch routine for os.times() timer |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 203 | def trace_dispatch(self, frame, event, arg): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 204 | timer = self.timer |
| 205 | t = timer() |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 206 | t = t[0] + t[1] - self.t - self.bias |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 207 | |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 208 | if event == "c_call": |
Nicholas Bastin | 12ac3e1 | 2004-07-12 23:38:02 +0000 | [diff] [blame] | 209 | self.c_func_name = arg.__name__ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 210 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 211 | if self.dispatch[event](self, frame,t): |
| 212 | t = timer() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 213 | self.t = t[0] + t[1] |
| 214 | else: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 215 | r = timer() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 216 | self.t = r[0] + r[1] - t # put back unrecorded delta |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 217 | |
Tim Peters | 6e22149 | 2001-10-07 04:02:36 +0000 | [diff] [blame] | 218 | # Dispatch routine for best timer program (return = scalar, fastest if |
| 219 | # an integer but float works too -- and time.clock() relies on that). |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 220 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 221 | def trace_dispatch_i(self, frame, event, arg): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 222 | timer = self.timer |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 223 | t = timer() - self.t - self.bias |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 224 | |
| 225 | if event == "c_call": |
Nicholas Bastin | 12ac3e1 | 2004-07-12 23:38:02 +0000 | [diff] [blame] | 226 | self.c_func_name = arg.__name__ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 227 | |
| 228 | if self.dispatch[event](self, frame, t): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 229 | self.t = timer() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 230 | else: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 231 | self.t = timer() - t # put back unrecorded delta |
Guido van Rossum | cbf3dd5 | 1997-10-08 15:23:02 +0000 | [diff] [blame] | 232 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 233 | # Dispatch routine for macintosh (timer returns time in ticks of |
| 234 | # 1/60th second) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 235 | |
| 236 | def trace_dispatch_mac(self, frame, event, arg): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 237 | timer = self.timer |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 238 | t = timer()/60.0 - self.t - self.bias |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 239 | |
| 240 | if event == "c_call": |
Nicholas Bastin | 12ac3e1 | 2004-07-12 23:38:02 +0000 | [diff] [blame] | 241 | self.c_func_name = arg.__name__ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 242 | |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 243 | if self.dispatch[event](self, frame, t): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 244 | self.t = timer()/60.0 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 245 | else: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 246 | self.t = timer()/60.0 - t # put back unrecorded delta |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 247 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 248 | # SLOW generic dispatch routine for timer returning lists of numbers |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 249 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 250 | def trace_dispatch_l(self, frame, event, arg): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 251 | get_time = self.get_time |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 252 | t = get_time() - self.t - self.bias |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 253 | |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 254 | if event == "c_call": |
Nicholas Bastin | 12ac3e1 | 2004-07-12 23:38:02 +0000 | [diff] [blame] | 255 | self.c_func_name = arg.__name__ |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 256 | |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 257 | if self.dispatch[event](self, frame, t): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 258 | self.t = get_time() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 259 | else: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 260 | self.t = get_time() - t # put back unrecorded delta |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 261 | |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 262 | # In the event handlers, the first 3 elements of self.cur are unpacked |
| 263 | # into vrbls w/ 3-letter names. The last two characters are meant to be |
| 264 | # mnemonic: |
| 265 | # _pt self.cur[0] "parent time" time to be charged to parent frame |
| 266 | # _it self.cur[1] "internal time" time spent directly in the function |
| 267 | # _et self.cur[2] "external time" time spent in subfunctions |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 269 | def trace_dispatch_exception(self, frame, t): |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 270 | rpt, rit, ret, rfn, rframe, rcur = self.cur |
Fred Drake | a0bc999 | 2001-10-03 21:12:32 +0000 | [diff] [blame] | 271 | if (rframe is not frame) and rcur: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 272 | return self.trace_dispatch_return(rframe, t) |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 273 | self.cur = rpt, rit+t, ret, rfn, rframe, rcur |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 274 | return 1 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 275 | |
| 276 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 277 | def trace_dispatch_call(self, frame, t): |
Tim Peters | df5cfd8 | 2001-10-05 23:15:10 +0000 | [diff] [blame] | 278 | if self.cur and frame.f_back is not self.cur[-2]: |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 279 | rpt, rit, ret, rfn, rframe, rcur = self.cur |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 280 | if not isinstance(rframe, Profile.fake_frame): |
Tim Peters | db1ed2a | 2001-10-07 04:30:53 +0000 | [diff] [blame] | 281 | assert rframe.f_back is frame.f_back, ("Bad call", rfn, |
| 282 | rframe, rframe.f_back, |
| 283 | frame, frame.f_back) |
Guido van Rossum | f137f75 | 2001-10-04 00:58:24 +0000 | [diff] [blame] | 284 | self.trace_dispatch_return(rframe, 0) |
Tim Peters | db1ed2a | 2001-10-07 04:30:53 +0000 | [diff] [blame] | 285 | assert (self.cur is None or \ |
| 286 | frame.f_back is self.cur[-2]), ("Bad call", |
| 287 | self.cur[-3]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 288 | fcode = frame.f_code |
| 289 | fn = (fcode.co_filename, fcode.co_firstlineno, fcode.co_name) |
| 290 | self.cur = (t, 0, 0, fn, frame, self.cur) |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 291 | timings = self.timings |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 292 | if fn in timings: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 293 | cc, ns, tt, ct, callers = timings[fn] |
| 294 | timings[fn] = cc, ns + 1, tt, ct, callers |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 295 | else: |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 296 | timings[fn] = 0, 0, 0, 0, {} |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 297 | return 1 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 298 | |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 299 | def trace_dispatch_c_call (self, frame, t): |
| 300 | fn = ("", 0, self.c_func_name) |
| 301 | self.cur = (t, 0, 0, fn, frame, self.cur) |
| 302 | timings = self.timings |
Brett Cannon | cc2f7b4 | 2008-08-03 22:38:19 +0000 | [diff] [blame] | 303 | if fn in timings: |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 304 | cc, ns, tt, ct, callers = timings[fn] |
| 305 | timings[fn] = cc, ns+1, tt, ct, callers |
| 306 | else: |
| 307 | timings[fn] = 0, 0, 0, 0, {} |
| 308 | return 1 |
| 309 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 310 | def trace_dispatch_return(self, frame, t): |
Tim Peters | df5cfd8 | 2001-10-05 23:15:10 +0000 | [diff] [blame] | 311 | if frame is not self.cur[-2]: |
Tim Peters | db1ed2a | 2001-10-07 04:30:53 +0000 | [diff] [blame] | 312 | assert frame is self.cur[-2].f_back, ("Bad return", self.cur[-3]) |
| 313 | self.trace_dispatch_return(self.cur[-2], 0) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 314 | |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 315 | # Prefix "r" means part of the Returning or exiting frame. |
| 316 | # Prefix "p" means part of the Previous or Parent or older frame. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 317 | |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 318 | rpt, rit, ret, rfn, frame, rcur = self.cur |
| 319 | rit = rit + t |
| 320 | frame_total = rit + ret |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 321 | |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 322 | ppt, pit, pet, pfn, pframe, pcur = rcur |
| 323 | self.cur = ppt, pit + rpt, pet + frame_total, pfn, pframe, pcur |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 324 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 325 | timings = self.timings |
| 326 | cc, ns, tt, ct, callers = timings[rfn] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 327 | if not ns: |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 328 | # This is the only occurrence of the function on the stack. |
| 329 | # Else this is a (directly or indirectly) recursive call, and |
| 330 | # its cumulative time will get updated when the topmost call to |
| 331 | # it returns. |
| 332 | ct = ct + frame_total |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 333 | cc = cc + 1 |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 334 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 335 | if pfn in callers: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 336 | callers[pfn] = callers[pfn] + 1 # hack: gather more |
| 337 | # stats such as the amount of time added to ct courtesy |
| 338 | # of this specific call, and the contribution to cc |
| 339 | # courtesy of this call. |
| 340 | else: |
| 341 | callers[pfn] = 1 |
Tim Peters | 8d061ed | 2001-10-07 08:35:44 +0000 | [diff] [blame] | 342 | |
| 343 | timings[rfn] = cc, ns - 1, tt + rit, ct, callers |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 344 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 345 | return 1 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 346 | |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 347 | |
| 348 | dispatch = { |
| 349 | "call": trace_dispatch_call, |
| 350 | "exception": trace_dispatch_exception, |
| 351 | "return": trace_dispatch_return, |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 352 | "c_call": trace_dispatch_c_call, |
Armin Rigo | f879024 | 2005-09-20 18:50:13 +0000 | [diff] [blame] | 353 | "c_exception": trace_dispatch_return, # the C function returned |
Nicholas Bastin | c69ebe8 | 2004-03-24 21:57:10 +0000 | [diff] [blame] | 354 | "c_return": trace_dispatch_return, |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | |
Tim Peters | db1ed2a | 2001-10-07 04:30:53 +0000 | [diff] [blame] | 358 | # The next few functions play with self.cmd. By carefully preloading |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 359 | # our parallel stack, we can force the profiled result to include |
| 360 | # an arbitrary string as the name of the calling function. |
| 361 | # We use self.cmd as that string, and the resulting stats look |
| 362 | # very nice :-). |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 363 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 364 | def set_cmd(self, cmd): |
Tim Peters | df5cfd8 | 2001-10-05 23:15:10 +0000 | [diff] [blame] | 365 | if self.cur[-1]: return # already set |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 366 | self.cmd = cmd |
| 367 | self.simulate_call(cmd) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 368 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 369 | class fake_code: |
| 370 | def __init__(self, filename, line, name): |
| 371 | self.co_filename = filename |
| 372 | self.co_line = line |
| 373 | self.co_name = name |
| 374 | self.co_firstlineno = 0 |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 375 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 376 | def __repr__(self): |
| 377 | return repr((self.co_filename, self.co_line, self.co_name)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 378 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 379 | class fake_frame: |
| 380 | def __init__(self, code, prior): |
| 381 | self.f_code = code |
| 382 | self.f_back = prior |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 383 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 384 | def simulate_call(self, name): |
| 385 | code = self.fake_code('profile', 0, name) |
| 386 | if self.cur: |
Tim Peters | df5cfd8 | 2001-10-05 23:15:10 +0000 | [diff] [blame] | 387 | pframe = self.cur[-2] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 388 | else: |
| 389 | pframe = None |
| 390 | frame = self.fake_frame(code, pframe) |
Fred Drake | d10ed8b | 2001-10-17 01:49:50 +0000 | [diff] [blame] | 391 | self.dispatch['call'](self, frame, 0) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 392 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 393 | # collect stats from pending stack, including getting final |
| 394 | # timings for self.cmd frame. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 395 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 396 | def simulate_cmd_complete(self): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 397 | get_time = self.get_time |
| 398 | t = get_time() - self.t |
Tim Peters | df5cfd8 | 2001-10-05 23:15:10 +0000 | [diff] [blame] | 399 | while self.cur[-1]: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 400 | # We *can* cause assertion errors here if |
| 401 | # dispatch_trace_return checks for a frame match! |
Fred Drake | d10ed8b | 2001-10-17 01:49:50 +0000 | [diff] [blame] | 402 | self.dispatch['return'](self, self.cur[-2], t) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 403 | t = 0 |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 404 | self.t = get_time() - t |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 405 | |
| 406 | |
Nicholas Bastin | 824b1b2 | 2004-03-23 18:44:39 +0000 | [diff] [blame] | 407 | def print_stats(self, sort=-1): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 408 | import pstats |
Nicholas Bastin | 824b1b2 | 2004-03-23 18:44:39 +0000 | [diff] [blame] | 409 | pstats.Stats(self).strip_dirs().sort_stats(sort). \ |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 410 | print_stats() |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 411 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 412 | def dump_stats(self, file): |
| 413 | f = open(file, 'wb') |
| 414 | self.create_stats() |
| 415 | marshal.dump(self.stats, f) |
| 416 | f.close() |
| 417 | |
| 418 | def create_stats(self): |
| 419 | self.simulate_cmd_complete() |
| 420 | self.snapshot_stats() |
| 421 | |
| 422 | def snapshot_stats(self): |
| 423 | self.stats = {} |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 424 | for func, (cc, ns, tt, ct, callers) in self.timings.iteritems(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 425 | callers = callers.copy() |
| 426 | nc = 0 |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 427 | for callcnt in callers.itervalues(): |
| 428 | nc += callcnt |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 429 | self.stats[func] = cc, nc, tt, ct, callers |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 430 | |
| 431 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 432 | # The following two methods can be called by clients to use |
| 433 | # a profiler to profile a statement, given as a string. |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 434 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 435 | def run(self, cmd): |
| 436 | import __main__ |
| 437 | dict = __main__.__dict__ |
| 438 | return self.runctx(cmd, dict, dict) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 439 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 440 | def runctx(self, cmd, globals, locals): |
| 441 | self.set_cmd(cmd) |
| 442 | sys.setprofile(self.dispatcher) |
| 443 | try: |
| 444 | exec cmd in globals, locals |
| 445 | finally: |
| 446 | sys.setprofile(None) |
| 447 | return self |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 448 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 449 | # This method is more useful to profile a single function call. |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 450 | def runcall(self, func, *args, **kw): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 451 | self.set_cmd(repr(func)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 452 | sys.setprofile(self.dispatcher) |
| 453 | try: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 454 | return func(*args, **kw) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 455 | finally: |
| 456 | sys.setprofile(None) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 457 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 458 | |
| 459 | #****************************************************************** |
| 460 | # The following calculates the overhead for using a profiler. The |
| 461 | # problem is that it takes a fair amount of time for the profiler |
| 462 | # to stop the stopwatch (from the time it receives an event). |
| 463 | # Similarly, there is a delay from the time that the profiler |
| 464 | # re-starts the stopwatch before the user's code really gets to |
| 465 | # continue. The following code tries to measure the difference on |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 466 | # a per-event basis. |
| 467 | # |
| 468 | # Note that this difference is only significant if there are a lot of |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 469 | # events, and relatively little user code per event. For example, |
| 470 | # code with small functions will typically benefit from having the |
| 471 | # profiler calibrated for the current platform. This *could* be |
| 472 | # done on the fly during init() time, but it is not worth the |
| 473 | # effort. Also note that if too large a value specified, then |
| 474 | # execution time on some functions will actually appear as a |
| 475 | # negative number. It is *normal* for some functions (with very |
| 476 | # low call counts) to have such negative stats, even if the |
| 477 | # calibration figure is "correct." |
| 478 | # |
| 479 | # One alternative to profile-time calibration adjustments (i.e., |
| 480 | # adding in the magic little delta during each event) is to track |
| 481 | # more carefully the number of events (and cumulatively, the number |
| 482 | # of events during sub functions) that are seen. If this were |
| 483 | # done, then the arithmetic could be done after the fact (i.e., at |
| 484 | # display time). Currently, we track only call/return events. |
| 485 | # These values can be deduced by examining the callees and callers |
| 486 | # vectors for each functions. Hence we *can* almost correct the |
| 487 | # internal time figure at print time (note that we currently don't |
| 488 | # track exception event processing counts). Unfortunately, there |
| 489 | # is currently no similar information for cumulative sub-function |
| 490 | # time. It would not be hard to "get all this info" at profiler |
| 491 | # time. Specifically, we would have to extend the tuples to keep |
| 492 | # counts of this in each frame, and then extend the defs of timing |
| 493 | # tuples to include the significant two figures. I'm a bit fearful |
| 494 | # that this additional feature will slow the heavily optimized |
| 495 | # event/time ratio (i.e., the profiler would run slower, fur a very |
| 496 | # low "value added" feature.) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 497 | #************************************************************** |
| 498 | |
Tim Peters | cce092d | 2001-10-09 05:31:56 +0000 | [diff] [blame] | 499 | def calibrate(self, m, verbose=0): |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 500 | if self.__class__ is not Profile: |
| 501 | raise TypeError("Subclasses must override .calibrate().") |
| 502 | |
| 503 | saved_bias = self.bias |
| 504 | self.bias = 0 |
| 505 | try: |
Tim Peters | e13cc92 | 2001-10-09 21:01:31 +0000 | [diff] [blame] | 506 | return self._calibrate_inner(m, verbose) |
Tim Peters | 659a603 | 2001-10-09 20:51:19 +0000 | [diff] [blame] | 507 | finally: |
| 508 | self.bias = saved_bias |
| 509 | |
Tim Peters | e13cc92 | 2001-10-09 21:01:31 +0000 | [diff] [blame] | 510 | def _calibrate_inner(self, m, verbose): |
Fred Drake | edb5ffb | 2001-06-08 04:25:24 +0000 | [diff] [blame] | 511 | get_time = self.get_time |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 512 | |
Tim Peters | cce092d | 2001-10-09 05:31:56 +0000 | [diff] [blame] | 513 | # Set up a test case to be run with and without profiling. Include |
| 514 | # lots of calls, because we're trying to quantify stopwatch overhead. |
| 515 | # Do not raise any exceptions, though, because we want to know |
| 516 | # exactly how many profile events are generated (one call event, + |
| 517 | # one return event, per Python-level call). |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 518 | |
Tim Peters | cce092d | 2001-10-09 05:31:56 +0000 | [diff] [blame] | 519 | def f1(n): |
| 520 | for i in range(n): |
| 521 | x = 1 |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 522 | |
Tim Peters | cce092d | 2001-10-09 05:31:56 +0000 | [diff] [blame] | 523 | def f(m, f1=f1): |
| 524 | for i in range(m): |
| 525 | f1(100) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 526 | |
Tim Peters | cce092d | 2001-10-09 05:31:56 +0000 | [diff] [blame] | 527 | f(m) # warm up the cache |
| 528 | |
| 529 | # elapsed_noprofile <- time f(m) takes without profiling. |
| 530 | t0 = get_time() |
| 531 | f(m) |
| 532 | t1 = get_time() |
| 533 | elapsed_noprofile = t1 - t0 |
| 534 | if verbose: |
| 535 | print "elapsed time without profiling =", elapsed_noprofile |
| 536 | |
| 537 | # elapsed_profile <- time f(m) takes with profiling. The difference |
| 538 | # is profiling overhead, only some of which the profiler subtracts |
| 539 | # out on its own. |
| 540 | p = Profile() |
| 541 | t0 = get_time() |
| 542 | p.runctx('f(m)', globals(), locals()) |
| 543 | t1 = get_time() |
| 544 | elapsed_profile = t1 - t0 |
| 545 | if verbose: |
| 546 | print "elapsed time with profiling =", elapsed_profile |
| 547 | |
| 548 | # reported_time <- "CPU seconds" the profiler charged to f and f1. |
| 549 | total_calls = 0.0 |
| 550 | reported_time = 0.0 |
| 551 | for (filename, line, funcname), (cc, ns, tt, ct, callers) in \ |
| 552 | p.timings.items(): |
| 553 | if funcname in ("f", "f1"): |
| 554 | total_calls += cc |
| 555 | reported_time += tt |
| 556 | |
| 557 | if verbose: |
| 558 | print "'CPU seconds' profiler reported =", reported_time |
| 559 | print "total # calls =", total_calls |
| 560 | if total_calls != m + 1: |
| 561 | raise ValueError("internal error: total calls = %d" % total_calls) |
| 562 | |
| 563 | # reported_time - elapsed_noprofile = overhead the profiler wasn't |
| 564 | # able to measure. Divide by twice the number of calls (since there |
| 565 | # are two profiler events per call in this test) to get the hidden |
| 566 | # overhead per event. |
| 567 | mean = (reported_time - elapsed_noprofile) / 2.0 / total_calls |
| 568 | if verbose: |
| 569 | print "mean stopwatch overhead per profile event =", mean |
| 570 | return mean |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 571 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 572 | #**************************************************************************** |
| 573 | def Stats(*args): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 574 | print 'Report generating functions are in the "pstats" module\a' |
Guido van Rossum | cc778eb | 1996-10-01 02:55:54 +0000 | [diff] [blame] | 575 | |
Johannes Gijsbers | 2abe785 | 2005-01-09 01:58:02 +0000 | [diff] [blame] | 576 | def main(): |
Nicholas Bastin | 824b1b2 | 2004-03-23 18:44:39 +0000 | [diff] [blame] | 577 | usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..." |
Johannes Gijsbers | 2abe785 | 2005-01-09 01:58:02 +0000 | [diff] [blame] | 578 | parser = OptionParser(usage=usage) |
Nicholas Bastin | 824b1b2 | 2004-03-23 18:44:39 +0000 | [diff] [blame] | 579 | parser.allow_interspersed_args = False |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 580 | parser.add_option('-o', '--outfile', dest="outfile", |
Nicholas Bastin | 824b1b2 | 2004-03-23 18:44:39 +0000 | [diff] [blame] | 581 | help="Save stats to <outfile>", default=None) |
| 582 | parser.add_option('-s', '--sort', dest="sort", |
Florent Xicluna | 9284745 | 2010-09-13 17:36:36 +0000 | [diff] [blame] | 583 | help="Sort order when printing to stdout, based on pstats.Stats class", |
| 584 | default=-1) |
Tim Peters | b497c10 | 2005-01-10 16:48:37 +0000 | [diff] [blame] | 585 | |
Johannes Gijsbers | 2abe785 | 2005-01-09 01:58:02 +0000 | [diff] [blame] | 586 | if not sys.argv[1:]: |
| 587 | parser.print_usage() |
| 588 | sys.exit(2) |
Tim Peters | b497c10 | 2005-01-10 16:48:37 +0000 | [diff] [blame] | 589 | |
Nicholas Bastin | 824b1b2 | 2004-03-23 18:44:39 +0000 | [diff] [blame] | 590 | (options, args) = parser.parse_args() |
Florent Xicluna | 9284745 | 2010-09-13 17:36:36 +0000 | [diff] [blame] | 591 | sys.argv[:] = args |
Tim Peters | b497c10 | 2005-01-10 16:48:37 +0000 | [diff] [blame] | 592 | |
Florent Xicluna | 9284745 | 2010-09-13 17:36:36 +0000 | [diff] [blame] | 593 | if len(args) > 0: |
| 594 | progname = args[0] |
| 595 | sys.path.insert(0, os.path.dirname(progname)) |
| 596 | with open(progname, 'rb') as fp: |
| 597 | code = compile(fp.read(), progname, 'exec') |
| 598 | globs = { |
| 599 | '__file__': progname, |
| 600 | '__name__': '__main__', |
| 601 | '__package__': None, |
| 602 | } |
| 603 | runctx(code, globs, None, options.outfile, options.sort) |
Nicholas Bastin | 824b1b2 | 2004-03-23 18:44:39 +0000 | [diff] [blame] | 604 | else: |
Johannes Gijsbers | 2abe785 | 2005-01-09 01:58:02 +0000 | [diff] [blame] | 605 | parser.print_usage() |
| 606 | return parser |
| 607 | |
| 608 | # When invoked as main program, invoke the profiler on a script |
| 609 | if __name__ == '__main__': |
| 610 | main() |