blob: a457482e5ad7722af2a1401e274623218858cae7 [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")
120 experiment_image = """
121 %s {
122 chromeos_image: %s
123 }
124 """ % (crosperf_label,
125 os.path.join(misc.GetImageDir(self._chromeos_root, self._board),
126 label,
127 "chromiumos_test_image.bin"))
128 print >>f, experiment_image
129 crosperf = os.path.join(os.path.dirname(__file__),
130 "crosperf",
131 "crosperf")
asharifb2cd6342013-02-19 19:42:57 +0000132 command = "%s --email=c-compiler-chrome %s" % (crosperf, experiment_file)
asharif96f59692013-02-16 03:13:36 +0000133 ret = self._ce.RunCommand(command)
134 if ret:
135 raise Exception("Couldn't run crosperf!")
136
137 def DoAll(self):
138 self._CheckoutChromeOS()
asharif96f59692013-02-16 03:13:36 +0000139 labels = []
140 vanilla_label = self._BuildAndImage("vanilla")
141 labels.append(vanilla_label)
142 for config in self._configs:
143 label = misc.GetFilenameFromString(config.gcc_config.githash)
144 if (not misc.DoesLabelExist(self._chromeos_root,
145 self._board,
146 label)):
147 self._BuildToolchain(config)
148 label = self._BuildAndImage(label)
149 labels.append(label)
150 self._TestLabels(labels)
asharif3e38de02013-02-19 19:34:59 +0000151 if self._clean:
152 ret = self._DeleteChroot()
153 if ret: return ret
asharif96f59692013-02-16 03:13:36 +0000154 return 0
155
156
157def Main(argv):
158 """The main function."""
159 # Common initializations
160### command_executer.InitCommandExecuter(True)
161 command_executer.InitCommandExecuter()
162 parser = optparse.OptionParser()
163 parser.add_option("--remote",
164 dest="remote",
165 help="Remote machines to run tests on.")
166 parser.add_option("--board",
167 dest="board",
168 default="x86-zgb",
169 help="The target board.")
170 parser.add_option("--githashes",
171 dest="githashes",
172 default="master",
173 help="The gcc githashes to test.")
asharif3e38de02013-02-19 19:34:59 +0000174 parser.add_option("--clean",
175 dest="clean",
176 default=False,
177 action="store_true",
178 help="Clean the chroot after testing.")
asharife6b72fe2013-02-19 19:58:18 +0000179 parser.add_option("--public",
180 dest="public",
181 default=False,
182 action="store_true",
183 help="Use the public checkout/build.")
asharif96f59692013-02-16 03:13:36 +0000184 options, _ = parser.parse_args(argv)
185 if not options.board:
186 print "Please give a board."
187 return 1
188 if not options.remote:
189 print "Please give at least one remote machine."
190 return 1
191 toolchain_configs = []
192 for githash in options.githashes.split(","):
193 gcc_config = GCCConfig(githash=githash)
194 toolchain_config = ToolchainConfig(gcc_config=gcc_config)
195 toolchain_configs.append(toolchain_config)
asharif3e38de02013-02-19 19:34:59 +0000196 fc = ToolchainComparator(options.board, options.remote, toolchain_configs,
asharife6b72fe2013-02-19 19:58:18 +0000197 options.clean, options.public)
asharif96f59692013-02-16 03:13:36 +0000198 return fc.DoAll()
199
200
201if __name__ == "__main__":
202 retval = Main(sys.argv)
203 sys.exit(retval)