blob: afec6c85bc21b2774da96e12b8386b677e0e4e38 [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
Caroline Tice7057cf62015-12-10 12:09:40 -080012import test_flag
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070013
Caroline Tice92774192013-09-10 16:29:18 -070014TEST_THAT_PATH = '/usr/bin/test_that'
15CHROME_MOUNT_DIR = '/tmp/chrome_root'
16
cmtice4f0309d2014-06-15 13:36:05 -070017def GetProfilerArgs (profiler_args):
cmticee5a746f2013-11-25 14:57:10 -050018 # Remove "--" from in front of profiler args.
19 args_list = shlex.split(profiler_args)
20 new_list = []
21 for arg in args_list:
22 if arg[0:2] == '--':
23 arg = arg[2:]
24 new_list.append(arg)
25 args_list = new_list
26
27 # Remove "perf_options=" from middle of profiler args.
28 new_list = []
29 for arg in args_list:
30 idx = arg.find("perf_options=")
31 if idx != -1:
32 prefix = arg[0:idx]
33 suffix = arg[idx + len("perf_options=") + 1 : -1]
34 new_arg = prefix + "'" + suffix + "'"
35 new_list.append(new_arg)
36 else:
37 new_list.append(arg)
38 args_list = new_list
39
40 return " ".join(args_list)
Caroline Tice92774192013-09-10 16:29:18 -070041
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070042
43class SuiteRunner(object):
44 """ This defines the interface from crosperf to test script.
45 """
Caroline Tice92774192013-09-10 16:29:18 -070046
cmtice4f0309d2014-06-15 13:36:05 -070047 def __init__(self, logger_to_use=None, log_level="verbose", cmd_exec=None,
48 cmd_term=None):
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070049 self._logger = logger_to_use
cmtice13909242014-03-11 13:38:07 -070050 self.log_level = log_level
cmtice4f0309d2014-06-15 13:36:05 -070051 self._ce = cmd_exec or command_executer.GetCommandExecuter(self._logger,
cmtice13909242014-03-11 13:38:07 -070052 log_level=self.log_level)
cmtice4f0309d2014-06-15 13:36:05 -070053 self._ct = cmd_term or command_executer.CommandTerminator()
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070054
cmticee5a746f2013-11-25 14:57:10 -050055 def Run(self, machine, label, benchmark, test_args, profiler_args):
Luis Lozanodf762222015-05-19 12:22:37 -070056 for i in range(0, benchmark.retries + 1):
57 self.PinGovernorExecutionFrequencies(machine, label.chromeos_root)
58 if benchmark.suite == "telemetry":
59 ret_tup = self.Telemetry_Run(machine, label, benchmark, profiler_args)
60 elif benchmark.suite == "telemetry_Crosperf":
61 ret_tup = self.Telemetry_Crosperf_Run(machine, label, benchmark,
62 test_args, profiler_args)
63 else:
64 ret_tup = self.Test_That_Run(machine, label, benchmark, test_args,
65 profiler_args)
66 if ret_tup[0] != 0:
67 self._logger.LogOutput("benchmark %s failed. Retries left: %s"
68 % (benchmark.name, benchmark.retries - i))
69 elif i > 0:
70 self._logger.LogOutput("benchmark %s succeded after %s retries"
71 % (benchmark.name, i))
72 break
73 else:
74 self._logger.LogOutput("benchmark %s succeded on first try"
75 % benchmark.name)
76 break
77 return ret_tup
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070078
Luis Lozano53c88e92013-10-08 15:15:48 -070079 def GetHighestStaticFrequency(self, machine_name, chromeos_root):
80 """ Gets the highest static frequency for the specified machine
81 """
Han Shenfd0b1782014-02-12 15:13:01 -080082 get_avail_freqs = ("cd /sys/devices/system/cpu/cpu0/cpufreq/; "
83 "if [[ -e scaling_available_frequencies ]]; then "
84 " cat scaling_available_frequencies; "
85 "else "
86 " cat scaling_max_freq ; "
87 "fi")
Luis Lozano53c88e92013-10-08 15:15:48 -070088 ret, freqs_str, _ = self._ce.CrosRunCommand(
89 get_avail_freqs, return_output=True, machine=machine_name,
90 chromeos_root=chromeos_root)
91 self._logger.LogFatalIf(ret, "Could not get available frequencies "
92 "from machine: %s" % machine_name)
93 freqs = freqs_str.split()
Luis Lozanod0b46122015-12-07 15:09:33 -080094 # We need to make sure that the frequencies are sorted in decreasing
95 # order
96 freqs.sort(key=int, reverse=True)
97
Han Shenfd0b1782014-02-12 15:13:01 -080098 ## When there is no scaling_available_frequencies file,
99 ## we have only 1 choice.
100 if len(freqs) == 1:
101 return freqs[0]
Luis Lozano53c88e92013-10-08 15:15:48 -0700102 # The dynamic frequency ends with a "1000". So, ignore it if found.
103 if freqs[0].endswith("1000"):
104 return freqs[1]
105 else:
106 return freqs[0]
107
108 def PinGovernorExecutionFrequencies(self, machine_name, chromeos_root):
109 """ Set min and max frequencies to max static frequency
110 """
111 highest_freq = self.GetHighestStaticFrequency(machine_name, chromeos_root)
112 BASH_FOR = "for f in {list}; do {body}; done"
113 CPUFREQ_DIRS = "/sys/devices/system/cpu/cpu*/cpufreq/"
114 change_max_freq = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_max_freq",
115 body="echo %s > $f" % highest_freq)
116 change_min_freq = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_min_freq",
117 body="echo %s > $f" % highest_freq)
118 change_perf_gov = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_governor",
119 body="echo performance > $f")
cmtice13909242014-03-11 13:38:07 -0700120 if self.log_level == "average":
121 self._logger.LogOutput("Pinning governor execution frequencies for %s"
122 % machine_name)
Luis Lozano53c88e92013-10-08 15:15:48 -0700123 ret = self._ce.CrosRunCommand(" && ".join(("set -e ",
124 change_max_freq,
125 change_min_freq,
126 change_perf_gov)),
127 machine=machine_name,
128 chromeos_root=chromeos_root)
129 self._logger.LogFatalIf(ret, "Could not pin frequencies on machine: %s"
130 % machine_name)
131
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700132 def RebootMachine(self, machine_name, chromeos_root):
Caroline Tice92774192013-09-10 16:29:18 -0700133 command = "reboot && exit"
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700134 self._ce.CrosRunCommand(command, machine=machine_name,
135 chromeos_root=chromeos_root)
136 time.sleep(60)
Luis Lozano53c88e92013-10-08 15:15:48 -0700137 # Whenever we reboot the machine, we need to restore the governor settings.
138 self.PinGovernorExecutionFrequencies(machine_name, chromeos_root)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700139
cmticee5a746f2013-11-25 14:57:10 -0500140 def Test_That_Run(self, machine, label, benchmark, test_args, profiler_args):
Caroline Ticeb47bff42013-08-19 15:59:02 -0700141 """Run the test_that test.."""
142 options = ""
143 if label.board:
144 options += " --board=%s" % label.board
145 if test_args:
146 options += " %s" % test_args
cmticee5a746f2013-11-25 14:57:10 -0500147 if profiler_args:
cmtice226e3e02014-04-27 22:28:42 -0700148 self._logger.LogFatal("test_that does not support profiler.")
Caroline Ticeb47bff42013-08-19 15:59:02 -0700149 command = "rm -rf /usr/local/autotest/results/*"
Luis Lozanobd447042015-10-01 14:24:27 -0700150 self._ce.CrosRunCommand(command, machine=machine,
Caroline Ticeb47bff42013-08-19 15:59:02 -0700151 chromeos_root=label.chromeos_root)
152
cmtice98a53692014-04-16 14:48:47 -0700153 # We do this because some tests leave the machine in weird states.
Luis Lozano53c88e92013-10-08 15:15:48 -0700154 # Rebooting between iterations has proven to help with this.
Caroline Ticeb47bff42013-08-19 15:59:02 -0700155 self.RebootMachine(machine, label.chromeos_root)
156
Caroline Tice8df44932015-12-02 10:58:53 -0800157 command = (("%s --autotest_dir ~/trunk/src/third_party/autotest/files --fast "
158 "%s %s %s") %
Caroline Tice92774192013-09-10 16:29:18 -0700159 (TEST_THAT_PATH, options, machine, benchmark.test_name))
cmtice13909242014-03-11 13:38:07 -0700160 if self.log_level != "verbose":
161 self._logger.LogOutput("Running test.")
162 self._logger.LogOutput("CMD: %s" % command)
Luis Lozano45b53c52015-09-30 11:36:27 -0700163 # Use --no-ns-pid so that cros_sdk does not create a different
164 # process namespace and we can kill process created easily by
165 # their process group.
Caroline Ticeb47bff42013-08-19 15:59:02 -0700166 return self._ce.ChrootRunCommand(label.chromeos_root,
167 command,
168 True,
Luis Lozano45b53c52015-09-30 11:36:27 -0700169 self._ct,
170 cros_sdk_options="--no-ns-pid")
Caroline Ticeb47bff42013-08-19 15:59:02 -0700171
cmtice146058f2015-07-24 14:04:12 -0700172 def RemoveTelemetryTempFile (self, machine, chromeos_root):
173 filename = "telemetry@%s" % machine
174 fullname = os.path.join (chromeos_root,
175 "chroot",
176 "tmp",
177 filename)
178 if os.path.exists(fullname):
179 os.remove(fullname)
Caroline Tice92774192013-09-10 16:29:18 -0700180
cmticee5a746f2013-11-25 14:57:10 -0500181 def Telemetry_Crosperf_Run (self, machine, label, benchmark, test_args,
182 profiler_args):
Caroline Tice92774192013-09-10 16:29:18 -0700183 if not os.path.isdir(label.chrome_src):
184 self._logger.LogFatal("Cannot find chrome src dir to"
cmtice5f3ccbd2013-11-20 16:36:04 -0800185 " run telemetry: %s" % label.chrome_src)
Caroline Tice92774192013-09-10 16:29:18 -0700186
cmtice146058f2015-07-24 14:04:12 -0700187 # Check for and remove temporary file that may have been left by
188 # previous telemetry runs (and which might prevent this run from
189 # working).
190 self.RemoveTelemetryTempFile (machine, label.chromeos_root)
191
Luis Lozano004bccb2015-03-20 18:19:58 -0700192 # For telemetry runs, we can use the autotest copy from the source
193 # location. No need to have one under /build/<board>.
194 autotest_dir_arg = '--autotest_dir ~/trunk/src/third_party/autotest/files'
195
cmtice4f0309d2014-06-15 13:36:05 -0700196 profiler_args = GetProfilerArgs (profiler_args)
Luis Lozano5d7e21d2015-03-20 17:57:36 -0700197 fast_arg = ""
198 if not profiler_args:
199 # --fast works unless we are doing profiling (autotest limitation).
200 # --fast avoids unnecessary copies of syslogs.
201 fast_arg = "--fast"
cmticee5a746f2013-11-25 14:57:10 -0500202 args_string = ""
203 if test_args:
204 # Strip double quotes off args (so we can wrap them in single
205 # quotes, to pass through to Telemetry).
206 if test_args[0] == '"' and test_args[-1] == '"':
207 test_args = test_args[1:-1]
208 args_string = "test_args='%s'" % test_args
Luis Lozano004bccb2015-03-20 18:19:58 -0700209
Ting-Yuan Huangbc2d3d12015-07-02 13:09:03 +0800210 cmd = ('{} {} {} --board={} --args="{} run_local={} test={} '
Luis Lozano5d7e21d2015-03-20 17:57:36 -0700211 '{}" {} telemetry_Crosperf'.format(TEST_THAT_PATH,
Luis Lozano004bccb2015-03-20 18:19:58 -0700212 autotest_dir_arg,
Luis Lozano5d7e21d2015-03-20 17:57:36 -0700213 fast_arg,
214 label.board,
215 args_string,
Ting-Yuan Huangbc2d3d12015-07-02 13:09:03 +0800216 benchmark.run_local,
Luis Lozano5d7e21d2015-03-20 17:57:36 -0700217 benchmark.test_name,
218 profiler_args,
219 machine))
220
Luis Lozano45b53c52015-09-30 11:36:27 -0700221 # Use --no-ns-pid so that cros_sdk does not create a different
222 # process namespace and we can kill process created easily by their
223 # process group.
224 chrome_root_options = ("--no-ns-pid "
225 "--chrome_root={} --chrome_root_mount={} "
226 "FEATURES=\"-usersandbox\" "
Luis Lozano5d7e21d2015-03-20 17:57:36 -0700227 "CHROME_ROOT={}".format(label.chrome_src,
228 CHROME_MOUNT_DIR,
229 CHROME_MOUNT_DIR))
cmtice13909242014-03-11 13:38:07 -0700230 if self.log_level != "verbose":
231 self._logger.LogOutput("Running test.")
232 self._logger.LogOutput("CMD: %s" % cmd)
Luis Lozano45b53c52015-09-30 11:36:27 -0700233 return self._ce.ChrootRunCommand(label.chromeos_root,
234 cmd,
235 return_output=True,
236 command_terminator=self._ct,
237 cros_sdk_options=chrome_root_options)
Caroline Tice92774192013-09-10 16:29:18 -0700238
239
cmtice226e3e02014-04-27 22:28:42 -0700240 def Telemetry_Run(self, machine, label, benchmark, profiler_args):
cmtice98a53692014-04-16 14:48:47 -0700241 telemetry_run_path = ""
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700242 if not os.path.isdir(label.chrome_src):
Caroline Tice6aa85282013-08-06 16:02:04 -0700243 self._logger.LogFatal("Cannot find chrome src dir to"
cmtice98a53692014-04-16 14:48:47 -0700244 " run telemetry.")
245 else:
246 telemetry_run_path = os.path.join(label.chrome_src, "src/tools/perf")
247 if not os.path.exists(telemetry_run_path):
248 self._logger.LogFatal("Cannot find %s directory." % telemetry_run_path)
249
cmtice226e3e02014-04-27 22:28:42 -0700250 if profiler_args:
251 self._logger.LogFatal("Telemetry does not support the perf profiler.")
252
cmtice146058f2015-07-24 14:04:12 -0700253 # Check for and remove temporary file that may have been left by
254 # previous telemetry runs (and which might prevent this run from
255 # working).
Caroline Tice7057cf62015-12-10 12:09:40 -0800256 if not test_flag.GetTestMode():
257 self.RemoveTelemetryTempFile (machine, label.chromeos_root)
cmtice146058f2015-07-24 14:04:12 -0700258
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700259 rsa_key = os.path.join(label.chromeos_root,
260 "src/scripts/mod_for_test_scripts/ssh_keys/testing_rsa")
261
262 cmd = ("cd {0} && "
cmtice98a53692014-04-16 14:48:47 -0700263 "./run_measurement "
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700264 "--browser=cros-chrome "
265 "--output-format=csv "
266 "--remote={1} "
267 "--identity {2} "
cmtice98a53692014-04-16 14:48:47 -0700268 "{3} {4}".format(telemetry_run_path, machine,
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700269 rsa_key,
270 benchmark.test_name,
271 benchmark.test_args))
cmtice13909242014-03-11 13:38:07 -0700272 if self.log_level != "verbose":
273 self._logger.LogOutput("Running test.")
274 self._logger.LogOutput("CMD: %s" % cmd)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700275 return self._ce.RunCommand(cmd, return_output=True,
276 print_to_console=False)
277
278 def Terminate(self):
279 self._ct.Terminate()
280
281
282class MockSuiteRunner(object):
283 def __init__(self):
Caroline Tice92774192013-09-10 16:29:18 -0700284 self._true = True
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700285
Caroline Tice92774192013-09-10 16:29:18 -0700286 def Run(self, *_args):
287 if self._true:
Luis Lozanodf762222015-05-19 12:22:37 -0700288 return [0, "", ""]
Caroline Tice92774192013-09-10 16:29:18 -0700289 else:
Luis Lozanodf762222015-05-19 12:22:37 -0700290 return [0, "", ""]