Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Yunlian Jiang | b8b76e2 | 2013-03-25 14:02:29 -0700 | [diff] [blame] | 2 | |
| 3 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 6 | |
| 7 | import getpass |
| 8 | import glob |
| 9 | import hashlib |
| 10 | import os |
| 11 | import pickle |
| 12 | import re |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 13 | import tempfile |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 14 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 15 | from utils import command_executer |
Ahmad Sharif | ee988c8 | 2012-05-14 17:11:56 -0700 | [diff] [blame] | 16 | from utils import misc |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 17 | |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 18 | from image_checksummer import ImageChecksummer |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 19 | |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 20 | SCRATCH_BASE = "/home/%s/cros_scratch" |
| 21 | SCRATCH_DIR = SCRATCH_BASE % getpass.getuser() |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 22 | RESULTS_FILE = "results.txt" |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 23 | MACHINE_FILE = "machine.txt" |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 24 | AUTOTEST_TARBALL = "autotest.tbz2" |
| 25 | PERF_RESULTS_FILE = "perf-results.txt" |
| 26 | |
| 27 | |
| 28 | class Result(object): |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 29 | """ This class manages what exactly is stored inside the cache without knowing |
| 30 | what the key of the cache is. For runs with perf, it stores perf.data, |
| 31 | perf.report, etc. The key generation is handled by the ResultsCache class. |
| 32 | """ |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 33 | |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 34 | def __init__(self, logger, label): |
| 35 | self._chromeos_root = label.chromeos_root |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 36 | self._logger = logger |
| 37 | self._ce = command_executer.GetCommandExecuter(self._logger) |
| 38 | self._temp_dir = None |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 39 | self.label = label |
| 40 | self.results_dir = None |
| 41 | self.perf_data_files = [] |
| 42 | self.perf_report_files = [] |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 43 | |
| 44 | def _CopyFilesTo(self, dest_dir, files_to_copy): |
| 45 | file_index = 0 |
| 46 | for file_to_copy in files_to_copy: |
| 47 | if not os.path.isdir(dest_dir): |
| 48 | command = "mkdir -p %s" % dest_dir |
| 49 | self._ce.RunCommand(command) |
| 50 | dest_file = os.path.join(dest_dir, |
| 51 | ("%s.%s" % (os.path.basename(file_to_copy), |
| 52 | file_index))) |
| 53 | ret = self._ce.CopyFiles(file_to_copy, |
| 54 | dest_file, |
| 55 | recursive=False) |
| 56 | if ret: |
| 57 | raise Exception("Could not copy results file: %s" % file_to_copy) |
| 58 | |
| 59 | def CopyResultsTo(self, dest_dir): |
| 60 | self._CopyFilesTo(dest_dir, self.perf_data_files) |
| 61 | self._CopyFilesTo(dest_dir, self.perf_report_files) |
| 62 | |
| 63 | def _GetKeyvals(self): |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 64 | results_in_chroot = os.path.join(self._chromeos_root, |
| 65 | "chroot", "tmp") |
| 66 | if not self._temp_dir: |
| 67 | self._temp_dir = tempfile.mkdtemp(dir=results_in_chroot) |
| 68 | command = "cp -r {0}/* {1}".format(self.results_dir, self._temp_dir) |
| 69 | self._ce.RunCommand(command) |
| 70 | |
| 71 | command = ("python generate_test_report --no-color --csv %s" % |
| 72 | (os.path.join("/tmp", os.path.basename(self._temp_dir)))) |
| 73 | [_, out, _] = self._ce.ChrootRunCommand(self._chromeos_root, |
| 74 | command, |
| 75 | return_output=True) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 76 | keyvals_dict = {} |
Yunlian Jiang | 006a5b0 | 2013-03-28 12:38:43 -0700 | [diff] [blame] | 77 | tmp_dir_in_chroot = misc.GetInsideChrootPath(self._chromeos_root, |
| 78 | self._temp_dir) |
Ahmad Sharif | a171f8d | 2012-05-24 16:35:24 -0700 | [diff] [blame] | 79 | for line in out.splitlines(): |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 80 | tokens = re.split("=|,", line) |
Ahmad Sharif | a171f8d | 2012-05-24 16:35:24 -0700 | [diff] [blame] | 81 | key = tokens[-2] |
Yunlian Jiang | b8b76e2 | 2013-03-25 14:02:29 -0700 | [diff] [blame] | 82 | if key.startswith(tmp_dir_in_chroot): |
| 83 | key = key[len(tmp_dir_in_chroot) + 1:] |
Ahmad Sharif | a171f8d | 2012-05-24 16:35:24 -0700 | [diff] [blame] | 84 | value = tokens[-1] |
| 85 | keyvals_dict[key] = value |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 86 | |
| 87 | return keyvals_dict |
| 88 | |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 89 | def _GetResultsDir(self): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 90 | mo = re.search(r"Results placed in (\S+)", self.out) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 91 | if mo: |
| 92 | result = mo.group(1) |
| 93 | return result |
| 94 | raise Exception("Could not find results directory.") |
| 95 | |
| 96 | def _FindFilesInResultsDir(self, find_args): |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 97 | if not self.results_dir: |
| 98 | return None |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 99 | command = "find %s %s" % (self.results_dir, |
| 100 | find_args) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 101 | ret, out, _ = self._ce.RunCommand(command, return_output=True) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 102 | if ret: |
| 103 | raise Exception("Could not run find command!") |
| 104 | return out |
| 105 | |
| 106 | def _GetPerfDataFiles(self): |
| 107 | return self._FindFilesInResultsDir("-name perf.data").splitlines() |
| 108 | |
| 109 | def _GetPerfReportFiles(self): |
| 110 | return self._FindFilesInResultsDir("-name perf.data.report").splitlines() |
| 111 | |
| 112 | def _GeneratePerfReportFiles(self): |
| 113 | perf_report_files = [] |
| 114 | for perf_data_file in self.perf_data_files: |
| 115 | # Generate a perf.report and store it side-by-side with the perf.data |
| 116 | # file. |
| 117 | chroot_perf_data_file = misc.GetInsideChrootPath(self._chromeos_root, |
| 118 | perf_data_file) |
| 119 | perf_report_file = "%s.report" % perf_data_file |
| 120 | if os.path.exists(perf_report_file): |
| 121 | raise Exception("Perf report file already exists: %s" % |
| 122 | perf_report_file) |
| 123 | chroot_perf_report_file = misc.GetInsideChrootPath(self._chromeos_root, |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 124 | perf_report_file) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 125 | command = ("/usr/sbin/perf report " |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 126 | "-n " |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 127 | "--symfs /build/%s " |
Ahmad Sharif | 5cc48b5 | 2012-05-18 14:31:20 -0700 | [diff] [blame] | 128 | "--vmlinux /build/%s/usr/lib/debug/boot/vmlinux " |
| 129 | "--kallsyms /build/%s/boot/System.map-* " |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 130 | "-i %s --stdio " |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 131 | "> %s" % |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 132 | (self._board, |
Ahmad Sharif | 5cc48b5 | 2012-05-18 14:31:20 -0700 | [diff] [blame] | 133 | self._board, |
| 134 | self._board, |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 135 | chroot_perf_data_file, |
| 136 | chroot_perf_report_file)) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 137 | self._ce.ChrootRunCommand(self._chromeos_root, |
| 138 | command) |
| 139 | |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 140 | # Add a keyval to the dictionary for the events captured. |
| 141 | perf_report_files.append( |
| 142 | misc.GetOutsideChrootPath(self._chromeos_root, |
| 143 | chroot_perf_report_file)) |
| 144 | return perf_report_files |
| 145 | |
| 146 | def _GatherPerfResults(self): |
| 147 | report_id = 0 |
| 148 | for perf_report_file in self.perf_report_files: |
| 149 | with open(perf_report_file, "r") as f: |
| 150 | report_contents = f.read() |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 151 | for group in re.findall(r"Events: (\S+) (\S+)", report_contents): |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 152 | num_events = group[0] |
| 153 | event_name = group[1] |
| 154 | key = "perf_%s_%s" % (report_id, event_name) |
| 155 | value = str(misc.UnitToNumber(num_events)) |
| 156 | self.keyvals[key] = value |
| 157 | |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 158 | def _PopulateFromRun(self, out, err, retval): |
| 159 | self._board = self.label.board |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 160 | self.out = out |
| 161 | self.err = err |
| 162 | self.retval = retval |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 163 | self.chroot_results_dir = self._GetResultsDir() |
| 164 | self.results_dir = misc.GetOutsideChrootPath(self._chromeos_root, |
| 165 | self.chroot_results_dir) |
| 166 | self.perf_data_files = self._GetPerfDataFiles() |
| 167 | # Include all perf.report data in table. |
| 168 | self.perf_report_files = self._GeneratePerfReportFiles() |
| 169 | # TODO(asharif): Do something similar with perf stat. |
| 170 | |
| 171 | # Grab keyvals from the directory. |
| 172 | self._ProcessResults() |
| 173 | |
| 174 | def _ProcessResults(self): |
| 175 | # Note that this function doesn't know anything about whether there is a |
| 176 | # cache hit or miss. It should process results agnostic of the cache hit |
| 177 | # state. |
| 178 | self.keyvals = self._GetKeyvals() |
| 179 | self.keyvals["retval"] = self.retval |
| 180 | # Generate report from all perf.data files. |
| 181 | # Now parse all perf report files and include them in keyvals. |
| 182 | self._GatherPerfResults() |
| 183 | |
| 184 | def _PopulateFromCacheDir(self, cache_dir): |
| 185 | # Read in everything from the cache directory. |
| 186 | with open(os.path.join(cache_dir, RESULTS_FILE), "r") as f: |
| 187 | self.out = pickle.load(f) |
| 188 | self.err = pickle.load(f) |
| 189 | self.retval = pickle.load(f) |
| 190 | |
| 191 | # Untar the tarball to a temporary directory |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 192 | self._temp_dir = tempfile.mkdtemp(dir=os.path.join(self._chromeos_root, |
| 193 | "chroot", "tmp")) |
| 194 | |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 195 | command = ("cd %s && tar xf %s" % |
| 196 | (self._temp_dir, |
| 197 | os.path.join(cache_dir, AUTOTEST_TARBALL))) |
| 198 | ret = self._ce.RunCommand(command) |
| 199 | if ret: |
| 200 | raise Exception("Could not untar cached tarball") |
| 201 | self.results_dir = self._temp_dir |
| 202 | self.perf_data_files = self._GetPerfDataFiles() |
| 203 | self.perf_report_files = self._GetPerfReportFiles() |
| 204 | self._ProcessResults() |
| 205 | |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 206 | def CleanUp(self, rm_chroot_tmp): |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 207 | if rm_chroot_tmp and self.results_dir: |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 208 | command = "rm -rf %s" % self.results_dir |
| 209 | self._ce.RunCommand(command) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 210 | if self._temp_dir: |
| 211 | command = "rm -rf %s" % self._temp_dir |
| 212 | self._ce.RunCommand(command) |
| 213 | |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 214 | def StoreToCacheDir(self, cache_dir, machine_manager): |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 215 | # Create the dir if it doesn't exist. |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 216 | temp_dir = tempfile.mkdtemp() |
| 217 | |
| 218 | # Store to the temp directory. |
| 219 | with open(os.path.join(temp_dir, RESULTS_FILE), "w") as f: |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 220 | pickle.dump(self.out, f) |
| 221 | pickle.dump(self.err, f) |
| 222 | pickle.dump(self.retval, f) |
| 223 | |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 224 | if self.results_dir: |
| 225 | tarball = os.path.join(temp_dir, AUTOTEST_TARBALL) |
| 226 | command = ("cd %s && " |
| 227 | "tar " |
| 228 | "--exclude=var/spool " |
| 229 | "--exclude=var/log " |
| 230 | "-cjf %s ." % (self.results_dir, tarball)) |
| 231 | ret = self._ce.RunCommand(command) |
| 232 | if ret: |
| 233 | raise Exception("Couldn't store autotest output directory.") |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 234 | # Store machine info. |
| 235 | # TODO(asharif): Make machine_manager a singleton, and don't pass it into |
| 236 | # this function. |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 237 | with open(os.path.join(temp_dir, MACHINE_FILE), "w") as f: |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 238 | f.write(machine_manager.machine_checksum_string[self.label.name]) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 239 | |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 240 | if os.path.exists(cache_dir): |
| 241 | command = "rm -rf {0}".format(cache_dir) |
| 242 | self._ce.RunCommand(command) |
| 243 | |
| 244 | command = "mkdir -p {0} && ".format(os.path.dirname(cache_dir)) |
Yunlian Jiang | 006a5b0 | 2013-03-28 12:38:43 -0700 | [diff] [blame] | 245 | command += "chmod g+x {0} && ".format(temp_dir) |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 246 | command += "mv {0} {1}".format(temp_dir, cache_dir) |
| 247 | ret = self._ce.RunCommand(command) |
| 248 | if ret: |
| 249 | command = "rm -rf {0}".format(temp_dir) |
| 250 | self._ce.RunCommand(command) |
| 251 | raise Exception("Could not move dir %s to dir %s" % |
| 252 | (temp_dir, cache_dir)) |
| 253 | |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 254 | @classmethod |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 255 | def CreateFromRun(cls, logger, label, out, err, retval, suite="pyauto"): |
| 256 | if suite == "telemetry": |
| 257 | result = TelemetryResult(logger, label) |
| 258 | else: |
| 259 | result = cls(logger, label) |
| 260 | result._PopulateFromRun(out, err, retval) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 261 | return result |
| 262 | |
| 263 | @classmethod |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 264 | def CreateFromCacheHit(cls, logger, label, cache_dir, |
| 265 | suite="pyauto"): |
| 266 | if suite == "telemetry": |
| 267 | result = TelemetryResult(logger, label) |
| 268 | else: |
| 269 | result = cls(logger, label) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 270 | try: |
| 271 | result._PopulateFromCacheDir(cache_dir) |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 272 | |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 273 | except Exception as e: |
| 274 | logger.LogError("Exception while using cache: %s" % e) |
| 275 | return None |
| 276 | return result |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 277 | |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 278 | class TelemetryResult(Result): |
| 279 | |
| 280 | def __init__(self, logger, label): |
| 281 | super(TelemetryResult, self).__init__(logger, label) |
| 282 | |
| 283 | def _PopulateFromRun(self, out, err, retval): |
| 284 | self.out = out |
| 285 | self.err = err |
| 286 | self.retval = retval |
| 287 | |
| 288 | self._ProcessResults() |
| 289 | |
| 290 | def _ProcessResults(self): |
| 291 | # The output is: |
| 292 | # url,average_commit_time (ms),... |
| 293 | # www.google.com,33.4,21.2,... |
| 294 | # We need to convert to this format: |
| 295 | # {"www.google.com:average_commit_time (ms)": "33.4", |
| 296 | # "www.google.com:...": "21.2"} |
| 297 | |
| 298 | lines = self.out.splitlines() |
| 299 | self.keyvals = {} |
| 300 | |
| 301 | if not lines: |
| 302 | return |
| 303 | labels = lines[0].split(",") |
| 304 | for line in lines[1:]: |
| 305 | fields = line.split(",") |
| 306 | if (len(fields) != len(labels)): |
| 307 | continue |
| 308 | for i in range(1, len(labels)): |
| 309 | key = "%s %s" % (fields[0], labels[i]) |
| 310 | value = fields[i] |
| 311 | self.keyvals[key] = value |
| 312 | self.keyvals["retval"] = self.retval |
| 313 | |
| 314 | def _PopulateFromCacheDir(self, cache_dir): |
| 315 | with open(os.path.join(cache_dir, RESULTS_FILE), "r") as f: |
| 316 | self.out = pickle.load(f) |
| 317 | self.err = pickle.load(f) |
| 318 | self.retval = pickle.load(f) |
| 319 | self._ProcessResults() |
| 320 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 321 | |
| 322 | class CacheConditions(object): |
| 323 | # Cache hit only if the result file exists. |
| 324 | CACHE_FILE_EXISTS = 0 |
| 325 | |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 326 | # Cache hit if the checksum of cpuinfo and totalmem of |
| 327 | # the cached result and the new run match. |
| 328 | MACHINES_MATCH = 1 |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 329 | |
| 330 | # Cache hit if the image checksum of the cached result and the new run match. |
| 331 | CHECKSUMS_MATCH = 2 |
| 332 | |
| 333 | # Cache hit only if the cached result was successful |
| 334 | RUN_SUCCEEDED = 3 |
| 335 | |
| 336 | # Never a cache hit. |
| 337 | FALSE = 4 |
| 338 | |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 339 | # Cache hit if the image path matches the cached image path. |
| 340 | IMAGE_PATH_MATCH = 5 |
| 341 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 342 | # Cache hit if the uuid of hard disk mataches the cached one |
| 343 | |
| 344 | SAME_MACHINE_MATCH = 6 |
| 345 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 346 | |
| 347 | class ResultsCache(object): |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 348 | """ This class manages the key of the cached runs without worrying about what |
| 349 | is exactly stored (value). The value generation is handled by the Results |
| 350 | class. |
| 351 | """ |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 352 | CACHE_VERSION = 6 |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 353 | |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 354 | def Init(self, chromeos_image, chromeos_root, test_name, iteration, |
| 355 | test_args, machine_manager, board, cache_conditions, |
| 356 | logger_to_use, label, share_users, suite): |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 357 | self.chromeos_image = chromeos_image |
| 358 | self.chromeos_root = chromeos_root |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 359 | self.test_name = test_name |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 360 | self.iteration = iteration |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 361 | self.test_args = test_args, |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 362 | self.board = board |
| 363 | self.cache_conditions = cache_conditions |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 364 | self.machine_manager = machine_manager |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 365 | self._logger = logger_to_use |
| 366 | self._ce = command_executer.GetCommandExecuter(self._logger) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 367 | self.label = label |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 368 | self.share_users = share_users |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 369 | self.suite = suite |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 370 | |
| 371 | def _GetCacheDirForRead(self): |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 372 | matching_dirs = [] |
| 373 | for glob_path in self._FormCacheDir(self._GetCacheKeyList(True)): |
| 374 | matching_dirs += glob.glob(glob_path) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 375 | |
| 376 | if matching_dirs: |
| 377 | # Cache file found. |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 378 | return matching_dirs[0] |
| 379 | else: |
| 380 | return None |
| 381 | |
| 382 | def _GetCacheDirForWrite(self): |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 383 | return self._FormCacheDir(self._GetCacheKeyList(False))[0] |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 384 | |
| 385 | def _FormCacheDir(self, list_of_strings): |
| 386 | cache_key = " ".join(list_of_strings) |
Ahmad Sharif | ee988c8 | 2012-05-14 17:11:56 -0700 | [diff] [blame] | 387 | cache_dir = misc.GetFilenameFromString(cache_key) |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 388 | if self.label.cache_dir: |
| 389 | cache_home = os.path.abspath(os.path.expanduser(self.label.cache_dir)) |
| 390 | cache_path = [os.path.join(cache_home, cache_dir)] |
| 391 | else: |
| 392 | cache_path = [os.path.join(SCRATCH_DIR, cache_dir)] |
| 393 | |
| 394 | for i in [x.strip() for x in self.share_users.split(",")]: |
| 395 | path = SCRATCH_BASE % i |
| 396 | cache_path.append(os.path.join(path, cache_dir)) |
| 397 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 398 | return cache_path |
| 399 | |
| 400 | def _GetCacheKeyList(self, read): |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 401 | if read and CacheConditions.MACHINES_MATCH not in self.cache_conditions: |
| 402 | machine_checksum = "*" |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 403 | else: |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 404 | machine_checksum = self.machine_manager.machine_checksum[self.label.name] |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 405 | if read and CacheConditions.CHECKSUMS_MATCH not in self.cache_conditions: |
| 406 | checksum = "*" |
| 407 | else: |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 408 | checksum = ImageChecksummer().Checksum(self.label) |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 409 | |
| 410 | if read and CacheConditions.IMAGE_PATH_MATCH not in self.cache_conditions: |
| 411 | image_path_checksum = "*" |
| 412 | else: |
| 413 | image_path_checksum = hashlib.md5(self.chromeos_image).hexdigest() |
| 414 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 415 | if read and CacheConditions.SAME_MACHINE_MATCH not in self.cache_conditions: |
| 416 | machine_id_checksum = "*" |
| 417 | else: |
| 418 | for machine in self.machine_manager.GetMachines(self.label): |
| 419 | if machine.name == self.label.remote[0]: |
| 420 | machine_id_checksum = machine.machine_id_checksum |
| 421 | break |
| 422 | |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 423 | test_args_checksum = hashlib.md5( |
| 424 | "".join(self.test_args)).hexdigest() |
Ahmad Sharif | fd356fb | 2012-05-07 12:02:16 -0700 | [diff] [blame] | 425 | return (image_path_checksum, |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 426 | self.test_name, str(self.iteration), |
| 427 | test_args_checksum, |
Ahmad Sharif | 92ab7af | 2012-03-01 18:03:46 -0800 | [diff] [blame] | 428 | checksum, |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 429 | machine_checksum, |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 430 | machine_id_checksum, |
Ahmad Sharif | 92ab7af | 2012-03-01 18:03:46 -0800 | [diff] [blame] | 431 | str(self.CACHE_VERSION)) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 432 | |
| 433 | def ReadResult(self): |
| 434 | if CacheConditions.FALSE in self.cache_conditions: |
| 435 | return None |
| 436 | cache_dir = self._GetCacheDirForRead() |
| 437 | |
| 438 | if not cache_dir: |
| 439 | return None |
| 440 | |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 441 | if not os.path.isdir(cache_dir): |
| 442 | return None |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 443 | |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 444 | self._logger.LogOutput("Trying to read from cache dir: %s" % cache_dir) |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 445 | result = Result.CreateFromCacheHit(self._logger, |
| 446 | self.label, |
| 447 | cache_dir, |
| 448 | self.suite) |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 449 | if not result: |
| 450 | return None |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 451 | |
Ahmad Sharif | 5ae8a5c | 2012-05-18 10:59:51 -0700 | [diff] [blame] | 452 | if (result.retval == 0 or |
| 453 | CacheConditions.RUN_SUCCEEDED not in self.cache_conditions): |
| 454 | return result |
| 455 | |
| 456 | return None |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 457 | |
| 458 | def StoreResult(self, result): |
| 459 | cache_dir = self._GetCacheDirForWrite() |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 460 | result.StoreToCacheDir(cache_dir, self.machine_manager) |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 461 | |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 462 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 463 | class MockResultsCache(ResultsCache): |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 464 | def Init(self, *args): |
| 465 | pass |
| 466 | |
| 467 | def ReadResult(self): |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 468 | return None |
Ahmad Sharif | 0dcbc4b | 2012-02-02 16:37:18 -0800 | [diff] [blame] | 469 | |
| 470 | def StoreResult(self, result): |
| 471 | pass |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 472 | |
| 473 | |
| 474 | class MockResult(Result): |
| 475 | def _PopulateFromRun(self, out, err, retval): |
| 476 | self.out = out |
| 477 | self.err = err |
| 478 | self.retval = retval |
| 479 | |