File: profiler.py

package info (click to toggle)
psyco 1.5.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,864 kB
  • ctags: 3,295
  • sloc: ansic: 24,491; python: 5,573; perl: 1,309; makefile: 166; sh: 1
file content (388 lines) | stat: -rw-r--r-- 11,524 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
###########################################################################
# 
#  Psyco profiler (Python part).
#   Copyright (C) 2001-2002  Armin Rigo et.al.

"""Psyco profiler (Python part).

The implementation of the non-time-critical parts of the profiler.
See profile() and full() in core.py for the easy interface.
"""
###########################################################################

import _psyco
from support import *
import math, time, types, atexit
now = time.time
try:
    import thread
except ImportError:
    import dummy_thread as thread


# current profiler instance
current = None

# enabled profilers, in order of priority
profilers = []

# logger module (when enabled by core.log())
logger = None

# a lock for a thread-safe go()
go_lock = thread.allocate_lock()

def go(stop=0):
    # run the highest-priority profiler in 'profilers'
    global current
    go_lock.acquire()
    try:
        prev = current
        if stop:
            del profilers[:]
        if prev:
            if profilers and profilers[0] is prev:
                return    # best profiler already running
            prev.stop()
            current = None
        for p in profilers[:]:
            if p.start():
                current = p
                if logger: # and p is not prev:
                    logger.write("%s: starting" % p.__class__.__name__, 5)
                return
    finally:
        go_lock.release()
    # no profiler is running now
    if stop:
        if logger:
            logger.writefinalstats()
    else:
        tag2bind()

atexit.register(go, 1)


def buildfncache(globals, cache):
    if hasattr(types.IntType, '__dict__'):
        clstypes = (types.ClassType, types.TypeType)
    else:
        clstypes = types.ClassType
    for x in globals.values():
        if isinstance(x, types.MethodType):
            x = x.im_func
        if isinstance(x, types.FunctionType):
            cache[x.func_code] = x, ''
        elif isinstance(x, clstypes):
            for y in x.__dict__.values():
                if isinstance(y, types.MethodType):
                    y = y.im_func
                if isinstance(y, types.FunctionType):
                    cache[y.func_code] = y, x.__name__

# code-to-function mapping (cache)
function_cache = {}

def trytobind(co, globals, log=1):
    try:
        f, clsname = function_cache[co]
    except KeyError:
        buildfncache(globals, function_cache)
        try:
            f, clsname = function_cache[co]
        except KeyError:
            if logger:
                logger.write('warning: cannot find function %s in %s' %
                             (co.co_name, globals.get('__name__', '?')), 3)
            return  # give up
    if logger and log:
        modulename = globals.get('__name__', '?')
        if clsname:
            modulename += '.' + clsname
        logger.write('bind function: %s.%s' % (modulename, co.co_name), 1)
    f.func_code = _psyco.proxycode(f)


if PYTHON_SUPPORT:
    # the list of code objects that have been tagged
    tagged_codes = []
    
    def tag(co, globals):
        if logger:
            try:
                f, clsname = function_cache[co]
            except KeyError:
                buildfncache(globals, function_cache)
                try:
                    f, clsname = function_cache[co]
                except KeyError:
                    clsname = ''  # give up
            modulename = globals.get('__name__', '?')
            if clsname:
                modulename += '.' + clsname
            logger.write('tag function: %s.%s' % (modulename, co.co_name), 1)
        tagged_codes.append((co, globals))
        _psyco.turbo_frame(co)
        _psyco.turbo_code(co)

    def tag2bind():
        if tagged_codes:
            if logger:
                logger.write('profiling stopped, binding %d functions' %
                             len(tagged_codes), 2)
            for co, globals in tagged_codes:
                trytobind(co, globals, 0)
            function_cache.clear()
            del tagged_codes[:]

else:
    # tagging is impossible, always bind
    tag = trytobind
    def tag2bind():
        pass



class Profiler:
    MemoryTimerResolution = 0.103

    def run(self, memory, time, memorymax, timemax):
        self.memory = memory
        self.memorymax = memorymax
        self.time = time
        if timemax is None:
            self.endtime = None
        else:
            self.endtime = now() + timemax
        self.alarms = []
        profilers.append(self)
        go()
    
    def start(self):
        curmem = _psyco.memory()
        memlimits = []
        if self.memorymax is not None:
            if curmem >= self.memorymax:
                if logger:
                    logger.writememory()
                return self.limitreached('memorymax')
            memlimits.append(self.memorymax)
        if self.memory is not None:
            if self.memory <= 0:
                if logger:
                    logger.writememory()
                return self.limitreached('memory')
            memlimits.append(curmem + self.memory)
            self.memory_at_start = curmem

        curtime = now()
        timelimits = []
        if self.endtime is not None:
            if curtime >= self.endtime:
                return self.limitreached('timemax')
            timelimits.append(self.endtime - curtime)
        if self.time is not None:
            if self.time <= 0.0:
                return self.limitreached('time')
            timelimits.append(self.time)
            self.time_at_start = curtime
        
        try:
            self.do_start()
        except error, e:
            if logger:
                logger.write('%s: disabled by psyco.error:' % (
                    self.__class__.__name__), 4)
                logger.write('    %s' % str(e), 3)
            return 0
        
        if memlimits:
            self.memlimits_args = (time.sleep, (self.MemoryTimerResolution,),
                                   self.check_memory, (min(memlimits),))
            self.alarms.append(_psyco.alarm(*self.memlimits_args))
        if timelimits:
            self.alarms.append(_psyco.alarm(time.sleep, (min(timelimits),),
                                            self.time_out))
        return 1
    
    def stop(self):
        for alarm in self.alarms:
            alarm.stop(0)
        for alarm in self.alarms:
            alarm.stop(1)   # wait for parallel threads to stop
        del self.alarms[:]
        if self.time is not None:
            self.time -= now() - self.time_at_start
        if self.memory is not None:
            self.memory -= _psyco.memory() - self.memory_at_start

        try:
            self.do_stop()
        except error:
            return 0
        return 1

    def check_memory(self, limit):
        if _psyco.memory() < limit:
            return self.memlimits_args
        go()

    def time_out(self):
        self.time = 0.0
        go()

    def limitreached(self, limitname):
        try:
            profilers.remove(self)
        except ValueError:
            pass
        if logger:
            logger.write('%s: disabled (%s limit reached)' % (
                self.__class__.__name__, limitname), 4)
        return 0


