~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to profile_imports.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Canonical Ltd
 
1
# Copyright (C) 2006, 2008, 2009, 2010 by Canonical Ltd
2
2
# Written by John Arbash Meinel <john@arbash-meinel.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
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
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
 
18
18
"""A custom importer and regex compiler which logs time spent."""
19
19
 
20
 
import sre
21
20
import sys
22
21
import time
23
22
 
24
23
 
 
24
import re
 
25
 
 
26
 
25
27
_parent_stack = []
26
28
_total_stack = {}
27
29
_info = {}
58
60
 
59
61
def log_stack_info(out_file, sorted=True, hide_fast=True):
60
62
    # Find all of the roots with import = 0
61
 
    out_file.write(' cum  inline name\t\t\t\t\t\tframe\n')
 
63
    out_file.write('%5s %5s %-40s @ %s:%s\n'
 
64
        % ('cum', 'inline', 'name', 'file', 'line'))
62
65
    todo = [(value[-1], key) for key,value in _info.iteritems() if value[0] == 0]
63
66
 
64
67
    if sorted:
83
86
 
84
87
        # indent, cum_time, mod_time, name,
85
88
        # scope_name, frame_name, frame_lineno
86
 
        out_file.write('%5.1f %5.1f %s %-35s\t@ %s:%d\n'
87
 
            % (info[-1]*1000., mod_time*1000., '+'*info[0], 
88
 
               cur[1][:35], info[1], info[2]))
 
89
        out_file.write('%5.1f %5.1f %-40s @ %s:%d\n'
 
90
            % (info[-1]*1000., mod_time*1000.,
 
91
               ('+'*info[0] + cur[1]),
 
92
               info[1], info[2]))
89
93
 
90
94
        if sorted:
91
95
            c_times.sort()
96
100
 
97
101
_real_import = __import__
98
102
 
99
 
def timed_import(name, globals, locals, fromlist):
 
103
def timed_import(name, globals=None, locals=None, fromlist=None, level=None):
100
104
    """Wrap around standard importer to log import time"""
 
105
    # normally there are 4, but if this is called as __import__ eg by
 
106
    # /usr/lib/python2.6/email/__init__.py then there may be only one
 
107
    # parameter
 
108
    # level is only passed by python2.6
101
109
 
102
 
    scope_name = globals.get('__name__', None)
103
 
    if scope_name is None:
104
 
        scope_name = globals.get('__file__', None)
105
 
    if scope_name is None:
106
 
        scope_name = globals.keys()
 
110
    if globals is None:
 
111
        # can't determine the scope name afaics; we could peek up the stack to
 
112
        # see where this is being called from, but it should be a rare case.
 
113
        scope_name = None
107
114
    else:
108
 
        # Trim out paths before bzrlib
109
 
        loc = scope_name.find('bzrlib')
110
 
        if loc != -1:
111
 
            scope_name = scope_name[loc:]
112
 
        # For stdlib, trim out early paths
113
 
        loc = scope_name.find('python2.4')
114
 
        if loc != -1:
115
 
            scope_name = scope_name[loc:]
 
115
        scope_name = globals.get('__name__', None)
 
116
        if scope_name is None:
 
117
            scope_name = globals.get('__file__', None)
 
118
        if scope_name is None:
 
119
            scope_name = globals.keys()
 
120
        else:
 
121
            # Trim out paths before bzrlib
 
122
            loc = scope_name.find('bzrlib')
 
123
            if loc != -1:
 
124
                scope_name = scope_name[loc:]
 
125
            # For stdlib, trim out early paths
 
126
            loc = scope_name.find('python2.4')
 
127
            if loc != -1:
 
128
                scope_name = scope_name[loc:]
116
129
 
117
130
    # Figure out the frame that is doing the importing
118
131
    frame = sys._getframe(1)
145
158
    return mod
146
159
 
147
160
 
148
 
_real_compile = sre._compile
 
161
_real_compile = re._compile
 
162
 
149
163
 
150
164
def timed_compile(*args, **kwargs):
151
165
    """Log how long it takes to compile a regex"""
177
191
def install():
178
192
    """Install the hooks for measuring import and regex compile time."""
179
193
    __builtins__['__import__'] = timed_import
180
 
    sre._compile = timed_compile
 
194
    re._compile = timed_compile
181
195
 
182
196
 
183
197
def uninstall():
184
198
    """Remove the import and regex compile timing hooks."""
185
199
    __builtins__['__import__'] = _real_import
186
 
    sre._compile = _real_compile
 
200
    re._compile = _real_compile
187
201