blob: 9c1e6ac15e913dd3a44904f44cf987046c069bec [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")
asharif96f59692013-02-16 03:13:36 +000078 setup_chromeos.Main(setup_chromeos_args)
79
80 def _BuildToolchain(self, config):
81 self._UnInstallToolchain()
82 self._SetupBoard({"USE": "git_gcc",
83 "GCC_GITHASH": config.gcc_config.githash,
84 "EMERGE_DEFAULT_OPTS": "--exclude=gcc"})
85
86
87class ToolchainComparator(ChromeOSCheckout):
asharife6b72fe2013-02-19 19:58:18 +000088 def __init__(self, board, remotes, configs, clean, public):
asharif96f59692013-02-16 03:13:36 +000089 self._board = board
90 self._remotes = remotes
91 self._chromeos_root = "chromeos"
92 self._configs = configs
asharif3e38de02013-02-19 19:34:59 +000093 self._clean = clean
asharife6b72fe2013-02-19 19:58:18 +000094 self._public = public
asharif96f59692013-02-16 03:13:36 +000095 self._ce = command_executer.GetCommandExecuter()
96 self._l = logger.GetLogger()
97 ChromeOSCheckout.__init__(self, board, self._chromeos_root)
98
99 def _TestLabels(self, labels):
100 experiment_file = "toolchain_experiment.txt"
101 experiment_header = """
102 board: %s
103 remote: %s
104 """ % (self._board, self._remotes)
105 experiment_tests = """
106 benchmark: desktopui_PyAutoPerfTests {
107 iterations: 1
108 }
109 """
110 with open(experiment_file, "w") as f:
111 print >>f, experiment_header
112 print >>f, experiment_tests
113 for label in labels:
114 # TODO(asharif): Fix crosperf so it accepts labels with symbols
115 crosperf_label = label
116 crosperf_label = crosperf_label.replace("-", "minus")
117 crosperf_label = crosperf_label.replace("+", "plus")
118 experiment_image = """
119 %s {
120 chromeos_image: %s
121 }
122 """ % (crosperf_label,
123 os.path.join(misc.GetImageDir(self._chromeos_root, self._board),
124 label,
125 "chromiumos_test_image.bin"))
126 print >>f, experiment_image
127 crosperf = os.path.join(os.path.dirname(__file__),
128 "crosperf",
129 "crosperf")
asharifb2cd6342013-02-19 19:42:57 +0000130 command = "%s --email=c-compiler-chrome %s" % (crosperf, experiment_file)
asharif96f59692013-02-16 03:13:36 +0000131 ret = self._ce.RunCommand(command)
132 if ret:
133 raise Exception("Couldn't run crosperf!")
134
135 def DoAll(self):
136 self._CheckoutChromeOS()
asharif96f59692013-02-16 03:13:36 +0000137 labels = []
138 vanilla_label = self._BuildAndImage("vanilla")
139 labels.append(vanilla_label)
140 for config in self._configs:
141 label = misc.GetFilenameFromString(config.gcc_config.githash)
142 if (not misc.DoesLabelExist(self._chromeos_root,
143 self._board,
144 label)):
145 self._BuildToolchain(config)
146 label = self._BuildAndImage(label)
147 labels.append(label)
148 self._TestLabels(labels)
asharif3e38de02013-02-19 19:34:59 +0000149 if self._clean:
150 ret = self._DeleteChroot()
151 if ret: return ret
asharif96f59692013-02-16 03:13:36 +0000152 return 0
153
154
155def Main(argv):
156 """The main function."""
157 # Common initializations
158### command_executer.InitCommandExecuter(True)
159 command_executer.InitCommandExecuter()
160 parser = optparse.OptionParser()
161 parser.add_option("--remote",
162 dest="remote",
163 help="Remote machines to run tests on.")
164 parser.add_option("--board",
165 dest="board",
166 default="x86-zgb",
167 help="The target board.")
168 parser.add_option("--githashes",
169 dest="githashes",
170 default="master",
171 help="The gcc githashes to test.")
asharif3e38de02013-02-19 19:34:59 +0000172 parser.add_option("--clean",
173 dest="clean",
174 default=False,
175 action="store_true",
176 help="Clean the chroot after testing.")
asharife6b72fe2013-02-19 19:58:18 +0000177 parser.add_option("--public",
178 dest="public",
179 default=False,
180 action="store_true",
181 help="Use the public checkout/build.")
asharif96f59692013-02-16 03:13:36 +0000182 options, _ = parser.parse_args(argv)
183 if not options.board:
184 print "Please give a board."
185 return 1
186 if not options.remote:
187 print "Please give at least one remote machine."
188 return 1
189 toolchain_configs = []
190 for githash in options.githashes.split(","):
191 gcc_config = GCCConfig(githash=githash)
192 toolchain_config = ToolchainConfig(gcc_config=gcc_config)
193 toolchain_configs.append(toolchain_config)
asharif3e38de02013-02-19 19:34:59 +0000194 fc = ToolchainComparator(options.board, options.remote, toolchain_configs,
asharife6b72fe2013-02-19 19:58:18 +0000195 options.clean, options.public)
asharif96f59692013-02-16 03:13:36 +0000196 return fc.DoAll()
197
198
199if __name__ == "__main__":
200 retval = Main(sys.argv)
201 sys.exit(retval)