class FullCompiler(Profiler):

    def do_start(self):
        _psyco.profiling('f')

    def do_stop(self):
        _psyco.profiling('.')


class RunOnly(Profiler):

    def do_start(self):
        _psyco.profiling('n')

    def do_stop(self):
        _psyco.profiling('.')


class ChargeProfiler(Profiler):

    def __init__(self, watermark, parentframe):
        self.watermark = watermark
        self.parent2 = parentframe * 2.0
        self.lock = thread.allocate_lock()

    def init_charges(self):
        _psyco.statwrite(watermark = self.watermark,
                         parent2   = self.parent2)

    def do_stop(self):
        _psyco.profiling('.')
        _psyco.statwrite(callback = None)


class ActiveProfiler(ChargeProfiler):

    def active_start(self):
        _psyco.profiling('p')

    def do_start(self):
        self.init_charges()
        self.active_start()
        _psyco.statwrite(callback = self.charge_callback)

    def charge_callback(self, frame, charge):
        tag(frame.f_code, frame.f_globals)


class PassiveProfiler(ChargeProfiler):

    initial_charge_unit   = _psyco.statread('unit')
    reset_stats_after     = 120      # half-lives (maximum 200!)
    reset_limit           = initial_charge_unit * (2.0 ** reset_stats_after)

    def __init__(self, watermark, halflife, pollfreq, parentframe):
        ChargeProfiler.__init__(self, watermark, parentframe)
        self.pollfreq = pollfreq
        # self.progress is slightly more than 1.0, and computed so that
        # do_profile() will double the change_unit every 'halflife' seconds.
        self.progress = 2.0 ** (1.0 / (halflife * pollfreq))

    def reset(self):
        _psyco.statwrite(unit = self.initial_charge_unit, callback = None)
        _psyco.statreset()
        if logger:
            logger.write("%s: resetting stats" % self.__class__.__name__, 1)

    def passive_start(self):
        self.passivealarm_args = (time.sleep, (1.0 / self.pollfreq,),
                                  self.do_profile)
        self.alarms.append(_psyco.alarm(*self.passivealarm_args))

    def do_start(self):
        tag2bind()
        self.init_charges()
        self.passive_start()

    def do_profile(self):
        _psyco.statcollect()
        if logger:
            logger.dumpcharges()
        nunit = _psyco.statread('unit') * self.progress
        if nunit > self.reset_limit:
            self.reset()
        else:
            _psyco.statwrite(unit = nunit, callback = self.charge_callback)
        return self.passivealarm_args

    def charge_callback(self, frame, charge):
        trytobind(frame.f_code, frame.f_globals)


class ActivePassiveProfiler(PassiveProfiler, ActiveProfiler):

    def do_start(self):
        self.init_charges()
        self.active_start()
        self.passive_start()

    def charge_callback(self, frame, charge):
        tag(frame.f_code, frame.f_globals)



#
# we register our own version of sys.settrace(), sys.setprofile()
# and thread.start_new_thread().
#

def psyco_settrace(*args, **kw):
    "This is the Psyco-aware version of sys.settrace()."
    result = original_settrace(*args, **kw)
    go()
    return result

def psyco_setprofile(*args, **kw):
    "This is the Psyco-aware version of sys.setprofile()."
    result = original_setprofile(*args, **kw)
    go()
    return result

def psyco_thread_stub(callable, args, kw):
    _psyco.statcollect()
    if kw is None:
        return callable(*args)
    else:
        return callable(*args, **kw)

def psyco_start_new_thread(callable, args, kw=None):
    "This is the Psyco-aware version of thread.start_new_thread()."
    return original_start_new_thread(psyco_thread_stub, (callable, args, kw))

original_settrace         = sys.settrace
original_setprofile       = sys.setprofile
original_start_new_thread = thread.start_new_thread
sys.settrace            = psyco_settrace
sys.setprofile          = psyco_setprofile
if PYTHON_SUPPORT:
    thread.start_new_thread = psyco_start_new_thread
    # hack to patch threading._start_new_thread if the module is
    # already loaded
    if (sys.modules.has_key('threading') and
        hasattr(sys.modules['threading'], '_start_new_thread')):
        sys.modules['threading']._start_new_thread = psyco_start_new_thread