~bzr-pqm/bzr/bzr.dev

1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
1
# Copyright (C) 2006 by Canonical Ltd
1724.2.13 by John Arbash Meinel
Remove pprint dependency
2
# Written by John Arbash Meinel <john@arbash-meinel.com>
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
3
#
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.
8
#
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.
13
#
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
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
17
1724.2.7 by John Arbash Meinel
It seems you cannot override __builtins__.__import__ in a sub-module.
18
"""A custom importer and regex compiler which logs time spent."""
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
19
1724.2.7 by John Arbash Meinel
It seems you cannot override __builtins__.__import__ in a sub-module.
20
import sre
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
21
import sys
22
import time
1724.2.7 by John Arbash Meinel
It seems you cannot override __builtins__.__import__ in a sub-module.
23
24
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
25
_parent_stack = []
26
_total_stack = {}
27
_info = {}
28
_cur_id = 0
3696.1.1 by John Arbash Meinel
Use the right timing function on win32
29
_timer = time.time
30
if sys.platform == 'win32':
31
    _timer = time.clock
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
32
33
34
def stack_add(name, frame_name, frame_lineno, scope_name=None):
35
    """Start a new record on the stack"""
36
    global _cur_id
37
    _cur_id += 1
38
    this_stack = (_cur_id, name)
39
40
    if _parent_stack:
41
        _total_stack[_parent_stack[-1]].append(this_stack)
42
    _total_stack[this_stack] = []
43
    _parent_stack.append(this_stack)
44
    _info[this_stack] = [len(_parent_stack)-1, frame_name, frame_lineno, scope_name]
45
46
    return this_stack
47
48
49
def stack_finish(this, cost):
50
    """Finish a given entry, and record its cost in time"""
51
    global _parent_stack
52
1724.2.13 by John Arbash Meinel
Remove pprint dependency
53
    assert _parent_stack[-1] == this, \
54
        'import stack does not end with this %s: %s' % (this, _parent_stack)
55
    _parent_stack.pop()
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
56
    _info[this].append(cost)
57
58
1724.2.12 by John Arbash Meinel
Default to hiding things that take less that .1ms to keep the output clean.
59
def log_stack_info(out_file, sorted=True, hide_fast=True):
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
60
    # Find all of the roots with import = 0
1724.2.12 by John Arbash Meinel
Default to hiding things that take less that .1ms to keep the output clean.
61
    out_file.write(' cum  inline name\t\t\t\t\t\tframe\n')
1724.2.10 by John Arbash Meinel
More layout work
62
    todo = [(value[-1], key) for key,value in _info.iteritems() if value[0] == 0]
63
64
    if sorted:
65
        todo.sort()
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
66
67
    while todo:
1724.2.10 by John Arbash Meinel
More layout work
68
        cum_time, cur = todo.pop()
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
69
        children = _total_stack[cur]
70
1724.2.10 by John Arbash Meinel
More layout work
71
        c_times = []
72
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
73
        info = _info[cur]
1724.2.12 by John Arbash Meinel
Default to hiding things that take less that .1ms to keep the output clean.
74
        if hide_fast and info[-1] < 0.0001:
75
            continue
76
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
77
        # Compute the module time by removing the children times
78
        mod_time = info[-1]
79
        for child in children:
80
            c_info = _info[child]
81
            mod_time -= c_info[-1]
1724.2.10 by John Arbash Meinel
More layout work
82
            c_times.append((c_info[-1], child))
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
83
84
        # indent, cum_time, mod_time, name,
85
        # scope_name, frame_name, frame_lineno
1724.2.11 by John Arbash Meinel
Remove scope name for now, still log it, in case we need it for demandload.
86
        out_file.write('%5.1f %5.1f %s %-35s\t@ %s:%d\n'
1724.2.9 by John Arbash Meinel
change layout to be easier to mentally parse.
87
            % (info[-1]*1000., mod_time*1000., '+'*info[0], 
1996.3.16 by John Arbash Meinel
Update output of --profile-imports to understand from foo import bar time
88
               cur[1][:35], info[1], info[2]))
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
89
1724.2.10 by John Arbash Meinel
More layout work
90
        if sorted:
91
            c_times.sort()
92
        else:
93
            c_times.reverse()
94
        todo.extend(c_times)
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
95
96
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
97
_real_import = __import__
98
4792.9.1 by Martin Pool
Fix --profile-imports for python2.6 import hook parameters
99
def timed_import(name, globals, locals, fromlist, level=None):
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
100
    """Wrap around standard importer to log import time"""
