def _wrapped(frame, event, arg): if event == 'call': fpath = abspath(frame.f_code.co_filename) depth[0] += 1 if fpath in fileset: if 'self' in frame.f_locals: klass = frame.f_locals['self'].__class__.__name__ else: klass = '?' print('c', fpath, frame.f_code.co_firstlineno, frame.f_code.co_name, mem_usage(), time.time(), klass, sep='|', file=stream) elif event == 'return' and depth[0] > 0: fpath = abspath(frame.f_code.co_filename) depth[0] -= 1 if fpath in fileset: print('r', fpath, frame.f_code.co_firstlineno, frame.f_code.co_name, mem_usage(), time.time(), '-', sep='|', file=stream)
def _trace_mem_call(frame, arg, stack, context): """ Called whenever a function is called that matches glob patterns and isinstance checks. """ memstack, _ = context memstack.append([(frame.f_code.co_filename, frame.f_code.co_firstlineno, frame.f_code.co_name), mem_usage()])
def _trace_return(frame, arg, stack, context): """ This is called when a matched function returns. This only happens if show_return is True when setup() is called. """ global time0 (qual_cache, method_counts, class_counts, id2count, verbose, memory, leaks, stream, show_ptrs) = context funcname = find_qualified_name(frame.f_code.co_filename, frame.f_code.co_firstlineno, qual_cache) self = frame.f_locals['self'] try: pname = "(%s)" % self.pathname except AttributeError: pname = "" sname = "%s#%d%s" % (self.__class__.__name__, id2count[id(self)], pname) indent = tab * len(stack) if memory is not None: current_mem = mem_usage() last_mem = memory.pop() if current_mem != last_mem: delta = current_mem - last_mem _printer( "%s<-- %s (time: %8.5f) (total: %6.3f MB) (diff: %+.0f KB)" % (indent, '.'.join((sname, funcname)), time.time() - time0, current_mem, delta * 1024.)) # add this delta to all callers so when they calculate their own delta, this # delta won't be included for i in range(len(memory) - 1, -1, -1): memory[i] += delta else: _printer("%s<-- %s (time: %8.5f) (total: %6.3f MB)" % (indent, '.'.join( (sname, funcname)), time.time() - time0, current_mem)) else: _printer("%s<-- %s" % (indent, '.'.join((sname, funcname)))) if verbose: if arg is not None: s = "%s %s" % (indent, arg) if not show_ptrs and ' object at ' in s: s = addr_regex.sub('', s) _printer(s) if leaks is not None: last_objs = leaks.pop() for name, _, delta_objs in objgraph.growth(peak_stats=last_objs): _printer("%s %s %+d" % (indent, name, delta_objs)) stream.flush()
def _trace_call(frame, arg, stack, context): """ This is called after we have matched based on glob pattern and isinstance check. """ global time0 if time0 is None: time0 = time.time() (qual_cache, method_counts, class_counts, id2count, verbose, memory, leaks, stream, show_ptrs) = context funcname = find_qualified_name(frame.f_code.co_filename, frame.f_code.co_firstlineno, qual_cache) self = frame.f_locals['self'] try: pname = "(%s)" % self.pathname except AttributeError: pname = "" cname = self.__class__.__name__ my_id = id(self) if my_id in id2count: id_count = id2count[my_id] else: class_counts[cname] += 1 id2count[my_id] = id_count = class_counts[cname] sname = "%s#%d%s" % (self.__class__.__name__, id_count, pname) fullname = '.'.join((sname, funcname)) method_counts[fullname] += 1 indent = tab * (len(stack) - 1) if verbose: _printer("%s--> %s (%d)" % (indent, fullname, method_counts[fullname])) _indented_print(frame.f_locals, frame.f_locals, len(stack) - 1, show_ptrs=show_ptrs) else: _printer("%s-->%s" % (indent, fullname)) if memory is not None: memory.append(mem_usage()) if leaks is not None: stats = objgraph.typestats() stats['frame'] += 1 stats['cell'] += 1 stats['list'] += 1 leaks.append(stats) stream.flush()
def _trace_mem_ret(frame, arg, stack, context): """ Called whenever a function returns that matches glob patterns and isinstance checks. """ memstack, mem_changes = context key, mem_start = memstack.pop() usage = mem_usage() delta = usage - mem_start if delta > 0.0: mem_changes[key][0] += delta mem_changes[key][1] += 1 if memstack: mem_changes[key][2].add(memstack[-1][0])