Mercurial > cpython-lru_cache-weakref
annotate Lib/profile.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | aaa6b724a4fc |
| children | 02150e60636b f6785bce54b5 |
| rev | line source |
|---|---|
|
4573
73758db2f464
/usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents:
4347
diff
changeset
|
1 #! /usr/bin/env python |
| 839 | 2 # |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
3 # Class for profiling python code. rev 1.0 6/2/94 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
4 # |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
5 # Based on prior profile module by Sjoerd Mullender... |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
6 # which was hacked somewhat by: Guido van Rossum |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
7 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
10827
diff
changeset
|
8 """Class for profiling Python code.""" |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
9 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
10 # Copyright 1994, by InfoSeek Corporation, all rights reserved. |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
11 # Written by James Roskind |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
12 # |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
13 # Permission to use, copy, modify, and distribute this Python software |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
14 # and its associated documentation for any purpose (subject to the |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
15 # restriction in the following sentence) without fee is hereby granted, |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
16 # provided that the above copyright notice appears in all copies, and |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
17 # that both that copyright notice and this permission notice appear in |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
18 # supporting documentation, and that the name of InfoSeek not be used in |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
19 # advertising or publicity pertaining to distribution of the software |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
20 # without specific, written prior permission. This permission is |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
21 # explicitly restricted to the copying and modification of the software |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
22 # to remain in Python, compiled Python, or other languages (such as C) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
23 # wherein the modified or derived code is exclusively imported into a |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
24 # Python module. |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
25 # |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
26 # INFOSEEK CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
27 # SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
28 # FITNESS. IN NO EVENT SHALL INFOSEEK CORPORATION BE LIABLE FOR ANY |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
29 # SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
30 # RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
31 # CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
32 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
33 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
34 |
| 839 | 35 |
| 36 import sys | |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
839
diff
changeset
|
37 import os |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
38 import time |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
839
diff
changeset
|
39 import marshal |
|
31782
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
40 from optparse import OptionParser |
| 839 | 41 |
|
31785
dc5bd8422d52
Add runctx to __all__.
Guido van Rossum <guido@python.org>
parents:
31782
diff
changeset
|
42 __all__ = ["run", "runctx", "help", "Profile"] |
| 839 | 43 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
44 # Sample timer for use with |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
45 #i_count = 0 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
46 #def integer_timer(): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
47 # global i_count |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
48 # i_count = i_count + 1 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
49 # return i_count |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
50 #itimes = integer_timer # replace with C coded timer returning integers |
| 839 | 51 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
52 #************************************************************************** |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
53 # The following are the static member functions for the profiler class |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
54 # Note that an instance of Profile() is *not* needed to call them. |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
55 #************************************************************************** |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
839
diff
changeset
|
56 |
|
31782
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
57 def run(statement, filename=None, sort=-1): |
|
17042
3375a2a98e56
Add doc string for run from profile.doc. (pydoc motivates me to write
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16562
diff
changeset
|
58 """Run statement under profiler optionally saving results in filename |
|
1006
903071377d29
Added a _v21 def to FL.py and added two new input field types
Guido van Rossum <guido@python.org>
parents:
839
diff
changeset
|
59 |
|
17042
3375a2a98e56
Add doc string for run from profile.doc. (pydoc motivates me to write
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16562
diff
changeset
|
60 This function takes a single argument that can be passed to the |
|
3375a2a98e56
Add doc string for run from profile.doc. (pydoc motivates me to write
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16562
diff
changeset
|
61 "exec" statement, and an optional file name. In all cases this |
|
3375a2a98e56
Add doc string for run from profile.doc. (pydoc motivates me to write
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16562
diff
changeset
|
62 routine attempts to "exec" its first argument and gather profiling |
|
3375a2a98e56
Add doc string for run from profile.doc. (pydoc motivates me to write
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16562
diff
changeset
|
63 statistics from the execution. If no file name is present, then this |
|
3375a2a98e56
Add doc string for run from profile.doc. (pydoc motivates me to write
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16562
diff
changeset
|
64 function automatically prints a simple profiling report, sorted by the |
|
3375a2a98e56
Add doc string for run from profile.doc. (pydoc motivates me to write
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16562
diff
changeset
|
65 standard name string (file/line/function-name) that is presented in |
|
3375a2a98e56
Add doc string for run from profile.doc. (pydoc motivates me to write
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16562
diff
changeset
|
66 each line. |
|
3375a2a98e56
Add doc string for run from profile.doc. (pydoc motivates me to write
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16562
diff
changeset
|
67 """ |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
68 prof = Profile() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
69 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
70 prof = prof.run(statement) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
71 except SystemExit: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
72 pass |
|
17042
3375a2a98e56
Add doc string for run from profile.doc. (pydoc motivates me to write
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16562
diff
changeset
|
73 if filename is not None: |
|
3375a2a98e56
Add doc string for run from profile.doc. (pydoc motivates me to write
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
16562
diff
changeset
|
74 prof.dump_stats(filename) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
75 else: |
|
31782
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
76 return prof.print_stats(sort) |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1335
diff
changeset
|
77 |
|
31770
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
78 def runctx(statement, globals, locals, filename=None): |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
79 """Run statement under profiler, supplying your own globals and locals, |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
80 optionally saving results in filename. |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
81 |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
82 statement and filename have the same semantics as profile.run |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
83 """ |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
84 prof = Profile() |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
85 try: |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
86 prof = prof.runctx(statement, globals, locals) |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
87 except SystemExit: |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
88 pass |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
89 |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
90 if filename is not None: |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
91 prof.dump_stats(filename) |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
92 else: |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
93 return prof.print_stats() |
|
ef591e7c34fd
Added global runctx function to profile to fix SF Bug #716587
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31515
diff
changeset
|
94 |
|
34382
a31681a6cf88
Bug #489256: remove out of date and out of place profile.doc, and let
Johannes Gijsbers <jlg@dds.nl>
parents:
34375
diff
changeset
|
95 # Backwards compatibility. |
|
1357
2fac209e1c3f
* profile.py, pdb.py: added help() function
Guido van Rossum <guido@python.org>
parents:
1335
diff
changeset
|
96 def help(): |
|
34382
a31681a6cf88
Bug #489256: remove out of date and out of place profile.doc, and let
Johannes Gijsbers <jlg@dds.nl>
parents:
34375
diff
changeset
|
97 print "Documentation for the profile module can be found " |
|
a31681a6cf88
Bug #489256: remove out of date and out of place profile.doc, and let
Johannes Gijsbers <jlg@dds.nl>
parents:
34375
diff
changeset
|
98 print "in the Python Library Reference, section 'The Python Profiler'." |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
99 |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
100 if os.name == "mac": |
|
18164
efb5069cc74f
An import MacOS was missing after the code-rearranging. Added.
Jack Jansen <jack.jansen@cwi.nl>
parents:
18056
diff
changeset
|
101 import MacOS |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
102 def _get_time_mac(timer=MacOS.GetTicks): |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
103 return timer() / 60.0 |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
104 |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
105 if hasattr(os, "times"): |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
106 def _get_time_times(timer=os.times): |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
107 t = timer() |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
108 return t[0] + t[1] |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
109 |
|
34663
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
110 # Using getrusage(3) is better than clock(3) if available: |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
111 # on some systems (e.g. FreeBSD), getrusage has a higher resolution |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
112 # Furthermore, on a POSIX system, returns microseconds, which |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
113 # wrap around after 36min. |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
114 _has_res = 0 |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
115 try: |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
116 import resource |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
117 resgetrusage = lambda: resource.getrusage(resource.RUSAGE_SELF) |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
118 def _get_time_resource(timer=resgetrusage): |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
119 t = timer() |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
120 return t[0] + t[1] |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
121 _has_res = 1 |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
122 except ImportError: |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
123 pass |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
124 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
125 class Profile: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
126 """Profiler class. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
127 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
128 self.cur is always a tuple. Each such tuple corresponds to a stack |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
129 frame that is currently active (self.cur[-2]). The following are the |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
130 definitions of its members. We use this external "parallel stack" to |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
131 avoid contaminating the program that we are profiling. (old profiler |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
132 used to write into the frames local dictionary!!) Derived classes |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
133 can change the definition of some entries, as long as they leave |
|
20289
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
134 [-2:] intact (frame and previous tuple). In case an internal error is |
|
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
135 detected, the -3 element is used as the function name. |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
10827
diff
changeset
|
136 |
|
20289
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
137 [ 0] = Time that needs to be charged to the parent frame's function. |
|
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
138 It is used so that a function call will not have to access the |
|
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
139 timing data for the parent frame. |
|
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
140 [ 1] = Total time spent in this frame's function, excluding time in |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
141 subfunctions (this latter is tallied in cur[2]). |
|
20302
c5345c0abd70
Typo repair in comment.
Tim Peters <tim.peters@gmail.com>
parents:
20300
diff
changeset
|
142 [ 2] = Total time spent in subfunctions, excluding time executing the |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
143 frame's function (this latter is tallied in cur[1]). |
|
20289
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
144 [-3] = Name of the function that corresponds to this frame. |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
145 [-2] = Actual frame that we correspond to (used to sync exception handling). |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
146 [-1] = Our parent 6-tuple (corresponds to frame.f_back). |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
10827
diff
changeset
|
147 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
148 Timing data for each function is stored as a 5-tuple in the dictionary |
|
20289
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
149 self.timings[]. The index is always the name stored in self.cur[-3]. |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
150 The following are the definitions of the members: |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
10827
diff
changeset
|
151 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
152 [0] = The number of times this function was called, not counting direct |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
153 or indirect recursion, |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
154 [1] = Number of times this function appears on the stack, minus one |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
155 [2] = Total time spent internal to this function |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
156 [3] = Cumulative time that this function was present on the stack. In |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
157 non-recursive functions, this is the total execution time from start |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
158 to finish of each invocation of a function, including time spent in |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
159 all subfunctions. |
|
20297
79392ab2c02a
Repair some longstanding comment errors:
Tim Peters <tim.peters@gmail.com>
parents:
20295
diff
changeset
|
160 [4] = A dictionary indicating for each function name, the number of times |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
161 it was called by us. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
162 """ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
163 |
|
20347
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
164 bias = 0 # calibration constant |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
165 |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
166 def __init__(self, timer=None, bias=None): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
167 self.timings = {} |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
168 self.cur = None |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
169 self.cmd = "" |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
170 self.c_func_name = "" |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
171 |
|
20347
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
172 if bias is None: |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
173 bias = self.bias |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
174 self.bias = bias # Materialize in local dict for lookup speed. |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
175 |
|
34663
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
176 if not timer: |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
177 if _has_res: |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
178 self.timer = resgetrusage |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
179 self.dispatcher = self.trace_dispatch |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
180 self.get_time = _get_time_resource |
|
9d608419cdf8
Patch #645894: Use getrusage for computing the time consumption in
Martin v. Löwis <martin@v.loewis.de>
parents:
34388
diff
changeset
|
181 elif os.name == 'mac': |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
182 self.timer = MacOS.GetTicks |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
183 self.dispatcher = self.trace_dispatch_mac |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
184 self.get_time = _get_time_mac |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
185 elif hasattr(time, 'clock'): |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
186 self.timer = self.get_time = time.clock |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
187 self.dispatcher = self.trace_dispatch_i |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
188 elif hasattr(os, 'times'): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
189 self.timer = os.times |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
190 self.dispatcher = self.trace_dispatch |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
191 self.get_time = _get_time_times |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
192 else: |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
193 self.timer = self.get_time = time.time |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
194 self.dispatcher = self.trace_dispatch_i |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
195 else: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
196 self.timer = timer |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
197 t = self.timer() # test out timer function |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
198 try: |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
199 length = len(t) |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
200 except TypeError: |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
201 self.get_time = timer |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
202 self.dispatcher = self.trace_dispatch_i |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
203 else: |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
204 if length == 2: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
205 self.dispatcher = self.trace_dispatch |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
206 else: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
207 self.dispatcher = self.trace_dispatch_l |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
208 # This get_time() implementation needs to be defined |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
209 # here to capture the passed-in timer in the parameter |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
210 # list (for performance). Note that we can't assume |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
211 # the timer() result contains two values in all |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
212 # cases. |
|
30756
b6902c0a7578
Replace a reduce() with sum().
Raymond Hettinger <python@rcn.com>
parents:
28089
diff
changeset
|
213 def get_time_timer(timer=timer, sum=sum): |
|
b6902c0a7578
Replace a reduce() with sum().
Raymond Hettinger <python@rcn.com>
parents:
28089
diff
changeset
|
214 return sum(timer()) |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
215 self.get_time = get_time_timer |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
216 self.t = self.get_time() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
217 self.simulate_call('profiler') |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
218 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
219 # Heavily optimized dispatch routine for os.times() timer |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
220 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
221 def trace_dispatch(self, frame, event, arg): |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
222 timer = self.timer |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
223 t = timer() |
|
20347
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
224 t = t[0] + t[1] - self.t - self.bias |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
225 |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
226 if event == "c_call": |
|
32642
db994849cdf3
Using repr() generates entries that the current stats package can't
Nicholas Bastin <nick.bastin@gmail.com>
parents:
32637
diff
changeset
|
227 self.c_func_name = arg.__name__ |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
228 |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
229 if self.dispatch[event](self, frame,t): |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
230 t = timer() |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
231 self.t = t[0] + t[1] |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
232 else: |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
233 r = timer() |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
234 self.t = r[0] + r[1] - t # put back unrecorded delta |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
235 |
|
20297
79392ab2c02a
Repair some longstanding comment errors:
Tim Peters <tim.peters@gmail.com>
parents:
20295
diff
changeset
|
236 # Dispatch routine for best timer program (return = scalar, fastest if |
|
79392ab2c02a
Repair some longstanding comment errors:
Tim Peters <tim.peters@gmail.com>
parents:
20295
diff
changeset
|
237 # an integer but float works too -- and time.clock() relies on that). |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
238 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
239 def trace_dispatch_i(self, frame, event, arg): |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
240 timer = self.timer |
|
20347
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
241 t = timer() - self.t - self.bias |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
242 |
|
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
243 if event == "c_call": |
|
32642
db994849cdf3
Using repr() generates entries that the current stats package can't
Nicholas Bastin <nick.bastin@gmail.com>
parents:
32637
diff
changeset
|
244 self.c_func_name = arg.__name__ |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
245 |
|
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
246 if self.dispatch[event](self, frame, t): |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
247 self.t = timer() |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
248 else: |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
249 self.t = timer() - t # put back unrecorded delta |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
250 |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
251 # Dispatch routine for macintosh (timer returns time in ticks of |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
252 # 1/60th second) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
253 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
254 def trace_dispatch_mac(self, frame, event, arg): |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
255 timer = self.timer |
|
20347
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
256 t = timer()/60.0 - self.t - self.bias |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
257 |
|
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
258 if event == "c_call": |
|
32642
db994849cdf3
Using repr() generates entries that the current stats package can't
Nicholas Bastin <nick.bastin@gmail.com>
parents:
32637
diff
changeset
|
259 self.c_func_name = arg.__name__ |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
260 |
|
20347
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
261 if self.dispatch[event](self, frame, t): |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
262 self.t = timer()/60.0 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
263 else: |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
264 self.t = timer()/60.0 - t # put back unrecorded delta |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
265 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
266 # SLOW generic dispatch routine for timer returning lists of numbers |
|
6451
10413179a72a
Use better timer on the mac
Guido van Rossum <guido@python.org>
parents:
4573
diff
changeset
|
267 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
268 def trace_dispatch_l(self, frame, event, arg): |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
269 get_time = self.get_time |
|
20347
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
270 t = get_time() - self.t - self.bias |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
271 |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
272 if event == "c_call": |
|
32642
db994849cdf3
Using repr() generates entries that the current stats package can't
Nicholas Bastin <nick.bastin@gmail.com>
parents:
32637
diff
changeset
|
273 self.c_func_name = arg.__name__ |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
274 |
|
20347
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
275 if self.dispatch[event](self, frame, t): |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
276 self.t = get_time() |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
277 else: |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
278 self.t = get_time() - t # put back unrecorded delta |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
279 |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
280 # In the event handlers, the first 3 elements of self.cur are unpacked |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
281 # into vrbls w/ 3-letter names. The last two characters are meant to be |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
282 # mnemonic: |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
283 # _pt self.cur[0] "parent time" time to be charged to parent frame |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
284 # _it self.cur[1] "internal time" time spent directly in the function |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
285 # _et self.cur[2] "external time" time spent in subfunctions |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
286 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
287 def trace_dispatch_exception(self, frame, t): |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
288 rpt, rit, ret, rfn, rframe, rcur = self.cur |
|
20235
acfb278662c6
Undo previous patch; it did not quite work out.
Fred Drake <fdrake@acm.org>
parents:
20133
diff
changeset
|
289 if (rframe is not frame) and rcur: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
290 return self.trace_dispatch_return(rframe, t) |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
291 self.cur = rpt, rit+t, ret, rfn, rframe, rcur |
|
20238
e7aca2a302cd
Hopefully fix the profiler right. Add a test suite that checks that
Guido van Rossum <guido@python.org>
parents:
20235
diff
changeset
|
292 return 1 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
293 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
294 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
295 def trace_dispatch_call(self, frame, t): |
|
20289
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
296 if self.cur and frame.f_back is not self.cur[-2]: |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
297 rpt, rit, ret, rfn, rframe, rcur = self.cur |
|
20238
e7aca2a302cd
Hopefully fix the profiler right. Add a test suite that checks that
Guido van Rossum <guido@python.org>
parents:
20235
diff
changeset
|
298 if not isinstance(rframe, Profile.fake_frame): |
|
20298
6211684ed9dd
At Guido's request, changed the code that's conceptually asserting stuff
Tim Peters <tim.peters@gmail.com>
parents:
20297
diff
changeset
|
299 assert rframe.f_back is frame.f_back, ("Bad call", rfn, |
|
6211684ed9dd
At Guido's request, changed the code that's conceptually asserting stuff
Tim Peters <tim.peters@gmail.com>
parents:
20297
diff
changeset
|
300 rframe, rframe.f_back, |
|
6211684ed9dd
At Guido's request, changed the code that's conceptually asserting stuff
Tim Peters <tim.peters@gmail.com>
parents:
20297
diff
changeset
|
301 frame, frame.f_back) |
|
20238
e7aca2a302cd
Hopefully fix the profiler right. Add a test suite that checks that
Guido van Rossum <guido@python.org>
parents:
20235
diff
changeset
|
302 self.trace_dispatch_return(rframe, 0) |
|
20298
6211684ed9dd
At Guido's request, changed the code that's conceptually asserting stuff
Tim Peters <tim.peters@gmail.com>
parents:
20297
diff
changeset
|
303 assert (self.cur is None or \ |
|
6211684ed9dd
At Guido's request, changed the code that's conceptually asserting stuff
Tim Peters <tim.peters@gmail.com>
parents:
20297
diff
changeset
|
304 frame.f_back is self.cur[-2]), ("Bad call", |
|
6211684ed9dd
At Guido's request, changed the code that's conceptually asserting stuff
Tim Peters <tim.peters@gmail.com>
parents:
20297
diff
changeset
|
305 self.cur[-3]) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
306 fcode = frame.f_code |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
307 fn = (fcode.co_filename, fcode.co_firstlineno, fcode.co_name) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
308 self.cur = (t, 0, 0, fn, frame, self.cur) |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
309 timings = self.timings |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
21238
diff
changeset
|
310 if fn in timings: |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
311 cc, ns, tt, ct, callers = timings[fn] |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
312 timings[fn] = cc, ns + 1, tt, ct, callers |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
313 else: |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
314 timings[fn] = 0, 0, 0, 0, {} |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
315 return 1 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
316 |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
317 def trace_dispatch_c_call (self, frame, t): |
|
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
318 fn = ("", 0, self.c_func_name) |
|
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
319 self.cur = (t, 0, 0, fn, frame, self.cur) |
|
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
320 timings = self.timings |
|
48145
4e45d0cfaf71
Remove a dict.has_key() usage in profile to silence a -3 DeprecationWarning.
Brett Cannon <bcannon@gmail.com>
parents:
35611
diff
changeset
|
321 if fn in timings: |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
322 cc, ns, tt, ct, callers = timings[fn] |
|
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
323 timings[fn] = cc, ns+1, tt, ct, callers |
|
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
324 else: |
|
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
325 timings[fn] = 0, 0, 0, 0, {} |
|
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
326 return 1 |
|
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
327 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
328 def trace_dispatch_return(self, frame, t): |
|
20289
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
329 if frame is not self.cur[-2]: |
|
20298
6211684ed9dd
At Guido's request, changed the code that's conceptually asserting stuff
Tim Peters <tim.peters@gmail.com>
parents:
20297
diff
changeset
|
330 assert frame is self.cur[-2].f_back, ("Bad return", self.cur[-3]) |
|
6211684ed9dd
At Guido's request, changed the code that's conceptually asserting stuff
Tim Peters <tim.peters@gmail.com>
parents:
20297
diff
changeset
|
331 self.trace_dispatch_return(self.cur[-2], 0) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
332 |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
333 # Prefix "r" means part of the Returning or exiting frame. |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
334 # Prefix "p" means part of the Previous or Parent or older frame. |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
335 |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
336 rpt, rit, ret, rfn, frame, rcur = self.cur |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
337 rit = rit + t |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
338 frame_total = rit + ret |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
339 |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
340 ppt, pit, pet, pfn, pframe, pcur = rcur |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
341 self.cur = ppt, pit + rpt, pet + frame_total, pfn, pframe, pcur |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
342 |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
343 timings = self.timings |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
344 cc, ns, tt, ct, callers = timings[rfn] |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
345 if not ns: |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
346 # This is the only occurrence of the function on the stack. |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
347 # Else this is a (directly or indirectly) recursive call, and |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
348 # its cumulative time will get updated when the topmost call to |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
349 # it returns. |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
350 ct = ct + frame_total |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
351 cc = cc + 1 |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
352 |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
21238
diff
changeset
|
353 if pfn in callers: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
354 callers[pfn] = callers[pfn] + 1 # hack: gather more |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
355 # stats such as the amount of time added to ct courtesy |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
356 # of this specific call, and the contribution to cc |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
357 # courtesy of this call. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
358 else: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
359 callers[pfn] = 1 |
|
20300
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
360 |
|
874bb17074b0
Guido points out that the comments for self.cur[2] were subtly but
Tim Peters <tim.peters@gmail.com>
parents:
20298
diff
changeset
|
361 timings[rfn] = cc, ns - 1, tt + rit, ct, callers |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
362 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
363 return 1 |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
364 |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
365 |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
366 dispatch = { |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
367 "call": trace_dispatch_call, |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
368 "exception": trace_dispatch_exception, |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
369 "return": trace_dispatch_return, |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
370 "c_call": trace_dispatch_c_call, |
|
35611
af3a06625a6e
test and fix for buggy handling of exceptions raised by C functions,
Armin Rigo <arigo@tunes.org>
parents:
34663
diff
changeset
|
371 "c_exception": trace_dispatch_return, # the C function returned |
|
31796
1c117dbb2def
Enable the profiling of C functions (builtins and extensions)
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31785
diff
changeset
|
372 "c_return": trace_dispatch_return, |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
373 } |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
374 |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
375 |
|
20298
6211684ed9dd
At Guido's request, changed the code that's conceptually asserting stuff
Tim Peters <tim.peters@gmail.com>
parents:
20297
diff
changeset
|
376 # The next few functions play with self.cmd. By carefully preloading |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
377 # our parallel stack, we can force the profiled result to include |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
378 # an arbitrary string as the name of the calling function. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
379 # We use self.cmd as that string, and the resulting stats look |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
380 # very nice :-). |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
381 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
382 def set_cmd(self, cmd): |
|
20289
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
383 if self.cur[-1]: return # already set |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
384 self.cmd = cmd |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
385 self.simulate_call(cmd) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
386 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
387 class fake_code: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
388 def __init__(self, filename, line, name): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
389 self.co_filename = filename |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
390 self.co_line = line |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
391 self.co_name = name |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
392 self.co_firstlineno = 0 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
393 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
394 def __repr__(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
395 return repr((self.co_filename, self.co_line, self.co_name)) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
396 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
397 class fake_frame: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
398 def __init__(self, code, prior): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
399 self.f_code = code |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
400 self.f_back = prior |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
401 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
402 def simulate_call(self, name): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
403 code = self.fake_code('profile', 0, name) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
404 if self.cur: |
|
20289
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
405 pframe = self.cur[-2] |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
406 else: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
407 pframe = None |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
408 frame = self.fake_frame(code, pframe) |
|
20480
dd0cdd3defd8
Minor code cleanups based on comments from Neal Norwitz.
Fred Drake <fdrake@acm.org>
parents:
20350
diff
changeset
|
409 self.dispatch['call'](self, frame, 0) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
410 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
411 # collect stats from pending stack, including getting final |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
412 # timings for self.cmd frame. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
413 |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
414 def simulate_cmd_complete(self): |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
415 get_time = self.get_time |
|
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
416 t = get_time() - self.t |
|
20289
6e7d5ae2800f
The fix to profile semantics broke the miserable but advertised way to
Tim Peters <tim.peters@gmail.com>
parents:
20238
diff
changeset
|
417 while self.cur[-1]: |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
418 # We *can* cause assertion errors here if |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
419 # dispatch_trace_return checks for a frame match! |
|
20480
dd0cdd3defd8
Minor code cleanups based on comments from Neal Norwitz.
Fred Drake <fdrake@acm.org>
parents:
20350
diff
changeset
|
420 self.dispatch['return'](self, self.cur[-2], t) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
421 t = 0 |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
422 self.t = get_time() - t |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
423 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
424 |
|
31782
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
425 def print_stats(self, sort=-1): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
426 import pstats |
|
31782
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
427 pstats.Stats(self).strip_dirs().sort_stats(sort). \ |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
428 print_stats() |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
429 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
430 def dump_stats(self, file): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
431 f = open(file, 'wb') |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
432 self.create_stats() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
433 marshal.dump(self.stats, f) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
434 f.close() |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
435 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
436 def create_stats(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
437 self.simulate_cmd_complete() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
438 self.snapshot_stats() |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
439 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
440 def snapshot_stats(self): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
441 self.stats = {} |
|
23634
dbee2f628a90
Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents:
23622
diff
changeset
|
442 for func, (cc, ns, tt, ct, callers) in self.timings.iteritems(): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
443 callers = callers.copy() |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
444 nc = 0 |
|
23634
dbee2f628a90
Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents:
23622
diff
changeset
|
445 for callcnt in callers.itervalues(): |
|
dbee2f628a90
Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents:
23622
diff
changeset
|
446 nc += callcnt |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
447 self.stats[func] = cc, nc, tt, ct, callers |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
448 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
449 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
450 # The following two methods can be called by clients to use |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
451 # a profiler to profile a statement, given as a string. |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
452 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
453 def run(self, cmd): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
454 import __main__ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
455 dict = __main__.__dict__ |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
456 return self.runctx(cmd, dict, dict) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
457 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
458 def runctx(self, cmd, globals, locals): |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
459 self.set_cmd(cmd) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
460 sys.setprofile(self.dispatcher) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
461 try: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
462 exec cmd in globals, locals |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
463 finally: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
464 sys.setprofile(None) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
465 return self |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
466 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
467 # This method is more useful to profile a single function call. |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
468 def runcall(self, func, *args, **kw): |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
30757
diff
changeset
|
469 self.set_cmd(repr(func)) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
470 sys.setprofile(self.dispatcher) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
471 try: |
|
28089
14f65d661084
Get rid of many apply() calls.
Guido van Rossum <guido@python.org>
parents:
23634
diff
changeset
|
472 return func(*args, **kw) |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
473 finally: |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
474 sys.setprofile(None) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
475 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
476 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
477 #****************************************************************** |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
478 # The following calculates the overhead for using a profiler. The |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
479 # problem is that it takes a fair amount of time for the profiler |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
480 # to stop the stopwatch (from the time it receives an event). |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
481 # Similarly, there is a delay from the time that the profiler |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
482 # re-starts the stopwatch before the user's code really gets to |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
483 # continue. The following code tries to measure the difference on |
|
20347
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
484 # a per-event basis. |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
485 # |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
486 # Note that this difference is only significant if there are a lot of |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
487 # events, and relatively little user code per event. For example, |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
488 # code with small functions will typically benefit from having the |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
489 # profiler calibrated for the current platform. This *could* be |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
490 # done on the fly during init() time, but it is not worth the |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
491 # effort. Also note that if too large a value specified, then |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
492 # execution time on some functions will actually appear as a |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
493 # negative number. It is *normal* for some functions (with very |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
494 # low call counts) to have such negative stats, even if the |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
495 # calibration figure is "correct." |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
496 # |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
497 # One alternative to profile-time calibration adjustments (i.e., |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
498 # adding in the magic little delta during each event) is to track |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
499 # more carefully the number of events (and cumulatively, the number |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
500 # of events during sub functions) that are seen. If this were |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
501 # done, then the arithmetic could be done after the fact (i.e., at |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
502 # display time). Currently, we track only call/return events. |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
503 # These values can be deduced by examining the callees and callers |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
504 # vectors for each functions. Hence we *can* almost correct the |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
505 # internal time figure at print time (note that we currently don't |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
506 # track exception event processing counts). Unfortunately, there |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
507 # is currently no similar information for cumulative sub-function |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
508 # time. It would not be hard to "get all this info" at profiler |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
509 # time. Specifically, we would have to extend the tuples to keep |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
510 # counts of this in each frame, and then extend the defs of timing |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
511 # tuples to include the significant two figures. I'm a bit fearful |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
512 # that this additional feature will slow the heavily optimized |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
513 # event/time ratio (i.e., the profiler would run slower, fur a very |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
514 # low "value added" feature.) |
|
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
515 #************************************************************** |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
516 |
|
20324
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
517 def calibrate(self, m, verbose=0): |
|
20347
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
518 if self.__class__ is not Profile: |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
519 raise TypeError("Subclasses must override .calibrate().") |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
520 |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
521 saved_bias = self.bias |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
522 self.bias = 0 |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
523 try: |
|
20350
0a915b22e006
Repair key stutter + auto-complete ugliness.
Tim Peters <tim.peters@gmail.com>
parents:
20347
diff
changeset
|
524 return self._calibrate_inner(m, verbose) |
|
20347
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
525 finally: |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
526 self.bias = saved_bias |
|
b79ffd3fca28
Allow the profiler's calibration constant to be specified in the constructor
Tim Peters <tim.peters@gmail.com>
parents:
20324
diff
changeset
|
527 |
|
20350
0a915b22e006
Repair key stutter + auto-complete ugliness.
Tim Peters <tim.peters@gmail.com>
parents:
20347
diff
changeset
|
528 def _calibrate_inner(self, m, verbose): |
|
18056
a232a6c3fc8c
Performance improvements to the profiler:
Fred Drake <fdrake@acm.org>
parents:
17042
diff
changeset
|
529 get_time = self.get_time |
|
20324
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
530 |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
531 # Set up a test case to be run with and without profiling. Include |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
532 # lots of calls, because we're trying to quantify stopwatch overhead. |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
533 # Do not raise any exceptions, though, because we want to know |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
534 # exactly how many profile events are generated (one call event, + |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
535 # one return event, per Python-level call). |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
536 |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
537 def f1(n): |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
538 for i in range(n): |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
539 x = 1 |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
540 |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
541 def f(m, f1=f1): |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
542 for i in range(m): |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
543 f1(100) |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
544 |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
545 f(m) # warm up the cache |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
546 |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
547 # elapsed_noprofile <- time f(m) takes without profiling. |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
548 t0 = get_time() |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
549 f(m) |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
550 t1 = get_time() |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
551 elapsed_noprofile = t1 - t0 |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
552 if verbose: |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
553 print "elapsed time without profiling =", elapsed_noprofile |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
554 |
|
20324
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
555 # elapsed_profile <- time f(m) takes with profiling. The difference |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
556 # is profiling overhead, only some of which the profiler subtracts |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
557 # out on its own. |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
558 p = Profile() |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
559 t0 = get_time() |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
560 p.runctx('f(m)', globals(), locals()) |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
561 t1 = get_time() |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
562 elapsed_profile = t1 - t0 |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
563 if verbose: |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
564 print "elapsed time with profiling =", elapsed_profile |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
565 |
|
20324
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
566 # reported_time <- "CPU seconds" the profiler charged to f and f1. |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
567 total_calls = 0.0 |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
568 reported_time = 0.0 |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
569 for (filename, line, funcname), (cc, ns, tt, ct, callers) in \ |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
570 p.timings.items(): |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
571 if funcname in ("f", "f1"): |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
572 total_calls += cc |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
573 reported_time += tt |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
574 |
|
20324
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
575 if verbose: |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
576 print "'CPU seconds' profiler reported =", reported_time |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
577 print "total # calls =", total_calls |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
578 if total_calls != m + 1: |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
579 raise ValueError("internal error: total calls = %d" % total_calls) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
580 |
|
20324
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
581 # reported_time - elapsed_noprofile = overhead the profiler wasn't |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
582 # able to measure. Divide by twice the number of calls (since there |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
583 # are two profiler events per call in this test) to get the hidden |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
584 # overhead per event. |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
585 mean = (reported_time - elapsed_noprofile) / 2.0 / total_calls |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
586 if verbose: |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
587 print "mean stopwatch overhead per profile event =", mean |
|
429a08e2fc78
A brand new implementation of Profile.calibrate(). This measures an
Tim Peters <tim.peters@gmail.com>
parents:
20302
diff
changeset
|
588 return mean |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
589 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
590 #**************************************************************************** |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1435
diff
changeset
|
591 def Stats(*args): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
13912
diff
changeset
|
592 print 'Report generating functions are in the "pstats" module\a' |
|
4347
a5c29accc438
Add main program similar to pdb.
Guido van Rossum <guido@python.org>
parents:
3634
diff
changeset
|
593 |
|
34375
c5975433aa68
Move code in __name__ == '__main__' block into main() function, rewrite code so
Johannes Gijsbers <jlg@dds.nl>
parents:
32642
diff
changeset
|
594 def main(): |
|
31782
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
595 usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..." |
|
34375
c5975433aa68
Move code in __name__ == '__main__' block into main() function, rewrite code so
Johannes Gijsbers <jlg@dds.nl>
parents:
32642
diff
changeset
|
596 parser = OptionParser(usage=usage) |
|
31782
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
597 parser.allow_interspersed_args = False |
|
32528
67539cfcf09f
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
31796
diff
changeset
|
598 parser.add_option('-o', '--outfile', dest="outfile", |
|
31782
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
599 help="Save stats to <outfile>", default=None) |
|
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
600 parser.add_option('-s', '--sort', dest="sort", |
|
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
601 help="Sort order when printing to stdout, based on pstats.Stats class", default=-1) |
|
34388
598d16fa02c1
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34382
diff
changeset
|
602 |
|
34375
c5975433aa68
Move code in __name__ == '__main__' block into main() function, rewrite code so
Johannes Gijsbers <jlg@dds.nl>
parents:
32642
diff
changeset
|
603 if not sys.argv[1:]: |
|
c5975433aa68
Move code in __name__ == '__main__' block into main() function, rewrite code so
Johannes Gijsbers <jlg@dds.nl>
parents:
32642
diff
changeset
|
604 parser.print_usage() |
|
c5975433aa68
Move code in __name__ == '__main__' block into main() function, rewrite code so
Johannes Gijsbers <jlg@dds.nl>
parents:
32642
diff
changeset
|
605 sys.exit(2) |
|
34388
598d16fa02c1
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34382
diff
changeset
|
606 |
|
31782
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
607 (options, args) = parser.parse_args() |
|
34388
598d16fa02c1
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
34382
diff
changeset
|
608 |
|
55940
aaa6b724a4fc
Merged revisions 74773 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
48145
diff
changeset
|
609 if (len(args) > 0): |
|
aaa6b724a4fc
Merged revisions 74773 via svnmerge from
Matthias Klose <doko@ubuntu.com>
parents:
48145
diff
changeset
|
610 sys.argv[:] = args |
|
31782
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
611 sys.path.insert(0, os.path.dirname(sys.argv[0])) |
|
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
612 run('execfile(%r)' % (sys.argv[0],), options.outfile, options.sort) |
|
6a1505613358
Added command line options for profile.py - one for stats output file
Nicholas Bastin <nick.bastin@gmail.com>
parents:
31770
diff
changeset
|
613 else: |
|
34375
c5975433aa68
Move code in __name__ == '__main__' block into main() function, rewrite code so
Johannes Gijsbers <jlg@dds.nl>
parents:
32642
diff
changeset
|
614 parser.print_usage() |
|
c5975433aa68
Move code in __name__ == '__main__' block into main() function, rewrite code so
Johannes Gijsbers <jlg@dds.nl>
parents:
32642
diff
changeset
|
615 return parser |
|
c5975433aa68
Move code in __name__ == '__main__' block into main() function, rewrite code so
Johannes Gijsbers <jlg@dds.nl>
parents:
32642
diff
changeset
|
616 |
|
c5975433aa68
Move code in __name__ == '__main__' block into main() function, rewrite code so
Johannes Gijsbers <jlg@dds.nl>
parents:
32642
diff
changeset
|
617 # When invoked as main program, invoke the profiler on a script |
|
c5975433aa68
Move code in __name__ == '__main__' block into main() function, rewrite code so
Johannes Gijsbers <jlg@dds.nl>
parents:
32642
diff
changeset
|
618 if __name__ == '__main__': |
|
c5975433aa68
Move code in __name__ == '__main__' block into main() function, rewrite code so
Johannes Gijsbers <jlg@dds.nl>
parents:
32642
diff
changeset
|
619 main() |
