blob: 37e0470a13f568b6dba5618693bca39d221e73b9 [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
asharif96f59692013-02-16 03:13:36 +000035 def _BuildAndImage(self, label=""):
36 if (not label or
37 not misc.DoesLabelExist(self._chromeos_root, self._board, label)):
38 build_chromeos_args = [build_chromeos.__file__,
39 "--chromeos_root=%s" % self._chromeos_root,
40 "--board=%s" % self._board,
41 "--rebuild"]
asharife6b72fe2013-02-19 19:58:18 +000042 if self._public:
43 build_chromeos_args.append("--env=USE=-chrome_internal")
asharif96f59692013-02-16 03:13:36 +000044 ret = build_chromeos.Main(build_chromeos_args)
45 if ret:
46 raise Exception("Couldn't build ChromeOS!")
47 if label:
48 misc.LabelLatestImage(self._chromeos_root, self._board, label)
49 return label
50
51 def _SetupBoard(self, env_dict):
52 env_string = misc.GetEnvStringFromDict(env_dict)
53 command = ("%s %s" %
54 (env_string,
55 misc.GetSetupBoardCommand(self._board,
56 usepkg=False)))
57 ret = self._ce.ChrootRunCommand(self._chromeos_root,
58 command)
59 assert ret == 0, "Could not setup board with new toolchain."
60
61 def _UnInstallToolchain(self):
62 command = ("sudo CLEAN_DELAY=0 emerge -C cross-%s/gcc" %
63 misc.GetCtargetFromBoard(self._board,
64 self._chromeos_root))
65 ret = self._ce.ChrootRunCommand(self._chromeos_root,
66 command)
67 if ret:
68 raise Exception("Couldn't uninstall the toolchain!")
69
70 def _CheckoutChromeOS(self):
71 # TODO(asharif): Setup a fixed ChromeOS version (quarterly snapshot).
72 if not os.path.exists(self._chromeos_root):
73 setup_chromeos_args = [setup_chromeos.__file__,
74 "--dir=%s" % self._chromeos_root,
75 "--minilayout"]
asharife6b72fe2013-02-19 19:58:18 +000076 if self._public:
77 setup_chromeos_args.append("--public")
asharif5fe40e22013-02-19 19:58:50 +000078 ret = setup_chromeos.Main(setup_chromeos_args)
79 if ret:
80 raise Exception("Couldn't run setup_chromeos!")
asharif96f59692013-02-16 03:13:36 +000081
82 def _BuildToolchain(self, config):
83 self._UnInstallToolchain()
84 self._SetupBoard({"USE": "git_gcc",
85 "GCC_GITHASH": config.gcc_config.githash,
86 "EMERGE_DEFAULT_OPTS": "--exclude=gcc"})
87
88
89class ToolchainComparator(ChromeOSCheckout):
asharife6b72fe2013-02-19 19:58:18 +000090 def __init__(self, board, remotes, configs, clean, public):
asharif96f59692013-02-16 03:13:36 +000091 self._board = board
92 self._remotes = remotes
93 self._chromeos_root = "chromeos"
94 self._configs = configs
asharif3e38de02013-02-19 19:34:59 +000095 self._clean = clean
asharife6b72fe2013-02-19 19:58:18 +000096 self._public = public
asharif96f59692013-02-16 03:13:36 +000097 self._ce = command_executer.GetCommandExecuter()
98 self._l = logger.GetLogger()
99 ChromeOSCheckout.__init__(self, board, self._chromeos_root)
100
101 def _TestLabels(self, labels):
102 experiment_file = "toolchain_experiment.txt"
103 experiment_header = """
104 board: %s
105 remote: %s
106 """ % (self._board, self._remotes)
107 experiment_tests = """
108 benchmark: desktopui_PyAutoPerfTests {
109 iterations: 1
110 }
111 """
112 with open(experiment_file, "w") as f:
113 print >>f, experiment_header
114 print >>f, experiment_tests
115 for label in labels:
116 # TODO(asharif): Fix crosperf so it accepts labels with symbols
117 crosperf_label = label
118 crosperf_label = crosperf_label.replace("-", "minus")
119 crosperf_label = crosperf_label.replace("+", "plus")
asharife4a5a8f2013-02-19 20:19:33 +0000120 crosperf_label = crosperf_label.replace(".", "")
asharif96f59692013-02-16 03:13:36 +0000121 experiment_image = """
122 %s {
123 chromeos_image: %s
124 }
125 """ % (crosperf_label,
126 os.path.join(misc.GetImageDir(self._chromeos_root, self._board),
127 label,
128 "chromiumos_test_image.bin"))
129 print >>f, experiment_image
130 crosperf = os.path.join(os.path.dirname(__file__),
131 "crosperf",
132 "crosperf")
asharifb2cd6342013-02-19 19:42:57 +0000133 command = "%s --email=c-compiler-chrome %s" % (crosperf, experiment_file)
asharif96f59692013-02-16 03:13:36 +0000134 ret = self._ce.RunCommand(command)
135 if ret:
136 raise Exception("Couldn't run crosperf!")
137
138 def DoAll(self):
139 self._CheckoutChromeOS()
asharif96f59692013-02-16 03:13:36 +0000140 labels = []
141 vanilla_label = self._BuildAndImage("vanilla")
142 labels.append(vanilla_label)
143 for config in self._configs:
144 label = misc.GetFilenameFromString(config.gcc_config.githash)
145 if (not misc.DoesLabelExist(self._chromeos_root,
146 self._board,
147 label)):
148 self._BuildToolchain(config)
149 label = self._BuildAndImage(label)
150 labels.append(label)
151 self._TestLabels(labels)
asharif3e38de02013-02-19 19:34:59 +0000152 if self._clean:
153 ret = self._DeleteChroot()
154 if ret: return ret
asharif96f59692013-02-16 03:13:36 +0000155 return 0
156
157
158def Main(argv):
159 """The main function."""
160 # Common initializations
161### command_executer.InitCommandExecuter(True)
162 command_executer.InitCommandExecuter()
163 parser = optparse.OptionParser()
164 parser.add_option("--remote",
165 dest="remote",
166 help="Remote machines to run tests on.")
167 parser.add_option("--board",
168 dest="board",
169 default="x86-zgb",
170 help="The target board.")
171 parser.add_option("--githashes",
172 dest="githashes",
173 default="master",
174 help="The gcc githashes to test.")
asharif3e38de02013-02-19 19:34:59 +0000175 parser.add_option("--clean",
176 dest="clean",
177 default=False,
178 action="store_true",
179 help="Clean the chroot after testing.")
asharife6b72fe2013-02-19 19:58:18 +0000180 parser.add_option("--public",
181 dest="public",
182 default=False,
183 action="store_true",
184 help="Use the public checkout/build.")
asharif96f59692013-02-16 03:13:36 +0000185 options, _ = parser.parse_args(argv)
186 if not options.board:
187 print "Please give a board."
188 return 1
189 if not options.remote:
190 print "Please give at least one remote machine."
191 return 1
192 toolchain_configs = []
193 for githash in options.githashes.split(","):
194 gcc_config = GCCConfig(githash=githash)
195 toolchain_config = ToolchainConfig(gcc_config=gcc_config)
196 toolchain_configs.append(toolchain_config)
asharif3e38de02013-02-19 19:34:59 +0000197 fc = ToolchainComparator(options.board, options.remote, toolchain_configs,
asharife6b72fe2013-02-19 19:58:18 +0000198 options.clean, options.public)
asharif96f59692013-02-16 03:13:36 +0000199 return fc.DoAll()
200
201
202if __name__ == "__main__":
203 retval = Main(sys.argv)
204 sys.exit(retval)