| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import json | 5 import json |
| 6 import logging | |
| 7 import os | 6 import os |
| 8 import pickle | 7 import pickle |
| 9 import subprocess | 8 import subprocess |
| 10 | 9 |
| 11 from crash_queries import crash_iterator | 10 from crash_queries import crash_iterator |
| 12 from crash_queries.delta_test import delta_util | 11 from crash_queries.delta_test import delta_util |
| 13 | 12 |
| 14 AZALEA_RESULTS_DIRECTORY = os.path.join(os.path.dirname(__file__), | 13 PREDATOR_RESULTS_DIRECTORY = os.path.join(os.path.dirname(__file__), |
| 15 'azalea_results') | 14 'predator_results') |
| 16 DELTA_TEST_DIRECTORY = os.path.dirname(__file__) | 15 DELTA_TEST_DIRECTORY = os.path.dirname(__file__) |
| 16 CRASH_FIELDS = ['crashed_version', 'stack_trace', 'signature', |
| 17 'platform', 'client_id', 'regression_range', |
| 18 'customized_data', 'historical_metadata'] |
| 19 |
| 17 | 20 |
| 18 | 21 |
| 19 # TODO(crbug.com/662540): Add unittests. | 22 # TODO(crbug.com/662540): Add unittests. |
| 20 class Delta(object): # pragma: no cover. | 23 class Delta(object): # pragma: no cover. |
| 21 """Stands for delta between two results. | 24 """Stands for delta between two results. |
| 22 | 25 |
| 23 Note, the 2 results should be the same kind and have the same structure. | 26 Note, the 2 results should be the same kind and have the same structure. |
| 24 """ | 27 """ |
| 25 | 28 |
| 26 def __init__(self, result1, result2, fields): | 29 def __init__(self, result1, result2): |
| 27 self._result1 = result1 | 30 self._result1 = result1 |
| 28 self._result2 = result2 | 31 self._result2 = result2 |
| 29 self._fields = fields | 32 self._delta_dict = None |
| 30 self._delta_dict = {} | 33 self._delta_str_dict = None |
| 31 self._delta_str_dict = {} | |
| 32 | 34 |
| 33 @property | 35 @property |
| 34 def delta_dict(self): | 36 def delta_dict(self): |
| 35 """Dict representation of delta. | 37 """Dict representation of delta. |
| 36 | 38 |
| 37 Returns: | 39 Returns: |
| 38 A dict. For example, for Culprit result, the delta dict is like below: | 40 A dict. For example, for Culprit result, the delta dict is like below: |
| 39 { | 41 { |
| 40 'project': 'chromium', | 42 'project': 'chromium', |
| 41 'components': ['Blink>API'], | 43 'components': ['Blink>API'], |
| 42 'cls': [], | 44 'cls': [], |
| 43 'regression_range': ['52.0.1200.1', '52.0.1200.3'] | 45 'regression_range': ['52.0.1200.1', '52.0.1200.3'] |
| 44 } | 46 } |
| 45 """ | 47 """ |
| 46 if self._delta_dict: | 48 if self._delta_dict: |
| 47 return self._delta_dict | 49 return self._delta_dict |
| 48 | 50 |
| 49 for field in self._fields: | 51 self._delta_dict = {} |
| 50 value1 = getattr(self._result1, field) | 52 result1 = self._result1.ToDicts()[0] if self._result1 else {'found': False} |
| 51 value2 = getattr(self._result2, field) | 53 result2 = self._result2.ToDicts()[0] if self._result2 else {'found': False} |
| 54 keys = (set(result1.keys()) if result1 else set() | |
| 55 set(result2.keys()) if result2 else set()) |
| 56 for key in keys: |
| 57 value1 = result1.get(key) |
| 58 value2 = result2.get(key) |
| 52 if value1 != value2: | 59 if value1 != value2: |
| 53 if hasattr(value1, 'ToDict') and callable(value1.ToDict): | 60 self._delta_dict[key] = (value1, value2) |
| 54 value1 = value1.ToDict() | |
| 55 value2 = value2.ToDict() | |
| 56 self._delta_dict[field] = (value1, value2) | |
| 57 | 61 |
| 58 return self._delta_dict | 62 return self._delta_dict |
| 59 | 63 |
| 60 @property | 64 @property |
| 61 def delta_str_dict(self): | 65 def delta_str_dict(self): |
| 62 """Converts delta of each field to a string.""" | 66 """Converts delta of each field to a string.""" |
| 63 if self._delta_str_dict: | 67 if self._delta_str_dict: |
| 64 return self._delta_str_dict | 68 return self._delta_str_dict |
| 65 | 69 |
| 70 self._delta_str_dict = {} |
| 66 for key, (value1, value2) in self.delta_dict.iteritems(): | 71 for key, (value1, value2) in self.delta_dict.iteritems(): |
| 67 self._delta_str_dict[key] = '%s: %s, %s' % (key, value1, value2) | 72 if key == 'suspected_cls': |
| 73 for value in [value1, value2]: |
| 74 if not value: |
| 75 continue |
| 76 |
| 77 for cl in value: |
| 78 cl['confidence'] = round(cl['confidence'], 2) |
| 79 cl.pop('reasons', None) |
| 80 |
| 81 value1 = json.dumps(value1, indent=4, sort_keys=True) |
| 82 value2 = json.dumps(value2, indent=4, sort_keys=True) |
| 83 |
| 84 self._delta_str_dict[key] = '%s 1: %s\n%s 2: %s\n' % (key, value1, |
| 85 key, value2) |
| 68 | 86 |
| 69 return self._delta_str_dict | 87 return self._delta_str_dict |
| 70 | 88 |
| 71 def ToDict(self): | 89 def ToDict(self): |
| 72 return self.delta_dict | 90 return self.delta_dict |
| 73 | 91 |
| 74 def __str__(self): | 92 def __str__(self): |
| 75 return '\n'.join(self.delta_str_dict.values()) | 93 return '\n'.join(self.delta_str_dict.values()) |
| 76 | 94 |
| 77 def __bool__(self): | 95 def __bool__(self): |
| (...skipping 13 matching lines...) Expand all Loading... |
| 91 """ | 109 """ |
| 92 deltas = {} | 110 deltas = {} |
| 93 for result_id, result1 in set1.iteritems(): | 111 for result_id, result1 in set1.iteritems(): |
| 94 # Even when the command are exactly the same, it's possible that one set is | 112 # Even when the command are exactly the same, it's possible that one set is |
| 95 # loaded from local result file, another is just queried from database, | 113 # loaded from local result file, another is just queried from database, |
| 96 # sometimes some crash results would get deleted. | 114 # sometimes some crash results would get deleted. |
| 97 if result_id not in set2: | 115 if result_id not in set2: |
| 98 continue | 116 continue |
| 99 | 117 |
| 100 result2 = set2[result_id] | 118 result2 = set2[result_id] |
| 101 delta = Delta(result1, result2, result1.fields) | 119 if not result1 and not result2: |
| 120 continue |
| 121 |
| 122 delta = Delta(result1, result2) |
| 102 if delta: | 123 if delta: |
| 103 deltas[result_id] = delta | 124 deltas[result_id] = delta |
| 104 | 125 |
| 105 return deltas | 126 return deltas |
| 106 | 127 |
| 107 | 128 |
| 108 # TODO(crbug.com/662540): Add unittests. | 129 # TODO(crbug.com/662540): Add unittests. |
| 109 def GetResults(crashes, client_id, git_hash, result_path, | 130 def GetResults(crashes, client_id, app_id, git_hash, result_path, |
| 110 verbose=False): # pragma: no cover. | 131 verbose=False): # pragma: no cover. |
| 111 """Returns an evaluator function to compute delta between 2 findit githashes. | 132 """Returns an evaluator function to compute delta between 2 findit githashes. |
| 112 | 133 |
| 113 Args: | 134 Args: |
| 114 crashes (list): A list of crash infos. | 135 crashes (list): A list of crash infos. |
| 115 client_id (str): Possible values - fracas/cracas/clustefuzz. | 136 client_id (str): Possible values - fracas/cracas/clustefuzz. |
| 137 app_id (str): Appengine app id to query. |
| 116 git_hash (str): A git hash of findit repository. | 138 git_hash (str): A git hash of findit repository. |
| 117 result_path (str): file path for subprocess to write results on. | 139 result_path (str): file path for subprocess to write results on. |
| 118 verbose (bool): If True, print all the findit results. | 140 verbose (bool): If True, print all the findit results. |
| 119 | 141 |
| 120 Return: | 142 Return: |
| 121 A dict mapping crash id to culprit for every crashes analyzed by | 143 A dict mapping crash id to culprit for every crashes analyzed by |
| 122 git_hash version. | 144 git_hash version. |
| 123 """ | 145 """ |
| 124 if not crashes: | 146 if not crashes: |
| 125 return {} | 147 return {} |
| 126 | 148 |
| 127 if verbose: | 149 if verbose: |
| 128 logging.info('\n\n***************************') | 150 print '***************************' |
| 129 logging.info('Switching to git %s', git_hash) | 151 print 'Switching to git %s' % git_hash |
| 130 logging.info('***************************\n\n') | 152 print '***************************\n\n' |
| 131 | 153 |
| 132 with open(os.devnull, 'w') as null_handle: | 154 with open(os.devnull, 'w') as null_handle: |
| 133 subprocess.check_call( | 155 subprocess.check_call( |
| 134 'cd %s; git checkout %s' % (DELTA_TEST_DIRECTORY, git_hash), | 156 'cd %s; git checkout %s' % (DELTA_TEST_DIRECTORY, git_hash), |
| 135 stdout=null_handle, | 157 stdout=null_handle, |
| 136 stderr=null_handle, | 158 stderr=null_handle, |
| 137 shell=True) | 159 shell=True) |
| 138 | 160 |
| 139 if not os.path.exists(result_path): | 161 if not os.path.exists(result_path): |
| 140 args = ['python', 'run-predator.py', result_path, '--client', client_id] | 162 args = ['python', 'run-predator.py', result_path, client_id, app_id] |
| 141 if verbose: | 163 if verbose: |
| 142 args.append('--verbose') | 164 args.append('--verbose') |
| 143 p = subprocess.Popen(args, stdin=subprocess.PIPE) | 165 p = subprocess.Popen(args, stdin=subprocess.PIPE) |
| 144 # TODO(katesonia): Cache crashes for crash_iterator and let subprocess read | 166 # TODO(katesonia): Cache crashes for crash_iterator and let subprocess read |
| 145 # corresponding cache file instead. | 167 # corresponding cache file instead. |
| 146 p.communicate(input=json.dumps(crashes)) | 168 p.communicate(input=json.dumps(crashes)) |
| 147 else: | 169 else: |
| 148 logging.info('\nLoading results from %s', result_path) | 170 print '\nLoading results from', result_path |
| 149 | 171 |
| 150 if not os.path.exists(result_path): | 172 if not os.path.exists(result_path): |
| 151 logging.error('Failed to get results.') | 173 print 'Failed to get results.' |
| 152 return {} | 174 return {} |
| 153 | 175 |
| 154 with open(result_path) as f: | 176 with open(result_path) as f: |
| 155 return pickle.load(f) | 177 return pickle.load(f) |
| 156 | 178 |
| 157 return {} | 179 return {} |
| 158 | 180 |
| 159 | 181 |
| 160 # TODO(crbug.com/662540): Add unittests. | 182 # TODO(crbug.com/662540): Add unittests. |
| 161 def DeltaEvaluator(git_hash1, git_hash2, | 183 def DeltaEvaluator(git_hash1, git_hash2, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 183 deltas (dict): Mappings id to delta for each culprit value. | 205 deltas (dict): Mappings id to delta for each culprit value. |
| 184 crash_count (int): Total count of all the crashes. | 206 crash_count (int): Total count of all the crashes. |
| 185 """ | 207 """ |
| 186 head_branch_name = subprocess.check_output( | 208 head_branch_name = subprocess.check_output( |
| 187 ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).replace('\n', '') | 209 ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).replace('\n', '') |
| 188 try: | 210 try: |
| 189 deltas = {} | 211 deltas = {} |
| 190 crash_count = 0 | 212 crash_count = 0 |
| 191 for index, crashes in enumerate( | 213 for index, crashes in enumerate( |
| 192 crash_iterator.IterateCrashes(client_id, app_id, | 214 crash_iterator.IterateCrashes(client_id, app_id, |
| 215 fields=CRASH_FIELDS, |
| 193 property_values=property_values, | 216 property_values=property_values, |
| 194 start_date=start_date, | 217 start_date=start_date, |
| 195 end_date=end_date, | 218 end_date=end_date, |
| 196 batch_size=batch_size, | 219 batch_size=batch_size, |
| 197 batch_run=True)): | 220 batch_run=True)): |
| 198 | 221 |
| 199 results = [] | 222 results = [] |
| 200 for git_hash in [git_hash1, git_hash2]: | 223 for git_hash in [git_hash1, git_hash2]: |
| 201 result_path = os.path.join( | 224 result_path = os.path.join( |
| 202 AZALEA_RESULTS_DIRECTORY, delta_util.GenerateFileName( | 225 PREDATOR_RESULTS_DIRECTORY, delta_util.GenerateFileName( |
| 203 client_id, property_values, start_date, end_date, | 226 client_id, property_values, start_date, end_date, |
| 204 batch_size, index, git_hash)) | 227 batch_size, index, git_hash)) |
| 205 results.append(GetResults(crashes, client_id, git_hash, result_path, | 228 results.append(GetResults(crashes, client_id, app_id, |
| 229 git_hash, result_path, |
| 206 verbose=verbose)) | 230 verbose=verbose)) |
| 207 | 231 |
| 208 crash_count += len(crashes) | 232 crash_count += len(crashes) |
| 209 deltas.update(GetDeltasFromTwoSetsOfResults(*results)) | 233 batch_deltas = GetDeltasFromTwoSetsOfResults(*results) |
| 234 # Print deltas of the current batch. |
| 235 print '========= Delta of this batch =========' |
| 236 delta_util.PrintDelta(batch_deltas, len(crashes), app_id) |
| 237 deltas.update(batch_deltas) |
| 210 | 238 |
| 211 return deltas, crash_count | 239 return deltas, crash_count |
| 212 finally: | 240 finally: |
| 213 with open(os.devnull, 'w') as null_handle: | 241 with open(os.devnull, 'w') as null_handle: |
| 214 subprocess.check_call(['git', 'checkout', head_branch_name], | 242 subprocess.check_call(['git', 'checkout', head_branch_name], |
| 215 stdout=null_handle, | 243 stdout=null_handle, |
| 216 stderr=null_handle) | 244 stderr=null_handle) |
| OLD | NEW |