Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Class for printing reports on profiled python code.""" |
| 2 | |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 3 | # Class for printing reports on profiled python code. rev 1.0 4/1/94 |
| 4 | # |
| 5 | # Based on prior profile module by Sjoerd Mullender... |
| 6 | # which was hacked somewhat by: Guido van Rossum |
| 7 | # |
Guido van Rossum | dabcd00 | 1999-04-13 04:24:22 +0000 | [diff] [blame] | 8 | # see profile.doc and profile.py for more info. |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 9 | |
| 10 | # Copyright 1994, by InfoSeek Corporation, all rights reserved. |
| 11 | # Written by James Roskind |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 12 | # |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 13 | # Permission to use, copy, modify, and distribute this Python software |
| 14 | # and its associated documentation for any purpose (subject to the |
| 15 | # restriction in the following sentence) without fee is hereby granted, |
| 16 | # provided that the above copyright notice appears in all copies, and |
| 17 | # that both that copyright notice and this permission notice appear in |
| 18 | # supporting documentation, and that the name of InfoSeek not be used in |
| 19 | # advertising or publicity pertaining to distribution of the software |
| 20 | # without specific, written prior permission. This permission is |
| 21 | # explicitly restricted to the copying and modification of the software |
| 22 | # to remain in Python, compiled Python, or other languages (such as C) |
| 23 | # wherein the modified or derived code is exclusively imported into a |
| 24 | # Python module. |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 25 | # |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 26 | # INFOSEEK CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS |
| 27 | # SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 28 | # FITNESS. IN NO EVENT SHALL INFOSEEK CORPORATION BE LIABLE FOR ANY |
| 29 | # SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 30 | # RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 31 | # CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 32 | # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 33 | |
| 34 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | import sys |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 36 | import os |
| 37 | import time |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 38 | import marshal |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 39 | import re |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 40 | |
Skip Montanaro | c62c81e | 2001-02-12 02:00:42 +0000 | [diff] [blame] | 41 | __all__ = ["Stats"] |
| 42 | |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 43 | class Stats: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 44 | """This class is used for creating reports from data generated by the |
| 45 | Profile class. It is a "friend" of that class, and imports data either |
| 46 | by direct access to members of Profile class, or by reading in a dictionary |
| 47 | that was emitted (via marshal) from the Profile class. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 48 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 49 | The big change from the previous Profiler (in terms of raw functionality) |
| 50 | is that an "add()" method has been provided to combine Stats from |
| 51 | several distinct profile runs. Both the constructor and the add() |
| 52 | method now take arbitrarily many file names as arguments. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 53 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 54 | All the print methods now take an argument that indicates how many lines |
| 55 | to print. If the arg is a floating point number between 0 and 1.0, then |
| 56 | it is taken as a decimal percentage of the available lines to be printed |
| 57 | (e.g., .1 means print 10% of all available lines). If it is an integer, |
| 58 | it is taken to mean the number of lines of data that you wish to have |
| 59 | printed. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 60 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 61 | The sort_stats() method now processes some additional options (i.e., in |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 62 | addition to the old -1, 0, 1, or 2). It takes an arbitrary number of |
| 63 | quoted strings to select the sort order. For example sort_stats('time', |
| 64 | 'name') sorts on the major key of 'internal function time', and on the |
| 65 | minor key of 'the name of the function'. Look at the two tables in |
| 66 | sort_stats() and get_sort_arg_defs(self) for more examples. |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 67 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 68 | All methods return self, so you can string together commands like: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 69 | Stats('foo', 'goo').strip_dirs().sort_stats('calls').\ |
| 70 | print_stats(5).print_callers(5) |
| 71 | """ |
| 72 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 73 | def __init__(self, *args, **kwds): |
| 74 | # I can't figure out how to explictly specify a stream keyword arg |
| 75 | # with *args: |
| 76 | # def __init__(self, *args, stream=sys.stdout): ... |
| 77 | # so I use **kwds and sqauwk if something unexpected is passed in. |
| 78 | self.stream = sys.stdout |
| 79 | if "stream" in kwds: |
| 80 | self.stream = kwds["stream"] |
| 81 | del kwds["stream"] |
| 82 | if kwds: |
| 83 | keys = kwds.keys() |
| 84 | keys.sort() |
| 85 | extras = ", ".join(["%s=%s" % (k, kwds[k]) for k in keys]) |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 86 | raise ValueError("unrecognized keyword args: %s" % extras) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 87 | if not len(args): |
| 88 | arg = None |
| 89 | else: |
| 90 | arg = args[0] |
| 91 | args = args[1:] |
| 92 | self.init(arg) |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 93 | self.add(*args) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 94 | |
| 95 | def init(self, arg): |
| 96 | self.all_callees = None # calc only if needed |
| 97 | self.files = [] |
| 98 | self.fcn_list = None |
| 99 | self.total_tt = 0 |
| 100 | self.total_calls = 0 |
| 101 | self.prim_calls = 0 |
| 102 | self.max_name_len = 0 |
| 103 | self.top_level = {} |
| 104 | self.stats = {} |
| 105 | self.sort_arg_dict = {} |
| 106 | self.load_stats(arg) |
| 107 | trouble = 1 |
| 108 | try: |
| 109 | self.get_top_level_stats() |
| 110 | trouble = 0 |
| 111 | finally: |
| 112 | if trouble: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 113 | print("Invalid timing data", end=' ', file=self.stream) |
| 114 | if self.files: print(self.files[-1], end=' ', file=self.stream) |
| 115 | print(file=self.stream) |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 116 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 117 | def load_stats(self, arg): |
| 118 | if not arg: self.stats = {} |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 119 | elif isinstance(arg, str): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 120 | f = open(arg, 'rb') |
| 121 | self.stats = marshal.load(f) |
| 122 | f.close() |
| 123 | try: |
| 124 | file_stats = os.stat(arg) |
Raymond Hettinger | 32200ae | 2002-06-01 19:51:15 +0000 | [diff] [blame] | 125 | arg = time.ctime(file_stats.st_mtime) + " " + arg |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 126 | except: # in case this is not unix |
| 127 | pass |
| 128 | self.files = [ arg ] |
| 129 | elif hasattr(arg, 'create_stats'): |
| 130 | arg.create_stats() |
| 131 | self.stats = arg.stats |
| 132 | arg.stats = {} |
| 133 | if not self.stats: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 134 | raise TypeError("Cannot create or construct a %r object from '%r''" |
| 135 | % (self.__class__, arg)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 136 | return |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 137 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 138 | def get_top_level_stats(self): |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 139 | for func, (cc, nc, tt, ct, callers) in self.stats.items(): |
| 140 | self.total_calls += nc |
| 141 | self.prim_calls += cc |
| 142 | self.total_tt += tt |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 143 | if ("jprofile", 0, "profiler") in callers: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 144 | self.top_level[func] = None |
| 145 | if len(func_std_string(func)) > self.max_name_len: |
| 146 | self.max_name_len = len(func_std_string(func)) |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 147 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 148 | def add(self, *arg_list): |
| 149 | if not arg_list: return self |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 150 | if len(arg_list) > 1: self.add(*arg_list[1:]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 151 | other = arg_list[0] |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 152 | if type(self) != type(other) or self.__class__ != other.__class__: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 153 | other = Stats(other) |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 154 | self.files += other.files |
| 155 | self.total_calls += other.total_calls |
| 156 | self.prim_calls += other.prim_calls |
| 157 | self.total_tt += other.total_tt |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 158 | for func in other.top_level: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 159 | self.top_level[func] = None |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 160 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 161 | if self.max_name_len < other.max_name_len: |
| 162 | self.max_name_len = other.max_name_len |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 163 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 164 | self.fcn_list = None |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 166 | for func, stat in other.stats.items(): |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 167 | if func in self.stats: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 168 | old_func_stat = self.stats[func] |
| 169 | else: |
| 170 | old_func_stat = (0, 0, 0, 0, {},) |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 171 | self.stats[func] = add_func_stats(old_func_stat, stat) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 172 | return self |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 173 | |
Fred Drake | 9c43910 | 2003-05-14 14:28:09 +0000 | [diff] [blame] | 174 | def dump_stats(self, filename): |
| 175 | """Write the profile data to a file we know how to load back.""" |
Alex Martelli | 01c77c6 | 2006-08-24 02:58:11 +0000 | [diff] [blame] | 176 | f = open(filename, 'wb') |
Fred Drake | 9c43910 | 2003-05-14 14:28:09 +0000 | [diff] [blame] | 177 | try: |
| 178 | marshal.dump(self.stats, f) |
| 179 | finally: |
| 180 | f.close() |
| 181 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 182 | # list the tuple indices and directions for sorting, |
| 183 | # along with some printable description |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 184 | sort_arg_dict_default = { |
| 185 | "calls" : (((1,-1), ), "call count"), |
| 186 | "cumulative": (((3,-1), ), "cumulative time"), |
| 187 | "file" : (((4, 1), ), "file name"), |
| 188 | "line" : (((5, 1), ), "line number"), |
| 189 | "module" : (((4, 1), ), "file name"), |
| 190 | "name" : (((6, 1), ), "function name"), |
| 191 | "nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"), |
| 192 | "pcalls" : (((0,-1), ), "call count"), |
| 193 | "stdname" : (((7, 1), ), "standard name"), |
| 194 | "time" : (((2,-1), ), "internal time"), |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 195 | } |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 196 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 197 | def get_sort_arg_defs(self): |
| 198 | """Expand all abbreviations that are unique.""" |
| 199 | if not self.sort_arg_dict: |
| 200 | self.sort_arg_dict = dict = {} |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 201 | bad_list = {} |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 202 | for word, tup in self.sort_arg_dict_default.items(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 203 | fragment = word |
| 204 | while fragment: |
| 205 | if not fragment: |
| 206 | break |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 207 | if fragment in dict: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 208 | bad_list[fragment] = 0 |
| 209 | break |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 210 | dict[fragment] = tup |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 211 | fragment = fragment[:-1] |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 212 | for word in bad_list: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 213 | del dict[word] |
| 214 | return self.sort_arg_dict |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 215 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 216 | def sort_stats(self, *field): |
| 217 | if not field: |
| 218 | self.fcn_list = 0 |
| 219 | return self |
| 220 | if len(field) == 1 and type(field[0]) == type(1): |
| 221 | # Be compatible with old profiler |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 222 | field = [ {-1: "stdname", |
| 223 | 0:"calls", |
| 224 | 1:"time", |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 225 | 2: "cumulative" } [ field[0] ] ] |
| 226 | |
| 227 | sort_arg_defs = self.get_sort_arg_defs() |
| 228 | sort_tuple = () |
| 229 | self.sort_type = "" |
| 230 | connector = "" |
| 231 | for word in field: |
| 232 | sort_tuple = sort_tuple + sort_arg_defs[word][0] |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 233 | self.sort_type += connector + sort_arg_defs[word][1] |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 234 | connector = ", " |
| 235 | |
| 236 | stats_list = [] |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 237 | for func, (cc, nc, tt, ct, callers) in self.stats.items(): |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 238 | stats_list.append((cc, nc, tt, ct) + func + |
| 239 | (func_std_string(func), func)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 240 | |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 241 | stats_list.sort(key=CmpToKey(TupleComp(sort_tuple).compare)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 242 | |
| 243 | self.fcn_list = fcn_list = [] |
| 244 | for tuple in stats_list: |
| 245 | fcn_list.append(tuple[-1]) |
| 246 | return self |
| 247 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 248 | def reverse_order(self): |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 249 | if self.fcn_list: |
| 250 | self.fcn_list.reverse() |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 251 | return self |
| 252 | |
| 253 | def strip_dirs(self): |
| 254 | oldstats = self.stats |
| 255 | self.stats = newstats = {} |
| 256 | max_name_len = 0 |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 257 | for func, (cc, nc, tt, ct, callers) in oldstats.items(): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 258 | newfunc = func_strip_path(func) |
| 259 | if len(func_std_string(newfunc)) > max_name_len: |
| 260 | max_name_len = len(func_std_string(newfunc)) |
| 261 | newcallers = {} |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 262 | for func2, caller in callers.items(): |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 263 | newcallers[func_strip_path(func2)] = caller |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 264 | |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 265 | if newfunc in newstats: |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 266 | newstats[newfunc] = add_func_stats( |
| 267 | newstats[newfunc], |
| 268 | (cc, nc, tt, ct, newcallers)) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 269 | else: |
| 270 | newstats[newfunc] = (cc, nc, tt, ct, newcallers) |
| 271 | old_top = self.top_level |
| 272 | self.top_level = new_top = {} |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 273 | for func in old_top: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 274 | new_top[func_strip_path(func)] = None |
| 275 | |
| 276 | self.max_name_len = max_name_len |
| 277 | |
| 278 | self.fcn_list = None |
| 279 | self.all_callees = None |
| 280 | return self |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 281 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 282 | def calc_callees(self): |
| 283 | if self.all_callees: return |
| 284 | self.all_callees = all_callees = {} |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 285 | for func, (cc, nc, tt, ct, callers) in self.stats.items(): |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 286 | if not func in all_callees: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 287 | all_callees[func] = {} |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 288 | for func2, caller in callers.items(): |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 289 | if not func2 in all_callees: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 290 | all_callees[func2] = {} |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 291 | all_callees[func2][func] = caller |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 292 | return |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 293 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 294 | #****************************************************************** |
| 295 | # The following functions support actual printing of reports |
| 296 | #****************************************************************** |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 297 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 298 | # Optional "amount" is either a line count, or a percentage of lines. |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 299 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 300 | def eval_print_amount(self, sel, list, msg): |
| 301 | new_list = list |
| 302 | if type(sel) == type(""): |
| 303 | new_list = [] |
| 304 | for func in list: |
| 305 | if re.search(sel, func_std_string(func)): |
| 306 | new_list.append(func) |
| 307 | else: |
| 308 | count = len(list) |
| 309 | if type(sel) == type(1.0) and 0.0 <= sel < 1.0: |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 310 | count = int(count * sel + .5) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 311 | new_list = list[:count] |
| 312 | elif type(sel) == type(1) and 0 <= sel < count: |
| 313 | count = sel |
| 314 | new_list = list[:count] |
| 315 | if len(list) != len(new_list): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 316 | msg = msg + " List reduced from %r to %r due to restriction <%r>\n" % ( |
| 317 | len(list), len(new_list), sel) |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 318 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 319 | return new_list, msg |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 320 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 321 | def get_print_list(self, sel_list): |
| 322 | width = self.max_name_len |
| 323 | if self.fcn_list: |
| 324 | list = self.fcn_list[:] |
| 325 | msg = " Ordered by: " + self.sort_type + '\n' |
| 326 | else: |
| 327 | list = self.stats.keys() |
| 328 | msg = " Random listing order was used\n" |
| 329 | |
| 330 | for selection in sel_list: |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 331 | list, msg = self.eval_print_amount(selection, list, msg) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 332 | |
| 333 | count = len(list) |
| 334 | |
| 335 | if not list: |
| 336 | return 0, list |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 337 | print(msg, file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 338 | if count < len(self.stats): |
| 339 | width = 0 |
| 340 | for func in list: |
| 341 | if len(func_std_string(func)) > width: |
| 342 | width = len(func_std_string(func)) |
| 343 | return width+2, list |
| 344 | |
| 345 | def print_stats(self, *amount): |
| 346 | for filename in self.files: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 347 | print(filename, file=self.stream) |
| 348 | if self.files: print(file=self.stream) |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 349 | indent = ' ' * 8 |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 350 | for func in self.top_level: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 351 | print(indent, func_get_function_name(func), file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 352 | |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 353 | print(indent, self.total_calls, "function calls", end=' ', file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 354 | if self.total_calls != self.prim_calls: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 355 | print("(%d primitive calls)" % self.prim_calls, end=' ', file=self.stream) |
| 356 | print("in %.3f CPU seconds" % self.total_tt, file=self.stream) |
| 357 | print(file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 358 | width, list = self.get_print_list(amount) |
| 359 | if list: |
| 360 | self.print_title() |
| 361 | for func in list: |
| 362 | self.print_line(func) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 363 | print(file=self.stream) |
| 364 | print(file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 365 | return self |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 366 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 367 | def print_callees(self, *amount): |
| 368 | width, list = self.get_print_list(amount) |
| 369 | if list: |
| 370 | self.calc_callees() |
| 371 | |
| 372 | self.print_call_heading(width, "called...") |
| 373 | for func in list: |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 374 | if func in self.all_callees: |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 375 | self.print_call_line(width, func, self.all_callees[func]) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 376 | else: |
| 377 | self.print_call_line(width, func, {}) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 378 | print(file=self.stream) |
| 379 | print(file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 380 | return self |
| 381 | |
| 382 | def print_callers(self, *amount): |
| 383 | width, list = self.get_print_list(amount) |
| 384 | if list: |
| 385 | self.print_call_heading(width, "was called by...") |
| 386 | for func in list: |
| 387 | cc, nc, tt, ct, callers = self.stats[func] |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 388 | self.print_call_line(width, func, callers, "<-") |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 389 | print(file=self.stream) |
| 390 | print(file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 391 | return self |
| 392 | |
| 393 | def print_call_heading(self, name_size, column_title): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 394 | print("Function ".ljust(name_size) + column_title, file=self.stream) |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 395 | # print sub-header only if we have new-style callers |
| 396 | subheader = False |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 397 | for cc, nc, tt, ct, callers in self.stats.values(): |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 398 | if callers: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 399 | value = next(iter(callers.values())) |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 400 | subheader = isinstance(value, tuple) |
| 401 | break |
| 402 | if subheader: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 403 | print(" "*name_size + " ncalls tottime cumtime", file=self.stream) |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 404 | |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 405 | def print_call_line(self, name_size, source, call_dict, arrow="->"): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 406 | print(func_std_string(source).ljust(name_size) + arrow, end=' ', file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 407 | if not call_dict: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 408 | print(file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 409 | return |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 410 | clist = sorted(call_dict.keys()) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 411 | indent = "" |
| 412 | for func in clist: |
| 413 | name = func_std_string(func) |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 414 | value = call_dict[func] |
| 415 | if isinstance(value, tuple): |
| 416 | nc, cc, tt, ct = value |
| 417 | if nc != cc: |
| 418 | substats = '%d/%d' % (nc, cc) |
| 419 | else: |
| 420 | substats = '%d' % (nc,) |
| 421 | substats = '%s %s %s %s' % (substats.rjust(7+2*len(indent)), |
| 422 | f8(tt), f8(ct), name) |
| 423 | left_width = name_size + 1 |
| 424 | else: |
| 425 | substats = '%s(%r) %s' % (name, value, f8(self.stats[func][3])) |
| 426 | left_width = name_size + 3 |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 427 | print(indent*left_width + substats, file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 428 | indent = " " |
| 429 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 430 | def print_title(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 431 | print(' ncalls tottime percall cumtime percall', end=' ', file=self.stream) |
| 432 | print('filename:lineno(function)', file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 433 | |
| 434 | def print_line(self, func): # hack : should print percentages |
| 435 | cc, nc, tt, ct, callers = self.stats[func] |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 436 | c = str(nc) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 437 | if nc != cc: |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 438 | c = c + '/' + str(cc) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 439 | print(c.rjust(9), end=' ', file=self.stream) |
| 440 | print(f8(tt), end=' ', file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 441 | if nc == 0: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 442 | print(' '*8, end=' ', file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 443 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 444 | print(f8(tt/nc), end=' ', file=self.stream) |
| 445 | print(f8(ct), end=' ', file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 446 | if cc == 0: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 447 | print(' '*8, end=' ', file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 448 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 449 | print(f8(ct/cc), end=' ', file=self.stream) |
| 450 | print(func_std_string(func), file=self.stream) |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 451 | |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 452 | class TupleComp: |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 453 | """This class provides a generic function for comparing any two tuples. |
| 454 | Each instance records a list of tuple-indices (from most significant |
| 455 | to least significant), and sort direction (ascending or decending) for |
| 456 | each tuple-index. The compare functions can then be used as the function |
| 457 | argument to the system sort() function when a list of tuples need to be |
| 458 | sorted in the instances order.""" |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 459 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 460 | def __init__(self, comp_select_list): |
| 461 | self.comp_select_list = comp_select_list |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 462 | |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 463 | def compare (self, left, right): |
| 464 | for index, direction in self.comp_select_list: |
| 465 | l = left[index] |
| 466 | r = right[index] |
| 467 | if l < r: |
| 468 | return -direction |
| 469 | if l > r: |
| 470 | return direction |
| 471 | return 0 |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 472 | |
Raymond Hettinger | 70b64fc | 2008-01-30 20:15:17 +0000 | [diff] [blame] | 473 | def CmpToKey(mycmp): |
| 474 | 'Convert a cmp= function into a key= function' |
| 475 | class K(object): |
| 476 | def __init__(self, obj): |
| 477 | self.obj = obj |
| 478 | def __lt__(self, other): |
| 479 | return mycmp(self.obj, other.obj) == -1 |
| 480 | return K |
| 481 | |
| 482 | |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 483 | #************************************************************************** |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 484 | # func_name is a triple (file:string, line:int, name:string) |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 485 | |
| 486 | def func_strip_path(func_name): |
Fred Drake | 9c43910 | 2003-05-14 14:28:09 +0000 | [diff] [blame] | 487 | filename, line, name = func_name |
| 488 | return os.path.basename(filename), line, name |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 489 | |
| 490 | def func_get_function_name(func): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 491 | return func[2] |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 492 | |
| 493 | def func_std_string(func_name): # match what old profile produced |
Armin Rigo | a871ef2 | 2006-02-08 12:53:56 +0000 | [diff] [blame] | 494 | if func_name[:2] == ('~', 0): |
| 495 | # special case for built-in functions |
| 496 | name = func_name[2] |
| 497 | if name.startswith('<') and name.endswith('>'): |
| 498 | return '{%s}' % name[1:-1] |
| 499 | else: |
| 500 | return name |
| 501 | else: |
| 502 | return "%s:%d(%s)" % func_name |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 503 | |
| 504 | #************************************************************************** |
| 505 | # The following functions combine statists for pairs functions. |
| 506 | # The bulk of the processing involves correctly handling "call" lists, |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 507 | # such as callers and callees. |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 508 | #************************************************************************** |
| 509 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 510 | def add_func_stats(target, source): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 511 | """Add together all the stats for two profile entries.""" |
| 512 | cc, nc, tt, ct, callers = source |
| 513 | t_cc, t_nc, t_tt, t_ct, t_callers = target |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 514 | return (cc+t_cc, nc+t_nc, tt+t_tt, ct+t_ct, |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 515 | add_callers(t_callers, callers)) |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 516 | |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 517 | def add_callers(target, source): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 518 | """Combine two caller lists in a single list.""" |
| 519 | new_callers = {} |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 520 | for func, caller in target.items(): |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 521 | new_callers[func] = caller |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 522 | for func, caller in source.items(): |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 523 | if func in new_callers: |
Georg Brandl | e90bce7 | 2010-08-02 17:36:05 +0000 | [diff] [blame] | 524 | if isinstance(caller, tuple): |
| 525 | # format used by cProfile |
| 526 | new_callers[func] = tuple([i[0] + i[1] for i in |
| 527 | zip(caller, new_callers[func])]) |
| 528 | else: |
| 529 | # format used by profile |
| 530 | new_callers[func] += caller |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 531 | else: |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 532 | new_callers[func] = caller |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 533 | return new_callers |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 534 | |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 535 | def count_calls(callers): |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 536 | """Sum the caller statistics to get total number of calls received.""" |
| 537 | nc = 0 |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 538 | for calls in callers.values(): |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 539 | nc += calls |
Tim Peters | 2344fae | 2001-01-15 00:50:52 +0000 | [diff] [blame] | 540 | return nc |
Guido van Rossum | adb3105 | 1994-06-23 11:42:52 +0000 | [diff] [blame] | 541 | |
| 542 | #************************************************************************** |
| 543 | # The following functions support printing of reports |
| 544 | #************************************************************************** |
| 545 | |
| 546 | def f8(x): |
Tim Peters | 7d01685 | 2001-10-08 06:13:19 +0000 | [diff] [blame] | 547 | return "%8.3f" % x |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 548 | |
| 549 | #************************************************************************** |
| 550 | # Statistics browser added by ESR, April 2001 |
| 551 | #************************************************************************** |
| 552 | |
| 553 | if __name__ == '__main__': |
| 554 | import cmd |
Eric S. Raymond | 9cb9857 | 2001-04-14 01:48:41 +0000 | [diff] [blame] | 555 | try: |
| 556 | import readline |
Fred Drake | e818761 | 2001-05-11 19:21:41 +0000 | [diff] [blame] | 557 | except ImportError: |
Eric S. Raymond | 9cb9857 | 2001-04-14 01:48:41 +0000 | [diff] [blame] | 558 | pass |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 559 | |
| 560 | class ProfileBrowser(cmd.Cmd): |
| 561 | def __init__(self, profile=None): |
Martin v. Löwis | 66b6e19 | 2001-07-28 14:44:03 +0000 | [diff] [blame] | 562 | cmd.Cmd.__init__(self) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 563 | self.prompt = "% " |
Raymond Hettinger | 16e3c42 | 2002-06-01 16:07:16 +0000 | [diff] [blame] | 564 | if profile is not None: |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 565 | self.stats = Stats(profile) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 566 | self.stream = self.stats.stream |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 567 | else: |
| 568 | self.stats = None |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 569 | self.stream = sys.stdout |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 570 | |
| 571 | def generic(self, fn, line): |
| 572 | args = line.split() |
| 573 | processed = [] |
| 574 | for term in args: |
| 575 | try: |
| 576 | processed.append(int(term)) |
| 577 | continue |
| 578 | except ValueError: |
| 579 | pass |
| 580 | try: |
| 581 | frac = float(term) |
| 582 | if frac > 1 or frac < 0: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 583 | print("Fraction argument must be in [0, 1]", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 584 | continue |
| 585 | processed.append(frac) |
| 586 | continue |
| 587 | except ValueError: |
| 588 | pass |
| 589 | processed.append(term) |
| 590 | if self.stats: |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 591 | getattr(self.stats, fn)(*processed) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 592 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 593 | print("No statistics object is loaded.", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 594 | return 0 |
Eric S. Raymond | 53b809d | 2001-04-26 07:32:38 +0000 | [diff] [blame] | 595 | def generic_help(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 596 | print("Arguments may be:", file=self.stream) |
| 597 | print("* An integer maximum number of entries to print.", file=self.stream) |
| 598 | print("* A decimal fractional number between 0 and 1, controlling", file=self.stream) |
| 599 | print(" what fraction of selected entries to print.", file=self.stream) |
| 600 | print("* A regular expression; only entries with function names", file=self.stream) |
| 601 | print(" that match it are printed.", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 602 | |
| 603 | def do_add(self, line): |
Georg Brandl | 44c5823 | 2010-08-01 19:04:55 +0000 | [diff] [blame] | 604 | if self.stats: |
| 605 | self.stats.add(line) |
| 606 | else: |
| 607 | print("No statistics object is loaded.", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 608 | return 0 |
| 609 | def help_add(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 610 | print("Add profile info from given file to current statistics object.", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 611 | |
| 612 | def do_callees(self, line): |
Eric S. Raymond | 3c1858a | 2001-04-14 15:16:05 +0000 | [diff] [blame] | 613 | return self.generic('print_callees', line) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 614 | def help_callees(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 615 | print("Print callees statistics from the current stat object.", file=self.stream) |
Eric S. Raymond | 53b809d | 2001-04-26 07:32:38 +0000 | [diff] [blame] | 616 | self.generic_help() |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 617 | |
| 618 | def do_callers(self, line): |
Eric S. Raymond | 3c1858a | 2001-04-14 15:16:05 +0000 | [diff] [blame] | 619 | return self.generic('print_callers', line) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 620 | def help_callers(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 621 | print("Print callers statistics from the current stat object.", file=self.stream) |
Eric S. Raymond | 53b809d | 2001-04-26 07:32:38 +0000 | [diff] [blame] | 622 | self.generic_help() |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 623 | |
| 624 | def do_EOF(self, line): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 625 | print("", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 626 | return 1 |
| 627 | def help_EOF(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 628 | print("Leave the profile brower.", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 629 | |
| 630 | def do_quit(self, line): |
| 631 | return 1 |
| 632 | def help_quit(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 633 | print("Leave the profile brower.", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 634 | |
| 635 | def do_read(self, line): |
| 636 | if line: |
| 637 | try: |
| 638 | self.stats = Stats(line) |
Georg Brandl | 50da60c | 2008-01-06 21:38:54 +0000 | [diff] [blame] | 639 | except IOError as err: |
| 640 | print(err.args[1], file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 641 | return |
Georg Brandl | 44c5823 | 2010-08-01 19:04:55 +0000 | [diff] [blame] | 642 | except Exception as err: |
| 643 | print(err.__class__.__name__ + ':', err, file=self.stream) |
| 644 | return |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 645 | self.prompt = line + "% " |
Martin v. Löwis | 22adac5 | 2001-06-07 05:49:05 +0000 | [diff] [blame] | 646 | elif len(self.prompt) > 2: |
Georg Brandl | 44c5823 | 2010-08-01 19:04:55 +0000 | [diff] [blame] | 647 | line = self.prompt[:-2] |
| 648 | self.do_read(line) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 649 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 650 | print("No statistics object is current -- cannot reload.", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 651 | return 0 |
| 652 | def help_read(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 653 | print("Read in profile data from a specified file.", file=self.stream) |
Georg Brandl | 44c5823 | 2010-08-01 19:04:55 +0000 | [diff] [blame] | 654 | print("Without argument, reload the current file.", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 655 | |
| 656 | def do_reverse(self, line): |
Georg Brandl | 44c5823 | 2010-08-01 19:04:55 +0000 | [diff] [blame] | 657 | if self.stats: |
| 658 | self.stats.reverse_order() |
| 659 | else: |
| 660 | print("No statistics object is loaded.", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 661 | return 0 |
| 662 | def help_reverse(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 663 | print("Reverse the sort order of the profiling report.", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 664 | |
| 665 | def do_sort(self, line): |
Georg Brandl | 44c5823 | 2010-08-01 19:04:55 +0000 | [diff] [blame] | 666 | if not self.stats: |
| 667 | print("No statistics object is loaded.", file=self.stream) |
| 668 | return |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 669 | abbrevs = self.stats.get_sort_arg_defs() |
Eric S. Raymond | 53b809d | 2001-04-26 07:32:38 +0000 | [diff] [blame] | 670 | if line and not filter(lambda x,a=abbrevs: x not in a,line.split()): |
Guido van Rossum | 68468eb | 2003-02-27 20:14:51 +0000 | [diff] [blame] | 671 | self.stats.sort_stats(*line.split()) |
Eric S. Raymond | 53b809d | 2001-04-26 07:32:38 +0000 | [diff] [blame] | 672 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 673 | print("Valid sort keys (unique prefixes are accepted):", file=self.stream) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 674 | for (key, value) in Stats.sort_arg_dict_default.items(): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 675 | print("%s -- %s" % (key, value[1]), file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 676 | return 0 |
| 677 | def help_sort(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 678 | print("Sort profile data according to specified keys.", file=self.stream) |
| 679 | print("(Typing `sort' without arguments lists valid keys.)", file=self.stream) |
Martin v. Löwis | 27c430e | 2001-07-30 10:21:13 +0000 | [diff] [blame] | 680 | def complete_sort(self, text, *args): |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 681 | return [a for a in Stats.sort_arg_dict_default if a.startswith(text)] |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 682 | |
| 683 | def do_stats(self, line): |
| 684 | return self.generic('print_stats', line) |
| 685 | def help_stats(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 686 | print("Print statistics from the current stat object.", file=self.stream) |
Eric S. Raymond | 53b809d | 2001-04-26 07:32:38 +0000 | [diff] [blame] | 687 | self.generic_help() |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 688 | |
| 689 | def do_strip(self, line): |
Georg Brandl | 44c5823 | 2010-08-01 19:04:55 +0000 | [diff] [blame] | 690 | if self.stats: |
| 691 | self.stats.strip_dirs() |
| 692 | else: |
| 693 | print("No statistics object is loaded.", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 694 | def help_strip(self): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 695 | print("Strip leading path information from filenames in the report.", file=self.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 696 | |
Georg Brandl | 44c5823 | 2010-08-01 19:04:55 +0000 | [diff] [blame] | 697 | def help_help(self): |
| 698 | print("Show help for a given command.", file=self.stream) |
| 699 | |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 700 | def postcmd(self, stop, line): |
| 701 | if stop: |
| 702 | return stop |
| 703 | return None |
| 704 | |
| 705 | import sys |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 706 | if len(sys.argv) > 1: |
| 707 | initprofile = sys.argv[1] |
| 708 | else: |
| 709 | initprofile = None |
| 710 | try: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 711 | browser = ProfileBrowser(initprofile) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 712 | print("Welcome to the profile statistics browser.", file=browser.stream) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 713 | browser.cmdloop() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 714 | print("Goodbye.", file=browser.stream) |
Eric S. Raymond | 4f3980d | 2001-04-13 00:23:01 +0000 | [diff] [blame] | 715 | except KeyboardInterrupt: |
| 716 | pass |
| 717 | |
| 718 | # That's all, folks. |