xref: /aosp_15_r20/external/toolchain-utils/buildbot_test_toolchains.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#
4# Copyright 2016 The ChromiumOS Authors
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8"""Script for running nightly compiler tests on ChromeOS.
9
10This script launches a buildbot to build ChromeOS with the latest compiler on
11a particular board; then it finds and downloads the trybot image and the
12corresponding official image, and runs crosperf performance tests comparing
13the two.  It then generates a report, emails it to the c-compiler-chrome, as
14well as copying the images into the seven-day reports directory.
15"""
16
17# Script to test different toolchains against ChromeOS benchmarks.
18
19
20import argparse
21import datetime
22import os
23import re
24import shutil
25import sys
26import time
27
28from cros_utils import buildbot_utils
29from cros_utils import command_executer
30from cros_utils import logger
31
32
33CROSTC_ROOT = "/usr/local/google/crostc"
34NIGHTLY_TESTS_DIR = os.path.join(CROSTC_ROOT, "nightly-tests")
35ROLE_ACCOUNT = "mobiletc-prebuild"
36TOOLCHAIN_DIR = os.path.dirname(os.path.realpath(__file__))
37TMP_TOOLCHAIN_TEST = "/tmp/toolchain-tests"
38MAIL_PROGRAM = "~/var/bin/mail-detective"
39PENDING_ARCHIVES_DIR = os.path.join(CROSTC_ROOT, "pending_archives")
40NIGHTLY_TESTS_RESULTS = os.path.join(CROSTC_ROOT, "nightly_test_reports")
41
42IMAGE_DIR = "{board}-{image_type}"
43IMAGE_VERSION_STR = r"{chrome_version}-{tip}\.{branch}\.{branch_branch}"
44IMAGE_FS = IMAGE_DIR + "/" + IMAGE_VERSION_STR
45TRYBOT_IMAGE_FS = IMAGE_FS + "-{build_id}"
46IMAGE_RE_GROUPS = {
47    "board": r"(?P<board>\S+)",
48    "image_type": r"(?P<image_type>\S+)",
49    "chrome_version": r"(?P<chrome_version>R\d+)",
50    "tip": r"(?P<tip>\d+)",
51    "branch": r"(?P<branch>\d+)",
52    "branch_branch": r"(?P<branch_branch>\d+)",
53    "build_id": r"(?P<build_id>b\d+)",
54}
55TRYBOT_IMAGE_RE = TRYBOT_IMAGE_FS.format(**IMAGE_RE_GROUPS)
56
57RECIPE_IMAGE_FS = IMAGE_FS + "-{build_id}-{buildbucket_id}"
58RECIPE_IMAGE_RE_GROUPS = {
59    "board": r"(?P<board>\S+)",
60    "image_type": r"(?P<image_type>\S+)",
61    "chrome_version": r"(?P<chrome_version>R\d+)",
62    "tip": r"(?P<tip>\d+)",
63    "branch": r"(?P<branch>\d+)",
64    "branch_branch": r"(?P<branch_branch>\d+)",
65    "build_id": r"(?P<build_id>\d+)",
66    "buildbucket_id": r"(?P<buildbucket_id>\d+)",
67}
68RECIPE_IMAGE_RE = RECIPE_IMAGE_FS.format(**RECIPE_IMAGE_RE_GROUPS)
69
70# CL that uses LLVM-Next to build the images (includes chrome).
71USE_LLVM_NEXT_PATCH = "513590"
72
73
74class ToolchainComparator(object):
75    """Class for doing the nightly tests work."""
76
77    def __init__(
78        self,
79        board,
80        remotes,
81        chromeos_root,
82        weekday,
83        patches,
84        recipe=False,
85        test=False,
86        noschedv2=False,
87        chrome_src="",
88    ):
89        self._board = board
90        self._remotes = remotes
91        self._chromeos_root = chromeos_root
92        self._chrome_src = chrome_src
93        self._base_dir = os.getcwd()
94        self._ce = command_executer.GetCommandExecuter()
95        self._l = logger.GetLogger()
96        self._build = "%s-release-tryjob" % board
97        self._patches = patches.split(",") if patches else []
98        self._patches_string = "_".join(str(p) for p in self._patches)
99        self._recipe = recipe
100        self._test = test
101        self._noschedv2 = noschedv2
102
103        if not weekday:
104            self._weekday = time.strftime("%a")
105        else:
106            self._weekday = weekday
107        self._date = datetime.date.today().strftime("%Y/%m/%d")
108        timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
109        self._reports_dir = os.path.join(
110            TMP_TOOLCHAIN_TEST if self._test else NIGHTLY_TESTS_RESULTS,
111            "%s.%s" % (timestamp, board),
112        )
113
114    def _GetVanillaImageName(self, trybot_image):
115        """Given a trybot artifact name, get latest vanilla image name.
116
117        Args:
118          trybot_image: artifact name such as
119            'daisy-release-tryjob/R40-6394.0.0-b1389'
120            for recipe images, name is in this format:
121            'lulu-llvm-next-nightly/R84-13037.0.0-31011-8883172717979984032/'
122
123        Returns:
124          Latest official image name, e.g. 'daisy-release/R57-9089.0.0'.
125        """
126        # For board names with underscores, we need to fix the trybot image name
127        # to replace the hyphen (for the recipe builder) with the underscore.
128        # Currently the only such board we use is 'veyron_tiger'.
129        if trybot_image.find("veyron-tiger") != -1:
130            trybot_image = trybot_image.replace("veyron-tiger", "veyron_tiger")
131        # We need to filter out -tryjob in the trybot_image.
132        if self._recipe:
133            trybot = re.sub("-llvm-next-nightly", "-release", trybot_image)
134            mo = re.search(RECIPE_IMAGE_RE, trybot)
135        else:
136            trybot = re.sub("-tryjob", "", trybot_image)
137            mo = re.search(TRYBOT_IMAGE_RE, trybot)
138        assert mo
139        dirname = IMAGE_DIR.replace("\\", "").format(**mo.groupdict())
140        return buildbot_utils.GetLatestImage(self._chromeos_root, dirname)
141
142    def _TestImages(self, trybot_image, vanilla_image):
143        """Create crosperf experiment file.
144
145        Given the names of the trybot, vanilla and non-AFDO images, create the
146        appropriate crosperf experiment file and launch crosperf on it.
147        """
148        if self._test:
149            experiment_file_dir = TMP_TOOLCHAIN_TEST
150        else:
151            experiment_file_dir = os.path.join(NIGHTLY_TESTS_DIR, self._weekday)
152        experiment_file_name = "%s_toolchain_experiment.txt" % self._board
153
154        compiler_string = "llvm"
155        if USE_LLVM_NEXT_PATCH in self._patches_string:
156            experiment_file_name = "%s_llvm_next_experiment.txt" % self._board
157            compiler_string = "llvm_next"
158
159        experiment_file = os.path.join(
160            experiment_file_dir, experiment_file_name
161        )
162        experiment_header = """
163    board: %s
164    remote: %s
165    retries: 1
166    """ % (
167            self._board,
168            self._remotes,
169        )
170        # TODO(b/244607231): Add graphic benchmarks removed in crrev.com/c/3869851.
171        experiment_tests = """
172    benchmark: all_toolchain_perf {
173      suite: telemetry_Crosperf
174      iterations: 5
175      run_local: False
176    }
177
178    benchmark: loading.desktop {
179      suite: telemetry_Crosperf
180      test_args: --story-tag-filter=typical
181      iterations: 3
182      run_local: False
183    }
184
185    benchmark: platform.ReportDiskUsage {
186      suite: tast
187      iterations: 1
188      run_local: False
189    }
190    """
191
192        with open(experiment_file, "w", encoding="utf-8") as f:
193            f.write(experiment_header)
194            f.write(experiment_tests)
195
196            # Now add vanilla to test file.
197            official_image = """
198      vanilla_image {
199        chromeos_root: %s
200        chrome_src: %s
201        build: %s
202        compiler: llvm
203      }
204      """ % (
205                self._chromeos_root,
206                self._chrome_src,
207                vanilla_image,
208            )
209            f.write(official_image)
210
211            label_string = "%s_trybot_image" % compiler_string
212
213            # Reuse autotest files from vanilla image for trybot images
214            autotest_files = os.path.join(
215                "/tmp", vanilla_image, "autotest_files"
216            )
217            experiment_image = """
218      %s {
219        chromeos_root: %s
220        chrome_src: %s
221        build: %s
222        autotest_path: %s
223        compiler: %s
224      }
225      """ % (
226                label_string,
227                self._chromeos_root,
228                self._chrome_src,
229                trybot_image,
230                autotest_files,
231                compiler_string,
232            )
233            f.write(experiment_image)
234
235        crosperf = os.path.join(TOOLCHAIN_DIR, "crosperf", "crosperf")
236        noschedv2_opts = "--noschedv2" if self._noschedv2 else ""
237        no_email = not self._test
238        command = (
239            f"{crosperf} --no_email={no_email} "
240            f"--results_dir={self._reports_dir} --logging_level=verbose "
241            f"--json_report=True {noschedv2_opts} {experiment_file}"
242        )
243
244        return self._ce.RunCommand(command)
245
246    def _SendEmail(self):
247        """Find email message generated by crosperf and send it."""
248        filename = os.path.join(self._reports_dir, "msg_body.html")
249        if os.path.exists(filename) and os.path.exists(
250            os.path.expanduser(MAIL_PROGRAM)
251        ):
252            email_title = "buildbot llvm test results"
253            if USE_LLVM_NEXT_PATCH in self._patches_string:
254                email_title = "buildbot llvm_next test results"
255            command = 'cat %s | %s -s "%s, %s %s" -team -html' % (
256                filename,
257                MAIL_PROGRAM,
258                email_title,
259                self._board,
260                self._date,
261            )
262            self._ce.RunCommand(command)
263
264    def _CopyJson(self):
265        # Make sure a destination directory exists.
266        os.makedirs(PENDING_ARCHIVES_DIR, exist_ok=True)
267        # Copy json report to pending archives directory.
268        command = "cp %s/*.json %s/." % (
269            self._reports_dir,
270            PENDING_ARCHIVES_DIR,
271        )
272        ret = self._ce.RunCommand(command)
273        # Failing to access json report means that crosperf terminated or all tests
274        # failed, raise an error.
275        if ret != 0:
276            raise RuntimeError(
277                "Crosperf failed to run tests, cannot copy json report!"
278            )
279
280    def DoAll(self):
281        """Main function inside ToolchainComparator class.
282
283        Launch trybot, get image names, create crosperf experiment file, run
284        crosperf, and copy images into seven-day report directories.
285        """
286        if self._recipe:
287            print("Using recipe buckets to get latest image.")
288            # crbug.com/1077313: Some boards are not consistently
289            # spelled, having underscores in some places and dashes in others.
290            # The image directories consistenly use dashes, so convert underscores
291            # to dashes to work around this.
292            trybot_image = buildbot_utils.GetLatestRecipeImage(
293                self._chromeos_root,
294                "%s-llvm-next-nightly" % self._board.replace("_", "-"),
295            )
296        else:
297            # Launch tryjob and wait to get image location.
298            buildbucket_id, trybot_image = buildbot_utils.GetTrybotImage(
299                self._chromeos_root,
300                self._build,
301                self._patches,
302                tryjob_flags=["--notests"],
303                build_toolchain=True,
304            )
305            print(
306                "trybot_url: \
307            http://cros-goldeneye/chromeos/healthmonitoring/buildDetails?buildbucketId=%s"
308                % buildbucket_id
309            )
310
311        if not trybot_image:
312            self._l.LogError("Unable to find trybot_image!")
313            return 2
314
315        vanilla_image = self._GetVanillaImageName(trybot_image)
316
317        print("trybot_image: %s" % trybot_image)
318        print("vanilla_image: %s" % vanilla_image)
319
320        ret = self._TestImages(trybot_image, vanilla_image)
321        # Always try to send report email as crosperf will generate report when
322        # tests partially succeeded.
323        if not self._test:
324            self._SendEmail()
325            self._CopyJson()
326        # Non-zero ret here means crosperf tests partially failed, raise error here
327        # so that toolchain summary report can catch it.
328        if ret != 0:
329            raise RuntimeError("Crosperf tests partially failed!")
330
331        return 0
332
333
334def Main(argv):
335    """The main function."""
336
337    # Common initializations
338    command_executer.InitCommandExecuter()
339    parser = argparse.ArgumentParser()
340    parser.add_argument(
341        "--remote", dest="remote", help="Remote machines to run tests on."
342    )
343    parser.add_argument(
344        "--board", dest="board", default="x86-zgb", help="The target board."
345    )
346    parser.add_argument(
347        "--chromeos_root",
348        dest="chromeos_root",
349        help="The chromeos root from which to run tests.",
350    )
351    parser.add_argument(
352        "--chrome_src",
353        dest="chrome_src",
354        default="",
355        help="The path to the source of chrome. "
356        "This is used to run telemetry benchmarks. "
357        "The default one is the src inside chroot.",
358    )
359    parser.add_argument(
360        "--weekday",
361        default="",
362        dest="weekday",
363        help="The day of the week for which to run tests.",
364    )
365    parser.add_argument(
366        "--patch",
367        dest="patches",
368        help="The patches to use for the testing, "
369        "seprate the patch numbers with ',' "
370        "for more than one patches.",
371    )
372    parser.add_argument(
373        "--noschedv2",
374        dest="noschedv2",
375        action="store_true",
376        default=False,
377        help="Pass --noschedv2 to crosperf.",
378    )
379    parser.add_argument(
380        "--recipe",
381        dest="recipe",
382        default=True,
383        help="Use images generated from recipe rather than"
384        "launching tryjob to get images.",
385    )
386    parser.add_argument(
387        "--test",
388        dest="test",
389        default=False,
390        help="Test this script on local desktop, "
391        "disabling mobiletc checking and email sending."
392        "Artifacts stored in /tmp/toolchain-tests",
393    )
394
395    options = parser.parse_args(argv[1:])
396    if not options.board:
397        print("Please give a board.")
398        return 1
399    if not options.remote:
400        print("Please give at least one remote machine.")
401        return 1
402    if not options.chromeos_root:
403        print("Please specify the ChromeOS root directory.")
404        return 1
405    if options.test:
406        print("Cleaning local test directory for this script.")
407        if os.path.exists(TMP_TOOLCHAIN_TEST):
408            shutil.rmtree(TMP_TOOLCHAIN_TEST)
409        os.mkdir(TMP_TOOLCHAIN_TEST)
410
411    fc = ToolchainComparator(
412        options.board,
413        options.remote,
414        options.chromeos_root,
415        options.weekday,
416        options.patches,
417        options.recipe,
418        options.test,
419        options.noschedv2,
420        chrome_src=options.chrome_src,
421    )
422    return fc.DoAll()
423
424
425if __name__ == "__main__":
426    retval = Main(sys.argv)
427    sys.exit(retval)
428