#!/usr/bin/env python # # this is a fairly trivial parser of the ftrace function stack trace output # the idea is to find the most common stack traces that lead to calling # given function, even those that don't use much CPU time and won't # show up in other profilers. # # It assumes ftrace output looks like this: # some text including the function name # <= func # <= func2 # <= func3 # some text including the function name # <= func4 # <= func5 # <= func6 # # The traces above would generate two histogram entries, one for # func,func2,func3 and one for func4,func5,fun6 # import sys, os, signal stacks = {} last = [] printed = False def summary(arg1, arg2): global printed; printed = True count = {} for lines, c in stacks.items(): count.setdefault(c, []).append(lines + ' ===========\n') limit = 50 keys = count.keys() keys.sort() for x in keys.__reversed__(): print "%d hits: \n%s" % (x, "".join(count[x])) limit -= 1 if limit <= 0: break sys.exit(0) signal.signal(signal.SIGTERM, summary) signal.signal(signal.SIGINT, summary) while True: try: l = sys.stdin.readline() except: break if not l: break if l[0] == '#': continue if not l.startswith(' <='): if last: lines = ''.join(last) val = stacks.get(lines, 0) + 1 stacks[lines] = val # we don't put the header line into the trace because that might # have timestamp info in it. last = [] else: last.append(l) if not printed: summary(1, 2)