"""
Usage: gprof2html.py executable gmon.out # recommended
or: gprof2html.py < gprof_text_file
Warning: dumps lots of .html files into the current directory.
"""
import os, sys
graphs = {}
def getgraph(name):
try:
return graphs[name]
except KeyError:
res = graphs[name] = []
return res
flat = []
def parse_flat(f):
for line in f:
fields = line.strip().split()
if not fields: break
name = fields[-1]
flat.append(line.replace(name, '%s' % (name,name)))
def parse(f):
for line in f:
if line.startswith(' time seconds'): # flat profile
flat.append(line)
parse_flat(f)
if line.startswith('index'): # call graph
break
firstgraph = None
while True:
lines = []
gnames = []
middle = None
for line in f:
if line.startswith("-------------------"):
break
if line.startswith("\f"):
return
fields = line.strip().split()
if line.startswith('['):
assert middle is None
middle = len(lines)
fields = [s for s in fields if not (s.startswith('[') and s.endswith(']'))]
name = None
i = 0
for fld in fields:
if not ('0' <= fld[0] <= '9' or fld[0]=='['):
name = fld
#fields[i] = '\\n'+name
break
i += 1
if name and name.startswith('<'):
name = None
if name:
line = line.replace(name, '%s' % (name,name))
lines.append('
'+line)
gnames.append(name)
assert middle is not None
name = gnames[middle]
if name:
g = getgraph(name)
g += lines
if firstgraph is None:
firstgraph = g
print name
if len(sys.argv) > 1:
f = os.popen("gprof --brief --no-flat-profile --graph %s" % ' '.join(sys.argv[1:]), 'r')
else:
f = sys.stdin
parse(f)
f.close()
if flat:
f = open('INDEX.html', 'w')
f.write('
')
f.writelines(flat)
f.write('')
f.close()
for name, content in graphs.items():
if content:
f = open(name+'.html', 'w')
f.write('')
f.writelines(content)
f.write('')
f.close()