Beispiel #1
0
 def __init__(self, namePrefix):
     self._namePrefix = namePrefix
     self._durationAverager = Averager('%s-durationAverager' % namePrefix)
     self._avgSession = None
     if TaskTracker.MinSamples is None:
         # number of samples required before spikes start getting identified
         TaskTracker.MinSamples = config.GetInt('profile-task-spike-min-samples', 30)
         # defines spike as longer than this multiple of avg task duration
         TaskTracker.SpikeThreshold = TaskProfiler.GetDefaultSpikeThreshold()
Beispiel #2
0
class TaskTracker:
    # call it TaskProfiler to avoid confusion for the user
    notify = directNotify.newCategory("TaskProfiler")
    MinSamples = None
    SpikeThreshold = None
    def __init__(self, namePrefix):
        self._namePrefix = namePrefix
        self._durationAverager = Averager('%s-durationAverager' % namePrefix)
        self._avgSession = None
        if TaskTracker.MinSamples is None:
            # number of samples required before spikes start getting identified
            TaskTracker.MinSamples = config.GetInt('profile-task-spike-min-samples', 30)
            # defines spike as longer than this multiple of avg task duration
            TaskTracker.SpikeThreshold = TaskProfiler.GetDefaultSpikeThreshold()
    def destroy(self):
        self.flush()
        del self._namePrefix
        del self._durationAverager
    def flush(self):
        self._durationAverager.reset()
        if self._avgSession:
            self._avgSession.release()
        self._avgSession = None
    def getNamePrefix(self, namePrefix):
        return self._namePrefix
    def _checkSpike(self, session):
        duration = session.getDuration()
        isSpike = False
        # do we have enough samples?
        if self.getNumDurationSamples() > self.MinSamples:
            # was this a spike?
            if duration > (self.getAvgDuration() * self.SpikeThreshold):
                isSpike = True
                avgSession = self.getAvgSession()
                s = '\n%s task CPU spike profile (%s) %s\n' % ('=' * 30, self._namePrefix, '=' * 30)
                s += ('|' * 80) + '\n'
                for sorts in (['cumulative'], ['time'], ['calls']):
                    s += ('-- AVERAGE --\n%s'
                          '-- SPIKE --\n%s' % (
                        avgSession.getResults(sorts=sorts, totalTime=duration),
                        session.getResults(sorts=sorts)))
                self.notify.info(s)
        return isSpike
    def addProfileSession(self, session):
        duration = session.getDuration()
        if duration == 0.:
            # profiled code was too fast for the clock, throw this result out
            # if we keep it we may get many false positive spike detects
            return

        isSpike = self._checkSpike(session)
        self._durationAverager.addValue(duration)

        storeAvg = True
        if self._avgSession is not None:
            avgDur = self.getAvgDuration()
            if abs(self._avgSession.getDuration() - avgDur) < abs(duration - avgDur):
                # current avg data is more average than this new sample, keep the data we've
                # already got stored
                storeAvg = False
        if storeAvg:
            if self._avgSession:
                self._avgSession.release()
            self._avgSession = session.getReference()

    def getAvgDuration(self):
        return self._durationAverager.getAverage()
    def getNumDurationSamples(self):
        return self._durationAverager.getCount()
    def getAvgSession(self):
        # returns profile session for closest-to-average sample
        return self._avgSession
    def log(self):
        if self._avgSession:
            s = 'task CPU profile (%s):\n' % self._namePrefix
            s += ('|' * 80) + '\n'
            for sorts in (['cumulative'], ['time'], ['calls']):
                s += self._avgSession.getResults(sorts=sorts)
            self.notify.info(s)
        else:
            self.notify.info('task CPU profile (%s): no data collected' % self._namePrefix)