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