blob: 18448ab220f1c9ea7ff019002212aa1159f1837f [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")
asharif96f59692013-02-16 03:13:36 +000049 ret = build_chromeos.Main(build_chromeos_args)
50 if ret:
51 raise Exception("Couldn't build ChromeOS!")
52 if label:
53 misc.LabelLatestImage(self._chromeos_root, self._board, label)
54 return label
55
56 def _SetupBoard(self, env_dict):
57 env_string = misc.GetEnvStringFromDict(env_dict)
58 command = ("%s %s" %
59 (env_string,
60 misc.GetSetupBoardCommand(self._board,
61 usepkg=False)))
62 ret = self._ce.ChrootRunCommand(self._chromeos_root,
63 command)
64 assert ret == 0, "Could not setup board with new toolchain."
65
66 def _UnInstallToolchain(self):
67 command = ("sudo CLEAN_DELAY=0 emerge -C cross-%s/gcc" %
68 misc.GetCtargetFromBoard(self._board,
69 self._chromeos_root))
70 ret = self._ce.ChrootRunCommand(self._chromeos_root,
71 command)
72 if ret:
73 raise Exception("Couldn't uninstall the toolchain!")
74
75 def _CheckoutChromeOS(self):
76 # TODO(asharif): Setup a fixed ChromeOS version (quarterly snapshot).
77 if not os.path.exists(self._chromeos_root):
78 setup_chromeos_args = [setup_chromeos.__file__,
79 "--dir=%s" % self._chromeos_root,
80 "--minilayout"]
asharife6b72fe2013-02-19 19:58:18 +000081 if self._public:
82 setup_chromeos_args.append("--public")
asharif5fe40e22013-02-19 19:58:50 +000083 ret = setup_chromeos.Main(setup_chromeos_args)
84 if ret:
85 raise Exception("Couldn't run setup_chromeos!")
asharif96f59692013-02-16 03:13:36 +000086
87 def _BuildToolchain(self, config):
88 self._UnInstallToolchain()
89 self._SetupBoard({"USE": "git_gcc",
90 "GCC_GITHASH": config.gcc_config.githash,
91 "EMERGE_DEFAULT_OPTS": "--exclude=gcc"})
92
93
94class ToolchainComparator(ChromeOSCheckout):
asharif58a8c9f2013-02-19 20:42:43 +000095 def __init__(self, board, remotes, configs, clean, public, force_mismatch):
asharif96f59692013-02-16 03:13:36 +000096 self._board = board
97 self._remotes = remotes
98 self._chromeos_root = "chromeos"
99 self._configs = configs
asharif3e38de02013-02-19 19:34:59 +0000100 self._clean = clean
asharife6b72fe2013-02-19 19:58:18 +0000101 self._public = public
asharif58a8c9f2013-02-19 20:42:43 +0000102 self._force_mismatch = force_mismatch
asharif96f59692013-02-16 03:13:36 +0000103 self._ce = command_executer.GetCommandExecuter()
104 self._l = logger.GetLogger()
105 ChromeOSCheckout.__init__(self, board, self._chromeos_root)
106
107 def _TestLabels(self, labels):
108 experiment_file = "toolchain_experiment.txt"
asharif58a8c9f2013-02-19 20:42:43 +0000109 image_args = ""
110 if self._force_mismatch:
asharif0b5d5c82013-02-19 20:42:56 +0000111 image_args = "--force-mismatch"
asharif96f59692013-02-16 03:13:36 +0000112 experiment_header = """
113 board: %s
114 remote: %s
115 """ % (self._board, self._remotes)
116 experiment_tests = """
117 benchmark: desktopui_PyAutoPerfTests {
ashariffce80422013-02-19 20:20:11 +0000118 iterations: 1
asharif96f59692013-02-16 03:13:36 +0000119 }
120 """
121 with open(experiment_file, "w") as f:
122 print >>f, experiment_header
123 print >>f, experiment_tests
124 for label in labels:
125 # TODO(asharif): Fix crosperf so it accepts labels with symbols
126 crosperf_label = label
127 crosperf_label = crosperf_label.replace("-", "minus")
128 crosperf_label = crosperf_label.replace("+", "plus")
asharife4a5a8f2013-02-19 20:19:33 +0000129 crosperf_label = crosperf_label.replace(".", "")
asharif96f59692013-02-16 03:13:36 +0000130 experiment_image = """
131 %s {
132 chromeos_image: %s
asharif58a8c9f2013-02-19 20:42:43 +0000133 image_args: %s
asharif96f59692013-02-16 03:13:36 +0000134 }
135 """ % (crosperf_label,
136 os.path.join(misc.GetImageDir(self._chromeos_root, self._board),
137 label,
asharif58a8c9f2013-02-19 20:42:43 +0000138 "chromiumos_test_image.bin"),
139 image_args)
asharif96f59692013-02-16 03:13:36 +0000140 print >>f, experiment_image
141 crosperf = os.path.join(os.path.dirname(__file__),
142 "crosperf",
143 "crosperf")
asharifb2cd6342013-02-19 19:42:57 +0000144 command = "%s --email=c-compiler-chrome %s" % (crosperf, experiment_file)
asharif96f59692013-02-16 03:13:36 +0000145 ret = self._ce.RunCommand(command)
146 if ret:
147 raise Exception("Couldn't run crosperf!")
148
149 def DoAll(self):
150 self._CheckoutChromeOS()
asharif96f59692013-02-16 03:13:36 +0000151 labels = []
152 vanilla_label = self._BuildAndImage("vanilla")
153 labels.append(vanilla_label)
154 for config in self._configs:
155 label = misc.GetFilenameFromString(config.gcc_config.githash)
156 if (not misc.DoesLabelExist(self._chromeos_root,
157 self._board,
158 label)):
159 self._BuildToolchain(config)
160 label = self._BuildAndImage(label)
161 labels.append(label)
162 self._TestLabels(labels)
asharif3e38de02013-02-19 19:34:59 +0000163 if self._clean:
164 ret = self._DeleteChroot()
165 if ret: return ret
asharif67973582013-02-19 20:19:40 +0000166 ret = self._DeleteCcahe()
167 if ret: return ret
asharif96f59692013-02-16 03:13:36 +0000168 return 0
169
170
171def Main(argv):
172 """The main function."""
173 # Common initializations
174### command_executer.InitCommandExecuter(True)
175 command_executer.InitCommandExecuter()
176 parser = optparse.OptionParser()
177 parser.add_option("--remote",
178 dest="remote",
179 help="Remote machines to run tests on.")
180 parser.add_option("--board",
181 dest="board",
182 default="x86-zgb",
183 help="The target board.")
184 parser.add_option("--githashes",
185 dest="githashes",
186 default="master",
187 help="The gcc githashes to test.")
asharif3e38de02013-02-19 19:34:59 +0000188 parser.add_option("--clean",
189 dest="clean",
190 default=False,
191 action="store_true",
192 help="Clean the chroot after testing.")
asharife6b72fe2013-02-19 19:58:18 +0000193 parser.add_option("--public",
194 dest="public",
195 default=False,
196 action="store_true",
197 help="Use the public checkout/build.")
asharif58a8c9f2013-02-19 20:42:43 +0000198 parser.add_option("--force-mismatch",
199 dest="force_mismatch",
200 default="",
201 help="Force the image regardless of board mismatch")
asharif96f59692013-02-16 03:13:36 +0000202 options, _ = parser.parse_args(argv)
203 if not options.board:
204 print "Please give a board."
205 return 1
206 if not options.remote:
207 print "Please give at least one remote machine."
208 return 1
209 toolchain_configs = []
210 for githash in options.githashes.split(","):
211 gcc_config = GCCConfig(githash=githash)
212 toolchain_config = ToolchainConfig(gcc_config=gcc_config)
213 toolchain_configs.append(toolchain_config)
asharif3e38de02013-02-19 19:34:59 +0000214 fc = ToolchainComparator(options.board, options.remote, toolchain_configs,
asharif58a8c9f2013-02-19 20:42:43 +0000215 options.clean, options.public,
216 options.force_mismatch)
asharif96f59692013-02-16 03:13:36 +0000217 return fc.DoAll()
218
219
220if __name__ == "__main__":
221 retval = Main(sys.argv)
222 sys.exit(retval)