blob: ecae6f4836f7e968b8c643d4a4aaf6075540598e [file] [log] [blame]
Caroline Tice88272d42016-01-13 09:48:29 -08001#!/usr/bin/python2
asharif96f59692013-02-16 03:13:36 +00002
3# Script to test different toolchains against ChromeOS benchmarks.
Caroline Tice88272d42016-01-13 09:48:29 -08004"""Toolchain team nightly performance test script (local builds)."""
5
6from __future__ import print_function
7
8import argparse
cmtice7cdb11a2015-05-28 10:24:41 -07009import datetime
asharif96f59692013-02-16 03:13:36 +000010import os
11import sys
12import build_chromeos
13import setup_chromeos
cmtice94bc4702014-05-29 16:29:04 -070014import time
Caroline Tice88272d42016-01-13 09:48:29 -080015from cros_utils import command_executer
16from cros_utils import misc
17from cros_utils import logger
asharif96f59692013-02-16 03:13:36 +000018
Luis Lozanof2a3ef42015-12-15 13:49:30 -080019CROSTC_ROOT = '/usr/local/google/crostc'
20MAIL_PROGRAM = '~/var/bin/mail-sheriff'
Luis Lozanof2a3ef42015-12-15 13:49:30 -080021PENDING_ARCHIVES_DIR = os.path.join(CROSTC_ROOT, 'pending_archives')
22NIGHTLY_TESTS_DIR = os.path.join(CROSTC_ROOT, 'nightly_test_reports')
asharif96f59692013-02-16 03:13:36 +000023
cmtice94bc4702014-05-29 16:29:04 -070024
asharif96f59692013-02-16 03:13:36 +000025class GCCConfig(object):
Caroline Tice88272d42016-01-13 09:48:29 -080026 """GCC configuration class."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080027
asharif96f59692013-02-16 03:13:36 +000028 def __init__(self, githash):
29 self.githash = githash
30
31
Caroline Tice88272d42016-01-13 09:48:29 -080032class ToolchainConfig(object):
33 """Toolchain configuration class."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080034
Caroline Tice88272d42016-01-13 09:48:29 -080035 def __init__(self, gcc_config=None):
asharif96f59692013-02-16 03:13:36 +000036 self.gcc_config = gcc_config
37
38
39class ChromeOSCheckout(object):
Caroline Tice88272d42016-01-13 09:48:29 -080040 """Main class for checking out, building and testing ChromeOS."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -080041
asharif96f59692013-02-16 03:13:36 +000042 def __init__(self, board, chromeos_root):
43 self._board = board
44 self._chromeos_root = chromeos_root
45 self._ce = command_executer.GetCommandExecuter()
46 self._l = logger.GetLogger()
cmtice56fb7162014-06-18 11:32:15 -070047 self._build_num = None
asharif96f59692013-02-16 03:13:36 +000048
asharif3e38de02013-02-19 19:34:59 +000049 def _DeleteChroot(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080050 command = 'cd %s; cros_sdk --delete' % self._chromeos_root
asharif3e38de02013-02-19 19:34:59 +000051 return self._ce.RunCommand(command)
52
asharif67973582013-02-19 20:19:40 +000053 def _DeleteCcahe(self):
54 # crosbug.com/34956
Luis Lozanof2a3ef42015-12-15 13:49:30 -080055 command = 'sudo rm -rf %s' % os.path.join(self._chromeos_root, '.cache')
asharif67973582013-02-19 20:19:40 +000056 return self._ce.RunCommand(command)
57
cmtice56fb7162014-06-18 11:32:15 -070058 def _GetBuildNumber(self):
Caroline Tice88272d42016-01-13 09:48:29 -080059 """Get the build number of the ChromeOS image from the chroot.
60
61 This function assumes a ChromeOS image has been built in the chroot.
cmtice56fb7162014-06-18 11:32:15 -070062 It translates the 'latest' symlink in the
63 <chroot>/src/build/images/<board> directory, to find the actual
64 ChromeOS build number for the image that was built. For example, if
65 src/build/image/lumpy/latest -> R37-5982.0.2014_06_23_0454-a1, then
66 This function would parse it out and assign 'R37-5982' to self._build_num.
67 This is used to determine the official, vanilla build to use for
68 comparison tests.
69 """
70 # Get the path to 'latest'
Luis Lozanof2a3ef42015-12-15 13:49:30 -080071 sym_path = os.path.join(
72 misc.GetImageDir(self._chromeos_root, self._board), 'latest')
cmtice56fb7162014-06-18 11:32:15 -070073 # Translate the symlink to its 'real' path.
74 real_path = os.path.realpath(sym_path)
75 # Break up the path and get the last piece
76 # (e.g. 'R37-5982.0.2014_06_23_0454-a1"
Luis Lozanof2a3ef42015-12-15 13:49:30 -080077 path_pieces = real_path.split('/')
cmtice56fb7162014-06-18 11:32:15 -070078 last_piece = path_pieces[-1]
79 # Break this piece into the image number + other pieces, and get the
80 # image number [ 'R37-5982', '0', '2014_06_23_0454-a1']
Luis Lozanof2a3ef42015-12-15 13:49:30 -080081 image_parts = last_piece.split('.')
cmtice56fb7162014-06-18 11:32:15 -070082 self._build_num = image_parts[0]
83
Caroline Tice88272d42016-01-13 09:48:29 -080084 def _BuildLabelName(self, config):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080085 pieces = config.split('/')
Caroline Tice80eab982015-11-04 14:03:14 -080086 compiler_version = pieces[-1]
Luis Lozanof2a3ef42015-12-15 13:49:30 -080087 label = compiler_version + '_tot_afdo'
Caroline Tice80eab982015-11-04 14:03:14 -080088 return label
89
Luis Lozanof2a3ef42015-12-15 13:49:30 -080090 def _BuildAndImage(self, label=''):
asharif96f59692013-02-16 03:13:36 +000091 if (not label or
92 not misc.DoesLabelExist(self._chromeos_root, self._board, label)):
Manoj Guptaaee96b72016-10-24 13:43:28 -070093 build_chromeos_args = [
94 build_chromeos.__file__, '--chromeos_root=%s' % self._chromeos_root,
95 '--board=%s' % self._board, '--rebuild'
96 ]
asharife6b72fe2013-02-19 19:58:18 +000097 if self._public:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080098 build_chromeos_args.append('--env=USE=-chrome_internal')
cmtice56fb7162014-06-18 11:32:15 -070099
asharif96f59692013-02-16 03:13:36 +0000100 ret = build_chromeos.Main(build_chromeos_args)
cmtice7f3190b2015-05-22 14:14:51 -0700101 if ret != 0:
102 raise RuntimeError("Couldn't build ChromeOS!")
cmtice56fb7162014-06-18 11:32:15 -0700103
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800104 if not self._build_num:
cmtice56fb7162014-06-18 11:32:15 -0700105 self._GetBuildNumber()
106 # Check to see if we need to create the symbolic link for the vanilla
107 # image, and do so if appropriate.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800108 if not misc.DoesLabelExist(self._chromeos_root, self._board, 'vanilla'):
109 build_name = '%s-release/%s.0.0' % (self._board, self._build_num)
110 full_vanilla_path = os.path.join(os.getcwd(), self._chromeos_root,
111 'chroot/tmp', build_name)
cmtice56fb7162014-06-18 11:32:15 -0700112 misc.LabelLatestImage(self._chromeos_root, self._board, label,
113 full_vanilla_path)
114 else:
asharif96f59692013-02-16 03:13:36 +0000115 misc.LabelLatestImage(self._chromeos_root, self._board, label)
116 return label
117
cmtice56fb7162014-06-18 11:32:15 -0700118 def _SetupBoard(self, env_dict, usepkg_flag, clobber_flag):
asharif96f59692013-02-16 03:13:36 +0000119 env_string = misc.GetEnvStringFromDict(env_dict)
Manoj Guptaaee96b72016-10-24 13:43:28 -0700120 command = ('%s %s' % (env_string, misc.GetSetupBoardCommand(
121 self._board, usepkg=usepkg_flag, force=clobber_flag)))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800122 ret = self._ce.ChrootRunCommand(self._chromeos_root, command)
cmtice56fb7162014-06-18 11:32:15 -0700123 error_str = "Could not setup board: '%s'" % command
124 assert ret == 0, error_str
asharif96f59692013-02-16 03:13:36 +0000125
126 def _UnInstallToolchain(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800127 command = ('sudo CLEAN_DELAY=0 emerge -C cross-%s/gcc' %
128 misc.GetCtargetFromBoard(self._board, self._chromeos_root))
129 ret = self._ce.ChrootRunCommand(self._chromeos_root, command)
cmtice7f3190b2015-05-22 14:14:51 -0700130 if ret != 0:
131 raise RuntimeError("Couldn't uninstall the toolchain!")
asharif96f59692013-02-16 03:13:36 +0000132
133 def _CheckoutChromeOS(self):
134 # TODO(asharif): Setup a fixed ChromeOS version (quarterly snapshot).
135 if not os.path.exists(self._chromeos_root):
Rahul Chaudhry4d4565e2016-01-27 10:46:09 -0800136 setup_chromeos_args = ['--dir=%s' % self._chromeos_root]
asharife6b72fe2013-02-19 19:58:18 +0000137 if self._public:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800138 setup_chromeos_args.append('--public')
asharif5fe40e22013-02-19 19:58:50 +0000139 ret = setup_chromeos.Main(setup_chromeos_args)
cmtice7f3190b2015-05-22 14:14:51 -0700140 if ret != 0:
141 raise RuntimeError("Couldn't run setup_chromeos!")
asharif96f59692013-02-16 03:13:36 +0000142
143 def _BuildToolchain(self, config):
cmtice56fb7162014-06-18 11:32:15 -0700144 # Call setup_board for basic, vanilla setup.
145 self._SetupBoard({}, usepkg_flag=True, clobber_flag=False)
146 # Now uninstall the vanilla compiler and setup/build our custom
147 # compiler.
asharif96f59692013-02-16 03:13:36 +0000148 self._UnInstallToolchain()
Manoj Guptaaee96b72016-10-24 13:43:28 -0700149 envdict = {
150 'USE': 'git_gcc',
151 'GCC_GITHASH': config.gcc_config.githash,
152 'EMERGE_DEFAULT_OPTS': '--exclude=gcc'
153 }
cmtice56fb7162014-06-18 11:32:15 -0700154 self._SetupBoard(envdict, usepkg_flag=False, clobber_flag=False)
asharif96f59692013-02-16 03:13:36 +0000155
156
157class ToolchainComparator(ChromeOSCheckout):
Caroline Tice88272d42016-01-13 09:48:29 -0800158 """Main class for running tests and generating reports."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800159
160 def __init__(self,
161 board,
162 remotes,
163 configs,
164 clean,
165 public,
166 force_mismatch,
167 noschedv2=False):
asharif96f59692013-02-16 03:13:36 +0000168 self._board = board
169 self._remotes = remotes
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800170 self._chromeos_root = 'chromeos'
asharif96f59692013-02-16 03:13:36 +0000171 self._configs = configs
asharif3e38de02013-02-19 19:34:59 +0000172 self._clean = clean
asharife6b72fe2013-02-19 19:58:18 +0000173 self._public = public
asharif58a8c9f2013-02-19 20:42:43 +0000174 self._force_mismatch = force_mismatch
asharif96f59692013-02-16 03:13:36 +0000175 self._ce = command_executer.GetCommandExecuter()
176 self._l = logger.GetLogger()
cmtice7f3190b2015-05-22 14:14:51 -0700177 timestamp = datetime.datetime.strftime(datetime.datetime.now(),
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800178 '%Y-%m-%d_%H:%M:%S')
Manoj Guptaaee96b72016-10-24 13:43:28 -0700179 self._reports_dir = os.path.join(
180 NIGHTLY_TESTS_DIR,
181 '%s.%s' % (timestamp, board),)
Han Shen43494292015-09-14 10:26:40 -0700182 self._noschedv2 = noschedv2
asharif96f59692013-02-16 03:13:36 +0000183 ChromeOSCheckout.__init__(self, board, self._chromeos_root)
184
cmticea6255d02014-01-10 10:27:22 -0800185 def _FinishSetup(self):
186 # Get correct .boto file
187 current_dir = os.getcwd()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800188 src = '/usr/local/google/home/mobiletc-prebuild/.boto'
cmticea6255d02014-01-10 10:27:22 -0800189 dest = os.path.join(current_dir, self._chromeos_root,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800190 'src/private-overlays/chromeos-overlay/'
191 'googlestorage_account.boto')
cmticea6255d02014-01-10 10:27:22 -0800192 # Copy the file to the correct place
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800193 copy_cmd = 'cp %s %s' % (src, dest)
Caroline Tice88272d42016-01-13 09:48:29 -0800194 retv = self._ce.RunCommand(copy_cmd)
195 if retv != 0:
cmtice7f3190b2015-05-22 14:14:51 -0700196 raise RuntimeError("Couldn't copy .boto file for google storage.")
cmticea6255d02014-01-10 10:27:22 -0800197
198 # Fix protections on ssh key
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800199 command = ('chmod 600 /var/cache/chromeos-cache/distfiles/target'
200 '/chrome-src-internal/src/third_party/chromite/ssh_keys'
201 '/testing_rsa')
Caroline Tice88272d42016-01-13 09:48:29 -0800202 retv = self._ce.ChrootRunCommand(self._chromeos_root, command)
203 if retv != 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800204 raise RuntimeError('chmod for testing_rsa failed')
cmticea6255d02014-01-10 10:27:22 -0800205
asharif96f59692013-02-16 03:13:36 +0000206 def _TestLabels(self, labels):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800207 experiment_file = 'toolchain_experiment.txt'
208 image_args = ''
asharif58a8c9f2013-02-19 20:42:43 +0000209 if self._force_mismatch:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800210 image_args = '--force-mismatch'
asharif96f59692013-02-16 03:13:36 +0000211 experiment_header = """
212 board: %s
213 remote: %s
cmticed1f03b82015-06-30 15:19:23 -0700214 retries: 1
asharif96f59692013-02-16 03:13:36 +0000215 """ % (self._board, self._remotes)
216 experiment_tests = """
cmtice0c84ea72015-06-25 14:22:36 -0700217 benchmark: all_toolchain_perf {
cmtice04403882013-11-04 16:38:37 -0500218 suite: telemetry_Crosperf
cmtice6de7f8f2014-03-14 14:08:21 -0700219 iterations: 3
asharif96f59692013-02-16 03:13:36 +0000220 }
221 """
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800222
223 with open(experiment_file, 'w') as f:
Caroline Tice88272d42016-01-13 09:48:29 -0800224 f.write(experiment_header)
225 f.write(experiment_tests)
asharif96f59692013-02-16 03:13:36 +0000226 for label in labels:
227 # TODO(asharif): Fix crosperf so it accepts labels with symbols
228 crosperf_label = label
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800229 crosperf_label = crosperf_label.replace('-', '_')
230 crosperf_label = crosperf_label.replace('+', '_')
231 crosperf_label = crosperf_label.replace('.', '')
cmtice56fb7162014-06-18 11:32:15 -0700232
233 # Use the official build instead of building vanilla ourselves.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800234 if label == 'vanilla':
cmtice56fb7162014-06-18 11:32:15 -0700235 build_name = '%s-release/%s.0.0' % (self._board, self._build_num)
236
237 # Now add 'official build' to test file.
238 official_image = """
239 official_image {
240 chromeos_root: %s
241 build: %s
242 }
243 """ % (self._chromeos_root, build_name)
Caroline Tice88272d42016-01-13 09:48:29 -0800244 f.write(official_image)
cmtice56fb7162014-06-18 11:32:15 -0700245
cmtice94bc4702014-05-29 16:29:04 -0700246 else:
cmtice56fb7162014-06-18 11:32:15 -0700247 experiment_image = """
248 %s {
249 chromeos_image: %s
250 image_args: %s
251 }
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800252 """ % (crosperf_label, os.path.join(
253 misc.GetImageDir(self._chromeos_root, self._board), label,
254 'chromiumos_test_image.bin'), image_args)
Caroline Tice88272d42016-01-13 09:48:29 -0800255 f.write(experiment_image)
cmtice56fb7162014-06-18 11:32:15 -0700256
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800257 crosperf = os.path.join(os.path.dirname(__file__), 'crosperf', 'crosperf')
Han Shen43494292015-09-14 10:26:40 -0700258 noschedv2_opts = '--noschedv2' if self._noschedv2 else ''
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800259 command = ('{crosperf} --no_email=True --results_dir={r_dir} '
260 '--json_report=True {noschedv2_opts} {exp_file}').format(
261 crosperf=crosperf,
262 r_dir=self._reports_dir,
263 noschedv2_opts=noschedv2_opts,
264 exp_file=experiment_file)
cmticeaa700b02015-06-12 13:26:47 -0700265
asharif96f59692013-02-16 03:13:36 +0000266 ret = self._ce.RunCommand(command)
cmtice7f3190b2015-05-22 14:14:51 -0700267 if ret != 0:
Manoj Guptaaee96b72016-10-24 13:43:28 -0700268 raise RuntimeError('Crosperf execution error!')
Caroline Ticeebbc3da2015-09-03 10:27:20 -0700269 else:
270 # Copy json report to pending archives directory.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800271 command = 'cp %s/*.json %s/.' % (self._reports_dir, PENDING_ARCHIVES_DIR)
Caroline Ticeebbc3da2015-09-03 10:27:20 -0700272 ret = self._ce.RunCommand(command)
cmtice7f3190b2015-05-22 14:14:51 -0700273 return
cmtice56fb7162014-06-18 11:32:15 -0700274
cmtice7f3190b2015-05-22 14:14:51 -0700275 def _SendEmail(self):
276 """Find email msesage generated by crosperf and send it."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800277 filename = os.path.join(self._reports_dir, 'msg_body.html')
cmtice7f3190b2015-05-22 14:14:51 -0700278 if (os.path.exists(filename) and
279 os.path.exists(os.path.expanduser(MAIL_PROGRAM))):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800280 command = ('cat %s | %s -s "Nightly test results, %s" -team -html' %
281 (filename, MAIL_PROGRAM, self._board))
cmtice7f3190b2015-05-22 14:14:51 -0700282 self._ce.RunCommand(command)
asharif96f59692013-02-16 03:13:36 +0000283
284 def DoAll(self):
285 self._CheckoutChromeOS()
asharif96f59692013-02-16 03:13:36 +0000286 labels = []
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800287 labels.append('vanilla')
asharif96f59692013-02-16 03:13:36 +0000288 for config in self._configs:
Caroline Tice88272d42016-01-13 09:48:29 -0800289 label = self._BuildLabelName(config.gcc_config.githash)
290 if not misc.DoesLabelExist(self._chromeos_root, self._board, label):
asharif96f59692013-02-16 03:13:36 +0000291 self._BuildToolchain(config)
292 label = self._BuildAndImage(label)
293 labels.append(label)
cmticea6255d02014-01-10 10:27:22 -0800294 self._FinishSetup()
cmticeb4588092015-05-27 08:07:50 -0700295 self._TestLabels(labels)
cmtice7f3190b2015-05-22 14:14:51 -0700296 self._SendEmail()
asharif3e38de02013-02-19 19:34:59 +0000297 if self._clean:
298 ret = self._DeleteChroot()
cmtice7f3190b2015-05-22 14:14:51 -0700299 if ret != 0:
300 return ret
asharif67973582013-02-19 20:19:40 +0000301 ret = self._DeleteCcahe()
cmtice7f3190b2015-05-22 14:14:51 -0700302 if ret != 0:
303 return ret
asharif96f59692013-02-16 03:13:36 +0000304 return 0
305
306
307def Main(argv):
308 """The main function."""
309 # Common initializations
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800310 ### command_executer.InitCommandExecuter(True)
asharif96f59692013-02-16 03:13:36 +0000311 command_executer.InitCommandExecuter()
Caroline Tice88272d42016-01-13 09:48:29 -0800312 parser = argparse.ArgumentParser()
Manoj Guptaaee96b72016-10-24 13:43:28 -0700313 parser.add_argument(
314 '--remote', dest='remote', help='Remote machines to run tests on.')
315 parser.add_argument(
316 '--board', dest='board', default='x86-alex', help='The target board.')
317 parser.add_argument(
318 '--githashes',
319 dest='githashes',
320 default='master',
321 help='The gcc githashes to test.')
322 parser.add_argument(
323 '--clean',
324 dest='clean',
325 default=False,
326 action='store_true',
327 help='Clean the chroot after testing.')
328 parser.add_argument(
329 '--public',
330 dest='public',
331 default=False,
332 action='store_true',
333 help='Use the public checkout/build.')
334 parser.add_argument(
335 '--force-mismatch',
336 dest='force_mismatch',
337 default='',
338 help='Force the image regardless of board mismatch')
339 parser.add_argument(
340 '--noschedv2',
341 dest='noschedv2',
342 action='store_true',
343 default=False,
344 help='Pass --noschedv2 to crosperf.')
Caroline Tice88272d42016-01-13 09:48:29 -0800345 options = parser.parse_args(argv)
asharif96f59692013-02-16 03:13:36 +0000346 if not options.board:
Caroline Tice88272d42016-01-13 09:48:29 -0800347 print('Please give a board.')
asharif96f59692013-02-16 03:13:36 +0000348 return 1
349 if not options.remote:
Caroline Tice88272d42016-01-13 09:48:29 -0800350 print('Please give at least one remote machine.')
asharif96f59692013-02-16 03:13:36 +0000351 return 1
352 toolchain_configs = []
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800353 for githash in options.githashes.split(','):
asharif96f59692013-02-16 03:13:36 +0000354 gcc_config = GCCConfig(githash=githash)
355 toolchain_config = ToolchainConfig(gcc_config=gcc_config)
356 toolchain_configs.append(toolchain_config)
asharif3e38de02013-02-19 19:34:59 +0000357 fc = ToolchainComparator(options.board, options.remote, toolchain_configs,
asharif58a8c9f2013-02-19 20:42:43 +0000358 options.clean, options.public,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800359 options.force_mismatch, options.noschedv2)
asharif96f59692013-02-16 03:13:36 +0000360 return fc.DoAll()
361
362
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800363if __name__ == '__main__':
Rahul Chaudhry748254e2016-01-25 16:49:21 -0800364 retval = Main(sys.argv[1:])
asharif96f59692013-02-16 03:13:36 +0000365 sys.exit(retval)