Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 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. |
| 6 | |
| 7 | import os |
| 8 | import time |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 9 | import shlex |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 10 | |
| 11 | from utils import command_executer |
| 12 | |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 13 | TEST_THAT_PATH = '/usr/bin/test_that' |
| 14 | CHROME_MOUNT_DIR = '/tmp/chrome_root' |
| 15 | |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 16 | def GetProfilerArgs (benchmark, profiler_args): |
| 17 | # Remove "--" from in front of profiler args. |
| 18 | args_list = shlex.split(profiler_args) |
| 19 | new_list = [] |
| 20 | for arg in args_list: |
| 21 | if arg[0:2] == '--': |
| 22 | arg = arg[2:] |
| 23 | new_list.append(arg) |
| 24 | args_list = new_list |
| 25 | |
| 26 | # Remove "perf_options=" from middle of profiler args. |
| 27 | new_list = [] |
| 28 | for arg in args_list: |
| 29 | idx = arg.find("perf_options=") |
| 30 | if idx != -1: |
| 31 | prefix = arg[0:idx] |
| 32 | suffix = arg[idx + len("perf_options=") + 1 : -1] |
| 33 | new_arg = prefix + "'" + suffix + "'" |
| 34 | new_list.append(new_arg) |
| 35 | else: |
| 36 | new_list.append(arg) |
| 37 | args_list = new_list |
| 38 | |
| 39 | return " ".join(args_list) |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 40 | |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 41 | |
| 42 | class SuiteRunner(object): |
| 43 | """ This defines the interface from crosperf to test script. |
| 44 | """ |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 45 | |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 46 | def __init__(self, logger_to_use=None): |
| 47 | self._logger = logger_to_use |
| 48 | self._ce = command_executer.GetCommandExecuter(self._logger) |
| 49 | self._ct = command_executer.CommandTerminator() |
| 50 | |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 51 | def Run(self, machine, label, benchmark, test_args, profiler_args): |
Luis Lozano | 53c88e9 | 2013-10-08 15:15:48 -0700 | [diff] [blame] | 52 | self.PinGovernorExecutionFrequencies(machine, label.chromeos_root) |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 53 | if benchmark.suite == "telemetry": |
| 54 | return self.Telemetry_Run(machine, label, benchmark) |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 55 | elif benchmark.suite == "telemetry_Crosperf": |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 56 | return self.Telemetry_Crosperf_Run(machine, label, benchmark, |
| 57 | test_args, profiler_args) |
Caroline Tice | b47bff4 | 2013-08-19 15:59:02 -0700 | [diff] [blame] | 58 | elif benchmark.use_test_that: |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 59 | return self.Test_That_Run(machine, label, benchmark, test_args, |
| 60 | profiler_args) |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 61 | else: |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 62 | return self.Pyauto_Run(machine, label, benchmark, test_args, |
| 63 | profiler_args) |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 64 | |
Luis Lozano | 53c88e9 | 2013-10-08 15:15:48 -0700 | [diff] [blame] | 65 | def GetHighestStaticFrequency(self, machine_name, chromeos_root): |
| 66 | """ Gets the highest static frequency for the specified machine |
| 67 | """ |
| 68 | get_avail_freqs = ("cat /sys/devices/system/cpu/cpu0/cpufreq/" |
| 69 | "scaling_available_frequencies") |
| 70 | ret, freqs_str, _ = self._ce.CrosRunCommand( |
| 71 | get_avail_freqs, return_output=True, machine=machine_name, |
| 72 | chromeos_root=chromeos_root) |
| 73 | self._logger.LogFatalIf(ret, "Could not get available frequencies " |
| 74 | "from machine: %s" % machine_name) |
| 75 | freqs = freqs_str.split() |
| 76 | # The dynamic frequency ends with a "1000". So, ignore it if found. |
| 77 | if freqs[0].endswith("1000"): |
| 78 | return freqs[1] |
| 79 | else: |
| 80 | return freqs[0] |
| 81 | |
| 82 | def PinGovernorExecutionFrequencies(self, machine_name, chromeos_root): |
| 83 | """ Set min and max frequencies to max static frequency |
| 84 | """ |
| 85 | highest_freq = self.GetHighestStaticFrequency(machine_name, chromeos_root) |
| 86 | BASH_FOR = "for f in {list}; do {body}; done" |
| 87 | CPUFREQ_DIRS = "/sys/devices/system/cpu/cpu*/cpufreq/" |
| 88 | change_max_freq = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_max_freq", |
| 89 | body="echo %s > $f" % highest_freq) |
| 90 | change_min_freq = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_min_freq", |
| 91 | body="echo %s > $f" % highest_freq) |
| 92 | change_perf_gov = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_governor", |
| 93 | body="echo performance > $f") |
| 94 | ret = self._ce.CrosRunCommand(" && ".join(("set -e ", |
| 95 | change_max_freq, |
| 96 | change_min_freq, |
| 97 | change_perf_gov)), |
| 98 | machine=machine_name, |
| 99 | chromeos_root=chromeos_root) |
| 100 | self._logger.LogFatalIf(ret, "Could not pin frequencies on machine: %s" |
| 101 | % machine_name) |
| 102 | |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 103 | def RebootMachine(self, machine_name, chromeos_root): |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 104 | command = "reboot && exit" |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 105 | self._ce.CrosRunCommand(command, machine=machine_name, |
| 106 | chromeos_root=chromeos_root) |
| 107 | time.sleep(60) |
Luis Lozano | 53c88e9 | 2013-10-08 15:15:48 -0700 | [diff] [blame] | 108 | # Whenever we reboot the machine, we need to restore the governor settings. |
| 109 | self.PinGovernorExecutionFrequencies(machine_name, chromeos_root) |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 110 | |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 111 | def Pyauto_Run(self, machine, label, benchmark, test_args, profiler_args): |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 112 | """Run the run_remote_test.""" |
| 113 | options = "" |
| 114 | if label.board: |
| 115 | options += " --board=%s" % label.board |
| 116 | if test_args: |
| 117 | options += " %s" % test_args |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 118 | if profiler_args: |
| 119 | options += " %s" % profiler_args |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 120 | command = "rm -rf /usr/local/autotest/results/*" |
| 121 | self._ce.CrosRunCommand(command, machine=machine, username="root", |
| 122 | chromeos_root=label.chromeos_root) |
| 123 | |
Luis Lozano | 53c88e9 | 2013-10-08 15:15:48 -0700 | [diff] [blame] | 124 | # We do this because PyAuto tests leave the machine in weird states. |
| 125 | # Rebooting between iterations has proven to help with this. |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 126 | self.RebootMachine(machine, label.chromeos_root) |
| 127 | |
Caroline Tice | f8b3a5a | 2013-09-25 10:45:57 -0700 | [diff] [blame] | 128 | command = ("./run_remote_tests.sh --use_emerged --remote=%s %s %s" % |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 129 | (machine, options, benchmark.test_name)) |
| 130 | return self._ce.ChrootRunCommand(label.chromeos_root, |
| 131 | command, |
| 132 | True, |
| 133 | self._ct) |
| 134 | |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 135 | def Test_That_Run(self, machine, label, benchmark, test_args, profiler_args): |
Caroline Tice | b47bff4 | 2013-08-19 15:59:02 -0700 | [diff] [blame] | 136 | """Run the test_that test..""" |
| 137 | options = "" |
| 138 | if label.board: |
| 139 | options += " --board=%s" % label.board |
| 140 | if test_args: |
| 141 | options += " %s" % test_args |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 142 | if profiler_args: |
| 143 | self._logger.LogError("test_that does not support profiler.") |
Caroline Tice | b47bff4 | 2013-08-19 15:59:02 -0700 | [diff] [blame] | 144 | command = "rm -rf /usr/local/autotest/results/*" |
| 145 | self._ce.CrosRunCommand(command, machine=machine, username="root", |
| 146 | chromeos_root=label.chromeos_root) |
| 147 | |
Luis Lozano | 53c88e9 | 2013-10-08 15:15:48 -0700 | [diff] [blame] | 148 | # We do this because PyAuto tests leave the machine in weird states. |
| 149 | # Rebooting between iterations has proven to help with this. |
Caroline Tice | b47bff4 | 2013-08-19 15:59:02 -0700 | [diff] [blame] | 150 | self.RebootMachine(machine, label.chromeos_root) |
| 151 | |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 152 | command = ("%s %s %s %s" % |
| 153 | (TEST_THAT_PATH, options, machine, benchmark.test_name)) |
Caroline Tice | b47bff4 | 2013-08-19 15:59:02 -0700 | [diff] [blame] | 154 | return self._ce.ChrootRunCommand(label.chromeos_root, |
| 155 | command, |
| 156 | True, |
| 157 | self._ct) |
| 158 | |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 159 | |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 160 | def Telemetry_Crosperf_Run (self, machine, label, benchmark, test_args, |
| 161 | profiler_args): |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 162 | if not os.path.isdir(label.chrome_src): |
| 163 | self._logger.LogFatal("Cannot find chrome src dir to" |
cmtice | 5f3ccbd | 2013-11-20 16:36:04 -0800 | [diff] [blame] | 164 | " run telemetry: %s" % label.chrome_src) |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 165 | |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 166 | profiler_args = GetProfilerArgs (benchmark, profiler_args) |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 167 | chrome_root_options = "" |
| 168 | |
| 169 | # If chrome_src is outside the chroot, mount it when entering the |
| 170 | # chroot. |
| 171 | if label.chrome_src.find(label.chromeos_root) == -1: |
| 172 | chrome_root_options = (" --chrome_root={0} --chrome_root_mount={1} " |
| 173 | " FEATURES=\"-usersandbox\" " |
| 174 | "CHROME_ROOT={2}".format(label.chrome_src, |
| 175 | CHROME_MOUNT_DIR, |
| 176 | CHROME_MOUNT_DIR)) |
| 177 | |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 178 | args_string = "" |
| 179 | if test_args: |
| 180 | # Strip double quotes off args (so we can wrap them in single |
| 181 | # quotes, to pass through to Telemetry). |
| 182 | if test_args[0] == '"' and test_args[-1] == '"': |
| 183 | test_args = test_args[1:-1] |
| 184 | args_string = "test_args='%s'" % test_args |
| 185 | cmd = ('{0} --board={1} --args="{2} test={3} ' |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 186 | '{4}" {5} telemetry_Crosperf'.format(TEST_THAT_PATH, |
| 187 | label.board, |
cmtice | e5a746f | 2013-11-25 14:57:10 -0500 | [diff] [blame] | 188 | args_string, |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 189 | benchmark.test_name, |
| 190 | profiler_args, |
| 191 | machine)) |
| 192 | return self._ce.ChrootRunCommand (label.chromeos_root, |
| 193 | cmd, |
| 194 | return_output=True, |
| 195 | command_terminator=self._ct, |
| 196 | cros_sdk_options=chrome_root_options) |
| 197 | |
| 198 | |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 199 | def Telemetry_Run(self, machine, label, benchmark): |
| 200 | if not os.path.isdir(label.chrome_src): |
Caroline Tice | 6aa8528 | 2013-08-06 16:02:04 -0700 | [diff] [blame] | 201 | self._logger.LogFatal("Cannot find chrome src dir to" |
| 202 | " run telemetry.") |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 203 | rsa_key = os.path.join(label.chromeos_root, |
| 204 | "src/scripts/mod_for_test_scripts/ssh_keys/testing_rsa") |
| 205 | |
| 206 | cmd = ("cd {0} && " |
Yunlian Jiang | fefa5c0 | 2013-07-02 15:47:02 -0700 | [diff] [blame] | 207 | "./tools/perf/run_measurement " |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 208 | "--browser=cros-chrome " |
| 209 | "--output-format=csv " |
| 210 | "--remote={1} " |
| 211 | "--identity {2} " |
| 212 | "{3} {4}".format(label.chrome_src, machine, |
| 213 | rsa_key, |
| 214 | benchmark.test_name, |
| 215 | benchmark.test_args)) |
| 216 | return self._ce.RunCommand(cmd, return_output=True, |
| 217 | print_to_console=False) |
| 218 | |
| 219 | def Terminate(self): |
| 220 | self._ct.Terminate() |
| 221 | |
| 222 | |
| 223 | class MockSuiteRunner(object): |
| 224 | def __init__(self): |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 225 | self._true = True |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 226 | |
Caroline Tice | 9277419 | 2013-09-10 16:29:18 -0700 | [diff] [blame] | 227 | def Run(self, *_args): |
| 228 | if self._true: |
| 229 | return ["", "", 0] |
| 230 | else: |
| 231 | return ["", "", 0] |