Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
pstats.Stats Class Reference

List of all members.

Public Member Functions

def __init__
def init
def load_stats
def get_top_level_stats
def add
def dump_stats
def get_sort_arg_defs
def sort_stats
def reverse_order
def strip_dirs
def calc_callees
def eval_print_amount
def get_print_list
def print_stats
def print_callees
def print_callers
def print_call_heading
def print_call_line
def print_title
def print_line

Public Attributes

 stream
 all_callees
 files
 fcn_list
 total_tt
 total_calls
 prim_calls
 max_name_len
 top_level
 stats
 sort_arg_dict
 sort_type

Static Public Attributes

dictionary sort_arg_dict_default

Detailed Description

This class is used for creating reports from data generated by the
Profile class.  It is a "friend" of that class, and imports data either
by direct access to members of Profile class, or by reading in a dictionary
that was emitted (via marshal) from the Profile class.

The big change from the previous Profiler (in terms of raw functionality)
is that an "add()" method has been provided to combine Stats from
several distinct profile runs.  Both the constructor and the add()
method now take arbitrarily many file names as arguments.

All the print methods now take an argument that indicates how many lines
to print.  If the arg is a floating point number between 0 and 1.0, then
it is taken as a decimal percentage of the available lines to be printed
(e.g., .1 means print 10% of all available lines).  If it is an integer,
it is taken to mean the number of lines of data that you wish to have
printed.

The sort_stats() method now processes some additional options (i.e., in
addition to the old -1, 0, 1, or 2).  It takes an arbitrary number of
quoted strings to select the sort order.  For example sort_stats('time',
'name') sorts on the major key of 'internal function time', and on the
minor key of 'the name of the function'.  Look at the two tables in
sort_stats() and get_sort_arg_defs(self) for more examples.

All methods return self, so you can string together commands like:
    Stats('foo', 'goo').strip_dirs().sort_stats('calls').\
                        print_stats(5).print_callers(5)

Definition at line 32 of file pstats.py.


Constructor & Destructor Documentation

def pstats.Stats.__init__ (   self,
  args,
  stream = None 
)

Definition at line 62 of file pstats.py.

00062 
00063     def __init__(self, *args, stream=None):
00064         self.stream = stream or sys.stdout
00065         if not len(args):
00066             arg = None
00067         else:
00068             arg = args[0]
00069             args = args[1:]
00070         self.init(arg)
00071         self.add(*args)

Here is the caller graph for this function:


Member Function Documentation

def pstats.Stats.add (   self,
  arg_list 
)

Definition at line 124 of file pstats.py.

00124 
00125     def add(self, *arg_list):
00126         if not arg_list:
00127             return self
00128         for item in reversed(arg_list):
00129             if type(self) != type(item):
00130                 item = Stats(item)
00131             self.files += item.files
00132             self.total_calls += item.total_calls
00133             self.prim_calls += item.prim_calls
00134             self.total_tt += item.total_tt
00135             for func in item.top_level:
00136                 self.top_level.add(func)
00137 
00138             if self.max_name_len < item.max_name_len:
00139                 self.max_name_len = item.max_name_len
00140 
00141             self.fcn_list = None
00142 
00143             for func, stat in item.stats.items():
00144                 if func in self.stats:
00145                     old_func_stat = self.stats[func]
00146                 else:
00147                     old_func_stat = (0, 0, 0, 0, {},)
00148                 self.stats[func] = add_func_stats(old_func_stat, stat)
00149         return self

Here is the call graph for this function:

Here is the caller graph for this function:

def pstats.Stats.calc_callees (   self)

Definition at line 258 of file pstats.py.

00258 
00259     def calc_callees(self):
00260         if self.all_callees:
00261             return
00262         self.all_callees = all_callees = {}
00263         for func, (cc, nc, tt, ct, callers) in self.stats.items():
00264             if not func in all_callees:
00265                 all_callees[func] = {}
00266             for func2, caller in callers.items():
00267                 if not func2 in all_callees:
00268                     all_callees[func2] = {}
00269                 all_callees[func2][func]  = caller
00270         return

Here is the caller graph for this function:

def pstats.Stats.dump_stats (   self,
  filename 
)
Write the profile data to a file we know how to load back.

Definition at line 150 of file pstats.py.

00150 
00151     def dump_stats(self, filename):
00152         """Write the profile data to a file we know how to load back."""
00153         f = open(filename, 'wb')
00154         try:
00155             marshal.dump(self.stats, f)
00156         finally:
00157             f.close()

def pstats.Stats.eval_print_amount (   self,
  sel,
  list,
  msg 
)

Definition at line 277 of file pstats.py.

