Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 from collections import defaultdict | |
| 6 | |
| 7 from common import chromium_deps | |
| 8 from common.change_log import ChangeLog | |
| 9 from common.git_repository import GitRepository | |
| 10 from common.http_client_appengine import HttpClientAppengine | |
| 11 from crash import crash_util | |
| 12 from crash.results import MatchResult, MatchResults | |
| 13 from crash.scorers.aggregator import Aggregator | |
| 14 from crash.scorers.top_frame_index import TopFrameIndex | |
| 15 from crash.scorers.min_distance import MinDistance | |
| 16 | |
| 17 | |
| 18 def GetDepsInCrashStack(crash_stack, crash_deps): | |
| 19 """Gets Dependencies in crash stack.""" | |
| 20 if not crash_stack: | |
| 21 return {} | |
| 22 | |
| 23 stack_deps = {} | |
| 24 for frame in crash_stack: | |
| 25 stack_deps[frame.dep_path] = crash_deps[frame.dep_path] | |
| 26 | |
| 27 return stack_deps | |
| 28 | |
| 29 | |
| 30 def GetFileToChangeLogsForDeps(regression_deps_rolls, stack_deps): | |
| 31 """For those deps we concern, gets a dict containing all the files touched by | |
| 32 changelogs in regression range and a set of revisions that should be ignored. | |
| 33 | |
| 34 Args: | |
| 35 regression_deps_rolls (dict): Maps dep_path to DependencyRoll in | |
| 36 regression range. | |
| 37 stack_deps (dict): Represents all the dependencies shown in | |
| 38 the crash stack. | |
| 39 | |
| 40 Returns: | |
| 41 A tuple (dep_file_to_changelogs, ignore_cls). | |
| 42 | |
| 43 dep_file_to_changelogs (dict): Maps dep_path to a dict mapping file path | |
| 44 to ChangeLogs that touched this file. | |
| 45 | |
| 46 For example: | |
| 47 { | |
| 48 'src/': { | |
| 49 'a.cc': [ | |
| 50 ChangeLog.FromDict({ | |
| 51 'author_name': 'test@chromium.org', | |
| 52 'message': 'dummy', | |
| 53 'committer_email': 'example@chromium.org', | |
| 54 'commit_position': 175976, | |
| 55 'author_email': 'example@chromium.org', | |
| 56 'touched_files': [ | |
| 57 { | |
| 58 'change_type': 'add', | |
| 59 'new_path': 'a.cc', | |
| 60 'old_path': 'b/a.cc' | |
| 61 }, | |
| 62 ... | |
| 63 ], | |
| 64 'author_time': 'Thu Mar 31 21:24:43 2016', | |
| 65 'committer_time': 'Thu Mar 31 21:28:39 2016', | |
| 66 'commit_url': | |
| 67 'https://repo.test/+/bcfd', | |
| 68 'code_review_url': 'https://codereview.chromium.org/3281', | |
| 69 'committer_name': 'example@chromium.org', | |
| 70 'revision': 'bcfd', | |
| 71 'reverted_revision': None | |
| 72 }), | |
| 73 ] | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 ignore_cls (set): A set of reverted revisions. | |
| 78 """ | |
| 79 dep_file_to_changelogs = defaultdict(lambda: defaultdict(list)) | |
| 80 ignore_cls = set() | |
| 81 | |
| 82 for dep in stack_deps: | |
| 83 dep_roll = regression_deps_rolls[dep] | |
| 84 | |
| 85 git_repository = GitRepository(dep_roll['repo_url'], HttpClientAppengine()) | |
| 86 changelogs = git_repository.GetChangeLogs(dep_roll['old_revision'], | |
| 87 dep_roll['new_revision']) | |
| 88 | |
| 89 for changelog in changelogs: | |
| 90 if changelog.reverted_revision: | |
| 91 # Add reverted revisions to ignore_cls to filter those reverted | |
| 92 # revisions later while matching cls. | |
| 93 ignore_cls.add(changelog.reverted_revision) | |
| 94 continue | |
| 95 | |
| 96 for touched_file in changelog.touched_files: | |
| 97 # If a dependency is deleted or is a .h file, skip this file. | |
|
Martin Barbella
2016/04/19 22:21:28
This is self-explanatory from the code itself.
Sharu Jiang
2016/04/20 18:54:24
Done.
| |
| 98 if (touched_file.change_type == 'delete' or | |
| 99 touched_file.new_path.endswith('.h')): | |
| 100 continue | |
| 101 | |
| 102 dep_file_to_changelogs[dep][touched_file.new_path].append(changelog) | |
| 103 | |
| 104 return dep_file_to_changelogs, ignore_cls | |
| 105 | |
| 106 | |
| 107 def GetFileToStackInfosForDeps(stacktrace, stack_deps): | |
| 108 """Gets a dict containing all the stack information of files in stacktrace. | |
| 109 | |
| 110 Args: | |
| 111 stacktrace (Stacktrace): Parsed stacktrace object. | |
| 112 stack_deps (dict): Represents all the dependencies show in | |
| 113 the crash stack. | |
| 114 | |
| 115 Returns: | |
| 116 A dict, maps dep path to a dict mapping file path to a list of stack | |
| 117 inforamtion of this file. A file may occur in several frames, one stack info | |
| 118 consist of a StackFrame and the callstack priority of it. | |
| 119 | |
| 120 For example: | |
| 121 { | |
| 122 'src/': { | |
| 123 'a.cc': [ | |
| 124 (StackFrame(0, 'src/', '', 'func', 'a.cc', [1]), 0), | |
| 125 (StackFrame(2, 'src/', '', 'func', 'a.cc', [33]), 0), | |
| 126 ] | |
| 127 } | |
| 128 } | |
| 129 """ | |
| 130 dep_file_to_stack_infos = defaultdict(lambda: defaultdict(list)) | |
| 131 | |
| 132 for callstack in stacktrace: | |
| 133 for frame in callstack: | |
| 134 if frame.dep_path not in stack_deps or frame.file_path.endswith('.h'): | |
| 135 continue | |
| 136 | |
| 137 dep_file_to_stack_infos[frame.dep_path][frame.file_path].append(( | |
| 138 frame, callstack.priority)) | |
| 139 | |
| 140 return dep_file_to_stack_infos | |
| 141 | |
| 142 | |
| 143 def GetFileToBlameForDeps(stacktrace, stack_deps): | |
| 144 """Gets Blames for files in stacktrace. | |
| 145 | |
| 146 Args: | |
| 147 stacktrace (Stacktrace): Parsed stacktrace object. | |
| 148 stack_deps (dict of Dependencys): Represents all the dependencies shown in | |
| 149 the crash stack. | |
| 150 | |
| 151 Returns: | |
| 152 A dict, maps dep_path to a dict mapping file path to its Blame. | |
| 153 | |
| 154 For example: | |
| 155 { | |
| 156 'src/': { | |
| 157 'a.cc': Blame(revision, 'a.cc') | |
| 158 'b.cc': Blame(revision, 'b.cc') | |
| 159 } | |
| 160 } | |
| 161 """ | |
| 162 dep_file_to_blame = defaultdict(dict) | |
| 163 for callstack in stacktrace: | |
| 164 for frame in callstack: | |
| 165 # We only care about those dependencies in crash stack. | |
| 166 if frame.dep_path not in stack_deps: | |
| 167 continue | |
| 168 | |
| 169 git_repository = GitRepository(stack_deps[frame.dep_path].repo_url, | |
| 170 HttpClientAppengine()) | |
| 171 dep_file_to_blame[frame.dep_path][frame.file_path] = ( | |
| 172 git_repository.GetBlame( | |
| 173 frame.file_path, stack_deps[frame.dep_path].revision)) | |
|
Martin Barbella
2016/04/19 22:21:29
Blank line after this line.
Sharu Jiang
2016/04/20 18:54:24
Done.
| |
| 174 return dep_file_to_blame | |
| 175 | |
| 176 | |
| 177 def FindMatchResults(dep_file_to_changelogs, | |
|
Martin Barbella
2016/04/19 22:21:28
These parameter names are a bit confusing.
Sharu Jiang
2016/04/20 18:54:24
Change it to dep_to_file_to_changelogs
| |
| 178 dep_file_to_stack_infos, | |
| 179 dep_file_to_blame, | |
| 180 ignore_cls=None): | |
| 181 """Finds culprit results by matching stacktrace and changelogs in regression | |
| 182 range. This method only applies to those crashes with regression range. | |
| 183 | |
| 184 Args: | |
| 185 dep_file_to_changelogs (dict): Maps dep_path to a dict mapping file path | |
| 186 to ChangeLogs that touched this file. | |
| 187 | |
| 188 dep_file_to_stack_infos (dict): Maps dep path to a dict mapping file path | |
| 189 to a list of stack inforamtion of this file. A file may occur in several | |
| 190 frames, one stack info consist of a StackFrame and the callstack priority | |
| 191 of it. | |
| 192 | |
| 193 dep_file_to_blame (dict): Maps dep_path to a dict mapping file path to | |
| 194 its Blame. | |
| 195 | |
| 196 ignore_cls (set): Set of reverted revisions. | |
| 197 | |
| 198 Returns: | |
| 199 A list of MatchResult instances with confidence and reason unset. | |
| 200 """ | |
| 201 match_results = MatchResults(ignore_cls) | |
| 202 | |
| 203 for dep, file_to_stack_infos in dep_file_to_stack_infos.iteritems(): | |
| 204 for crashed_file_path, stack_infos in file_to_stack_infos.iteritems(): | |
| 205 | |
| 206 file_to_changelogs = dep_file_to_changelogs[dep] | |
| 207 for touched_file_path, changelogs in file_to_changelogs.iteritems(): | |
| 208 if not crash_util.IsSameFilePath(crashed_file_path, touched_file_path): | |
| 209 continue | |
| 210 | |
| 211 match_results.GenerateMatchResults( | |
| 212 crashed_file_path, dep, stack_infos, changelogs, | |
| 213 dep_file_to_blame[dep][crashed_file_path]) | |
| 214 | |
| 215 return match_results.values() | |
| 216 | |
| 217 | |
| 218 def FindItForCrash(stacktrace, regression_deps_rolls, crashed_deps): | |
| 219 """Finds culprit results for crash. | |
| 220 | |
| 221 Args: | |
| 222 stacktrace (Stactrace): Parsed Stactrace object. | |
| 223 regression_deps_rolls (dict): Maps dep_path to DependencyRoll in | |
| 224 regression range. | |
| 225 crashed_deps (dict of Dependencys): Represents all the dependencies of | |
| 226 crashed revision. | |
| 227 | |
| 228 Returns: | |
| 229 List of dicts of culprit results, sorted by confidence from highest to | |
| 230 lowest. | |
| 231 """ | |
| 232 if not regression_deps_rolls: | |
| 233 return [] | |
| 234 | |
| 235 crash_stack = stacktrace.GetCrashStack() | |
| 236 stack_deps = GetDepsInCrashStack(crash_stack, crashed_deps) | |
| 237 | |
| 238 # Get dep and file to changelogs, stack_info and blame dicts. | |
| 239 dep_file_to_changelogs, ignore_cls = GetFileToChangeLogsForDeps( | |
| 240 regression_deps_rolls, stack_deps) | |
| 241 dep_file_to_stack_infos = GetFileToStackInfosForDeps(stacktrace, stack_deps) | |
| 242 dep_file_to_blame = GetFileToBlameForDeps(stacktrace, stack_deps) | |
| 243 | |
| 244 results = FindMatchResults(dep_file_to_changelogs, | |
| 245 dep_file_to_stack_infos, | |
| 246 dep_file_to_blame, | |
| 247 ignore_cls) | |
| 248 | |
| 249 if not results: | |
| 250 return [] | |
| 251 | |
| 252 aggregator = Aggregator([TopFrameIndex(), MinDistance()]) | |
| 253 | |
| 254 map(aggregator.ScoreAndReason, results) | |
| 255 | |
| 256 return sorted([result.ToDict() for result in results], | |
| 257 key=lambda r: -r['confidence']) | |
| OLD | NEW |