handle case where profile is already running

This commit is contained in:
Darren Ranalli 2008-10-03 18:15:44 +00:00
parent 1e09da24a5
commit 26a541c244
2 changed files with 116 additions and 85 deletions

View File

@ -27,9 +27,11 @@ class ProfileSession:
self._func = func
self._name = name
self._filename = 'profileData-%s-%s' % (self._name, id(self))
self._profileSucceeded = False
self._wallClockDur = None
self._ramFile = None
self._refCount = 0
self._resultCache = {}
self.acquire()
def acquire(self):
@ -45,13 +47,20 @@ class ProfileSession:
del self._filename
del self._wallClockDur
del self._ramFile
del self._resultCache
def run(self):
# make sure this instance doesn't get destroyed inside self._func
self.acquire()
self._profileSucceeded = False
# if we're already profiling, just run the func and don't profile
if 'globalProfileFunc' in __builtin__.__dict__:
result = self._func()
self._wallClockDur = 0.
else:
# put the function in the global namespace so that profile can find it
assert 'globalProfileFunc' not in __builtin__.__dict__
__builtin__.globalProfileFunc = self._func
__builtin__.globalProfileResult = [None]
@ -97,6 +106,8 @@ class ProfileSession:
del __builtin__.__dict__['globalProfileFunc']
del __builtin__.__dict__['globalProfileResult']
self._profileSucceeded = True
self.release()
return result
@ -104,10 +115,25 @@ class ProfileSession:
# this might not be accurate, it may include time taken up by other processes
return self._wallClockDur
def profileSucceeded(self):
return self._profileSucceeded
def getResults(self,
lines=80,
sorts=['cumulative', 'time', 'calls'],
callInfo=False):
if not self._profileSucceeded:
output = '%s: profiler already running, could not profile' % self._name
else:
# make sure the arguments will hash efficiently if callers provide different types
lines = int(lines)
sorts = list(sorts)
callInfo = bool(callInfo)
k = str((lines, sorts, callInfo))
if k in self._resultCache:
# we've already created this output string, get it from the cache
output = self._resultCache[k]
else:
# set up the RAM file
_installProfileCustomFuncs(self._filename)
# install the stored RAM file from self.run()
@ -137,5 +163,8 @@ class ProfileSession:
# clean up the RAM file support
_removeProfileCustomFuncs(self._filename)
# cache this result
self._resultCache[k] = output
return output

View File

@ -129,6 +129,8 @@ class TaskProfiler:
# set up for the next frame
if (self._task is not None) and taskMgr._hasProfiledDesignatedTask():
session = taskMgr._getLastProfileSession()
# if we couldn't profile, throw this result out
if session.profileSucceeded():
sessionDur = session.getWallClockDuration()
namePattern = self._task.getNamePattern()
if namePattern not in self._namePattern2tracker: