blob: 89f06ff72be90b557fc5ad3e2514bbfc6aa3a278 [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 """
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 Jiang04dc5dc2013-04-23 15:05:05 -0700103 def RebootMachine(self, machine_name, chromeos_root):
Caroline Tice92774192013-09-10 16:29:18 -0700104 command = "reboot && exit"
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700105 self._ce.CrosRunCommand(command, machine=machine_name,
106 chromeos_root=chromeos_root)
107 time.sleep(60)
Luis Lozano53c88e92013-10-08 15:15:48 -0700108 # Whenever we reboot the machine, we need to restore the governor settings.
109 self.PinGovernorExecutionFrequencies(machine_name, chromeos_root)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700110
cmticee5a746f2013-11-25 14:57:10 -0500111 def Pyauto_Run(self, machine, label, benchmark, test_args, profiler_args):
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700112 """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
cmticee5a746f2013-11-25 14:57:10 -0500118 if profiler_args:
119 options += " %s" % profiler_args
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700120 command = "rm -rf /usr/local/autotest/results/*"
121 self._ce.CrosRunCommand(command, machine=machine, username="root",
122 chromeos_root=label.chromeos_root)
123
Luis Lozano53c88e92013-10-08 15:15:48 -0700124 # We do this because PyAuto tests leave the machine in weird states.
125 # Rebooting between iterations has proven to help with this.
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700126 self.RebootMachine(machine, label.chromeos_root)
127
Caroline Ticef8b3a5a2013-09-25 10:45:57 -0700128 command = ("./run_remote_tests.sh --use_emerged --remote=%s %s %s" %
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700129 (machine, options, benchmark.test_name))
130 return self._ce.ChrootRunCommand(label.chromeos_root,
131 command,
132 True,
133 self._ct)
134
cmticee5a746f2013-11-25 14:57:10 -0500135 def Test_That_Run(self, machine, label, benchmark, test_args, profiler_args):
Caroline Ticeb47bff42013-08-19 15:59:02 -0700136 """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
cmticee5a746f2013-11-25 14:57:10 -0500142 if profiler_args:
143 self._logger.LogError("test_that does not support profiler.")
Caroline Ticeb47bff42013-08-19 15:59:02 -0700144 command = "rm -rf /usr/local/autotest/results/*"
145 self._ce.CrosRunCommand(command, machine=machine, username="root",
146 chromeos_root=label.chromeos_root)
147
Luis Lozano53c88e92013-10-08 15:15:48 -0700148 # We do this because PyAuto tests leave the machine in weird states.
149 # Rebooting between iterations has proven to help with this.
Caroline Ticeb47bff42013-08-19 15:59:02 -0700150 self.RebootMachine(machine, label.chromeos_root)
151
Caroline Tice92774192013-09-10 16:29:18 -0700152 command = ("%s %s %s %s" %
153 (TEST_THAT_PATH, options, machine, benchmark.test_name))
Caroline Ticeb47bff42013-08-19 15:59:02 -0700154 return self._ce.ChrootRunCommand(label.chromeos_root,
155 command,
156 True,
157 self._ct)
158
Caroline Tice92774192013-09-10 16:29:18 -0700159
cmticee5a746f2013-11-25 14:57:10 -0500160 def Telemetry_Crosperf_Run (self, machine, label, benchmark, test_args,
161 profiler_args):
Caroline Tice92774192013-09-10 16:29:18 -0700162 if not os.path.isdir(label.chrome_src):
163 self._logger.LogFatal("Cannot find chrome src dir to"
cmtice5f3ccbd2013-11-20 16:36:04 -0800164 " run telemetry: %s" % label.chrome_src)
Caroline Tice92774192013-09-10 16:29:18 -0700165
cmticee5a746f2013-11-25 14:57:10 -0500166 profiler_args = GetProfilerArgs (benchmark, profiler_args)
Caroline Tice92774192013-09-10 16:29:18 -0700167 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
cmticee5a746f2013-11-25 14:57:10 -0500178 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 Tice92774192013-09-10 16:29:18 -0700186 '{4}" {5} telemetry_Crosperf'.format(TEST_THAT_PATH,
187 label.board,
cmticee5a746f2013-11-25 14:57:10 -0500188 args_string,
Caroline Tice92774192013-09-10 16:29:18 -0700189 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 Jiang04dc5dc2013-04-23 15:05:05 -0700199 def Telemetry_Run(self, machine, label, benchmark):
200 if not os.path.isdir(label.chrome_src):
Caroline Tice6aa85282013-08-06 16:02:04 -0700201 self._logger.LogFatal("Cannot find chrome src dir to"
202 " run telemetry.")
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700203 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 Jiangfefa5c02013-07-02 15:47:02 -0700207 "./tools/perf/run_measurement "
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700208 "--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
223class MockSuiteRunner(object):
224 def __init__(self):
Caroline Tice92774192013-09-10 16:29:18 -0700225 self._true = True
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700226
Caroline Tice92774192013-09-10 16:29:18 -0700227 def Run(self, *_args):
228 if self._true:
229 return ["", "", 0]
230 else:
231 return ["", "", 0]