Ejemplo n.º 1
0
def loadHotshot(filename, yieldCount=10000):
    """Given a hotshot profile file, load to in-memory structures
    
    yields recordCount, { fileno: filename, ... }, { (fileno,lineno): FunctionRecord, ...}
    
    for every yieldCount records in the file
    """
    reader = _hotshot.logreader(filename)
    files = {}
    functions = {}
    stackSize = sys.getrecursionlimit() * 2
    frames = [None] * stackSize
    localDeltas = numpy.zeros((stackSize, ), 'l')
    # make this local for speed...
    givesDelta = GIVES_DELTA.has_key

    secondsFraction = SECONDS_FRACTION
    getFunction = functions.get
    defineFile = _hotshot.WHAT_DEFINE_FILE
    defineFunction = _hotshot.WHAT_DEFINE_FUNC
    whatEnter = _hotshot.WHAT_ENTER
    whatExit = _hotshot.WHAT_EXIT
    depth = -1
    i = 0
    for i, (what, tdelta, fileno, lineno) in enumerate(reader):
        if (not i % yieldCount) and i:
            yield i, files, functions
        if givesDelta(what):
            key = fileno, lineno
            print 'lineno', lineno, tdelta, getattr(getFunction(key), 'name',
                                                    '')
            if what == whatEnter:
                key = (fileno, lineno)
                function = getFunction(key)
                depth += 1
                try:
                    localDeltas[depth] = 0
                except IndexError, err:
                    print 'extend localDeltas'
                    localDeltas = numpy.resize(localDeltas, (depth + 200, ))
                if function is not None:
                    try:
                        frames[depth] = function.accumArray
                    except IndexError, err:
                        print 'extend frames'
                        frames.extend([None] * ((depth + 200) - len(frames)))
                    function.callArray[0] += 1
                    # XXX like to get rid of this copy eventually...
                    for frame in frames[:depth]:
                        if frame is function.accumArray:
                            function.callArray[1] += 1
                            break
                else:
                    try:
                        frames[depth] = None
                    except IndexError, err:
                        print 'extend frames'
                        frames.extend([None] * ((depth + 200) - len(frames)))
def loadHotshot( filename, yieldCount=10000 ):
    """Given a hotshot profile file, load to in-memory structures
    
    yields recordCount, { fileno: filename, ... }, { (fileno,lineno): FunctionRecord, ...}
    
    for every yieldCount records in the file
    """
    reader = _hotshot.logreader(filename)
    files = {}
    functions = {}
    stackSize = sys.getrecursionlimit() * 2
    frames = [None]*stackSize
    localDeltas = numpy.zeros( (stackSize,), 'l' )
    # make this local for speed...
    givesDelta = GIVES_DELTA.has_key
    
    secondsFraction = SECONDS_FRACTION
    getFunction = functions.get
    defineFile = _hotshot.WHAT_DEFINE_FILE
    defineFunction = _hotshot.WHAT_DEFINE_FUNC
    whatEnter = _hotshot.WHAT_ENTER
    whatExit = _hotshot.WHAT_EXIT
    depth = -1
    i = 0
    for i, (what, tdelta, fileno, lineno) in enumerate(reader):
        if (not i%yieldCount) and i:
            yield i, files, functions
        if givesDelta( what ):
            key = fileno,lineno 
            print 'lineno', lineno, tdelta, getattr(getFunction( key ),'name','')
            if what == whatEnter:
                key = (fileno,lineno)
                function = getFunction( key )
                depth += 1
                try:
                    localDeltas[depth] = 0
                except IndexError, err:
                    print 'extend localDeltas'
                    localDeltas = numpy.resize( localDeltas, (depth+200,))
                if function is not None:
                    try:
                        frames[depth] = function.accumArray
                    except IndexError, err:
                        print 'extend frames'
                        frames.extend( [None]*((depth+200)-len(frames)) )
                    function.callArray[0]+=1
                    # XXX like to get rid of this copy eventually...
                    for frame in frames[:depth]:
                        if frame is function.accumArray:
                            function.callArray[1] += 1
                            break
                else:
                    try:
                        frames[depth] = None
                    except IndexError, err:
                        print 'extend frames'
                        frames.extend( [None]*((depth+200)-len(frames)) )
Ejemplo n.º 3
0
def loadHotshot2(filename):
    """Yield a tree-like structure with stack: total values"""
    reader = _hotshot.logreader(filename)
    givesDelta = GIVES_DELTA.has_key
    secondsFraction = SECONDS_FRACTION
    defineFile = _hotshot.WHAT_DEFINE_FILE
    defineFunction = _hotshot.WHAT_DEFINE_FUNC
    whatEnter = _hotshot.WHAT_ENTER
    whatExit = _hotshot.WHAT_EXIT
    whatLine = _hotshot.WHAT_LINENO
    whatLineTime = _hotshot.WHAT_LINE_TIMES
    stack = ()
    files = {}
    functions = {}
    heatmap = {}

    currentDelta = 0.0
    for i, (what, tdelta, fileno, lineno) in enumerate(reader):
        if givesDelta(what):
            tdelta *= secondsFraction
            if what == whatEnter:
                key = (fileno, lineno)
                stack += (key, )
            heatmap[stack] = heatmap.get(stack, 0.0) + tdelta
            if what == whatExit:
                stack = stack[:-1]
        elif what == defineFile:
            files[fileno] = FileRecord(fileno, tdelta)
        elif what == defineFunction:
            file = files.get(fileno)
            record = FunctionRecord(fileno, lineno, tdelta, file)
            functions[(fileno, lineno)] = record
            if file is not None:
                file.functions[lineno] = record
        elif what == whatLineTime:
            print('line time', (tdelta, fileno, lineno))
        else:
            print('unrecognised what', what)
            for name in [n for n in dir(_hotshot) if n.startswith('WHAT_')]:
                if getattr(_hotshot, name) == what:
                    print(' == %s' % (name, ))
                    break
    return asTree(heatmap), functions
