xref: /aosp_15_r20/external/angle/scripts/trigger.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1#!/usr/bin/python3
2#
3# Copyright 2019 The ANGLE Project Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# trigger.py:
8#   Helper script for triggering GPU tests on LUCI swarming.
9#
10# HOW TO USE THIS SCRIPT
11#
12# Prerequisites:
13#   - Your host OS must be able to build the targets. Linux can cross-compile Android and Windows.
14#   - You might need to be logged in to some services. Look in the error output to verify.
15#
16# Steps:
17#   1. Visit https://ci.chromium.org/p/angle/g/ci/console and find a builder with a similar OS and configuration.
18#      Replicating GN args exactly is not necessary. For example, linux-test:
19#         https://ci.chromium.org/p/angle/builders/ci/linux-test
20#   2. Find a recent green build from the builder, for example:
21#         https://ci.chromium.org/ui/p/angle/builders/ci/linux-test/2443/overview
22#   3. Find a test step shard that matches your test and intended target. For example, angle_unittests on Intel:
23#         https://chromium-swarm.appspot.com/task?id=5d6eecdda8e82210
24#   4. Now run this script without arguments to print the help message. For example:
25#         usage: trigger.py [-h] [-s SHARDS] [-p POOL] [-g GPU] [-t DEVICE_TYPE] [-o DEVICE_OS] [-l LOG] [--gold]
26#           [--priority PRIORITY] [-e ENV] gn_path test os_dim
27#   5. Next, find values for "GPU" on a desktop platform, and "DEVICE_TYPE" and "DEVICE_OS" on Android. You'll
28#      also need to find a value for "os_dim". For the above example:
29#         "os_dim" -> Ubuntu-18.04.6, "GPU" -> 8086:9bc5-20.0.8.
30#   6. For "gn_path" and "test", use your local GN out directory path and triggered test name. The test name must
31#      match an entry in infra/specs/gn_isolate_map.pyl. For example:
32#         trigger.py -g 8086:9bc5-20.0.8 out/Debug angle_unittests Ubuntu-18.04.6
33#   7. Finally, append the same arguments you'd run with locally when invoking this trigger script, e.g:
34#         --gtest_filter=*YourTest*
35#         --use-angle=backend
36#   8. Note that you can look up test artifacts in the test CAS outputs. For example:
37#         https://cas-viewer.appspot.com/projects/chromium-swarm/instances/default_instance/blobs/6165a0ede67ef2530f595ed9a1202671a571da952b6c887f516641993c9a96d4/87/tree
38#
39# Additional Notes:
40#   - Use --priority 1 to ensure your task is scheduled immediately, just be mindful of resources.
41#   - For Skia Gold tests specifically, append --gold. Otherwise ignore this argument.
42#   - You can also specify environment variables with --env.
43#   - For SwiftShader, use a GPU dimension of "none".
44
45import argparse
46import json
47import hashlib
48import logging
49import os
50import re
51import subprocess
52import sys
53
54# This is the same as the trybots.
55DEFAULT_TASK_PRIORITY = 30
56DEFAULT_POOL = 'chromium.tests.gpu'
57DEFAULT_LOG_LEVEL = 'info'
58DEFAULT_REALM = 'chromium:try'
59GOLD_SERVICE_ACCOUNT = '[email protected]'
60EXIT_SUCCESS = 0
61EXIT_FAILURE = 1
62
63
64def parse_args():
65    parser = argparse.ArgumentParser(os.path.basename(sys.argv[0]))
66    parser.add_argument('gn_path', help='path to GN. (e.g. out/Release)')
67    parser.add_argument('test', help='test name. (e.g. angle_end2end_tests)')
68    parser.add_argument('os_dim', help='OS dimension. (e.g. Windows-10)')
69    parser.add_argument('-s', '--shards', default=1, help='number of shards', type=int)
70    parser.add_argument(
71        '-p', '--pool', default=DEFAULT_POOL, help='swarming pool, default is %s.' % DEFAULT_POOL)
72    parser.add_argument('-g', '--gpu', help='GPU dimension. (e.g. intel-hd-630-win10-stable)')
73    parser.add_argument('-t', '--device-type', help='Android device type (e.g. bullhead)')
74    parser.add_argument('-o', '--device-os', help='Android OS.')
75    parser.add_argument(
76        '-l',
77        '--log',
78        default=DEFAULT_LOG_LEVEL,
79        help='Log level. Default is %s.' % DEFAULT_LOG_LEVEL)
80    parser.add_argument(
81        '--gold', action='store_true', help='Use swarming arguments for Gold tests.')
82    parser.add_argument(
83        '--priority',
84        help='Task priority. Default is %s. Use judiciously.' % DEFAULT_TASK_PRIORITY,
85        default=DEFAULT_TASK_PRIORITY)
86    parser.add_argument(
87        '-e',
88        '--env',
89        action='append',
90        default=[],
91        help='Environment variables. Can be specified multiple times.')
92
93    return parser.parse_known_args()
94
95
96def invoke_mb(args, stdout=None):
97    mb_script_path = os.path.join('tools', 'mb', 'mb.py')
98    mb_args = [sys.executable, mb_script_path] + args
99
100    # Attempt to detect standalone vs chromium component build.
101    is_standalone = not os.path.isdir(os.path.join('third_party', 'angle'))
102
103    if is_standalone:
104        logging.info('Standalone mode detected.')
105        mb_args += ['-i', os.path.join('infra', 'specs', 'gn_isolate_map.pyl')]
106
107    logging.info('Invoking mb: %s' % ' '.join(mb_args))
108    proc = subprocess.run(mb_args, stdout=stdout)
109    if proc.returncode != EXIT_SUCCESS:
110        print('Aborting run because mb retured a failure.')
111        sys.exit(EXIT_FAILURE)
112    if stdout != None:
113        return proc.stdout.decode()
114
115
116def main():
117    args, unknown = parse_args()
118
119    logging.basicConfig(level=args.log.upper())
120
121    path = args.gn_path.replace('\\', '/')
122    out_gn_path = '//' + path
123    out_file_path = os.path.join(*path.split('/'))
124
125    get_command_output = invoke_mb(['get-swarming-command', out_gn_path, args.test, '--as-list'],
126                                   stdout=subprocess.PIPE)
127    swarming_cmd = json.loads(get_command_output)
128    logging.info('Swarming command: %s' % ' '.join(swarming_cmd))
129
130    invoke_mb(['isolate', out_gn_path, args.test])
131
132    isolate_cmd_path = os.path.join('tools', 'luci-go', 'isolate')
133    isolate_file = os.path.join(out_file_path, '%s.isolate' % args.test)
134    archive_file = os.path.join(out_file_path, '%s.archive.json' % args.test)
135
136    isolate_args = [
137        isolate_cmd_path, 'archive', '-i', isolate_file, '-cas-instance', 'chromium-swarm',
138        '-dump-json', archive_file
139    ]
140    logging.info('Invoking isolate: %s' % ' '.join(isolate_args))
141    subprocess.check_call(isolate_args)
142    with open(archive_file) as f:
143        digest = json.load(f).get(args.test)
144
145    logging.info('Got an CAS digest %s' % digest)
146    swarming_script_path = os.path.join('tools', 'luci-go', 'swarming')
147
148    swarming_args = [
149        swarming_script_path, 'trigger', '-S', 'chromium-swarm.appspot.com', '-d',
150        'os=' + args.os_dim, '-d', 'pool=' + args.pool, '-digest', digest
151    ]
152
153    # Set priority. Don't abuse this!
154    swarming_args += ['-priority', str(args.priority), '-realm', DEFAULT_REALM]
155
156    # Define a user tag.
157    try:
158        whoami = subprocess.check_output(['whoami'])
159        # Strip extra stuff (e.g. on Windows we are 'hostname\username')
160        whoami = re.sub(r'\w+[^\w]', '', whoami.strip())
161        swarming_args += ['-user', whoami]
162    except:
163        pass
164
165    if args.gpu:
166        swarming_args += ['-d', 'gpu=' + args.gpu]
167
168    if args.device_type:
169        swarming_args += ['-d', 'device_type=' + args.device_type]
170
171    if args.device_os:
172        swarming_args += ['-d', 'device_os=' + args.device_os]
173
174    cmd_args = ['-relative-cwd', args.gn_path, '--']
175
176    if args.gold:
177        swarming_args += ['-service-account', GOLD_SERVICE_ACCOUNT]
178        cmd_args += ['luci-auth', 'context', '--']
179
180    for env in args.env:
181        swarming_args += ['-env', env]
182
183    cmd_args += swarming_cmd
184
185    if unknown:
186        cmd_args += unknown
187
188    if args.shards > 1:
189        for i in range(args.shards):
190            shard_args = swarming_args[:]
191            shard_args.extend([
192                '--env',
193                'GTEST_TOTAL_SHARDS=%d' % args.shards,
194                '--env',
195                'GTEST_SHARD_INDEX=%d' % i,
196            ])
197
198            shard_args += cmd_args
199
200            logging.info('Invoking swarming: %s' % ' '.join(shard_args))
201            subprocess.call(shard_args)
202    else:
203        swarming_args += cmd_args
204        logging.info('Invoking swarming: %s' % ' '.join(swarming_args))
205        subprocess.call(swarming_args)
206    return EXIT_SUCCESS
207
208
209if __name__ == '__main__':
210    sys.exit(main())
211