1 """Class for printing reports on profiled python code."""
3 # Class for printing reports on profiled python code. rev 1.0 4/1/94
5 # Based on prior profile module by Sjoerd Mullender...
6 # which was hacked somewhat by: Guido van Rossum
8 # see profile.doc and profile.py for more info.
10 # Copyright 1994, by InfoSeek Corporation, all rights reserved.
11 # Written by James Roskind
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
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.
45 """This class is used for creating reports from data generated by the
46 Profile class. It is a "friend" of that class, and imports data either
47 by direct access to members of Profile class, or by reading in a dictionary
48 that was emitted (via marshal) from the Profile class.
50 The big change from the previous Profiler (in terms of raw functionality)
51 is that an "add()" method has been provided to combine Stats from
52 several distinct profile runs. Both the constructor and the add()
53 method now take arbitrarily many file names as arguments.
55 All the print methods now take an argument that indicates how many lines
56 to print. If the arg is a floating point number between 0 and 1.0, then
57 it is taken as a decimal percentage of the available lines to be printed
58 (e.g., .1 means print 10% of all available lines). If it is an integer,
59 it is taken to mean the number of lines of data that you wish to have
62 The sort_stats() method now processes some additional options (i.e., in
63 addition to the old -1, 0, 1, or 2). It takes an arbitrary number of quoted
64 strings to select the sort order. For example sort_stats('time', 'name')
65 sorts on the major key of "internal function time", and on the minor
66 key of 'the name of the function'. Look at the two tables in sort_stats()
67 and get_sort_arg_defs(self) for more examples.
69 All methods now return "self", so you can string together commands like:
70 Stats('foo', 'goo').strip_dirs().sort_stats('calls').\
71 print_stats(5).print_callers(5)
74 def __init__(self
, *args
):
81 apply(self
.add
, args
).ignore()
84 self
.all_callees
= None # calc only if needed
93 self
.sort_arg_dict
= {}
97 self
.get_top_level_stats()
101 print "Invalid timing data",
102 if self
.files
: print self
.files
[-1],
106 def load_stats(self
, arg
):
107 if not arg
: self
.stats
= {}
108 elif type(arg
) == type(""):
110 self
.stats
= marshal
.load(f
)
113 file_stats
= os
.stat(arg
)
114 arg
= time
.ctime(file_stats
[8]) + " " + arg
115 except: # in case this is not unix
118 elif hasattr(arg
, 'create_stats'):
120 self
.stats
= arg
.stats
123 raise TypeError, "Cannot create or construct a " \
125 + " object from '" + `arg`
+ "'"
128 def get_top_level_stats(self
):
129 for func
in self
.stats
.keys():
130 cc
, nc
, tt
, ct
, callers
= self
.stats
[func
]
131 self
.total_calls
= self
.total_calls
+ nc
132 self
.prim_calls
= self
.prim_calls
+ cc
133 self
.total_tt
= self
.total_tt
+ tt
134 if callers
.has_key(("jprofile", 0, "profiler")):
135 self
.top_level
[func
] = None
136 if len(func_std_string(func
)) > self
.max_name_len
:
137 self
.max_name_len
= len(func_std_string(func
))
139 def add(self
, *arg_list
):
140 if not arg_list
: return self
141 if len(arg_list
) > 1: apply(self
.add
, arg_list
[1:])
143 if type(self
) != type(other
) or \
144 self
.__class
__ != other
.__class
__:
146 self
.files
= self
.files
+ other
.files
147 self
.total_calls
= self
.total_calls
+ other
.total_calls
148 self
.prim_calls
= self
.prim_calls
+ other
.prim_calls
149 self
.total_tt
= self
.total_tt
+ other
.total_tt
150 for func
in other
.top_level
.keys():
151 self
.top_level
[func
] = None
153 if self
.max_name_len
< other
.max_name_len
:
154 self
.max_name_len
= other
.max_name_len
158 for func
in other
.stats
.keys():
159 if self
.stats
.has_key(func
):
160 old_func_stat
= self
.stats
[func
]
162 old_func_stat
= (0, 0, 0, 0, {},)
163 self
.stats
[func
] = add_func_stats(old_func_stat
, \
169 # list the tuple indices and directions for sorting,
170 # along with some printable description
171 sort_arg_dict_default
= {\
172 "calls" : (((1,-1), ), "call count"),\
173 "cumulative": (((3,-1), ), "cumulative time"),\
174 "file" : (((4, 1), ), "file name"),\
175 "line" : (((5, 1), ), "line number"),\
176 "module" : (((4, 1), ), "file name"),\
177 "name" : (((6, 1), ), "function name"),\
178 "nfl" : (((6, 1),(4, 1),(5, 1),), "name/file/line"), \
179 "pcalls" : (((0,-1), ), "call count"),\
180 "stdname" : (((7, 1), ), "standard name"),\
181 "time" : (((2,-1), ), "internal time"),\
184 def get_sort_arg_defs(self
):
185 """Expand all abbreviations that are unique."""
186 if not self
.sort_arg_dict
:
187 self
.sort_arg_dict
= dict = {}
188 std_list
= dict.keys()
190 for word
in self
.sort_arg_dict_default
.keys():
195 if dict.has_key(fragment
):
196 bad_list
[fragment
] = 0
198 dict[fragment
] = self
. \
199 sort_arg_dict_default
[word
]
200 fragment
= fragment
[:-1]
201 for word
in bad_list
.keys():
203 return self
.sort_arg_dict
206 def sort_stats(self
, *field
):
210 if len(field
) == 1 and type(field
[0]) == type(1):
211 # Be compatible with old profiler
212 field
= [ {-1: "stdname", \
215 2: "cumulative" } [ field
[0] ] ]
217 sort_arg_defs
= self
.get_sort_arg_defs()
222 sort_tuple
= sort_tuple
+ sort_arg_defs
[word
][0]
223 self
.sort_type
= self
.sort_type
+ connector
+ \
224 sort_arg_defs
[word
][1]
228 for func
in self
.stats
.keys():
229 cc
, nc
, tt
, ct
, callers
= self
.stats
[func
]
230 stats_list
.append((cc
, nc
, tt
, ct
) + func_split(func
) \
231 + (func_std_string(func
), func
,) )
233 stats_list
.sort(TupleComp(sort_tuple
).compare
)
235 self
.fcn_list
= fcn_list
= []
236 for tuple in stats_list
:
237 fcn_list
.append(tuple[-1])
241 def reverse_order(self
):
242 if self
.fcn_list
: self
.fcn_list
.reverse()
245 def strip_dirs(self
):
246 oldstats
= self
.stats
247 self
.stats
= newstats
= {}
249 for func
in oldstats
.keys():
250 cc
, nc
, tt
, ct
, callers
= oldstats
[func
]
251 newfunc
= func_strip_path(func
)
252 if len(func_std_string(newfunc
)) > max_name_len
:
253 max_name_len
= len(func_std_string(newfunc
))
255 for func2
in callers
.keys():
256 newcallers
[func_strip_path(func2
)] = \
259 if newstats
.has_key(newfunc
):
260 newstats
[newfunc
] = add_func_stats( \
262 (cc
, nc
, tt
, ct
, newcallers
))
264 newstats
[newfunc
] = (cc
, nc
, tt
, ct
, newcallers
)
265 old_top
= self
.top_level
266 self
.top_level
= new_top
= {}
267 for func
in old_top
.keys():
268 new_top
[func_strip_path(func
)] = None
270 self
.max_name_len
= max_name_len
273 self
.all_callees
= None
278 def calc_callees(self
):
279 if self
.all_callees
: return
280 self
.all_callees
= all_callees
= {}
281 for func
in self
.stats
.keys():
282 if not all_callees
.has_key(func
):
283 all_callees
[func
] = {}
284 cc
, nc
, tt
, ct
, callers
= self
.stats
[func
]
285 for func2
in callers
.keys():
286 if not all_callees
.has_key(func2
):
287 all_callees
[func2
] = {}
288 all_callees
[func2
][func
] = callers
[func2
]
291 #******************************************************************
292 # The following functions support actual printing of reports
293 #******************************************************************
295 # Optional "amount" is either a line count, or a percentage of lines.
297 def eval_print_amount(self
, sel
, list, msg
):
299 if type(sel
) == type(""):
302 if re
.search(sel
, func_std_string(func
)):
303 new_list
.append(func
)
306 if type(sel
) == type(1.0) and 0.0 <= sel
< 1.0:
307 count
= int (count
* sel
+ .5)
308 new_list
= list[:count
]
309 elif type(sel
) == type(1) and 0 <= sel
< count
:
311 new_list
= list[:count
]
312 if len(list) != len(new_list
):
313 msg
= msg
+ " List reduced from " + `
len(list)` \
314 + " to " + `
len(new_list
)`
+ \
315 " due to restriction <" + `sel`
+ ">\n"
321 def get_print_list(self
, sel_list
):
322 width
= self
.max_name_len
324 list = self
.fcn_list
[:]
325 msg
= " Ordered by: " + self
.sort_type
+ '\n'
327 list = self
.stats
.keys()
328 msg
= " Random listing order was used\n"
330 for selection
in sel_list
:
331 list,msg
= self
.eval_print_amount(selection
, list, msg
)
338 if count
< len(self
.stats
):
341 if len(func_std_string(func
)) > width
:
342 width
= len(func_std_string(func
))
345 def print_stats(self
, *amount
):
346 for filename
in self
.files
:
350 for func
in self
.top_level
.keys():
351 print indent
, func_get_function_name(func
)
353 print indent
, self
.total_calls
, "function calls",
354 if self
.total_calls
!= self
.prim_calls
:
355 print "(" + `self
.prim_calls`
, "primitive calls)",
356 print "in", fpformat
.fix(self
.total_tt
, 3), "CPU seconds"
358 width
, list = self
.get_print_list(amount
)
362 self
.print_line(func
)
368 def print_callees(self
, *amount
):
369 width
, list = self
.get_print_list(amount
)
373 self
.print_call_heading(width
, "called...")
375 if self
.all_callees
.has_key(func
):
376 self
.print_call_line(width
, \
377 func
, self
.all_callees
[func
])
379 self
.print_call_line(width
, func
, {})
384 def print_callers(self
, *amount
):
385 width
, list = self
.get_print_list(amount
)
387 self
.print_call_heading(width
, "was called by...")
389 cc
, nc
, tt
, ct
, callers
= self
.stats
[func
]
390 self
.print_call_line(width
, func
, callers
)
395 def print_call_heading(self
, name_size
, column_title
):
396 print "Function ".ljust(name_size
) + column_title
399 def print_call_line(self
, name_size
, source
, call_dict
):
400 print func_std_string(source
).ljust(name_size
),
404 clist
= call_dict
.keys()
406 name_size
= name_size
+ 1
409 name
= func_std_string(func
)
410 print indent
*name_size
+ name
+ '(' \
411 + `call_dict
[func
]`
+')', \
412 f8(self
.stats
[func
][3])
417 def print_title(self
):
418 print 'ncalls'.rjust(9),
419 print 'tottime'.rjust(8),
420 print 'percall'.rjust(8),
421 print 'cumtime'.rjust(8),
422 print 'percall'.rjust(8),
423 print 'filename:lineno(function)'
426 def print_line(self
, func
): # hack : should print percentages
427 cc
, nc
, tt
, ct
, callers
= self
.stats
[func
]
442 print func_std_string(func
)
446 pass # has no return value, so use at end of line :-)
450 """This class provides a generic function for comparing any two tuples.
451 Each instance records a list of tuple-indices (from most significant
452 to least significant), and sort direction (ascending or decending) for
453 each tuple-index. The compare functions can then be used as the function
454 argument to the system sort() function when a list of tuples need to be
455 sorted in the instances order."""
457 def __init__(self
, comp_select_list
):
458 self
.comp_select_list
= comp_select_list
460 def compare (self
, left
, right
):
461 for index
, direction
in self
.comp_select_list
:
472 #**************************************************************************
474 def func_strip_path(func_name
):
475 file, line
, name
= func_name
476 return os
.path
.basename(file), line
, name
478 def func_get_function_name(func
):
481 def func_std_string(func_name
): # match what old profile produced
482 file, line
, name
= func_name
483 return file + ":" + `line`
+ "(" + name
+ ")"
485 def func_split(func_name
):
488 #**************************************************************************
489 # The following functions combine statists for pairs functions.
490 # The bulk of the processing involves correctly handling "call" lists,
491 # such as callers and callees.
492 #**************************************************************************
494 def add_func_stats(target
, source
):
495 """Add together all the stats for two profile entries."""
496 cc
, nc
, tt
, ct
, callers
= source
497 t_cc
, t_nc
, t_tt
, t_ct
, t_callers
= target
498 return (cc
+t_cc
, nc
+t_nc
, tt
+t_tt
, ct
+t_ct
, \
499 add_callers(t_callers
, callers
))
502 def add_callers(target
, source
):
503 """Combine two caller lists in a single list."""
505 for func
in target
.keys():
506 new_callers
[func
] = target
[func
]
507 for func
in source
.keys():
508 if new_callers
.has_key(func
):
509 new_callers
[func
] = source
[func
] + new_callers
[func
]
511 new_callers
[func
] = source
[func
]
514 def count_calls(callers
):
515 """Sum the caller statistics to get total number of calls received."""
517 for func
in callers
.keys():
518 nc
= nc
+ callers
[func
]
521 #**************************************************************************
522 # The following functions support printing of reports
523 #**************************************************************************
526 return fpformat
.fix(x
, 3).rjust(8)