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