Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 2 | |
| 3 | from __future__ import print_function |
| 4 | |
| 5 | import yaml |
| 6 | # Try to use the C parser. |
| 7 | try: |
| 8 | from yaml import CLoader as Loader |
| 9 | except ImportError: |
| 10 | print("For faster parsing, you may want to install libYAML for PyYAML") |
| 11 | from yaml import Loader |
| 12 | |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 13 | import cgi |
Brian Gesiak | 5e0a946 | 2017-06-29 18:56:25 +0000 | [diff] [blame] | 14 | from collections import defaultdict |
Adam Nemet | 659d7db | 2017-07-17 18:00:41 +0000 | [diff] [blame] | 15 | import fnmatch |
Brian Gesiak | 5e0a946 | 2017-06-29 18:56:25 +0000 | [diff] [blame] | 16 | import functools |
| 17 | from multiprocessing import Lock |
Adam Nemet | 659d7db | 2017-07-17 18:00:41 +0000 | [diff] [blame] | 18 | import os, os.path |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 19 | import subprocess |
Brian Gesiak | efd227f | 2017-08-11 17:56:57 +0000 | [diff] [blame] | 20 | try: |
| 21 | # The previously builtin function `intern()` was moved |
| 22 | # to the `sys` module in Python 3. |
| 23 | from sys import intern |
| 24 | except: |
| 25 | pass |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 26 | |
Brian Gesiak | 5e0a946 | 2017-06-29 18:56:25 +0000 | [diff] [blame] | 27 | import optpmap |
| 28 | |
Brian Gesiak | 9b4e897 | 2017-06-26 16:51:24 +0000 | [diff] [blame] | 29 | try: |
| 30 | dict.iteritems |
| 31 | except AttributeError: |
| 32 | # Python 3 |
| 33 | def itervalues(d): |
| 34 | return iter(d.values()) |
| 35 | def iteritems(d): |
| 36 | return iter(d.items()) |
| 37 | else: |
| 38 | # Python 2 |
| 39 | def itervalues(d): |
| 40 | return d.itervalues() |
| 41 | def iteritems(d): |
| 42 | return d.iteritems() |
| 43 | |
| 44 | |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 45 | def html_file_name(filename): |
Brian Gesiak | a886997 | 2017-07-18 19:25:34 +0000 | [diff] [blame] | 46 | return filename.replace('/', '_').replace('#', '_') + ".html" |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 47 | |
Brian Gesiak | 9b4e897 | 2017-06-26 16:51:24 +0000 | [diff] [blame] | 48 | |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 49 | def make_link(File, Line): |
Filipe Cabecinhas | cc07564 | 2017-06-07 14:57:20 +0000 | [diff] [blame] | 50 | return "\"{}#L{}\"".format(html_file_name(File), Line) |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 51 | |
| 52 | |
| 53 | class Remark(yaml.YAMLObject): |
| 54 | # Work-around for http://pyyaml.org/ticket/154. |
| 55 | yaml_loader = Loader |
| 56 | |
Adam Nemet | 95e0c5f | 2017-11-29 17:07:41 +0000 | [diff] [blame] | 57 | default_demangler = 'c++filt -n' |
| 58 | demangler_proc = None |
| 59 | |
| 60 | @classmethod |
| 61 | def set_demangler(cls, demangler): |
| 62 | cls.demangler_proc = subprocess.Popen(demangler.split(), stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| 63 | cls.demangler_lock = Lock() |
| 64 | |
| 65 | @classmethod |
| 66 | def demangle(cls, name): |
| 67 | with cls.demangler_lock: |
| 68 | cls.demangler_proc.stdin.write((name + '\n').encode('utf-8')) |
| 69 | cls.demangler_proc.stdin.flush() |
| 70 | return cls.demangler_proc.stdout.readline().rstrip().decode('utf-8') |
| 71 | |
Adam Nemet | 817e90f | 2017-07-19 22:04:59 +0000 | [diff] [blame] | 72 | # Intern all strings since we have lot of duplication across filenames, |
| 73 | # remark text. |
| 74 | # |
| 75 | # Change Args from a list of dicts to a tuple of tuples. This saves |
| 76 | # memory in two ways. One, a small tuple is significantly smaller than a |
| 77 | # small dict. Two, using tuple instead of list allows Args to be directly |
| 78 | # used as part of the key (in Python only immutable types are hashable). |
| 79 | def _reduce_memory(self): |
Adam Nemet | 1d5f5b3 | 2017-07-19 22:04:56 +0000 | [diff] [blame] | 80 | self.Pass = intern(self.Pass) |
| 81 | self.Name = intern(self.Name) |
| 82 | self.Function = intern(self.Function) |
| 83 | |
Adam Nemet | 817e90f | 2017-07-19 22:04:59 +0000 | [diff] [blame] | 84 | def _reduce_memory_dict(old_dict): |
Adam Nemet | 1d5f5b3 | 2017-07-19 22:04:56 +0000 | [diff] [blame] | 85 | new_dict = dict() |
Brian Gesiak | 34f07f9 | 2017-08-11 18:02:07 +0000 | [diff] [blame] | 86 | for (k, v) in iteritems(old_dict): |
Adam Nemet | 1d5f5b3 | 2017-07-19 22:04:56 +0000 | [diff] [blame] | 87 | if type(k) is str: |
| 88 | k = intern(k) |
| 89 | |
| 90 | if type(v) is str: |
| 91 | v = intern(v) |
| 92 | elif type(v) is dict: |
Adam Nemet | 817e90f | 2017-07-19 22:04:59 +0000 | [diff] [blame] | 93 | # This handles [{'Caller': ..., 'DebugLoc': { 'File': ... }}] |
| 94 | v = _reduce_memory_dict(v) |
Adam Nemet | 1d5f5b3 | 2017-07-19 22:04:56 +0000 | [diff] [blame] | 95 | new_dict[k] = v |
Adam Nemet | 817e90f | 2017-07-19 22:04:59 +0000 | [diff] [blame] | 96 | return tuple(new_dict.items()) |
Adam Nemet | 1d5f5b3 | 2017-07-19 22:04:56 +0000 | [diff] [blame] | 97 | |
Adam Nemet | 817e90f | 2017-07-19 22:04:59 +0000 | [diff] [blame] | 98 | self.Args = tuple([_reduce_memory_dict(arg_dict) for arg_dict in self.Args]) |
| 99 | |
| 100 | # The inverse operation of the dictonary-related memory optimization in |
| 101 | # _reduce_memory_dict. E.g. |
| 102 | # (('DebugLoc', (('File', ...) ... ))) -> [{'DebugLoc': {'File': ...} ....}] |
| 103 | def recover_yaml_structure(self): |
| 104 | def tuple_to_dict(t): |
| 105 | d = dict() |
| 106 | for (k, v) in t: |
| 107 | if type(v) is tuple: |
| 108 | v = tuple_to_dict(v) |
| 109 | d[k] = v |
| 110 | return d |
| 111 | |
| 112 | self.Args = [tuple_to_dict(arg_tuple) for arg_tuple in self.Args] |
Adam Nemet | 1d5f5b3 | 2017-07-19 22:04:56 +0000 | [diff] [blame] | 113 | |
| 114 | def canonicalize(self): |
Adam Nemet | a8b692a | 2017-03-02 17:00:53 +0000 | [diff] [blame] | 115 | if not hasattr(self, 'Hotness'): |
| 116 | self.Hotness = 0 |
| 117 | if not hasattr(self, 'Args'): |
| 118 | self.Args = [] |
Adam Nemet | 817e90f | 2017-07-19 22:04:59 +0000 | [diff] [blame] | 119 | self._reduce_memory() |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 120 | |
| 121 | @property |
| 122 | def File(self): |
| 123 | return self.DebugLoc['File'] |
| 124 | |
| 125 | @property |
| 126 | def Line(self): |
| 127 | return int(self.DebugLoc['Line']) |
| 128 | |
| 129 | @property |
| 130 | def Column(self): |
| 131 | return self.DebugLoc['Column'] |
| 132 | |
| 133 | @property |
| 134 | def DebugLocString(self): |
| 135 | return "{}:{}:{}".format(self.File, self.Line, self.Column) |
| 136 | |
| 137 | @property |
| 138 | def DemangledFunctionName(self): |
Adam Nemet | 95e0c5f | 2017-11-29 17:07:41 +0000 | [diff] [blame] | 139 | return self.demangle(self.Function) |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 140 | |
| 141 | @property |
| 142 | def Link(self): |
| 143 | return make_link(self.File, self.Line) |
| 144 | |
| 145 | def getArgString(self, mapping): |
Adam Nemet | 817e90f | 2017-07-19 22:04:59 +0000 | [diff] [blame] | 146 | mapping = dict(list(mapping)) |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 147 | dl = mapping.get('DebugLoc') |
| 148 | if dl: |
| 149 | del mapping['DebugLoc'] |
| 150 | |
| 151 | assert(len(mapping) == 1) |
Brian Gesiak | 60a3185 | 2017-08-14 04:16:43 +0000 | [diff] [blame] | 152 | (key, value) = list(mapping.items())[0] |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 153 | |
| 154 | if key == 'Caller' or key == 'Callee': |
Adam Nemet | 95e0c5f | 2017-11-29 17:07:41 +0000 | [diff] [blame] | 155 | value = cgi.escape(self.demangle(value)) |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 156 | |
| 157 | if dl and key != 'Caller': |
Adam Nemet | 817e90f | 2017-07-19 22:04:59 +0000 | [diff] [blame] | 158 | dl_dict = dict(list(dl)) |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 159 | return "<a href={}>{}</a>".format( |
Adam Nemet | 817e90f | 2017-07-19 22:04:59 +0000 | [diff] [blame] | 160 | make_link(dl_dict['File'], dl_dict['Line']), value) |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 161 | else: |
| 162 | return value |
| 163 | |
Adam Nemet | 9e5e51a | 2017-12-06 16:50:50 +0000 | [diff] [blame^] | 164 | # Return a cached dictionary for the arguments. The key for each entry is |
| 165 | # the argument key (e.g. 'Callee' for inlining remarks. The value is a |
| 166 | # list containing the value (e.g. for 'Callee' the function) and |
| 167 | # optionally a DebugLoc. |
| 168 | def getArgDict(self): |
| 169 | if hasattr(self, 'ArgDict'): |
| 170 | return self.ArgDict |
| 171 | self.ArgDict = {} |
| 172 | for arg in self.Args: |
| 173 | if len(arg) == 2: |
| 174 | if arg[0][0] == 'DebugLoc': |
| 175 | dbgidx = 0 |
| 176 | else: |
| 177 | assert(arg[1][0] == 'DebugLoc') |
| 178 | dbgidx = 1 |
| 179 | |
| 180 | key = arg[1 - dbgidx][0] |
| 181 | entry = (arg[1 - dbgidx][1], arg[dbgidx][1]) |
| 182 | else: |
| 183 | arg = arg[0] |
| 184 | key = arg[0] |
| 185 | entry = (arg[1], ) |
| 186 | |
| 187 | self.ArgDict[key] = entry |
| 188 | return self.ArgDict |
| 189 | |
Adam Nemet | 6ab2d48 | 2017-03-02 17:00:59 +0000 | [diff] [blame] | 190 | def getDiffPrefix(self): |
| 191 | if hasattr(self, 'Added'): |
| 192 | if self.Added: |
| 193 | return '+' |
| 194 | else: |
| 195 | return '-' |
| 196 | return '' |
| 197 | |
| 198 | @property |
| 199 | def PassWithDiffPrefix(self): |
| 200 | return self.getDiffPrefix() + self.Pass |
| 201 | |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 202 | @property |
| 203 | def message(self): |
| 204 | # Args is a list of mappings (dictionaries) |
| 205 | values = [self.getArgString(mapping) for mapping in self.Args] |
| 206 | return "".join(values) |
| 207 | |
| 208 | @property |
| 209 | def RelativeHotness(self): |
| 210 | if self.max_hotness: |
Adam Nemet | 3a762d9 | 2017-09-29 16:56:54 +0000 | [diff] [blame] | 211 | return "{0:.2f}%".format(self.Hotness * 100. / self.max_hotness) |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 212 | else: |
| 213 | return '' |
| 214 | |
| 215 | @property |
| 216 | def key(self): |
Adam Nemet | 817e90f | 2017-07-19 22:04:59 +0000 | [diff] [blame] | 217 | return (self.__class__, self.PassWithDiffPrefix, self.Name, self.File, |
| 218 | self.Line, self.Column, self.Function, self.Args) |
Adam Nemet | 7370dad | 2017-03-02 17:00:56 +0000 | [diff] [blame] | 219 | |
| 220 | def __hash__(self): |
| 221 | return hash(self.key) |
| 222 | |
| 223 | def __eq__(self, other): |
| 224 | return self.key == other.key |
| 225 | |
| 226 | def __repr__(self): |
| 227 | return str(self.key) |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 228 | |
| 229 | |
| 230 | class Analysis(Remark): |
| 231 | yaml_tag = '!Analysis' |
| 232 | |
| 233 | @property |
| 234 | def color(self): |
| 235 | return "white" |
| 236 | |
| 237 | |
| 238 | class AnalysisFPCommute(Analysis): |
| 239 | yaml_tag = '!AnalysisFPCommute' |
| 240 | |
| 241 | |
| 242 | class AnalysisAliasing(Analysis): |
| 243 | yaml_tag = '!AnalysisAliasing' |
| 244 | |
| 245 | |
| 246 | class Passed(Remark): |
| 247 | yaml_tag = '!Passed' |
| 248 | |
| 249 | @property |
| 250 | def color(self): |
| 251 | return "green" |
| 252 | |
| 253 | |
| 254 | class Missed(Remark): |
| 255 | yaml_tag = '!Missed' |
| 256 | |
| 257 | @property |
| 258 | def color(self): |
| 259 | return "red" |
| 260 | |
| 261 | |
| 262 | def get_remarks(input_file): |
| 263 | max_hotness = 0 |
| 264 | all_remarks = dict() |
| 265 | file_remarks = defaultdict(functools.partial(defaultdict, list)) |
| 266 | |
| 267 | with open(input_file) as f: |
| 268 | docs = yaml.load_all(f, Loader=Loader) |
| 269 | for remark in docs: |
Adam Nemet | 1d5f5b3 | 2017-07-19 22:04:56 +0000 | [diff] [blame] | 270 | remark.canonicalize() |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 271 | # Avoid remarks withoug debug location or if they are duplicated |
| 272 | if not hasattr(remark, 'DebugLoc') or remark.key in all_remarks: |
| 273 | continue |
| 274 | all_remarks[remark.key] = remark |
| 275 | |
| 276 | file_remarks[remark.File][remark.Line].append(remark) |
| 277 | |
Adam Nemet | 6ab2d48 | 2017-03-02 17:00:59 +0000 | [diff] [blame] | 278 | # If we're reading a back a diff yaml file, max_hotness is already |
| 279 | # captured which may actually be less than the max hotness found |
| 280 | # in the file. |
| 281 | if hasattr(remark, 'max_hotness'): |
| 282 | max_hotness = remark.max_hotness |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 283 | max_hotness = max(max_hotness, remark.Hotness) |
| 284 | |
| 285 | return max_hotness, all_remarks, file_remarks |
| 286 | |
| 287 | |
Brian Gesiak | 5e0a946 | 2017-06-29 18:56:25 +0000 | [diff] [blame] | 288 | def gather_results(filenames, num_jobs, should_print_progress): |
| 289 | if should_print_progress: |
| 290 | print('Reading YAML files...') |
Adam Nemet | 95e0c5f | 2017-11-29 17:07:41 +0000 | [diff] [blame] | 291 | if not Remark.demangler_proc: |
| 292 | Remark.set_demangler(Remark.default_demangler) |
Brian Gesiak | 5e0a946 | 2017-06-29 18:56:25 +0000 | [diff] [blame] | 293 | remarks = optpmap.pmap( |
| 294 | get_remarks, filenames, num_jobs, should_print_progress) |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 295 | max_hotness = max(entry[0] for entry in remarks) |
| 296 | |
| 297 | def merge_file_remarks(file_remarks_job, all_remarks, merged): |
Brian Gesiak | 9b4e897 | 2017-06-26 16:51:24 +0000 | [diff] [blame] | 298 | for filename, d in iteritems(file_remarks_job): |
| 299 | for line, remarks in iteritems(d): |
Adam Nemet | b7278af | 2017-03-01 21:35:00 +0000 | [diff] [blame] | 300 | for remark in remarks: |
| 301 | # Bring max_hotness into the remarks so that |
| 302 | # RelativeHotness does not depend on an external global. |
| 303 | remark.max_hotness = max_hotness |
| 304 | if remark.key not in all_remarks: |
| 305 | merged[filename][line].append(remark) |
| 306 | |
| 307 | all_remarks = dict() |
| 308 | file_remarks = defaultdict(functools.partial(defaultdict, list)) |
| 309 | for _, all_remarks_job, file_remarks_job in remarks: |
| 310 | merge_file_remarks(file_remarks_job, all_remarks, file_remarks) |
| 311 | all_remarks.update(all_remarks_job) |
| 312 | |
| 313 | return all_remarks, file_remarks, max_hotness != 0 |
Adam Nemet | 659d7db | 2017-07-17 18:00:41 +0000 | [diff] [blame] | 314 | |
| 315 | |
Adam Nemet | 9d57dc6 | 2017-09-29 05:20:53 +0000 | [diff] [blame] | 316 | def find_opt_files(*dirs_or_files): |
Adam Nemet | 659d7db | 2017-07-17 18:00:41 +0000 | [diff] [blame] | 317 | all = [] |
| 318 | for dir_or_file in dirs_or_files: |
| 319 | if os.path.isfile(dir_or_file): |
| 320 | all.append(dir_or_file) |
| 321 | else: |
| 322 | for dir, subdirs, files in os.walk(dir_or_file): |
| 323 | # Exclude mounted directories and symlinks (os.walk default). |
| 324 | subdirs[:] = [d for d in subdirs |
| 325 | if not os.path.ismount(os.path.join(dir, d))] |
| 326 | for file in files: |
| 327 | if fnmatch.fnmatch(file, "*.opt.yaml"): |
| 328 | all.append(os.path.join(dir, file)) |
| 329 | return all |