00277 
00278     def eval_print_amount(self, sel, list, msg):
00279         new_list = list
00280         if isinstance(sel, str):
00281             try:
00282                 rex = re.compile(sel)
00283             except re.error:
00284                 msg += "   <Invalid regular expression %r>\n" % sel
00285                 return new_list, msg
00286             new_list = []
00287             for func in list:
00288                 if rex.search(func_std_string(func)):
00289                     new_list.append(func)
00290         else:
00291             count = len(list)
00292             if isinstance(sel, float) and 0.0 <= sel < 1.0:
00293                 count = int(count * sel + .5)
00294                 new_list = list[:count]
00295             elif isinstance(sel, int) and 0 <= sel < count:
00296                 count = sel
00297                 new_list = list[:count]
00298         if len(list) != len(new_list):
00299             msg += "   List reduced from %r to %r due to restriction <%r>\n" % (
00300                 len(list), len(new_list), sel)
00301 
00302         return new_list, msg

Here is the call graph for this function:

Here is the caller graph for this function:

def pstats.Stats.get_print_list (   self,
  sel_list 
)

Definition at line 303 of file pstats.py.

00303 
00304     def get_print_list(self, sel_list):
00305         width = self.max_name_len
00306         if self.fcn_list:
00307             stat_list = self.fcn_list[:]
00308             msg = "   Ordered by: " + self.sort_type + '\n'
00309         else:
00310             stat_list = list(self.stats.keys())
00311             msg = "   Random listing order was used\n"
00312 
00313         for selection in sel_list:
00314             stat_list, msg = self.eval_print_amount(selection, stat_list, msg)
00315 
00316         count = len(stat_list)
00317 
00318         if not stat_list:
00319             return 0, stat_list
00320         print(msg, file=self.stream)
00321         if count < len(self.stats):
00322             width = 0
00323             for func in stat_list:
00324                 if  len(func_std_string(func)) > width:
00325                     width = len(func_std_string(func))
00326         return width+2, stat_list

Here is the call graph for this function:

Here is the caller graph for this function:

Expand all abbreviations that are unique.

Definition at line 173 of file pstats.py.

00173 
00174     def get_sort_arg_defs(self):
00175         """Expand all abbreviations that are unique."""
00176         if not self.sort_arg_dict:
00177             self.sort_arg_dict = dict = {}
00178             bad_list = {}
00179             for word, tup in self.sort_arg_dict_default.items():
00180                 fragment = word
00181                 while fragment:
00182                     if not fragment:
00183                         break
00184                     if fragment in dict:
00185                         bad_list[fragment] = 0
00186                         break
00187                     dict[fragment] = tup
00188                     fragment = fragment[:-1]
00189             for word in bad_list:
00190                 del dict[word]
00191         return self.sort_arg_dict

Here is the caller graph for this function:

Definition at line 114 of file pstats.py.

00114 
00115     def get_top_level_stats(self):
00116         for func, (cc, nc, tt, ct, callers) in self.stats.items():
00117             self.total_calls += nc
00118             self.prim_calls  += cc
00119             self.total_tt    += tt
00120             if ("jprofile", 0, "profiler") in callers:
00121                 self.top_level.add(func)
00122             if len(func_std_string(func)) > self.max_name_len:
00123                 self.max_name_len = len(func_std_string(func))

Here is the call graph for this function:

def pstats.Stats.init (   self,
  arg 
)

Definition at line 72 of file pstats.py.

00072 
00073     def init(self, arg):
00074         self.all_callees = None  # calc only if needed
00075         self.files = []
00076         self.fcn_list = None
00077         self.total_tt = 0
00078         self.total_calls = 0
00079         self.prim_calls = 0
00080         self.max_name_len = 0
00081         self.top_level = set()
00082         self.stats = {}
00083         self.sort_arg_dict = {}
00084         self.load_stats(arg)
00085         try:
00086             self.get_top_level_stats()
00087         except Exception:
00088             print("Invalid timing data %s" %
00089                   (self.files[-1] if self.files else ''), file=self.stream)
00090             raise

Here is the caller graph for this function:

def pstats.Stats.load_stats (   self,
  arg 
)

Definition at line 91 of file pstats.py.

00091 
00092     def load_stats(self, arg):
00093         if arg is None:
00094             self.stats = {}
00095             return
00096         elif isinstance(arg, str):
00097             f = open(arg, 'rb')
00098             self.stats = marshal.load(f)
00099             f.close()
00100             try:
00101                 file_stats = os.stat(arg)
00102                 arg = time.ctime(file_stats.st_mtime) + "    " + arg
00103             except:  # in case this is not unix
00104                 pass
00105             self.files = [arg]
00106         elif hasattr(arg, 'create_stats'):
00107             arg.create_stats()
00108             self.stats = arg.stats
00109             arg.stats = {}
00110         if not self.stats:
00111             raise TypeError("Cannot create or construct a %r object from %r"
00112                             % (self.__class__, arg))
00113         return

