1
# Copyright (C) 2006, 2009 by Canonical Ltd
1
# Copyright (C) 2006, 2008, 2009, 2010 by Canonical Ltd
2
2
# Written by John Arbash Meinel <john@arbash-meinel.com>
4
4
# This program is free software; you can redistribute it and/or modify
65
65
def log_stack_info(out_file, sorted=True, hide_fast=True):
66
66
# Find all of the roots with import = 0
67
out_file.write(' cum inline name\t\t\t\t\t\tframe\n')
67
out_file.write('%5s %5s %-40s @ %s:%s\n'
68
% ('cum', 'inline', 'name', 'file', 'line'))
68
69
todo = [(value[-1], key) for key,value in _info.iteritems() if value[0] == 0]
90
91
# indent, cum_time, mod_time, name,
91
92
# scope_name, frame_name, frame_lineno
92
out_file.write('%5.1f %5.1f %s %-35s\t@ %s:%d\n'
93
% (info[-1]*1000., mod_time*1000., '+'*info[0],
94
cur[1][:35], info[1], info[2]))
93
out_file.write('%5.1f %5.1f %-40s @ %s:%d\n'
94
% (info[-1]*1000., mod_time*1000.,
95
('+'*info[0] + cur[1]),
103
105
_real_import = __import__
105
def timed_import(name, globals, locals, fromlist, level=None):
107
def timed_import(name, globals=None, locals=None, fromlist=None, level=None):
106
108
"""Wrap around standard importer to log import time"""
109
# normally there are 4, but if this is called as __import__ eg by
110
# /usr/lib/python2.6/email/__init__.py then there may be only one
107
112
# level is only passed by python2.6
109
scope_name = globals.get('__name__', None)
110
if scope_name is None:
111
scope_name = globals.get('__file__', None)
112
if scope_name is None:
113
scope_name = globals.keys()
115
# can't determine the scope name afaics; we could peek up the stack to
116
# see where this is being called from, but it should be a rare case.
115
# Trim out paths before bzrlib
116
loc = scope_name.find('bzrlib')
118
scope_name = scope_name[loc:]
119
# For stdlib, trim out early paths
120
loc = scope_name.find('python2.4')
122
scope_name = scope_name[loc:]
119
scope_name = globals.get('__name__', None)
120
if scope_name is None:
121
scope_name = globals.get('__file__', None)
122
if scope_name is None:
123
scope_name = globals.keys()
125
# Trim out paths before bzrlib
126
loc = scope_name.find('bzrlib')
128
scope_name = scope_name[loc:]
129
# For stdlib, trim out early paths
130
loc = scope_name.find('python2.4')
132
scope_name = scope_name[loc:]
124
134
# Figure out the frame that is doing the importing
125
135
frame = sys._getframe(1)