blob: 9a076353c72e7c88b6aa8d4344c9ce34ee2a54b6 [file] [log] [blame]
asharif96f59692013-02-16 03:13:36 +00001#!/usr/bin/python
2
3# Script to test different toolchains against ChromeOS benchmarks.
4import optparse
5import os
6import sys
7import build_chromeos
8import setup_chromeos
9from utils import command_executer
10from utils import misc
11from utils import logger
12
13
14class GCCConfig(object):
15 def __init__(self, githash):
16 self.githash = githash
17
18
19class ToolchainConfig:
20 def __init__(self, gcc_config=None, binutils_config=None):
21 self.gcc_config = gcc_config
22
23
24class ChromeOSCheckout(object):
25 def __init__(self, board, chromeos_root):
26 self._board = board
27 self._chromeos_root = chromeos_root
28 self._ce = command_executer.GetCommandExecuter()
29 self._l = logger.GetLogger()
30
asharif3e38de02013-02-19 19:34:59 +000031 def _DeleteChroot(self):
asharif87414272013-02-19 19:58:45 +000032 command = "cd %s; cros_sdk --delete" % self._chromeos_root
asharif3e38de02013-02-19 19:34:59 +000033 return self._ce.RunCommand(command)
34
asharif67973582013-02-19 20:19:40 +000035 def _DeleteCcahe(self):
36 # crosbug.com/34956
37 command = "sudo rm -rf %s" % os.path.join(self._chromeos_root, ".cache")
38 return self._ce.RunCommand(command)
39
asharif96f59692013-02-16 03:13:36 +000040 def _BuildAndImage(self, label=""):
41 if (not label or
42 not misc.DoesLabelExist(self._chromeos_root, self._board, label)):
43 build_chromeos_args = [build_chromeos.__file__,
44 "--chromeos_root=%s" % self._chromeos_root,
45 "--board=%s" % self._board,
46 "--rebuild"]
asharife6b72fe2013-02-19 19:58:18 +000047 if self._public:
48 build_chromeos_args.append("--env=USE=-chrome_internal")
Yunlian Jiangd145a582013-08-19 13:59:34 -070049 if label == "vanilla":
50 build_chromeos_args.append("--vanilla_image")
asharif96f59692013-02-16 03:13:36 +000051 ret = build_chromeos.Main(build_chromeos_args)
52 if ret:
53 raise Exception("Couldn't build ChromeOS!")
54 if label:
55 misc.LabelLatestImage(self._chromeos_root, self._board, label)
56 return label
57
58 def _SetupBoard(self, env_dict):
59 env_string = misc.GetEnvStringFromDict(env_dict)
60 command = ("%s %s" %
61 (env_string,
62 misc.GetSetupBoardCommand(self._board,
63 usepkg=False)))
64 ret = self._ce.ChrootRunCommand(self._chromeos_root,
65 command)
66 assert ret == 0, "Could not setup board with new toolchain."
67
68 def _UnInstallToolchain(self):
69 command = ("sudo CLEAN_DELAY=0 emerge -C cross-%s/gcc" %
70 misc.GetCtargetFromBoard(self._board,
71 self._chromeos_root))
72 ret = self._ce.ChrootRunCommand(self._chromeos_root,
73 command)
74 if ret:
75 raise Exception("Couldn't uninstall the toolchain!")
76
77 def _CheckoutChromeOS(self):
78 # TODO(asharif): Setup a fixed ChromeOS version (quarterly snapshot).
79 if not os.path.exists(self._chromeos_root):
Caroline Ticec35909e2013-10-02 17:13:13 -070080 #setup_chromeos_args = [setup_chromeos.__file__,
81 # "--dir=%s" % self._chromeos_root,
82 # "--minilayout"]
asharif96f59692013-02-16 03:13:36 +000083 setup_chromeos_args = [setup_chromeos.__file__,
Caroline Ticec35909e2013-10-02 17:13:13 -070084 "--dir=%s" % self._chromeos_root]
asharife6b72fe2013-02-19 19:58:18 +000085 if self._public:
86 setup_chromeos_args.append("--public")
asharif5fe40e22013-02-19 19:58:50 +000087 ret = setup_chromeos.Main(setup_chromeos_args)
88 if ret:
89 raise Exception("Couldn't run setup_chromeos!")
asharif96f59692013-02-16 03:13:36 +000090
Caroline Ticec35909e2013-10-02 17:13:13 -070091
asharif96f59692013-02-16 03:13:36 +000092 def _BuildToolchain(self, config):
93 self._UnInstallToolchain()
94 self._SetupBoard({"USE": "git_gcc",
95 "GCC_GITHASH": config.gcc_config.githash,
96 "EMERGE_DEFAULT_OPTS": "--exclude=gcc"})
97
98
99class ToolchainComparator(ChromeOSCheckout):
asharif58a8c9f2013-02-19 20:42:43 +0000100 def __init__(self, board, remotes, configs, clean, public, force_mismatch):
asharif96f59692013-02-16 03:13:36 +0000101 self._board = board
102 self._remotes = remotes
103 self._chromeos_root = "chromeos"
104 self._configs = configs
asharif3e38de02013-02-19 19:34:59 +0000105 self._clean = clean
asharife6b72fe2013-02-19 19:58:18 +0000106 self._public = public
asharif58a8c9f2013-02-19 20:42:43 +0000107 self._force_mismatch = force_mismatch
asharif96f59692013-02-16 03:13:36 +0000108 self._ce = command_executer.GetCommandExecuter()
109 self._l = logger.GetLogger()
110 ChromeOSCheckout.__init__(self, board, self._chromeos_root)
111
112 def _TestLabels(self, labels):
113 experiment_file = "toolchain_experiment.txt"
asharif58a8c9f2013-02-19 20:42:43 +0000114 image_args = ""
115 if self._force_mismatch:
asharif0b5d5c82013-02-19 20:42:56 +0000116 image_args = "--force-mismatch"
asharif96f59692013-02-16 03:13:36 +0000117 experiment_header = """
118 board: %s
119 remote: %s
120 """ % (self._board, self._remotes)
121 experiment_tests = """
122 benchmark: desktopui_PyAutoPerfTests {
ashariffce80422013-02-19 20:20:11 +0000123 iterations: 1
asharif96f59692013-02-16 03:13:36 +0000124 }
125 """
126 with open(experiment_file, "w") as f:
127 print >>f, experiment_header
128 print >>f, experiment_tests
129 for label in labels:
130 # TODO(asharif): Fix crosperf so it accepts labels with symbols
131 crosperf_label = label
132 crosperf_label = crosperf_label.replace("-", "minus")
133 crosperf_label = crosperf_label.replace("+", "plus")
asharife4a5a8f2013-02-19 20:19:33 +0000134 crosperf_label = crosperf_label.replace(".", "")
asharif96f59692013-02-16 03:13:36 +0000135 experiment_image = """
136 %s {
137 chromeos_image: %s
asharif58a8c9f2013-02-19 20:42:43 +0000138 image_args: %s
asharif96f59692013-02-16 03:13:36 +0000139 }
140 """ % (crosperf_label,
141 os.path.join(misc.GetImageDir(self._chromeos_root, self._board),
142 label,
asharif58a8c9f2013-02-19 20:42:43 +0000143 "chromiumos_test_image.bin"),
144 image_args)
asharif96f59692013-02-16 03:13:36 +0000145 print >>f, experiment_image
146 crosperf = os.path.join(os.path.dirname(__file__),
147 "crosperf",
148 "crosperf")
asharifb2cd6342013-02-19 19:42:57 +0000149 command = "%s --email=c-compiler-chrome %s" % (crosperf, experiment_file)
asharif96f59692013-02-16 03:13:36 +0000150 ret = self._ce.RunCommand(command)
151 if ret:
152 raise Exception("Couldn't run crosperf!")
153
154 def DoAll(self):
155 self._CheckoutChromeOS()
asharif96f59692013-02-16 03:13:36 +0000156 labels = []
157 vanilla_label = self._BuildAndImage("vanilla")
158 labels.append(vanilla_label)
159 for config in self._configs:
160 label = misc.GetFilenameFromString(config.gcc_config.githash)
161 if (not misc.DoesLabelExist(self._chromeos_root,
162 self._board,
163 label)):
164 self._BuildToolchain(config)
165 label = self._BuildAndImage(label)
166 labels.append(label)
167 self._TestLabels(labels)
asharif3e38de02013-02-19 19:34:59 +0000168 if self._clean:
169 ret = self._DeleteChroot()
170 if ret: return ret
asharif67973582013-02-19 20:19:40 +0000171 ret = self._DeleteCcahe()
172 if ret: return ret
asharif96f59692013-02-16 03:13:36 +0000173 return 0
174
175
176def Main(argv):
177 """The main function."""
178 # Common initializations
179### command_executer.InitCommandExecuter(True)
180 command_executer.InitCommandExecuter()
181 parser = optparse.OptionParser()
182 parser.add_option("--remote",
183 dest="remote",
184 help="Remote machines to run tests on.")
185 parser.add_option("--board",
186 dest="board",
187 default="x86-zgb",
188 help="The target board.")
189 parser.add_option("--githashes",
190 dest="githashes",
191 default="master",
192 help="The gcc githashes to test.")
asharif3e38de02013-02-19 19:34:59 +0000193 parser.add_option("--clean",
194 dest="clean",
195 default=False,
196 action="store_true",
197 help="Clean the chroot after testing.")
asharife6b72fe2013-02-19 19:58:18 +0000198 parser.add_option("--public",
199 dest="public",
200 default=False,
201 action="store_true",
202 help="Use the public checkout/build.")
asharif58a8c9f2013-02-19 20:42:43 +0000203 parser.add_option("--force-mismatch",
204 dest="force_mismatch",
205 default="",
206 help="Force the image regardless of board mismatch")
asharif96f59692013-02-16 03:13:36 +0000207 options, _ = parser.parse_args(argv)
208 if not options.board:
209 print "Please give a board."
210 return 1
211 if not options.remote:
212 print "Please give at least one remote machine."
213 return 1
214 toolchain_configs = []
215 for githash in options.githashes.split(","):
216 gcc_config = GCCConfig(githash=githash)
217 toolchain_config = ToolchainConfig(gcc_config=gcc_config)
218 toolchain_configs.append(toolchain_config)
asharif3e38de02013-02-19 19:34:59 +0000219 fc = ToolchainComparator(options.board, options.remote, toolchain_configs,
asharif58a8c9f2013-02-19 20:42:43 +0000220 options.clean, options.public,
221 options.force_mismatch)
asharif96f59692013-02-16 03:13:36 +0000222 return fc.DoAll()
223
224
225if __name__ == "__main__":
226 retval = Main(sys.argv)
227 sys.exit(retval)