def pstats.Stats.print_call_heading (   self,
  name_size,
  column_title 
)

Definition at line 376 of file pstats.py.

00376 
00377     def print_call_heading(self, name_size, column_title):
00378         print("Function ".ljust(name_size) + column_title, file=self.stream)
00379         # print sub-header only if we have new-style callers
00380         subheader = False
00381         for cc, nc, tt, ct, callers in self.stats.values():
00382             if callers:
00383                 value = next(iter(callers.values()))
00384                 subheader = isinstance(value, tuple)
00385                 break
00386         if subheader:
00387             print(" "*name_size + "    ncalls  tottime  cumtime", file=self.stream)

Here is the call graph for this function:

Here is the caller graph for this function:

def pstats.Stats.print_call_line (   self,
  name_size,
  source,
  call_dict,
  arrow = "->" 
)

Definition at line 388 of file pstats.py.

00388 
00389     def print_call_line(self, name_size, source, call_dict, arrow="->"):
00390         print(func_std_string(source).ljust(name_size) + arrow, end=' ', file=self.stream)
00391         if not call_dict:
00392             print(file=self.stream)
00393             return
00394         clist = sorted(call_dict.keys())
00395         indent = ""
00396         for func in clist:
00397             name = func_std_string(func)
00398             value = call_dict[func]
00399             if isinstance(value, tuple):
00400                 nc, cc, tt, ct = value
00401                 if nc != cc:
00402                     substats = '%d/%d' % (nc, cc)
00403                 else:
00404                     substats = '%d' % (nc,)
00405                 substats = '%s %s %s  %s' % (substats.rjust(7+2*len(indent)),
00406                                              f8(tt), f8(ct), name)
00407                 left_width = name_size + 1
00408             else:
00409                 substats = '%s(%r) %s' % (name, value, f8(self.stats[func][3]))
00410                 left_width = name_size + 3
00411             print(indent*left_width + substats, file=self.stream)
00412             indent = " "

Here is the call graph for this function:

Here is the caller graph for this function:

def pstats.Stats.print_callees (   self,
  amount 
)

Definition at line 350 of file pstats.py.

00350 
00351     def print_callees(self, *amount):
00352         width, list = self.get_print_list(amount)
00353         if list:
00354             self.calc_callees()
00355 
00356             self.print_call_heading(width, "called...")
00357             for func in list:
00358                 if func in self.all_callees:
00359                     self.print_call_line(width, func, self.all_callees[func])
00360                 else:
00361                     self.print_call_line(width, func, {})
00362             print(file=self.stream)
00363             print(file=self.stream)
00364         return self

Here is the call graph for this function:

def pstats.Stats.print_callers (   self,
  amount 
)

Definition at line 365 of file pstats.py.

00365 
00366     def print_callers(self, *amount):
00367         width, list = self.get_print_list(amount)
00368         if list:
00369             self.print_call_heading(width, "was called by...")
00370             for func in list:
00371                 cc, nc, tt, ct, callers = self.stats[func]
00372                 self.print_call_line(width, func, callers, "<-")
00373             print(file=self.stream)
00374             print(file=self.stream)
00375         return self

Here is the call graph for this function:

def pstats.Stats.print_line (   self,
  func 
)

Definition at line 417 of file pstats.py.

00417 
00418     def print_line(self, func):  # hack: should print percentages
00419         cc, nc, tt, ct, callers = self.stats[func]
00420         c = str(nc)
00421         if nc != cc:
00422             c = c + '/' + str(cc)
00423         print(c.rjust(9), end=' ', file=self.stream)
00424         print(f8(tt), end=' ', file=self.stream)
00425         if nc == 0:
00426             print(' '*8, end=' ', file=self.stream)
00427         else:
00428             print(f8(tt/nc), end=' ', file=self.stream)
00429         print(f8(ct), end=' ', file=self.stream)
00430         if cc == 0:
00431             print(' '*8, end=' ', file=self.stream)
00432         else:
00433             print(f8(ct/cc), end=' ', file=self.stream)
00434         print(func_std_string(func), file=self.stream)

Here is the call graph for this function:

Here is the caller graph for this function:

def pstats.Stats.print_stats (   self,
  amount 
)

Definition at line 327 of file pstats.py.

00327 
00328     def print_stats(self, *amount):
00329         for filename in self.files:
00330             print(filename, file=self.stream)
00331         if self.files:
00332             print(file=self.stream)
00333         indent = ' ' * 8
00334         for func in self.top_level:
00335             print(indent, func_get_function_name(func), file=self.stream)
00336 
00337         print(indent, self.total_calls, "function calls", end=' ', file=self.stream)
00338         if self.total_calls != self.prim_calls:
00339             print("(%d primitive calls)" % self.prim_calls, end=' ', file=self.stream)
00340         print("in %.3f seconds" % self.total_tt, file=self.stream)
00341         print(file=self.stream)
00342         width, list = self.get_print_list(amount)
00343         if list:
00344             self.print_title()
00345             for func in list:
00346                 self.print_line(func)
00347             print(file=self.stream)
00348             print(file=self.stream)
00349         return self

