blob: 1f816e426ec7a84977fec1bd1c6e5f94ac144542 [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":
cmtice226e3e02014-04-27 22:28:42 -070056 return self.Telemetry_Run(machine, label, benchmark, profiler_args)
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)
cmtice98a53692014-04-16 14:48:47 -070060 else:
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
Luis Lozano53c88e92013-10-08 15:15:48 -070064 def GetHighestStaticFrequency(self, machine_name, chromeos_root):
65 """ Gets the highest static frequency for the specified machine
66 """
Han Shenfd0b1782014-02-12 15:13:01 -080067 get_avail_freqs = ("cd /sys/devices/system/cpu/cpu0/cpufreq/; "
68 "if [[ -e scaling_available_frequencies ]]; then "
69 " cat scaling_available_frequencies; "
70 "else "
71 " cat scaling_max_freq ; "
72 "fi")
Luis Lozano53c88e92013-10-08 15:15:48 -070073 ret, freqs_str, _ = self._ce.CrosRunCommand(
74 get_avail_freqs, return_output=True, machine=machine_name,
75 chromeos_root=chromeos_root)
76 self._logger.LogFatalIf(ret, "Could not get available frequencies "
77 "from machine: %s" % machine_name)
78 freqs = freqs_str.split()
Han Shenfd0b1782014-02-12 15:13:01 -080079 ## When there is no scaling_available_frequencies file,
80 ## we have only 1 choice.
81 if len(freqs) == 1:
82 return freqs[0]
Luis Lozano53c88e92013-10-08 15:15:48 -070083 # The dynamic frequency ends with a "1000". So, ignore it if found.
84 if freqs[0].endswith("1000"):
85 return freqs[1]
86 else:
87 return freqs[0]
88
89 def PinGovernorExecutionFrequencies(self, machine_name, chromeos_root):
90 """ Set min and max frequencies to max static frequency
91 """
92 highest_freq = self.GetHighestStaticFrequency(machine_name, chromeos_root)
93 BASH_FOR = "for f in {list}; do {body}; done"
94 CPUFREQ_DIRS = "/sys/devices/system/cpu/cpu*/cpufreq/"
95 change_max_freq = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_max_freq",
96 body="echo %s > $f" % highest_freq)
97 change_min_freq = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_min_freq",
98 body="echo %s > $f" % highest_freq)
99 change_perf_gov = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_governor",
100 body="echo performance > $f")
cmtice13909242014-03-11 13:38:07 -0700101 if self.log_level == "average":
102 self._logger.LogOutput("Pinning governor execution frequencies for %s"
103 % machine_name)
Luis Lozano53c88e92013-10-08 15:15:48 -0700104 ret = self._ce.CrosRunCommand(" && ".join(("set -e ",
105 change_max_freq,
106 change_min_freq,
107 change_perf_gov)),
108 machine=machine_name,
109 chromeos_root=chromeos_root)
110 self._logger.LogFatalIf(ret, "Could not pin frequencies on machine: %s"
111 % machine_name)
112
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700113 def RebootMachine(self, machine_name, chromeos_root):
Caroline Tice92774192013-09-10 16:29:18 -0700114 command = "reboot && exit"
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700115 self._ce.CrosRunCommand(command, machine=machine_name,
116 chromeos_root=chromeos_root)
117 time.sleep(60)
Luis Lozano53c88e92013-10-08 15:15:48 -0700118 # Whenever we reboot the machine, we need to restore the governor settings.
119 self.PinGovernorExecutionFrequencies(machine_name, chromeos_root)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700120
cmticee5a746f2013-11-25 14:57:10 -0500121 def Test_That_Run(self, machine, label, benchmark, test_args, profiler_args):
Caroline Ticeb47bff42013-08-19 15:59:02 -0700122 """Run the test_that test.."""
123 options = ""
124 if label.board:
125 options += " --board=%s" % label.board
126 if test_args:
127 options += " %s" % test_args
cmticee5a746f2013-11-25 14:57:10 -0500128 if profiler_args:
cmtice226e3e02014-04-27 22:28:42 -0700129 self._logger.LogFatal("test_that does not support profiler.")
Caroline Ticeb47bff42013-08-19 15:59:02 -0700130 command = "rm -rf /usr/local/autotest/results/*"
131 self._ce.CrosRunCommand(command, machine=machine, username="root",
132 chromeos_root=label.chromeos_root)
133
cmtice98a53692014-04-16 14:48:47 -0700134 # We do this because some tests leave the machine in weird states.
Luis Lozano53c88e92013-10-08 15:15:48 -0700135 # Rebooting between iterations has proven to help with this.
Caroline Ticeb47bff42013-08-19 15:59:02 -0700136 self.RebootMachine(machine, label.chromeos_root)
137
Caroline Tice92774192013-09-10 16:29:18 -0700138 command = ("%s %s %s %s" %
139 (TEST_THAT_PATH, options, machine, benchmark.test_name))
cmtice13909242014-03-11 13:38:07 -0700140 if self.log_level != "verbose":
141 self._logger.LogOutput("Running test.")
142 self._logger.LogOutput("CMD: %s" % command)
Caroline Ticeb47bff42013-08-19 15:59:02 -0700143 return self._ce.ChrootRunCommand(label.chromeos_root,
144 command,
145 True,
146 self._ct)
147
Caroline Tice92774192013-09-10 16:29:18 -0700148
cmticee5a746f2013-11-25 14:57:10 -0500149 def Telemetry_Crosperf_Run (self, machine, label, benchmark, test_args,
150 profiler_args):
Caroline Tice92774192013-09-10 16:29:18 -0700151 if not os.path.isdir(label.chrome_src):
152 self._logger.LogFatal("Cannot find chrome src dir to"
cmtice5f3ccbd2013-11-20 16:36:04 -0800153 " run telemetry: %s" % label.chrome_src)
Caroline Tice92774192013-09-10 16:29:18 -0700154
cmticee5a746f2013-11-25 14:57:10 -0500155 profiler_args = GetProfilerArgs (benchmark, profiler_args)
Caroline Tice92774192013-09-10 16:29:18 -0700156 chrome_root_options = ""
157
158 # If chrome_src is outside the chroot, mount it when entering the
159 # chroot.
160 if label.chrome_src.find(label.chromeos_root) == -1:
161 chrome_root_options = (" --chrome_root={0} --chrome_root_mount={1} "
162 " FEATURES=\"-usersandbox\" "
163 "CHROME_ROOT={2}".format(label.chrome_src,
164 CHROME_MOUNT_DIR,
165 CHROME_MOUNT_DIR))
166
cmticee5a746f2013-11-25 14:57:10 -0500167 args_string = ""
168 if test_args:
169 # Strip double quotes off args (so we can wrap them in single
170 # quotes, to pass through to Telemetry).
171 if test_args[0] == '"' and test_args[-1] == '"':
172 test_args = test_args[1:-1]
173 args_string = "test_args='%s'" % test_args
174 cmd = ('{0} --board={1} --args="{2} test={3} '
Caroline Tice92774192013-09-10 16:29:18 -0700175 '{4}" {5} telemetry_Crosperf'.format(TEST_THAT_PATH,
176 label.board,
cmticee5a746f2013-11-25 14:57:10 -0500177 args_string,
Caroline Tice92774192013-09-10 16:29:18 -0700178 benchmark.test_name,
179 profiler_args,
180 machine))
cmtice13909242014-03-11 13:38:07 -0700181 if self.log_level != "verbose":
182 self._logger.LogOutput("Running test.")
183 self._logger.LogOutput("CMD: %s" % cmd)
Caroline Tice92774192013-09-10 16:29:18 -0700184 return self._ce.ChrootRunCommand (label.chromeos_root,
185 cmd,
186 return_output=True,
187 command_terminator=self._ct,
188 cros_sdk_options=chrome_root_options)
189
190
cmtice226e3e02014-04-27 22:28:42 -0700191 def Telemetry_Run(self, machine, label, benchmark, profiler_args):
cmtice98a53692014-04-16 14:48:47 -0700192 telemetry_run_path = ""
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700193 if not os.path.isdir(label.chrome_src):
Caroline Tice6aa85282013-08-06 16:02:04 -0700194 self._logger.LogFatal("Cannot find chrome src dir to"
cmtice98a53692014-04-16 14:48:47 -0700195 " run telemetry.")
196 else:
197 telemetry_run_path = os.path.join(label.chrome_src, "src/tools/perf")
198 if not os.path.exists(telemetry_run_path):
199 self._logger.LogFatal("Cannot find %s directory." % telemetry_run_path)
200
cmtice226e3e02014-04-27 22:28:42 -0700201 if profiler_args:
202 self._logger.LogFatal("Telemetry does not support the perf profiler.")
203
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700204 rsa_key = os.path.join(label.chromeos_root,
205 "src/scripts/mod_for_test_scripts/ssh_keys/testing_rsa")
206
207 cmd = ("cd {0} && "
cmtice98a53692014-04-16 14:48:47 -0700208 "./run_measurement "
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700209 "--browser=cros-chrome "
210 "--output-format=csv "
211 "--remote={1} "
212 "--identity {2} "
cmtice98a53692014-04-16 14:48:47 -0700213 "{3} {4}".format(telemetry_run_path, machine,
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700214 rsa_key,
215 benchmark.test_name,
216 benchmark.test_args))
cmtice13909242014-03-11 13:38:07 -0700217 if self.log_level != "verbose":
218 self._logger.LogOutput("Running test.")
219 self._logger.LogOutput("CMD: %s" % cmd)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700220 return self._ce.RunCommand(cmd, return_output=True,
221 print_to_console=False)
222
223 def Terminate(self):
224 self._ct.Terminate()
225
226
227class MockSuiteRunner(object):
228 def __init__(self):
Caroline Tice92774192013-09-10 16:29:18 -0700229 self._true = True
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700230
Caroline Tice92774192013-09-10 16:29:18 -0700231 def Run(self, *_args):
232 if self._true:
233 return ["", "", 0]
234 else:
235 return ["", "", 0]