4792.9.1 by Martin Pool
Fix --profile-imports for python2.6 import hook parameters
101
    # level is only passed by python2.6
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
102
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
103
    scope_name = globals.get('__name__', None)
104
    if scope_name is None:
105
        scope_name = globals.get('__file__', None)
106
    if scope_name is None:
107
        scope_name = globals.keys()
108
    else:
109
        # Trim out paths before bzrlib
110
        loc = scope_name.find('bzrlib')
111
        if loc != -1:
112
            scope_name = scope_name[loc:]
113
        # For stdlib, trim out early paths
114
        loc = scope_name.find('python2.4')
115
        if loc != -1:
116
            scope_name = scope_name[loc:]
117
118
    # Figure out the frame that is doing the importing
119
    frame = sys._getframe(1)
120
    frame_name = frame.f_globals.get('__name__', '<unknown>')
121
    extra = ''
122
    if frame_name.endswith('demandload'):
123
        # If this was demandloaded, we have 3 frames to ignore
2063.4.3 by John Arbash Meinel
Update profile imports to be aware of the new compiler
124
        extra = '(demandload) '
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
125
        frame = sys._getframe(4)
1996.3.2 by John Arbash Meinel
Make profile_imports aware of lazy_import stuff
126
        frame_name = frame.f_globals.get('__name__', '<unknown>')
127
    elif frame_name.endswith('lazy_import'):
128
        # If this was lazily imported, we have 3 frames to ignore
2063.4.3 by John Arbash Meinel
Update profile imports to be aware of the new compiler
129
        extra = '[l] '
1996.3.2 by John Arbash Meinel
Make profile_imports aware of lazy_import stuff
130
        frame = sys._getframe(4)
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
131
        frame_name = frame.f_globals.get('__name__', '<unknown>')
1996.3.16 by John Arbash Meinel
Update output of --profile-imports to understand from foo import bar time
132
    if fromlist:
133
        extra += ' [%s]' % (', '.join(map(str, fromlist)),)
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
134
    frame_lineno = frame.f_lineno
135
2063.4.3 by John Arbash Meinel
Update profile imports to be aware of the new compiler
136
    this = stack_add(extra + name, frame_name, frame_lineno, scope_name)
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
137
3696.1.1 by John Arbash Meinel
Use the right timing function on win32
138
    tstart = _timer()
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
139
    try:
140
        # Do the import
141
        mod = _real_import(name, globals, locals, fromlist)
142
    finally:
3696.1.1 by John Arbash Meinel
Use the right timing function on win32
143
        tload = _timer()-tstart
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
144
        stack_finish(this, tload)
145
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
146
    return mod
147
148
149
_real_compile = sre._compile
150
1724.2.7 by John Arbash Meinel
It seems you cannot override __builtins__.__import__ in a sub-module.
151
def timed_compile(*args, **kwargs):
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
152
    """Log how long it takes to compile a regex"""
153
154
    # And who is requesting this?
155
    frame = sys._getframe(2)
156
    frame_name = frame.f_globals.get('__name__', '<unknown>')
2063.4.3 by John Arbash Meinel
Update profile imports to be aware of the new compiler
157
158
    extra = ''
159
    if frame_name.endswith('lazy_regex'):
160
        # If this was lazily compiled, we have 3 more frames to ignore
161
        extra = '[l] '
162
        frame = sys._getframe(5)
163
        frame_name = frame.f_globals.get('__name__', '<unknown>')
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
164
    frame_lineno = frame.f_lineno
2063.4.3 by John Arbash Meinel
Update profile imports to be aware of the new compiler
165
    this = stack_add(extra+repr(args[0]), frame_name, frame_lineno)
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
166
3696.1.1 by John Arbash Meinel
Use the right timing function on win32
167
    tstart = _timer()
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
168
    try:
169
        # Measure the compile time
170
        comp = _real_compile(*args, **kwargs)
171
    finally:
3696.1.1 by John Arbash Meinel
Use the right timing function on win32
172
        tcompile = _timer() - tstart
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
173
        stack_finish(this, tcompile)
174
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
175
    return comp
1724.2.14 by John Arbash Meinel
Refactor import stuff into separate functions. Update news
176
177
178
def install():
179
    """Install the hooks for measuring import and regex compile time."""
180
    __builtins__['__import__'] = timed_import
181
    sre._compile = timed_compile
182
183
184
def uninstall():
185
    """Remove the import and regex compile timing hooks."""
186
    __builtins__['__import__'] = _real_import
187
    sre._compile = _real_compile
188