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