Last active
March 9, 2016 04:21
-
-
Save prasanthj/10cf6ebe17494061e64d to your computer and use it in GitHub Desktop.
Print Top-N java methods from java flame graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys,operator,getopt | |
class bcolors: | |
HEADER = '\033[95m' | |
OKBLUE = '\033[94m' | |
OKGREEN = '\033[92m' | |
WARNING = '\033[93m' | |
FAIL = '\033[91m' | |
ENDC = '\033[0m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
def print_top_methods(n, java): | |
sum = 0.0 | |
output = {} | |
for line in sys.stdin: | |
tokens = line.split(' ') | |
method = tokens[0].split(";")[-1] | |
sum += int(tokens[-1]) | |
if (java == True): | |
if (method.startswith('L') and '/' in method): | |
output[method] = int(tokens[-1]) | |
else: | |
output[method] = int(tokens[-1]) | |
for key, value in output.iteritems(): | |
output[key] = float(value/sum) | |
sorted_o = sorted(output.items(), key=operator.itemgetter(1), reverse=True) | |
for item in sorted_o[:int(n)]: | |
k = item[0] | |
v = item[1] * 100.0 | |
if (v < 5.0): | |
print bcolors.OKGREEN + "{0:.2f}".format(v) + "%\t" + "- " + k + bcolors.ENDC | |
elif (v >= 5.0 and v < 10.0): | |
print bcolors.WARNING + "{0:.2f}".format(v) + "%\t" + "- " + k + bcolors.ENDC | |
else: | |
print bcolors.FAIL + bcolors.BOLD + "{0:.2f}".format(v) + "%\t" + "- " + k + bcolors.ENDC | |
def usage(): | |
print "\nPrint top-n methods from stack collapse perf output.\n" | |
print 'Sample usage: sudo perf script -f comm,pid,tid,cpu,time,event,ip,sym,dso,trace | ./stackcollapse-perf.pl --pid | '+sys.argv[0]+' -n 10 -j' | |
print sys.argv[0] + ' options:' | |
print '\t-n\t-\ttop-n methods [default:10]' | |
print '\t-j\t-\toutput java only methods (stackcollapse-perf.pl script should be run with --pid option) [default:false]' | |
print '\t-h\t-\thelp' | |
def main(): | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "hn:j", ["help", "n=", "java"]) | |
except getopt.GetoptError as err: | |
# print help information and exit: | |
print str(err) # will print something like "option -a not recognized" | |
usage() | |
sys.exit(2) | |
n = 10 | |
java = False | |
for o, a in opts: | |
if o in ("-j", "--java"): | |
java = True | |
elif o in ("-h", "--help"): | |
usage() | |
sys.exit() | |
elif o in ("-n", "--topn"): | |
n = a | |
else: | |
assert False, "unhandled option" | |
print_top_methods(n, java) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sudo perf record -F 99 -a -g -- sleep 30; sudo jmaps
sudo perf script -f comm,pid,tid,cpu,time,event,ip,sym,dso,trace | ./stackcollapse-perf.pl --pid | ./topjavamethods.py -j -n 10