asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Script to test different toolchains against ChromeOS benchmarks. |
| 4 | import optparse |
| 5 | import os |
| 6 | import sys |
| 7 | import build_chromeos |
| 8 | import setup_chromeos |
| 9 | from utils import command_executer |
| 10 | from utils import misc |
| 11 | from utils import logger |
| 12 | |
| 13 | |
| 14 | class GCCConfig(object): |
| 15 | def __init__(self, githash): |
| 16 | self.githash = githash |
| 17 | |
| 18 | |
| 19 | class ToolchainConfig: |
| 20 | def __init__(self, gcc_config=None, binutils_config=None): |
| 21 | self.gcc_config = gcc_config |
| 22 | |
| 23 | |
| 24 | class 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 | |
asharif | 3e38de0 | 2013-02-19 19:34:59 +0000 | [diff] [blame] | 31 | def _DeleteChroot(self): |
asharif | 8741427 | 2013-02-19 19:58:45 +0000 | [diff] [blame] | 32 | command = "cd %s; cros_sdk --delete" % self._chromeos_root |
asharif | 3e38de0 | 2013-02-19 19:34:59 +0000 | [diff] [blame] | 33 | return self._ce.RunCommand(command) |
| 34 | |
asharif | 6797358 | 2013-02-19 20:19:40 +0000 | [diff] [blame] | 35 | 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 | |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 40 | 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"] |
asharif | e6b72fe | 2013-02-19 19:58:18 +0000 | [diff] [blame] | 47 | if self._public: |
| 48 | build_chromeos_args.append("--env=USE=-chrome_internal") |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 49 | 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"] |
asharif | e6b72fe | 2013-02-19 19:58:18 +0000 | [diff] [blame] | 81 | if self._public: |
| 82 | setup_chromeos_args.append("--public") |
asharif | 5fe40e2 | 2013-02-19 19:58:50 +0000 | [diff] [blame] | 83 | ret = setup_chromeos.Main(setup_chromeos_args) |
| 84 | if ret: |
| 85 | raise Exception("Couldn't run setup_chromeos!") |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 86 | |
| 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 | |
| 94 | class ToolchainComparator(ChromeOSCheckout): |
asharif | 58a8c9f | 2013-02-19 20:42:43 +0000 | [diff] [blame] | 95 | def __init__(self, board, remotes, configs, clean, public, force_mismatch): |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 96 | self._board = board |
| 97 | self._remotes = remotes |
| 98 | self._chromeos_root = "chromeos" |
| 99 | self._configs = configs |
asharif | 3e38de0 | 2013-02-19 19:34:59 +0000 | [diff] [blame] | 100 | self._clean = clean |
asharif | e6b72fe | 2013-02-19 19:58:18 +0000 | [diff] [blame] | 101 | self._public = public |
asharif | 58a8c9f | 2013-02-19 20:42:43 +0000 | [diff] [blame] | 102 | self._force_mismatch = force_mismatch |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 103 | 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" |
asharif | 58a8c9f | 2013-02-19 20:42:43 +0000 | [diff] [blame] | 109 | image_args = "" |
| 110 | if self._force_mismatch: |
asharif | 0b5d5c8 | 2013-02-19 20:42:56 +0000 | [diff] [blame] | 111 | image_args = "--force-mismatch" |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 112 | experiment_header = """ |
| 113 | board: %s |
| 114 | remote: %s |
| 115 | """ % (self._board, self._remotes) |
| 116 | experiment_tests = """ |
| 117 | benchmark: desktopui_PyAutoPerfTests { |
asharif | fce8042 | 2013-02-19 20:20:11 +0000 | [diff] [blame] | 118 | iterations: 1 |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 119 | } |
| 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") |
asharif | e4a5a8f | 2013-02-19 20:19:33 +0000 | [diff] [blame] | 129 | crosperf_label = crosperf_label.replace(".", "") |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 130 | experiment_image = """ |
| 131 | %s { |
| 132 | chromeos_image: %s |
asharif | 58a8c9f | 2013-02-19 20:42:43 +0000 | [diff] [blame] | 133 | image_args: %s |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 134 | } |
| 135 | """ % (crosperf_label, |
| 136 | os.path.join(misc.GetImageDir(self._chromeos_root, self._board), |
| 137 | label, |
asharif | 58a8c9f | 2013-02-19 20:42:43 +0000 | [diff] [blame] | 138 | "chromiumos_test_image.bin"), |
| 139 | image_args) |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 140 | print >>f, experiment_image |
| 141 | crosperf = os.path.join(os.path.dirname(__file__), |
| 142 | "crosperf", |
| 143 | "crosperf") |
asharif | b2cd634 | 2013-02-19 19:42:57 +0000 | [diff] [blame] | 144 | command = "%s --email=c-compiler-chrome %s" % (crosperf, experiment_file) |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 145 | ret = self._ce.RunCommand(command) |
| 146 | if ret: |
| 147 | raise Exception("Couldn't run crosperf!") |
| 148 | |
| 149 | def DoAll(self): |
| 150 | self._CheckoutChromeOS() |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 151 | 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) |
asharif | 3e38de0 | 2013-02-19 19:34:59 +0000 | [diff] [blame] | 163 | if self._clean: |
| 164 | ret = self._DeleteChroot() |
| 165 | if ret: return ret |
asharif | 6797358 | 2013-02-19 20:19:40 +0000 | [diff] [blame] | 166 | ret = self._DeleteCcahe() |
| 167 | if ret: return ret |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 168 | return 0 |
| 169 | |
| 170 | |
| 171 | def 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.") |
asharif | 3e38de0 | 2013-02-19 19:34:59 +0000 | [diff] [blame] | 188 | parser.add_option("--clean", |
| 189 | dest="clean", |
| 190 | default=False, |
| 191 | action="store_true", |
| 192 | help="Clean the chroot after testing.") |
asharif | e6b72fe | 2013-02-19 19:58:18 +0000 | [diff] [blame] | 193 | parser.add_option("--public", |
| 194 | dest="public", |
| 195 | default=False, |
| 196 | action="store_true", |
| 197 | help="Use the public checkout/build.") |
asharif | 58a8c9f | 2013-02-19 20:42:43 +0000 | [diff] [blame] | 198 | parser.add_option("--force-mismatch", |
| 199 | dest="force_mismatch", |
| 200 | default="", |
| 201 | help="Force the image regardless of board mismatch") |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 202 | 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) |
asharif | 3e38de0 | 2013-02-19 19:34:59 +0000 | [diff] [blame] | 214 | fc = ToolchainComparator(options.board, options.remote, toolchain_configs, |
asharif | 58a8c9f | 2013-02-19 20:42:43 +0000 | [diff] [blame] | 215 | options.clean, options.public, |
| 216 | options.force_mismatch) |
asharif | 96f5969 | 2013-02-16 03:13:36 +0000 | [diff] [blame] | 217 | return fc.DoAll() |
| 218 | |
| 219 | |
| 220 | if __name__ == "__main__": |
| 221 | retval = Main(sys.argv) |
| 222 | sys.exit(retval) |