mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-18 12:43:44 -04:00
handle case where profile is already running
This commit is contained in:
parent
1e09da24a5
commit
26a541c244
@ -27,9 +27,11 @@ class ProfileSession:
|
|||||||
self._func = func
|
self._func = func
|
||||||
self._name = name
|
self._name = name
|
||||||
self._filename = 'profileData-%s-%s' % (self._name, id(self))
|
self._filename = 'profileData-%s-%s' % (self._name, id(self))
|
||||||
|
self._profileSucceeded = False
|
||||||
self._wallClockDur = None
|
self._wallClockDur = None
|
||||||
self._ramFile = None
|
self._ramFile = None
|
||||||
self._refCount = 0
|
self._refCount = 0
|
||||||
|
self._resultCache = {}
|
||||||
self.acquire()
|
self.acquire()
|
||||||
|
|
||||||
def acquire(self):
|
def acquire(self):
|
||||||
@ -45,13 +47,20 @@ class ProfileSession:
|
|||||||
del self._filename
|
del self._filename
|
||||||
del self._wallClockDur
|
del self._wallClockDur
|
||||||
del self._ramFile
|
del self._ramFile
|
||||||
|
del self._resultCache
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
# make sure this instance doesn't get destroyed inside self._func
|
# make sure this instance doesn't get destroyed inside self._func
|
||||||
self.acquire()
|
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
|
# put the function in the global namespace so that profile can find it
|
||||||
assert 'globalProfileFunc' not in __builtin__.__dict__
|
|
||||||
__builtin__.globalProfileFunc = self._func
|
__builtin__.globalProfileFunc = self._func
|
||||||
__builtin__.globalProfileResult = [None]
|
__builtin__.globalProfileResult = [None]
|
||||||
|
|
||||||
@ -97,6 +106,8 @@ class ProfileSession:
|
|||||||
del __builtin__.__dict__['globalProfileFunc']
|
del __builtin__.__dict__['globalProfileFunc']
|
||||||
del __builtin__.__dict__['globalProfileResult']
|
del __builtin__.__dict__['globalProfileResult']
|
||||||
|
|
||||||
|
self._profileSucceeded = True
|
||||||
|
|
||||||
self.release()
|
self.release()
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -104,10 +115,25 @@ class ProfileSession:
|
|||||||
# this might not be accurate, it may include time taken up by other processes
|
# this might not be accurate, it may include time taken up by other processes
|
||||||
return self._wallClockDur
|
return self._wallClockDur
|
||||||
|
|
||||||
|
def profileSucceeded(self):
|
||||||
|
return self._profileSucceeded
|
||||||
|
|
||||||
def getResults(self,
|
def getResults(self,
|
||||||
lines=80,
|
lines=80,
|
||||||
sorts=['cumulative', 'time', 'calls'],
|
sorts=['cumulative', 'time', 'calls'],
|
||||||
callInfo=False):
|
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
|
# set up the RAM file
|
||||||
_installProfileCustomFuncs(self._filename)
|
_installProfileCustomFuncs(self._filename)
|
||||||
# install the stored RAM file from self.run()
|
# install the stored RAM file from self.run()
|
||||||
@ -137,5 +163,8 @@ class ProfileSession:
|
|||||||
# clean up the RAM file support
|
# clean up the RAM file support
|
||||||
_removeProfileCustomFuncs(self._filename)
|
_removeProfileCustomFuncs(self._filename)
|
||||||
|
|
||||||
|
# cache this result
|
||||||
|
self._resultCache[k] = output
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
@ -129,6 +129,8 @@ class TaskProfiler:
|
|||||||
# set up for the next frame
|
# set up for the next frame
|
||||||
if (self._task is not None) and taskMgr._hasProfiledDesignatedTask():
|
if (self._task is not None) and taskMgr._hasProfiledDesignatedTask():
|
||||||
session = taskMgr._getLastProfileSession()
|
session = taskMgr._getLastProfileSession()
|
||||||
|
# if we couldn't profile, throw this result out
|
||||||
|
if session.profileSucceeded():
|
||||||
sessionDur = session.getWallClockDuration()
|
sessionDur = session.getWallClockDuration()
|
||||||
namePattern = self._task.getNamePattern()
|
namePattern = self._task.getNamePattern()
|
||||||
if namePattern not in self._namePattern2tracker:
|
if namePattern not in self._namePattern2tracker:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user