def loadHotshot2( filename ):
    """Yield a tree-like structure with stack: total values"""
    reader = _hotshot.logreader(filename)
    givesDelta = GIVES_DELTA.has_key
    secondsFraction = SECONDS_FRACTION
    defineFile = _hotshot.WHAT_DEFINE_FILE
    defineFunction = _hotshot.WHAT_DEFINE_FUNC
    whatEnter = _hotshot.WHAT_ENTER
    whatExit = _hotshot.WHAT_EXIT
    whatLine = _hotshot.WHAT_LINENO
    whatLineTime = _hotshot.WHAT_LINE_TIMES
    stack = ()
    files = {}
    functions = {}
    heatmap = {}
    
    currentDelta = 0.0
    for i, (what, tdelta, fileno, lineno) in enumerate(reader):
        if givesDelta( what ):
            tdelta*=secondsFraction
            if what == whatEnter:
                key = (fileno,lineno)
                stack += (key,)
            heatmap[stack] = heatmap.get(stack,0.0) + tdelta
            if what == whatExit:
                stack = stack[:-1]
        elif what == defineFile:
            files[ fileno ] = FileRecord( fileno, tdelta )
        elif what == defineFunction:
            file = files.get( fileno )
            record = FunctionRecord( fileno,lineno,tdelta, file)
            functions[ (fileno,lineno) ] = record
            if file is not None:
                file.functions[ lineno ] = record
        elif what == whatLineTime:
            print 'line time',(tdelta,fileno,lineno)
        else:
            print 'unrecognised what', what
            for name in [n for n in dir(_hotshot) if n.startswith( 'WHAT_')]:
                if getattr( _hotshot,name) == what:
                    print ' == %s'%(name,)
                    break
    return asTree( heatmap ), functions
Ejemplo n.º 5
0
def loadHotshot(filename, yieldCount=10000):
    """Given a hotshot profile file, load to in-memory structures
    
    yields recordCount, { fileno: filename, ... }, { (fileno,lineno): FunctionRecord, ...}
    
    for every yieldCount records in the file
    """
    reader = _hotshot.logreader(filename)
    files = {}
    functions = {}
    stackSize = sys.getrecursionlimit() * 2
    frames = [None] * stackSize
    localDeltas = numpy.zeros((stackSize, ), 'l')
    # make this local for speed...
    givesDelta = GIVES_DELTA.has_key

    secondsFraction = SECONDS_FRACTION
    getFunction = functions.get
    defineFile = _hotshot.WHAT_DEFINE_FILE
    defineFunction = _hotshot.WHAT_DEFINE_FUNC
    whatEnter = _hotshot.WHAT_ENTER
    whatExit = _hotshot.WHAT_EXIT
    depth = -1
    i = 0
    for i, (what, tdelta, fileno, lineno) in enumerate(reader):
        if (not i % yieldCount) and i:
            yield i, files, functions
        if givesDelta(what):
            key = fileno, lineno
            print('lineno', lineno, tdelta,
                  getattr(getFunction(key), 'name', ''))
            if what == whatEnter:
                key = (fileno, lineno)
                function = getFunction(key)
                depth += 1
                try:
                    localDeltas[depth] = 0
                except IndexError as err:
                    print('extend localDeltas')
                    localDeltas = numpy.resize(localDeltas, (depth + 200, ))
                if function is not None:
                    try:
                        frames[depth] = function.accumArray
                    except IndexError as err:
                        print('extend frames')
                        frames.extend([None] * ((depth + 200) - len(frames)))
                    function.callArray[0] += 1
                    # XXX like to get rid of this copy eventually...
                    for frame in frames[:depth]:
                        if frame is function.accumArray:
                            function.callArray[1] += 1
                            break
                else:
                    try:
                        frames[depth] = None
                    except IndexError as err:
                        print('extend frames')
                        frames.extend([None] * ((depth + 200) - len(frames)))
            # should both enter and exit tdelta get credited to the lower function?
            # current does so
            localDeltas[depth] += tdelta
            if what == whatExit:
                # add time spent in this frame to cummulative for all open frames
                localDelta = localDeltas[depth] * secondsFraction
                # XXX should avoid this list-copy somehow...
                lastParent = None
                for frame in frames[:depth]:
                    if frame is not None:
                        frame[1] += localDelta
                try:
                    # add time spent in this instance of this frame to local cummulative for this frame
                    depth -= 1
                    if frames[depth] is not None:
                        frames[depth][0] += localDelta
                except IndexError as err:
                    print('Warning frame underflow!')
        elif what == defineFile:
            files[fileno] = FileRecord(fileno, tdelta)
        elif what == defineFunction:
            file = files.get(fileno)
            record = FunctionRecord(fileno, lineno, tdelta, file)
            functions[(fileno, lineno)] = record
            if file is not None:
                file.functions[lineno] = record
        else:
            print('unrecognised what', what)
            for name in [n for n in dir(_hotshot) if n.startswith('WHAT_')]:
                if getattr(_hotshot, name) == what:
                    print(' == %s' % (name, ))
                    break

    yield i, files, functions