blob: 087f64da53e645815c6d17acabc83793433dc9f5 [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
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070046 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
cmticee5a746f2013-11-25 14:57:10 -050051 def Run(self, machine, label, benchmark, test_args, profiler_args):
Luis Lozano53c88e92013-10-08 15:15:48 -070052 self.PinGovernorExecutionFrequencies(machine, label.chromeos_root)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070053 if benchmark.suite == "telemetry":
54 return self.Telemetry_Run(machine, label, benchmark)
Caroline Tice92774192013-09-10 16:29:18 -070055 elif benchmark.suite == "telemetry_Crosperf":
cmticee5a746f2013-11-25 14:57:10 -050056 return self.Telemetry_Crosperf_Run(machine, label, benchmark,
57 test_args, profiler_args)
Caroline Ticeb47bff42013-08-19 15:59:02 -070058 elif benchmark.use_test_that:
cmticee5a746f2013-11-25 14:57:10 -050059 return self.Test_That_Run(machine, label, benchmark, test_args,
60 profiler_args)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070061 else:
cmticee5a746f2013-11-25 14:57:10 -050062 return self.Pyauto_Run(machine, label, benchmark, test_args,
63 profiler_args)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070064
Luis Lozano53c88e92013-10-08 15:15:48 -070065 def GetHighestStaticFrequency(self, machine_name, chromeos_root):
66 """ Gets the highest static frequency for the specified machine
67 """
Han Shenfd0b1782014-02-12 15:13:01 -080068 get_avail_freqs = ("cd /sys/devices/system/cpu/cpu0/cpufreq/; "
69 "if [[ -e scaling_available_frequencies ]]; then "
70 " cat scaling_available_frequencies; "
71 "else "
72 " cat scaling_max_freq ; "
73 "fi")
Luis Lozano53c88e92013-10-08 15:15:48 -070074 ret, freqs_str, _ = self._ce.CrosRunCommand(
75 get_avail_freqs, return_output=True, machine=machine_name,
76 chromeos_root=chromeos_root)
77 self._logger.LogFatalIf(ret, "Could not get available frequencies "
78 "from machine: %s" % machine_name)
79 freqs = freqs_str.split()
Han Shenfd0b1782014-02-12 15:13:01 -080080 ## When there is no scaling_available_frequencies file,
81 ## we have only 1 choice.
82 if len(freqs) == 1:
83 return freqs[0]
Luis Lozano53c88e92013-10-08 15:15:48 -070084 # The dynamic frequency ends with a "1000". So, ignore it if found.
85 if freqs[0].endswith("1000"):
86 return freqs[1]
87 else:
88 return freqs[0]
89
90 def PinGovernorExecutionFrequencies(self, machine_name, chromeos_root):
91 """ Set min and max frequencies to max static frequency
92 """
93 highest_freq = self.GetHighestStaticFrequency(machine_name, chromeos_root)
94 BASH_FOR = "for f in {list}; do {body}; done"
95 CPUFREQ_DIRS = "/sys/devices/system/cpu/cpu*/cpufreq/"
96 change_max_freq = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_max_freq",
97 body="echo %s > $f" % highest_freq)
98 change_min_freq = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_min_freq",
99 body="echo %s > $f" % highest_freq)
100 change_perf_gov = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_governor",
101 body="echo performance > $f")
102 ret = self._ce.CrosRunCommand(" && ".join(("set -e ",
103 change_max_freq,
104 change_min_freq,
105 change_perf_gov)),
106 machine=machine_name,
107 chromeos_root=chromeos_root)
108 self._logger.LogFatalIf(ret, "Could not pin frequencies on machine: %s"
109 % machine_name)
110
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700111 def RebootMachine(self, machine_name, chromeos_root):
Caroline Tice92774192013-09-10 16:29:18 -0700112 command = "reboot && exit"
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700113 self._ce.CrosRunCommand(command, machine=machine_name,
114 chromeos_root=chromeos_root)
115 time.sleep(60)
Luis Lozano53c88e92013-10-08 15:15:48 -0700116 # Whenever we reboot the machine, we need to restore the governor settings.
117 self.PinGovernorExecutionFrequencies(machine_name, chromeos_root)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700118
cmticee5a746f2013-11-25 14:57:10 -0500119 def Pyauto_Run(self, machine, label, benchmark, test_args, profiler_args):
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700120 """Run the run_remote_test."""
121 options = ""
122 if label.board:
123 options += " --board=%s" % label.board
124 if test_args:
125 options += " %s" % test_args
cmticee5a746f2013-11-25 14:57:10 -0500126 if profiler_args:
127 options += " %s" % profiler_args
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700128 command = "rm -rf /usr/local/autotest/results/*"
129 self._ce.CrosRunCommand(command, machine=machine, username="root",
130 chromeos_root=label.chromeos_root)
131
Luis Lozano53c88e92013-10-08 15:15:48 -0700132 # We do this because PyAuto tests leave the machine in weird states.
133 # Rebooting between iterations has proven to help with this.
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700134 self.RebootMachine(machine, label.chromeos_root)
135
Caroline Ticef8b3a5a2013-09-25 10:45:57 -0700136 command = ("./run_remote_tests.sh --use_emerged --remote=%s %s %s" %
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700137 (machine, options, benchmark.test_name))
138 return self._ce.ChrootRunCommand(label.chromeos_root,
139 command,
140 True,
141 self._ct)
142
cmticee5a746f2013-11-25 14:57:10 -0500143 def Test_That_Run(self, machine, label, benchmark, test_args, profiler_args):
Caroline Ticeb47bff42013-08-19 15:59:02 -0700144 """Run the test_that test.."""
145 options = ""
146 if label.board:
147 options += " --board=%s" % label.board
148 if test_args:
149 options += " %s" % test_args
cmticee5a746f2013-11-25 14:57:10 -0500150 if profiler_args:
151 self._logger.LogError("test_that does not support profiler.")
Caroline Ticeb47bff42013-08-19 15:59:02 -0700152 command = "rm -rf /usr/local/autotest/results/*"
153 self._ce.CrosRunCommand(command, machine=machine, username="root",
154 chromeos_root=label.chromeos_root)
155
Luis Lozano53c88e92013-10-08 15:15:48 -0700156 # We do this because PyAuto tests leave the machine in weird states.
157 # Rebooting between iterations has proven to help with this.
Caroline Ticeb47bff42013-08-19 15:59:02 -0700158 self.RebootMachine(machine, label.chromeos_root)
159
Caroline Tice92774192013-09-10 16:29:18 -0700160 command = ("%s %s %s %s" %
161 (TEST_THAT_PATH, options, machine, benchmark.test_name))
Caroline Ticeb47bff42013-08-19 15:59:02 -0700162 return self._ce.ChrootRunCommand(label.chromeos_root,
163 command,
164 True,
165 self._ct)
166
Caroline Tice92774192013-09-10 16:29:18 -0700167
cmticee5a746f2013-11-25 14:57:10 -0500168 def Telemetry_Crosperf_Run (self, machine, label, benchmark, test_args,
169 profiler_args):
Caroline Tice92774192013-09-10 16:29:18 -0700170 if not os.path.isdir(label.chrome_src):
171 self._logger.LogFatal("Cannot find chrome src dir to"
cmtice5f3ccbd2013-11-20 16:36:04 -0800172 " run telemetry: %s" % label.chrome_src)
Caroline Tice92774192013-09-10 16:29:18 -0700173
cmticee5a746f2013-11-25 14:57:10 -0500174 profiler_args = GetProfilerArgs (benchmark, profiler_args)
Caroline Tice92774192013-09-10 16:29:18 -0700175 chrome_root_options = ""
176
177 # If chrome_src is outside the chroot, mount it when entering the
178 # chroot.
179 if label.chrome_src.find(label.chromeos_root) == -1:
180 chrome_root_options = (" --chrome_root={0} --chrome_root_mount={1} "
181 " FEATURES=\"-usersandbox\" "
182 "CHROME_ROOT={2}".format(label.chrome_src,
183 CHROME_MOUNT_DIR,
184 CHROME_MOUNT_DIR))
185
cmticee5a746f2013-11-25 14:57:10 -0500186 args_string = ""
187 if test_args:
188 # Strip double quotes off args (so we can wrap them in single
189 # quotes, to pass through to Telemetry).
190 if test_args[0] == '"' and test_args[-1] == '"':
191 test_args = test_args[1:-1]
192 args_string = "test_args='%s'" % test_args
193 cmd = ('{0} --board={1} --args="{2} test={3} '
Caroline Tice92774192013-09-10 16:29:18 -0700194 '{4}" {5} telemetry_Crosperf'.format(TEST_THAT_PATH,
195 label.board,
cmticee5a746f2013-11-25 14:57:10 -0500196 args_string,
Caroline Tice92774192013-09-10 16:29:18 -0700197 benchmark.test_name,
198 profiler_args,
199 machine))
200 return self._ce.ChrootRunCommand (label.chromeos_root,
201 cmd,
202 return_output=True,
203 command_terminator=self._ct,
204 cros_sdk_options=chrome_root_options)
205
206
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700207 def Telemetry_Run(self, machine, label, benchmark):
208 if not os.path.isdir(label.chrome_src):
Caroline Tice6aa85282013-08-06 16:02:04 -0700209 self._logger.LogFatal("Cannot find chrome src dir to"
210 " run telemetry.")
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700211 rsa_key = os.path.join(label.chromeos_root,
212 "src/scripts/mod_for_test_scripts/ssh_keys/testing_rsa")
213
214 cmd = ("cd {0} && "
Yunlian Jiangfefa5c02013-07-02 15:47:02 -0700215 "./tools/perf/run_measurement "
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700216 "--browser=cros-chrome "
217 "--output-format=csv "
218 "--remote={1} "
219 "--identity {2} "
220 "{3} {4}".format(label.chrome_src, machine,
221 rsa_key,
222 benchmark.test_name,
223 benchmark.test_args))
224 return self._ce.RunCommand(cmd, return_output=True,
225 print_to_console=False)
226
227 def Terminate(self):
228 self._ct.Terminate()
229
230
231class MockSuiteRunner(object):
232 def __init__(self):
Caroline Tice92774192013-09-10 16:29:18 -0700233 self._true = True
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700234
Caroline Tice92774192013-09-10 16:29:18 -0700235 def Run(self, *_args):
236 if self._true:
237 return ["", "", 0]
238 else:
239 return ["", "", 0]