Here is the call graph for this function:

def pstats.Stats.print_title (   self)

Definition at line 413 of file pstats.py.

00413 
00414     def print_title(self):
00415         print('   ncalls  tottime  percall  cumtime  percall', end=' ', file=self.stream)
00416         print('filename:lineno(function)', file=self.stream)

Here is the caller graph for this function:

Definition at line 224 of file pstats.py.

00224 
00225     def reverse_order(self):
00226         if self.fcn_list:
00227             self.fcn_list.reverse()
00228         return self

def pstats.Stats.sort_stats (   self,
  field 
)

Definition at line 192 of file pstats.py.

00192 
00193     def sort_stats(self, *field):
00194         if not field:
00195             self.fcn_list = 0
00196             return self
00197         if len(field) == 1 and isinstance(field[0], int):
00198             # Be compatible with old profiler
00199             field = [ {-1: "stdname",
00200                        0:  "calls",
00201                        1:  "time",
00202                        2:  "cumulative"}[field[0]] ]
00203 
00204         sort_arg_defs = self.get_sort_arg_defs()
00205         sort_tuple = ()
00206         self.sort_type = ""
00207         connector = ""
00208         for word in field:
00209             sort_tuple = sort_tuple + sort_arg_defs[word][0]
00210             self.sort_type += connector + sort_arg_defs[word][1]
00211             connector = ", "
00212 
00213         stats_list = []
00214         for func, (cc, nc, tt, ct, callers) in self.stats.items():
00215             stats_list.append((cc, nc, tt, ct) + func +
00216                               (func_std_string(func), func))
00217 
00218         stats_list.sort(key=cmp_to_key(TupleComp(sort_tuple).compare))
00219 
00220         self.fcn_list = fcn_list = []
00221         for tuple in stats_list:
00222             fcn_list.append(tuple[-1])
00223         return self

Here is the call graph for this function:

def pstats.Stats.strip_dirs (   self)

Definition at line 229 of file pstats.py.

00229 
00230     def strip_dirs(self):
00231         oldstats = self.stats
00232         self.stats = newstats = {}
00233         max_name_len = 0
00234         for func, (cc, nc, tt, ct, callers) in oldstats.items():
00235             newfunc = func_strip_path(func)
00236             if len(func_std_string(newfunc)) > max_name_len:
00237                 max_name_len = len(func_std_string(newfunc))
00238             newcallers = {}
00239             for func2, caller in callers.items():
00240                 newcallers[func_strip_path(func2)] = caller
00241 
00242             if newfunc in newstats:
00243                 newstats[newfunc] = add_func_stats(
00244                                         newstats[newfunc],
00245                                         (cc, nc, tt, ct, newcallers))
00246             else:
00247                 newstats[newfunc] = (cc, nc, tt, ct, newcallers)
00248         old_top = self.top_level
00249         self.top_level = new_top = set()
00250         for func in old_top:
00251             new_top.add(func_strip_path(func))
00252 
00253         self.max_name_len = max_name_len
00254 
00255         self.fcn_list = None
00256         self.all_callees = None
00257         return self

Here is the call graph for this function:


Member Data Documentation

Definition at line 73 of file pstats.py.

Definition at line 75 of file pstats.py.

Definition at line 74 of file pstats.py.

Definition at line 79 of file pstats.py.

Definition at line 78 of file pstats.py.

Definition at line 82 of file pstats.py.

Initial value:
{
              "calls"     : (((1,-1),              ), "call count"),
              "cumulative": (((3,-1),              ), "cumulative time"),
              "file"      : (((4, 1),              ), "file name"),
              "line"      : (((5, 1),              ), "line number"),
              "module"    : (((4, 1),              ), "file name"),
              "name"      : (((6, 1),              ), "function name"),
              "nfl"       : (((6, 1),(4, 1),(5, 1),), "name/file/line"),
              "pcalls"    : (((0,-1),              ), "call count"),
              "stdname"   : (((7, 1),              ), "standard name"),
              "time"      : (((2,-1),              ), "internal time"),
              }

Definition at line 160 of file pstats.py.

Definition at line 205 of file pstats.py.

Definition at line 81 of file pstats.py.

Definition at line 63 of file pstats.py.

Definition at line 80 of file pstats.py.

Definition at line 77 of file pstats.py.

Definition at line 76 of file pstats.py.


The documentation for this class was generated from the following file: