blob: f69f37287f0b845ca5d68786457639ea007789be [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
9
10from utils import command_executer
11
Caroline Tice92774192013-09-10 16:29:18 -070012TEST_THAT_PATH = '/usr/bin/test_that'
13CHROME_MOUNT_DIR = '/tmp/chrome_root'
14
15def GetProfilerArgs (benchmark):
16 if benchmark.perf_args:
17 perf_args_list = benchmark.perf_args.split(" ")
18 perf_args_list = [perf_args_list[0]] + ["-a"] + perf_args_list[1:]
19 perf_args = " ".join(perf_args_list)
20 if not perf_args_list[0] in ["record", "stat"]:
21 raise Exception("perf_args must start with either record or stat")
22 extra_test_args = ["profiler=custom_perf",
cmticea1d03082013-10-23 11:44:51 -070023 ("profiler_args='%s'" %
Caroline Tice92774192013-09-10 16:29:18 -070024 perf_args)]
25 return " ".join(extra_test_args)
26 else:
27 return ""
28
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070029
30class SuiteRunner(object):
31 """ This defines the interface from crosperf to test script.
32 """
Caroline Tice92774192013-09-10 16:29:18 -070033
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070034 def __init__(self, logger_to_use=None):
35 self._logger = logger_to_use
36 self._ce = command_executer.GetCommandExecuter(self._logger)
37 self._ct = command_executer.CommandTerminator()
38
39 def Run(self, machine, label, benchmark, test_args):
Luis Lozano53c88e92013-10-08 15:15:48 -070040 self.PinGovernorExecutionFrequencies(machine, label.chromeos_root)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070041 if benchmark.suite == "telemetry":
42 return self.Telemetry_Run(machine, label, benchmark)
Caroline Tice92774192013-09-10 16:29:18 -070043 elif benchmark.suite == "telemetry_Crosperf":
44 return self.Telemetry_Crosperf_Run(machine, label, benchmark)
Caroline Ticeb47bff42013-08-19 15:59:02 -070045 elif benchmark.use_test_that:
46 return self.Test_That_Run(machine, label, benchmark, test_args)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070047 else:
48 return self.Pyauto_Run(machine, label, benchmark, test_args)
49
Luis Lozano53c88e92013-10-08 15:15:48 -070050 def GetHighestStaticFrequency(self, machine_name, chromeos_root):
51 """ Gets the highest static frequency for the specified machine
52 """
53 get_avail_freqs = ("cat /sys/devices/system/cpu/cpu0/cpufreq/"
54 "scaling_available_frequencies")
55 ret, freqs_str, _ = self._ce.CrosRunCommand(
56 get_avail_freqs, return_output=True, machine=machine_name,
57 chromeos_root=chromeos_root)
58 self._logger.LogFatalIf(ret, "Could not get available frequencies "
59 "from machine: %s" % machine_name)
60 freqs = freqs_str.split()
61 # The dynamic frequency ends with a "1000". So, ignore it if found.
62 if freqs[0].endswith("1000"):
63 return freqs[1]
64 else:
65 return freqs[0]
66
67 def PinGovernorExecutionFrequencies(self, machine_name, chromeos_root):
68 """ Set min and max frequencies to max static frequency
69 """
70 highest_freq = self.GetHighestStaticFrequency(machine_name, chromeos_root)
71 BASH_FOR = "for f in {list}; do {body}; done"
72 CPUFREQ_DIRS = "/sys/devices/system/cpu/cpu*/cpufreq/"
73 change_max_freq = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_max_freq",
74 body="echo %s > $f" % highest_freq)
75 change_min_freq = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_min_freq",
76 body="echo %s > $f" % highest_freq)
77 change_perf_gov = BASH_FOR.format(list=CPUFREQ_DIRS + "scaling_governor",
78 body="echo performance > $f")
79 ret = self._ce.CrosRunCommand(" && ".join(("set -e ",
80 change_max_freq,
81 change_min_freq,
82 change_perf_gov)),
83 machine=machine_name,
84 chromeos_root=chromeos_root)
85 self._logger.LogFatalIf(ret, "Could not pin frequencies on machine: %s"
86 % machine_name)
87
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070088 def RebootMachine(self, machine_name, chromeos_root):
Caroline Tice92774192013-09-10 16:29:18 -070089 command = "reboot && exit"
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070090 self._ce.CrosRunCommand(command, machine=machine_name,
91 chromeos_root=chromeos_root)
92 time.sleep(60)
Luis Lozano53c88e92013-10-08 15:15:48 -070093 # Whenever we reboot the machine, we need to restore the governor settings.
94 self.PinGovernorExecutionFrequencies(machine_name, chromeos_root)
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -070095
96 def Pyauto_Run(self, machine, label, benchmark, test_args):
97 """Run the run_remote_test."""
98 options = ""
99 if label.board:
100 options += " --board=%s" % label.board
101 if test_args:
102 options += " %s" % test_args
103 command = "rm -rf /usr/local/autotest/results/*"
104 self._ce.CrosRunCommand(command, machine=machine, username="root",
105 chromeos_root=label.chromeos_root)
106
Luis Lozano53c88e92013-10-08 15:15:48 -0700107 # We do this because PyAuto tests leave the machine in weird states.
108 # Rebooting between iterations has proven to help with this.
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700109 self.RebootMachine(machine, label.chromeos_root)
110
Caroline Ticef8b3a5a2013-09-25 10:45:57 -0700111 command = ("./run_remote_tests.sh --use_emerged --remote=%s %s %s" %
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700112 (machine, options, benchmark.test_name))
113 return self._ce.ChrootRunCommand(label.chromeos_root,
114 command,
115 True,
116 self._ct)
117
Caroline Ticeb47bff42013-08-19 15:59:02 -0700118 def Test_That_Run(self, machine, label, benchmark, test_args):
119 """Run the test_that test.."""
120 options = ""
121 if label.board:
122 options += " --board=%s" % label.board
123 if test_args:
124 options += " %s" % test_args
125 command = "rm -rf /usr/local/autotest/results/*"
126 self._ce.CrosRunCommand(command, machine=machine, username="root",
127 chromeos_root=label.chromeos_root)
128
Luis Lozano53c88e92013-10-08 15:15:48 -0700129 # We do this because PyAuto tests leave the machine in weird states.
130 # Rebooting between iterations has proven to help with this.
Caroline Ticeb47bff42013-08-19 15:59:02 -0700131 self.RebootMachine(machine, label.chromeos_root)
132
Caroline Tice92774192013-09-10 16:29:18 -0700133 command = ("%s %s %s %s" %
134 (TEST_THAT_PATH, options, machine, benchmark.test_name))
Caroline Ticeb47bff42013-08-19 15:59:02 -0700135 return self._ce.ChrootRunCommand(label.chromeos_root,
136 command,
137 True,
138 self._ct)
139
Caroline Tice92774192013-09-10 16:29:18 -0700140
141 def Telemetry_Crosperf_Run (self, machine, label, benchmark):
142 if not os.path.isdir(label.chrome_src):
143 self._logger.LogFatal("Cannot find chrome src dir to"
144 " run telemetry.")
145
146 profiler_args = GetProfilerArgs (benchmark)
147 chrome_root_options = ""
148
149 # If chrome_src is outside the chroot, mount it when entering the
150 # chroot.
151 if label.chrome_src.find(label.chromeos_root) == -1:
152 chrome_root_options = (" --chrome_root={0} --chrome_root_mount={1} "
153 " FEATURES=\"-usersandbox\" "
154 "CHROME_ROOT={2}".format(label.chrome_src,
155 CHROME_MOUNT_DIR,
156 CHROME_MOUNT_DIR))
157
158 cmd = ('{0} --board={1} --args="iterations={2} test={3} '
159 '{4}" {5} telemetry_Crosperf'.format(TEST_THAT_PATH,
160 label.board,
161 benchmark.iterations,
162 benchmark.test_name,
163 profiler_args,
164 machine))
165 return self._ce.ChrootRunCommand (label.chromeos_root,
166 cmd,
167 return_output=True,
168 command_terminator=self._ct,
169 cros_sdk_options=chrome_root_options)
170
171
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700172 def Telemetry_Run(self, machine, label, benchmark):
173 if not os.path.isdir(label.chrome_src):
Caroline Tice6aa85282013-08-06 16:02:04 -0700174 self._logger.LogFatal("Cannot find chrome src dir to"
175 " run telemetry.")
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700176 rsa_key = os.path.join(label.chromeos_root,
177 "src/scripts/mod_for_test_scripts/ssh_keys/testing_rsa")
178
179 cmd = ("cd {0} && "
Yunlian Jiangfefa5c02013-07-02 15:47:02 -0700180 "./tools/perf/run_measurement "
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700181 "--browser=cros-chrome "
182 "--output-format=csv "
183 "--remote={1} "
184 "--identity {2} "
185 "{3} {4}".format(label.chrome_src, machine,
186 rsa_key,
187 benchmark.test_name,
188 benchmark.test_args))
189 return self._ce.RunCommand(cmd, return_output=True,
190 print_to_console=False)
191
192 def Terminate(self):
193 self._ct.Terminate()
194
195
196class MockSuiteRunner(object):
197 def __init__(self):
Caroline Tice92774192013-09-10 16:29:18 -0700198 self._true = True
Yunlian Jiang04dc5dc2013-04-23 15:05:05 -0700199
Caroline Tice92774192013-09-10 16:29:18 -0700200 def Run(self, *_args):
201 if self._true:
202 return ["", "", 0]
203 else:
204 return ["", "", 0]