blob: df855608d6db8f52c3b94b3852d420bba34a8224 [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):
asharife6b72fe2013-02-19 19:58:18 +000095 def __init__(self, board, remotes, configs, clean, public):
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
asharif96f59692013-02-16 03:13:36 +0000102 self._ce = command_executer.GetCommandExecuter()
103 self._l = logger.GetLogger()
104 ChromeOSCheckout.__init__(self, board, self._chromeos_root)
105
106 def _TestLabels(self, labels):
107 experiment_file = "toolchain_experiment.txt"
108 experiment_header = """
109 board: %s
110 remote: %s
111 """ % (self._board, self._remotes)
112 experiment_tests = """
113 benchmark: desktopui_PyAutoPerfTests {
asharif8a8ce962013-02-19 20:20:04 +0000114 iterations: 6
asharif96f59692013-02-16 03:13:36 +0000115 }
116 """
117 with open(experiment_file, "w") as f:
118 print >>f, experiment_header
119 print >>f, experiment_tests
120 for label in labels:
121 # TODO(asharif): Fix crosperf so it accepts labels with symbols
122 crosperf_label = label
123 crosperf_label = crosperf_label.replace("-", "minus")
124 crosperf_label = crosperf_label.replace("+", "plus")
asharife4a5a8f2013-02-19 20:19:33 +0000125 crosperf_label = crosperf_label.replace(".", "")
asharif96f59692013-02-16 03:13:36 +0000126 experiment_image = """
127 %s {
128 chromeos_image: %s
129 }
130 """ % (crosperf_label,
131 os.path.join(misc.GetImageDir(self._chromeos_root, self._board),
132 label,
133 "chromiumos_test_image.bin"))
134 print >>f, experiment_image
135 crosperf = os.path.join(os.path.dirname(__file__),
136 "crosperf",
137 "crosperf")
asharifb2cd6342013-02-19 19:42:57 +0000138 command = "%s --email=c-compiler-chrome %s" % (crosperf, experiment_file)
asharif96f59692013-02-16 03:13:36 +0000139 ret = self._ce.RunCommand(command)
140 if ret:
141 raise Exception("Couldn't run crosperf!")
142
143 def DoAll(self):
144 self._CheckoutChromeOS()
asharif96f59692013-02-16 03:13:36 +0000145 labels = []
146 vanilla_label = self._BuildAndImage("vanilla")
147 labels.append(vanilla_label)
148 for config in self._configs:
149 label = misc.GetFilenameFromString(config.gcc_config.githash)
150 if (not misc.DoesLabelExist(self._chromeos_root,
151 self._board,
152 label)):
153 self._BuildToolchain(config)
154 label = self._BuildAndImage(label)
155 labels.append(label)
156 self._TestLabels(labels)
asharif3e38de02013-02-19 19:34:59 +0000157 if self._clean:
158 ret = self._DeleteChroot()
159 if ret: return ret
asharif67973582013-02-19 20:19:40 +0000160 ret = self._DeleteCcahe()
161 if ret: return ret
asharif96f59692013-02-16 03:13:36 +0000162 return 0
163
164
165def Main(argv):
166 """The main function."""
167 # Common initializations
168### command_executer.InitCommandExecuter(True)
169 command_executer.InitCommandExecuter()
170 parser = optparse.OptionParser()
171 parser.add_option("--remote",
172 dest="remote",
173 help="Remote machines to run tests on.")
174 parser.add_option("--board",
175 dest="board",
176 default="x86-zgb",
177 help="The target board.")
178 parser.add_option("--githashes",
179 dest="githashes",
180 default="master",
181 help="The gcc githashes to test.")
asharif3e38de02013-02-19 19:34:59 +0000182 parser.add_option("--clean",
183 dest="clean",
184 default=False,
185 action="store_true",
186 help="Clean the chroot after testing.")
asharife6b72fe2013-02-19 19:58:18 +0000187 parser.add_option("--public",
188 dest="public",
189 default=False,
190 action="store_true",
191 help="Use the public checkout/build.")
asharif96f59692013-02-16 03:13:36 +0000192 options, _ = parser.parse_args(argv)
193 if not options.board:
194 print "Please give a board."
195 return 1
196 if not options.remote:
197 print "Please give at least one remote machine."
198 return 1
199 toolchain_configs = []
200 for githash in options.githashes.split(","):
201 gcc_config = GCCConfig(githash=githash)
202 toolchain_config = ToolchainConfig(gcc_config=gcc_config)
203 toolchain_configs.append(toolchain_config)
asharif3e38de02013-02-19 19:34:59 +0000204 fc = ToolchainComparator(options.board, options.remote, toolchain_configs,
asharife6b72fe2013-02-19 19:58:18 +0000205 options.clean, options.public)
asharif96f59692013-02-16 03:13:36 +0000206 return fc.DoAll()
207
208
209if __name__ == "__main__":
210 retval = Main(sys.argv)
211 sys.exit(retval)