1
# Copyright (C) 2006 by Canonical Ltd
 
 
2
# Written by John Arbash Meinel <john@arbash-meinel.com>
 
 
4
# This program is free software; you can redistribute it and/or modify
 
 
5
# it under the terms of the GNU General Public License as published by
 
 
6
# the Free Software Foundation; either version 2 of the License, or
 
 
7
# (at your option) any later version.
 
 
9
# This program is distributed in the hope that it will be useful,
 
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 
12
# GNU General Public License for more details.
 
 
14
# You should have received a copy of the GNU General Public License
 
 
15
# along with this program; if not, write to the Free Software
 
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
18
"""A custom importer and regex compiler which logs time spent."""
 
 
31
def stack_add(name, frame_name, frame_lineno, scope_name=None):
 
 
32
    """Start a new record on the stack"""
 
 
35
    this_stack = (_cur_id, name)
 
 
38
        _total_stack[_parent_stack[-1]].append(this_stack)
 
 
39
    _total_stack[this_stack] = []
 
 
40
    _parent_stack.append(this_stack)
 
 
41
    _info[this_stack] = [len(_parent_stack)-1, frame_name, frame_lineno, scope_name]
 
 
46
def stack_finish(this, cost):
 
 
47
    """Finish a given entry, and record its cost in time"""
 
 
50
    assert _parent_stack[-1] == this, \
 
 
51
        'import stack does not end with this %s: %s' % (this, _parent_stack)
 
 
53
    _info[this].append(cost)
 
 
56
def log_stack_info(out_file, sorted=True, hide_fast=True):
 
 
57
    # Find all of the roots with import = 0
 
 
58
    out_file.write(' cum  inline name\t\t\t\t\t\tframe\n')
 
 
59
    todo = [(value[-1], key) for key,value in _info.iteritems() if value[0] == 0]
 
 
65
        cum_time, cur = todo.pop()
 
 
66
        children = _total_stack[cur]
 
 
71
        if hide_fast and info[-1] < 0.0001:
 
 
74
        # Compute the module time by removing the children times
 
 
76
        for child in children:
 
 
78
            mod_time -= c_info[-1]
 
 
79
            c_times.append((c_info[-1], child))
 
 
81
        # indent, cum_time, mod_time, name,
 
 
82
        # scope_name, frame_name, frame_lineno
 
 
83
        out_file.write('%5.1f %5.1f %s %-35s\t@ %s:%d\n'
 
 
84
            % (info[-1]*1000., mod_time*1000., '+'*info[0], 
 
 
85
               cur[1][:35], info[1], info[2]))
 
 
94
_real_import = __import__
 
 
96
def timed_import(name, globals, locals, fromlist):
 
 
97
    """Wrap around standard importer to log import time"""
 
 
99
    scope_name = globals.get('__name__', None)
 
 
100
    if scope_name is None:
 
 
101
        scope_name = globals.get('__file__', None)
 
 
102
    if scope_name is None:
 
 
103
        scope_name = globals.keys()
 
 
105
        # Trim out paths before bzrlib
 
 
106
        loc = scope_name.find('bzrlib')
 
 
108
            scope_name = scope_name[loc:]
 
 
109
        # For stdlib, trim out early paths
 
 
110
        loc = scope_name.find('python2.4')
 
 
112
            scope_name = scope_name[loc:]
 
 
114
    # Figure out the frame that is doing the importing
 
 
115
    frame = sys._getframe(1)
 
 
116
    frame_name = frame.f_globals.get('__name__', '<unknown>')
 
 
118
    if frame_name.endswith('demandload'):
 
 
119
        # If this was demandloaded, we have 3 frames to ignore
 
 
120
        extra = '(demandload) '
 
 
121
        frame = sys._getframe(4)
 
 
122
        frame_name = frame.f_globals.get('__name__', '<unknown>')
 
 
123
    elif frame_name.endswith('lazy_import'):
 
 
124
        # If this was lazily imported, we have 3 frames to ignore
 
 
126
        frame = sys._getframe(4)
 
 
127
        frame_name = frame.f_globals.get('__name__', '<unknown>')
 
 
129
        extra += ' [%s]' % (', '.join(map(str, fromlist)),)
 
 
130
    frame_lineno = frame.f_lineno
 
 
132
    this = stack_add(extra + name, frame_name, frame_lineno, scope_name)
 
 
137
        mod = _real_import(name, globals, locals, fromlist)
 
 
139
        tload = time.time()-tstart
 
 
140
        stack_finish(this, tload)
 
 
145
_real_compile = sre._compile
 
 
147
def timed_compile(*args, **kwargs):
 
 
148
    """Log how long it takes to compile a regex"""
 
 
150
    # And who is requesting this?
 
 
151
    frame = sys._getframe(2)
 
 
152
    frame_name = frame.f_globals.get('__name__', '<unknown>')
 
 
155
    if frame_name.endswith('lazy_regex'):
 
 
156
        # If this was lazily compiled, we have 3 more frames to ignore
 
 
158
        frame = sys._getframe(5)
 
 
159
        frame_name = frame.f_globals.get('__name__', '<unknown>')
 
 
160
    frame_lineno = frame.f_lineno
 
 
161
    this = stack_add(extra+repr(args[0]), frame_name, frame_lineno)
 
 
165
        # Measure the compile time
 
 
166
        comp = _real_compile(*args, **kwargs)
 
 
168
        tcompile = time.time() - tstart
 
 
169
        stack_finish(this, tcompile)
 
 
175
    """Install the hooks for measuring import and regex compile time."""
 
 
176
    __builtins__['__import__'] = timed_import
 
 
177
    sre._compile = timed_compile
 
 
181
    """Remove the import and regex compile timing hooks."""
 
 
182
    __builtins__['__import__'] = _real_import
 
 
183
    sre._compile = _real_compile