~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
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
29
30
31
def stack_add(name, frame_name, frame_lineno, scope_name=None):
32
    """Start a new record on the stack"""
33
    global _cur_id
34
    _cur_id += 1
35
    this_stack = (_cur_id, name)
36
37
    if _parent_stack:
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]
42
43
    return this_stack
44
45
46
def stack_finish(this, cost):
47
    """Finish a given entry, and record its cost in time"""
48
    global _parent_stack
49
1724.2.13 by John Arbash Meinel
Remove pprint dependency
50
    assert _parent_stack[-1] == this, \
51
        'import stack does not end with this %s: %s' % (this, _parent_stack)
52
    _parent_stack.pop()
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
53
    _info[this].append(cost)
54
55
1724.2.12 by John Arbash Meinel
Default to hiding things that take less that .1ms to keep the output clean.
56
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.
57
    # 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.
58
    out_file.write(' cum  inline name\t\t\t\t\t\tframe\n')
1724.2.10 by John Arbash Meinel
More layout work
59
    todo = [(value[-1], key) for key,value in _info.iteritems() if value[0] == 0]
60
61
    if sorted:
62
        todo.sort()
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
63
64
    while todo:
1724.2.10 by John Arbash Meinel
More layout work
65
        cum_time, cur = todo.pop()
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
66
        children = _total_stack[cur]
67
1724.2.10 by John Arbash Meinel
More layout work
68
        c_times = []
69
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
70
        info = _info[cur]
1724.2.12 by John Arbash Meinel
Default to hiding things that take less that .1ms to keep the output clean.
71
        if hide_fast and info[-1] < 0.0001:
72
            continue
73
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
74
        # Compute the module time by removing the children times
75
        mod_time = info[-1]
76
        for child in children:
77
            c_info = _info[child]
78
            mod_time -= c_info[-1]
1724.2.10 by John Arbash Meinel
More layout work
79
            c_times.append((c_info[-1], child))
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
80
81
        # indent, cum_time, mod_time, name,
82
        # 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.
83
        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.
84
            % (info[-1]*1000., mod_time*1000., '+'*info[0], 
1724.2.11 by John Arbash Meinel
Remove scope name for now, still log it, in case we need it for demandload.
85
               cur[1][:40], info[1], info[2]))
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
86
1724.2.10 by John Arbash Meinel
More layout work
87
        if sorted:
88
            c_times.sort()
89
        else:
90
            c_times.reverse()
91
        todo.extend(c_times)
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
92
93
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
94
_real_import = __import__
95
1724.2.7 by John Arbash Meinel
It seems you cannot override __builtins__.__import__ in a sub-module.
96
def timed_import(name, globals, locals, fromlist):
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
97
    """Wrap around standard importer to log import time"""
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
98
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
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()
104
    else:
105
        # Trim out paths before bzrlib
106
        loc = scope_name.find('bzrlib')
107
        if loc != -1:
108
            scope_name = scope_name[loc:]
109
        # For stdlib, trim out early paths
110
        loc = scope_name.find('python2.4')
111
        if loc != -1:
112
            scope_name = scope_name[loc:]
113
114
    # Figure out the frame that is doing the importing
115
    frame = sys._getframe(1)
116
    frame_name = frame.f_globals.get('__name__', '<unknown>')
117
    extra = ''
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)
1996.1.22 by John Arbash Meinel
Update --profile-imports to support the new lazy_import code
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
125
        extra = ' (lazy)'
126
        frame = sys._getframe(4)
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
127
        frame_name = frame.f_globals.get('__name__', '<unknown>')
128
    frame_lineno = frame.f_lineno
129
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
130
    this = stack_add(name+extra, frame_name, frame_lineno, scope_name)
131
132
    tstart = time.time()
133
    try:
134
        # Do the import
135
        mod = _real_import(name, globals, locals, fromlist)
136
    finally:
137
        tload = time.time()-tstart
138
        stack_finish(this, tload)
139
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
140
    return mod
141
142
143
_real_compile = sre._compile
144
1724.2.7 by John Arbash Meinel
It seems you cannot override __builtins__.__import__ in a sub-module.
145
def timed_compile(*args, **kwargs):
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
146
    """Log how long it takes to compile a regex"""
147
148
    # And who is requesting this?
149
    frame = sys._getframe(2)
150
    frame_name = frame.f_globals.get('__name__', '<unknown>')
151
    frame_lineno = frame.f_lineno
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
152
1724.2.9 by John Arbash Meinel
change layout to be easier to mentally parse.
153
    this = stack_add(repr(args[0]), frame_name, frame_lineno)
1724.2.8 by John Arbash Meinel
New --profile-imports output which puts parents before children.
154
155
    tstart = time.time()
156
    try:
157
        # Measure the compile time
158
        comp = _real_compile(*args, **kwargs)
159
    finally:
160
        tcompile = time.time() - tstart
161
        stack_finish(this, tcompile)
162
1724.2.4 by John Arbash Meinel
Move the custom importers into a separate module
163
    return comp
1724.2.14 by John Arbash Meinel
Refactor import stuff into separate functions. Update news
164
165
166
def install():
167
    """Install the hooks for measuring import and regex compile time."""
168
    __builtins__['__import__'] = timed_import
169
    sre._compile = timed_compile
170
171
172
def uninstall():
173
    """Remove the import and regex compile timing hooks."""
174
    __builtins__['__import__'] = _real_import
175
    sre._compile = _real_compile
176