blob: 00e6e51e0e218724c14b36b1f8110103fed4bfdb [file] [log] [blame]
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -07001#!/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
7import os
8import time
cmticee5a746f2013-11-25 14:57:10 -05009import shlex
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070010
11from utils import command_executer
12
Caroline Tice92774192013-09-10 16:29:18 -070013TEST_THAT_PATH = '/usr/bin/test_that'
14CHROME_MOUNT_DIR = '/tmp/chrome_root'
15
cmticee5a746f2013-11-25 14:57:10 -050016def 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 Tice92774192013-09-10 16:29:18 -070040
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070041
42class SuiteRunner(object):
43 """ This defines the interface from crosperf to test script.
44 """
Caroline Tice92774192013-09-10 16:29:18 -070045
cmtice13909242014-03-11 13:38:07 -070046 def __init__(self, logger_to_use=None, log_level="verbose"):
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070047 self._logger = logger_to_use
cmtice13909242014-03-11 13:38:07 -070048 self.log_level = log_level
49 self._ce = command_executer.GetCommandExecuter(self._logger,
50 log_level=self.log_level)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070051 self._ct = command_executer.CommandTerminator()
52
cmticee5a746f2013-11-25 14:57:10 -050053 def Run(self, machine, label, benchmark, test_args, profiler_args):
Luis Lozano53c88e92013-10-08 15:15:48 -070054 self.PinGovernorExecutionFrequencies(machine, label.chromeos_root)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070055 if benchmark.suite == "telemetry":
56 return self.Telemetry_Run(machine, label, benchmark)
Caroline Tice92774192013-09-10 16:29:18 -070057 elif benchmark.suite == "telemetry_Crosperf":
cmticee5a746f2013-11-25 14:57:10 -050058 return self.Telemetry_Crosperf_Run(machine, label, benchmark,
59 test_args, profiler_args)
Caroline Ticeb47bff42013-08-19 15:59:02 -070060 elif benchmark.use_test_that:
cmticee5a746f2013-11-25 14:57:10 -050061 return self.Test_That_Run(machine, label, benchmark, test_args,
62 profiler_args)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070063 else:
cmticee5a746f2013-11-25 14:57:10 -050064 return self.Pyauto_Run(machine, label, benchmark, test_args,
65 profiler_args)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070066
Luis Lozano53c88e92013-10-08 15:15:48 -070067 def GetHighestStaticFrequency(self, machine_name, chromeos_root):
68 """ Gets the highest static frequency for the specified machine
69 """
Han Shenfd0b1782014-02-12 15:13:01 -080070 get_avail_freqs = ("cd /sys/devices/system/cpu/cpu0/cpufreq/; "
71 "if [[ -e scaling_available_frequencies ]]; then "
72 " cat scaling_available_frequencies; "
73 "else "
74 " cat scaling_max_freq ; "
75 "fi")
Luis Lozano53c88e92013-10-08 15:15:48 -070076 ret, freqs_str, _ = self._ce.CrosRunCommand(
77 get_avail_freqs, return_output=True, machine=machine_name,
78 chromeos_root=chromeos_root)
79 self._logger.LogFatalIf(ret, "Could not get available frequencies "
80 "from machine: %s" % machine_name)
81 freqs = freqs_str.split()
Han Shenfd0b1782014-02-12 15:13:01 -080082 ## When there is no scaling_available_frequencies file,
83 ## we have only 1 choice.
84 if len(freqs) == 1:
85 return freqs[0]
Luis Lozano53c88e92013-10-08 15:15:48 -070086 # The dynamic frequency ends with a "1000". So, ignore it if found.
87 if freqs[0].endswith("1000"):
88 return freqs[1]
89 else:
90 return freqs[0]
91
92 def PinGovernorExecutionFrequencies(self, machine_name, chromeos_root):
93 """ Set min and max frequencies to max static frequency
94 """
95 highest_freq = self.GetHighestStaticFrequency(machine_name, chromeos_root)
96 BASH_FOR = "for f in {list}; do {body}; done"
97 CPUFREQ_DIRS = "/sys/devices/system/cpu/cpu*/cpufreq/"
98 change_max_freq = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_max_freq",
99 body="echo %s > $f" % highest_freq)
100 change_min_freq = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_min_freq",
101 body="echo %s > $f" % highest_freq)
102 change_perf_gov = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_governor",
103 body="echo performance > $f")
cmtice13909242014-03-11 13:38:07 -0700104 if self.log_level == "average":
105 self._logger.LogOutput("Pinning governor execution frequencies for %s"
106 % machine_name)
Luis Lozano53c88e92013-10-08 15:15:48 -0700107 ret = self._ce.CrosRunCommand(" && ".join(("set -e ",
108 change_max_freq,
109 change_min_freq,
110 change_perf_gov)),
111 machine=machine_name,
112 chromeos_root=chromeos_root)
113 self._logger.LogFatalIf(ret, "Could not pin frequencies on machine: %s"
114 % machine_name)
115
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700116 def RebootMachine(self, machine_name, chromeos_root):
Caroline Tice92774192013-09-10 16:29:18 -0700117 command = "reboot && exit"
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700118 self._ce.CrosRunCommand(command, machine=machine_name,
119 chromeos_root=chromeos_root)
120 time.sleep(60)
Luis Lozano53c88e92013-10-08 15:15:48 -0700121 # Whenever we reboot the machine, we need to restore the governor settings.
122 self.PinGovernorExecutionFrequencies(machine_name, chromeos_root)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700123
cmticee5a746f2013-11-25 14:57:10 -0500124 def Pyauto_Run(self, machine, label, benchmark, test_args, profiler_args):
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700125 """Run the run_remote_test."""
126 options = ""
127 if label.board:
128 options += " --board=%s" % label.board
129 if test_args:
130 options += " %s" % test_args
cmticee5a746f2013-11-25 14:57:10 -0500131 if profiler_args:
132 options += " %s" % profiler_args
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700133 command = "rm -rf /usr/local/autotest/results/*"
134 self._ce.CrosRunCommand(command, machine=machine, username="root",
135 chromeos_root=label.chromeos_root)
136
Luis Lozano53c88e92013-10-08 15:15:48 -0700137 # We do this because PyAuto tests leave the machine in weird states.
138 # Rebooting between iterations has proven to help with this.
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700139 self.RebootMachine(machine, label.chromeos_root)
140
Caroline Ticef8b3a5a2013-09-25 10:45:57 -0700141 command = ("./run_remote_tests.sh --use_emerged --remote=%s %s %s" %
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700142 (machine, options, benchmark.test_name))
cmtice13909242014-03-11 13:38:07 -0700143 if self.log_level != "verbose":
144 self._logger.LogOutput("Running test.")
145 self._logger.LogOutput("CMD: %s" % command)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700146 return self._ce.ChrootRunCommand(label.chromeos_root,
147 command,
148 True,
149 self._ct)
150
cmticee5a746f2013-11-25 14:57:10 -0500151 def Test_That_Run(self, machine, label, benchmark, test_args, profiler_args):
Caroline Ticeb47bff42013-08-19 15:59:02 -0700152 """Run the test_that test.."""
153 options = ""
154 if label.board:
155 options += " --board=%s" % label.board
156 if test_args:
157 options += " %s" % test_args
cmticee5a746f2013-11-25 14:57:10 -0500158 if profiler_args:
159 self._logger.LogError("test_that does not support profiler.")
Caroline Ticeb47bff42013-08-19 15:59:02 -0700160 command = "rm -rf /usr/local/autotest/results/*"
161 self._ce.CrosRunCommand(command, machine=machine, username="root",
162 chromeos_root=label.chromeos_root)
163
Luis Lozano53c88e92013-10-08 15:15:48 -0700164 # We do this because PyAuto tests leave the machine in weird states.
165 # Rebooting between iterations has proven to help with this.
Caroline Ticeb47bff42013-08-19 15:59:02 -0700166 self.RebootMachine(machine, label.chromeos_root)
167
Caroline Tice92774192013-09-10 16:29:18 -0700168 command = ("%s %s %s %s" %
169 (TEST_THAT_PATH, options, machine, benchmark.test_name))
cmtice13909242014-03-11 13:38:07 -0700170 if self.log_level != "verbose":
171 self._logger.LogOutput("Running test.")
172 self._logger.LogOutput("CMD: %s" % command)
Caroline Ticeb47bff42013-08-19 15:59:02 -0700173 return self._ce.ChrootRunCommand(label.chromeos_root,
174 command,
175 True,
176 self._ct)
177
Caroline Tice92774192013-09-10 16:29:18 -0700178
cmticee5a746f2013-11-25 14:57:10 -0500179 def Telemetry_Crosperf_Run (self, machine, label, benchmark, test_args,
180 profiler_args):
Caroline Tice92774192013-09-10 16:29:18 -0700181 if not os.path.isdir(label.chrome_src):
182 self._logger.LogFatal("Cannot find chrome src dir to"
cmtice5f3ccbd2013-11-20 16:36:04 -0800183 " run telemetry: %s" % label.chrome_src)
Caroline Tice92774192013-09-10 16:29:18 -0700184
cmticee5a746f2013-11-25 14:57:10 -0500185 profiler_args = GetProfilerArgs (benchmark, profiler_args)
Caroline Tice92774192013-09-10 16:29:18 -0700186 chrome_root_options = ""
187
188 # If chrome_src is outside the chroot, mount it when entering the
189 # chroot.
190 if label.chrome_src.find(label.chromeos_root) == -1:
191 chrome_root_options = (" --chrome_root={0} --chrome_root_mount={1} "
192 " FEATURES=\"-usersandbox\" "
193 "CHROME_ROOT={2}".format(label.chrome_src,
194 CHROME_MOUNT_DIR,
195 CHROME_MOUNT_DIR))
196
cmticee5a746f2013-11-25 14:57:10 -0500197 args_string = ""
198 if test_args:
199 # Strip double quotes off args (so we can wrap them in single
200 # quotes, to pass through to Telemetry).
201 if test_args[0] == '"' and test_args[-1] == '"':
202 test_args = test_args[1:-1]
203 args_string = "test_args='%s'" % test_args
204 cmd = ('{0} --board={1} --args="{2} test={3} '
Caroline Tice92774192013-09-10 16:29:18 -0700205 '{4}" {5} telemetry_Crosperf'.format(TEST_THAT_PATH,
206 label.board,
cmticee5a746f2013-11-25 14:57:10 -0500207 args_string,
Caroline Tice92774192013-09-10 16:29:18 -0700208 benchmark.test_name,
209 profiler_args,
210 machine))
cmtice13909242014-03-11 13:38:07 -0700211 if self.log_level != "verbose":
212 self._logger.LogOutput("Running test.")
213 self._logger.LogOutput("CMD: %s" % cmd)
Caroline Tice92774192013-09-10 16:29:18 -0700214 return self._ce.ChrootRunCommand (label.chromeos_root,
215 cmd,
216 return_output=True,
217 command_terminator=self._ct,
218 cros_sdk_options=chrome_root_options)
219
220
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700221 def Telemetry_Run(self, machine, label, benchmark):
222 if not os.path.isdir(label.chrome_src):
Caroline Tice6aa85282013-08-06 16:02:04 -0700223 self._logger.LogFatal("Cannot find chrome src dir to"
224 " run telemetry.")
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700225 rsa_key = os.path.join(label.chromeos_root,
226 "src/scripts/mod_for_test_scripts/ssh_keys/testing_rsa")
227
228 cmd = ("cd {0} && "
Yunlian Jiangfefa5c02013-07-02 15:47:02 -0700229 "./tools/perf/run_measurement "
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700230 "--browser=cros-chrome "
231 "--output-format=csv "
232 "--remote={1} "
233 "--identity {2} "
234 "{3} {4}".format(label.chrome_src, machine,
235 rsa_key,
236 benchmark.test_name,
237 benchmark.test_args))
cmtice13909242014-03-11 13:38:07 -0700238 if self.log_level != "verbose":
239 self._logger.LogOutput("Running test.")
240 self._logger.LogOutput("CMD: %s" % cmd)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700241 return self._ce.RunCommand(cmd, return_output=True,
242 print_to_console=False)
243
244 def Terminate(self):
245 self._ct.Terminate()
246
247
248class MockSuiteRunner(object):
249 def __init__(self):
Caroline Tice92774192013-09-10 16:29:18 -0700250 self._true = True
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700251
Caroline Tice92774192013-09-10 16:29:18 -0700252 def Run(self, *_args):
253 if self._true:
254 return ["", "", 0]
255 else:
256 return ["", "", 0]