2016-10-07 17:06:34 +00:00
|
|
|
#!/usr/bin/env python2.7
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
desc = '''Generate HTML output to visualize optimization records from the YAML files
|
|
|
|
generated with -fsave-optimization-record and -fdiagnostics-show-hotness.
|
|
|
|
|
2016-11-15 08:40:51 +00:00
|
|
|
The tools requires PyYAML and Pygments Python packages.
|
|
|
|
|
|
|
|
For faster parsing, you may want to use libYAML with PyYAML.'''
|
2016-10-07 17:06:34 +00:00
|
|
|
|
|
|
|
import yaml
|
2016-11-15 08:40:51 +00:00
|
|
|
# Try to use the C parser.
|
|
|
|
try:
|
|
|
|
from yaml import CLoader as Loader
|
|
|
|
except ImportError:
|
|
|
|
from yaml import Loader
|
2016-10-07 17:06:34 +00:00
|
|
|
import argparse
|
|
|
|
import os.path
|
2016-11-11 01:51:34 +00:00
|
|
|
import re
|
2016-10-07 17:06:34 +00:00
|
|
|
import subprocess
|
|
|
|
import shutil
|
2016-11-11 00:51:32 +00:00
|
|
|
from pygments import highlight
|
|
|
|
from pygments.lexers.c_cpp import CppLexer
|
|
|
|
from pygments.formatters import HtmlFormatter
|
2016-10-07 17:06:34 +00:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description=desc)
|
|
|
|
parser.add_argument('yaml_files', nargs='+')
|
|
|
|
parser.add_argument('output_dir')
|
2016-11-11 01:08:02 +00:00
|
|
|
parser.add_argument('-source-dir', '-s', default='', help='set source directory')
|
2016-10-07 17:06:34 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
p = subprocess.Popen(['c++filt', '-n'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
2016-11-11 04:51:27 +00:00
|
|
|
|
|
|
|
|
2016-10-07 17:06:34 +00:00
|
|
|
def demangle(name):
|
|
|
|
p.stdin.write(name + '\n')
|
|
|
|
return p.stdout.readline().rstrip()
|
|
|
|
|
2016-11-11 04:51:27 +00:00
|
|
|
|
2016-10-07 17:06:34 +00:00
|
|
|
class Remark(yaml.YAMLObject):
|
2016-11-11 06:11:56 +00:00
|
|
|
max_hotness = 0
|
|
|
|
|
2016-11-15 08:40:51 +00:00
|
|
|
# Work-around for http://pyyaml.org/ticket/154.
|
|
|
|
yaml_loader = Loader
|
|
|
|
|
2016-11-11 06:11:56 +00:00
|
|
|
@classmethod
|
|
|
|
def should_display_hotness(cls):
|
|
|
|
# If max_hotness is 0 at the end, we assume hotness information is
|
|
|
|
# missing and no relative hotness information is displayed
|
|
|
|
return cls.max_hotness != 0
|
|
|
|
|
2016-11-11 01:25:04 +00:00
|
|
|
# Map function names to their source location for function where inlining happened
|
|
|
|
caller_loc = dict()
|
2016-10-11 16:20:38 +00:00
|
|
|
|
2016-11-11 06:11:56 +00:00
|
|
|
def __getattr__(self, name):
|
|
|
|
# If hotness is missing, assume 0
|
|
|
|
if name == 'Hotness':
|
|
|
|
return 0
|
|
|
|
raise AttributeError
|
|
|
|
|
2016-10-07 17:06:34 +00:00
|
|
|
@property
|
|
|
|
def File(self):
|
|
|
|
return self.DebugLoc['File']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def Line(self):
|
|
|
|
return int(self.DebugLoc['Line'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def Column(self):
|
|
|
|
return self.DebugLoc['Column']
|
|
|
|
|
2016-10-11 16:19:06 +00:00
|
|
|
@property
|
|
|
|
def DebugLocString(self):
|
2016-10-07 17:06:34 +00:00
|
|
|
return "{}:{}:{}".format(self.File, self.Line, self.Column)
|
|
|
|
|
2016-10-11 16:19:06 +00:00
|
|
|
@property
|
|
|
|
def DemangledFunctionName(self):
|
|
|
|
return demangle(self.Function)
|
|
|
|
|
2016-11-07 22:41:13 +00:00
|
|
|
@classmethod
|
|
|
|
def make_link(cls, File, Line):
|
|
|
|
return "{}#L{}".format(SourceFileRenderer.html_file_name(File), Line)
|
|
|
|
|
2016-10-11 16:19:06 +00:00
|
|
|
@property
|
|
|
|
def Link(self):
|
2016-11-07 22:41:13 +00:00
|
|
|
return Remark.make_link(self.File, self.Line)
|
|
|
|
|
|
|
|
def getArgString(self, mapping):
|
|
|
|
mapping = mapping.copy()
|
|
|
|
dl = mapping.get('DebugLoc')
|
|
|
|
if dl:
|
|
|
|
del mapping['DebugLoc']
|
|
|
|
|
|
|
|
assert(len(mapping) == 1)
|
|
|
|
(key, value) = mapping.items()[0]
|
|
|
|
|
|
|
|
if key == 'Caller' or key == 'Callee':
|
|
|
|
value = demangle(value)
|
2016-10-07 17:06:34 +00:00
|
|
|
|
2016-11-07 22:41:13 +00:00
|
|
|
if dl and key != 'Caller':
|
|
|
|
return "<a href={}>{}</a>".format(
|
|
|
|
Remark.make_link(dl['File'], dl['Line']), value)
|
|
|
|
else:
|
|
|
|
return value
|
2016-10-07 17:06:34 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def message(self):
|
2016-11-07 22:41:13 +00:00
|
|
|
# Args is a list of mappings (dictionaries)
|
|
|
|
values = [self.getArgString(mapping) for mapping in self.Args]
|
2016-10-11 16:20:40 +00:00
|
|
|
return "".join(values)
|
2016-10-07 17:06:34 +00:00
|
|
|
|
2016-10-11 16:20:38 +00:00
|
|
|
@property
|
|
|
|
def RelativeHotness(self):
|
2016-11-11 06:11:56 +00:00
|
|
|
if Remark.should_display_hotness():
|
|
|
|
return "{}%".format(int(round(self.Hotness * 100 / Remark.max_hotness)))
|
|
|
|
else:
|
|
|
|
return ''
|
2016-10-11 16:20:38 +00:00
|
|
|
|
2016-11-10 18:42:56 +00:00
|
|
|
@property
|
|
|
|
def key(self):
|
|
|
|
return (self.__class__, self.Pass, self.Name, self.File, self.Line, self.Column, self.message)
|
|
|
|
|
2016-11-11 04:51:27 +00:00
|
|
|
|
2016-10-07 17:06:34 +00:00
|
|
|
class Analysis(Remark):
|
|
|
|
yaml_tag = '!Analysis'
|
|
|
|
|
|
|
|
@property
|
2016-11-11 04:51:27 +00:00
|
|
|
def color(self):
|
|
|
|
return "white"
|
|
|
|
|
2016-10-07 17:06:34 +00:00
|
|
|
|
|
|
|
class AnalysisFPCommute(Analysis):
|
|
|
|
yaml_tag = '!AnalysisFPCommute'
|
|
|
|
|
2016-11-11 04:51:27 +00:00
|
|
|
|
2016-10-07 17:06:34 +00:00
|
|
|
class AnalysisAliasing(Analysis):
|
|
|
|
yaml_tag = '!AnalysisAliasing'
|
|
|
|
|
2016-11-11 04:51:27 +00:00
|
|
|
|
2016-10-07 17:06:34 +00:00
|
|
|
class Passed(Remark):
|
|
|
|
yaml_tag = '!Passed'
|
|
|
|
|
|
|
|
@property
|
2016-11-11 04:51:27 +00:00
|
|
|
def color(self):
|
|
|
|
return "green"
|
|
|
|
|
2016-10-07 17:06:34 +00:00
|
|
|
|
|
|
|
class Missed(Remark):
|
|
|
|
yaml_tag = '!Missed'
|
|
|
|
|
|
|
|
@property
|
2016-11-11 04:51:27 +00:00
|
|
|
def color(self):
|
|
|
|
return "red"
|
|
|
|
|
2016-10-07 17:06:34 +00:00
|
|
|
|
|
|
|
class SourceFileRenderer:
|
|
|
|
def __init__(self, filename):
|
2016-11-11 01:08:02 +00:00
|
|
|
existing_filename = None
|
|
|
|
if os.path.exists(filename):
|
|
|
|
existing_filename = filename
|
|
|
|
else:
|
|
|
|
fn = os.path.join(args.source_dir, filename)
|
|
|
|
if os.path.exists(fn):
|
|
|
|
existing_filename = fn
|
|
|
|
|
2016-10-07 17:06:34 +00:00
|
|
|
self.stream = open(os.path.join(args.output_dir, SourceFileRenderer.html_file_name(filename)), 'w')
|
2016-11-11 01:08:02 +00:00
|
|
|
if existing_filename:
|
|
|
|
self.source_stream = open(existing_filename)
|
|
|
|
else:
|
|
|
|
self.source_stream = None
|
|
|
|
print('''
|
|
|
|
<html>
|
|
|
|
<h1>Unable to locate file {}</h1>
|
|
|
|
</html>
|
|
|
|
'''.format(filename), file=self.stream)
|
|
|
|
|
2016-11-11 00:51:32 +00:00
|
|
|
self.html_formatter = HtmlFormatter()
|
|
|
|
self.cpp_lexer = CppLexer()
|
2016-10-07 17:06:34 +00:00
|
|
|
|
|
|
|
def render_source_line(self, linenum, line):
|
2016-11-11 00:51:32 +00:00
|
|
|
html_line = highlight(line, self.cpp_lexer, self.html_formatter)
|
2016-10-07 17:06:34 +00:00
|
|
|
print('''
|
|
|
|
<tr>
|
|
|
|
<td><a name=\"L{linenum}\">{linenum}</a></td>
|
|
|
|
<td></td>
|
|
|
|
<td></td>
|
2016-11-11 00:51:32 +00:00
|
|
|
<td>{html_line}</td>
|
2016-10-07 17:06:34 +00:00
|
|
|
</tr>'''.format(**locals()), file=self.stream)
|
|
|
|
|
2016-11-11 01:51:34 +00:00
|
|
|
def render_inline_remarks(self, r, line):
|
2016-11-11 01:25:04 +00:00
|
|
|
inlining_context = r.DemangledFunctionName
|
|
|
|
dl = Remark.caller_loc.get(r.Function)
|
|
|
|
if dl:
|
|
|
|
link = Remark.make_link(dl['File'], dl['Line'] - 2)
|
|
|
|
inlining_context = "<a href={link}>{r.DemangledFunctionName}</a>".format(**locals())
|
|
|
|
|
2016-11-11 01:51:34 +00:00
|
|
|
# Column is the number of characters *including* tabs, keep those and
|
|
|
|
# replace everything else with spaces.
|
|
|
|
indent = line[:r.Column - 1]
|
|
|
|
indent = re.sub('\S', ' ', indent)
|
2016-10-07 17:06:34 +00:00
|
|
|
print('''
|
|
|
|
<tr>
|
|
|
|
<td></td>
|
2016-11-11 06:11:56 +00:00
|
|
|
<td>{r.RelativeHotness}</td>
|
2016-10-07 17:06:34 +00:00
|
|
|
<td class=\"column-entry-{r.color}\">{r.Pass}</td>
|
2016-11-11 01:51:34 +00:00
|
|
|
<td><pre style="display:inline">{indent}</pre><span class=\"column-entry-yellow\"> {r.message} </span></td>
|
2016-11-11 01:25:04 +00:00
|
|
|
<td class=\"column-entry-yellow\">{inlining_context}</td>
|
2016-10-07 17:06:34 +00:00
|
|
|
</tr>'''.format(**locals()), file=self.stream)
|
|
|
|
|
|
|
|
def render(self, line_remarks):
|
2016-11-11 01:08:02 +00:00
|
|
|
if not self.source_stream:
|
|
|
|
return
|
|
|
|
|
2016-10-07 17:06:34 +00:00
|
|
|
print('''
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<link rel='stylesheet' type='text/css' href='style.css'>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="centered">
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<td>Line</td>
|
|
|
|
<td>Hotness</td>
|
|
|
|
<td>Optimization</td>
|
|
|
|
<td>Source</td>
|
2016-11-11 01:25:04 +00:00
|
|
|
<td>Inline Context</td>
|
2016-10-07 17:06:34 +00:00
|
|
|
</tr>''', file=self.stream)
|
|
|
|
for (linenum, line) in enumerate(self.source_stream.readlines(), start=1):
|
|
|
|
self.render_source_line(linenum, line)
|
|
|
|
for remark in line_remarks.get(linenum, []):
|
2016-11-11 01:51:34 +00:00
|
|
|
self.render_inline_remarks(remark, line)
|
2016-10-07 17:06:34 +00:00
|
|
|
print('''
|
|
|
|
</table>
|
|
|
|
</body>
|
|
|
|
</html>''', file=self.stream)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def html_file_name(cls, filename):
|
|
|
|
return filename.replace('/', '_') + ".html"
|
|
|
|
|
2016-11-11 04:51:27 +00:00
|
|
|
|
2016-10-07 17:06:34 +00:00
|
|
|
class IndexRenderer:
|
|
|
|
def __init__(self):
|
|
|
|
self.stream = open(os.path.join(args.output_dir, 'index.html'), 'w')
|
|
|
|
|
2016-10-11 16:19:06 +00:00
|
|
|
def render_entry(self, r):
|
|
|
|
print('''
|
|
|
|
<tr>
|
|
|
|
<td><a href={r.Link}>{r.DebugLocString}</a></td>
|
2016-11-11 06:11:56 +00:00
|
|
|
<td>{r.RelativeHotness}</td>
|
2016-10-11 16:19:06 +00:00
|
|
|
<td>{r.DemangledFunctionName}</td>
|
|
|
|
<td class=\"column-entry-{r.color}\">{r.Pass}</td>
|
|
|
|
</tr>'''.format(**locals()), file=self.stream)
|
2016-10-07 17:06:34 +00:00
|
|
|
|
|
|
|
def render(self, all_remarks):
|
|
|
|
print('''
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<link rel='stylesheet' type='text/css' href='style.css'>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="centered">
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<td>Source Location</td>
|
|
|
|
<td>Hotness</td>
|
|
|
|
<td>Function</td>
|
|
|
|
<td>Pass</td>
|
|
|
|
</tr>''', file=self.stream)
|
|
|
|
for remark in all_remarks:
|
|
|
|
self.render_entry(remark)
|
|
|
|
print('''
|
|
|
|
</table>
|
|
|
|
</body>
|
|
|
|
</html>''', file=self.stream)
|
|
|
|
|
|
|
|
|
2016-11-10 18:42:56 +00:00
|
|
|
all_remarks = dict()
|
2016-11-11 04:51:27 +00:00
|
|
|
file_remarks = dict()
|
2016-10-07 17:06:34 +00:00
|
|
|
|
|
|
|
for input_file in args.yaml_files:
|
|
|
|
f = open(input_file)
|
2016-11-15 08:40:51 +00:00
|
|
|
docs = yaml.load_all(f, Loader=Loader)
|
2016-10-07 17:06:34 +00:00
|
|
|
for remark in docs:
|
2016-11-14 18:38:48 +00:00
|
|
|
# Avoid remarks withoug debug location or if they are duplicated
|
|
|
|
if not hasattr(remark, 'DebugLoc') or remark.key in all_remarks:
|
2016-11-11 06:11:56 +00:00
|
|
|
continue
|
|
|
|
all_remarks[remark.key] = remark
|
2016-11-10 18:42:56 +00:00
|
|
|
|
2016-11-11 06:11:56 +00:00
|
|
|
file_remarks.setdefault(remark.File, dict()).setdefault(remark.Line, []).append(remark)
|
2016-11-10 18:42:56 +00:00
|
|
|
|
2016-11-11 06:11:56 +00:00
|
|
|
Remark.max_hotness = max(Remark.max_hotness, remark.Hotness)
|
2016-10-07 17:06:34 +00:00
|
|
|
|
2016-11-11 01:25:04 +00:00
|
|
|
# Set up a map between function names and their source location for function where inlining happened
|
|
|
|
for remark in all_remarks.itervalues():
|
|
|
|
if type(remark) == Passed and remark.Pass == "inline" and remark.Name == "Inlined":
|
|
|
|
for arg in remark.Args:
|
|
|
|
caller = arg.get('Caller')
|
|
|
|
if caller:
|
|
|
|
Remark.caller_loc[caller] = arg['DebugLoc']
|
|
|
|
|
2016-11-11 06:11:56 +00:00
|
|
|
if Remark.should_display_hotness():
|
|
|
|
sorted_remarks = sorted(all_remarks.itervalues(), key=lambda r: r.Hotness, reverse=True)
|
|
|
|
else:
|
|
|
|
sorted_remarks = sorted(all_remarks.itervalues(), key=lambda r: (r.File, r.Line, r.Column))
|
2016-10-07 17:06:34 +00:00
|
|
|
|
|
|
|
if not os.path.exists(args.output_dir):
|
|
|
|
os.mkdir(args.output_dir)
|
|
|
|
|
|
|
|
for (filename, remarks) in file_remarks.iteritems():
|
|
|
|
SourceFileRenderer(filename).render(remarks)
|
|
|
|
|
2016-11-10 18:42:56 +00:00
|
|
|
IndexRenderer().render(sorted_remarks)
|
2016-10-07 17:06:34 +00:00
|
|
|
|
|
|
|
shutil.copy(os.path.join(os.path.dirname(os.path.realpath(__file__)), "style.css"), args.output_dir)
|