summaryrefslogtreecommitdiff
path: root/test/testrunner/testrunner.py
blob: 14fd0ca469b58b5a6762a6f51863c3210bca6868 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
#!/usr/bin/env python3
#
# [VPYTHON:BEGIN]
# python_version: "3.8"
# [VPYTHON:END]
#
# Copyright 2017, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""ART Run-Test TestRunner

The testrunner runs the ART run-tests by simply invoking the script.
It fetches the list of eligible tests from art/test directory, and list of
disabled tests from art/test/knownfailures.json. It runs the tests by
invoking art/test/run-test script and checks the exit value to decide if the
test passed or failed.

Before invoking the script, first build all the tests dependencies.
There are two major build targets for building target and host tests
dependencies:
1) test-art-host-run-test
2) test-art-target-run-test

There are various options to invoke the script which are:
-t: Either the test name as in art/test or the test name including the variant
    information. Eg, "-t 001-HelloWorld",
    "-t test-art-host-run-test-debug-prebuild-optimizing-relocate-ntrace-cms-checkjni-picimage-ndebuggable-001-HelloWorld32"
-j: Number of thread workers to be used. Eg - "-j64"
--dry-run: Instead of running the test name, just print its name.
--verbose
-b / --build-dependencies: to build the dependencies before running the test

To specify any specific variants for the test, use --<<variant-name>>.
For eg, for compiler type as optimizing, use --optimizing.


In the end, the script will print the failed and skipped tests if any.

"""
import argparse
import collections

# b/140161314 diagnostics.
try:
  import concurrent.futures
except Exception:
  import sys
  sys.stdout.write("\n\n" + sys.executable + " " + sys.version + "\n\n")
  sys.stdout.flush()
  raise

import csv
import datetime
import fnmatch
import itertools
import json
import multiprocessing
import os
import re
import shlex
import shutil
import signal
import subprocess
import sys
import tempfile
import threading
import time

import env
from target_config import target_config
from device_config import device_config
from typing import Dict, Set, List
from functools import lru_cache

# TODO: make it adjustable per tests and for buildbots
#
# Note: this needs to be larger than run-test timeouts, as long as this script
#       does not push the value to run-test. run-test is somewhat complicated:
#                      base: 25m  (large for ASAN)
#        + timeout handling:  2m
#        +   gcstress extra: 20m
#        -----------------------
#                            47m
timeout = 3600 # 60 minutes

if env.ART_TEST_RUN_ON_ARM_FVP:
  # Increase timeout to 600 minutes due to the emulation overhead on FVP.
  timeout = 36000

# DISABLED_TEST_CONTAINER holds information about the disabled tests. It is a map
# that has key as the test name (like 001-HelloWorld), and value as set of
# variants that the test is disabled for.
DISABLED_TEST_CONTAINER = {}

# The Dict contains the list of all possible variants for a given type. For example,
# for key TARGET, the value would be target and host. The list is used to parse
# the test name given as the argument to run.
VARIANT_TYPE_DICT: Dict[str, Set[str]] = {}

# The set of all variant sets that are incompatible and will always be skipped.
NONFUNCTIONAL_VARIANT_SETS = set()

# The set contains all the variants of each time.
TOTAL_VARIANTS_SET: Set[str] = set()

# The colors are used in the output. When a test passes, COLOR_PASS is used,
# and so on.
COLOR_ERROR = '\033[91m'
COLOR_PASS = '\033[92m'
COLOR_SKIP = '\033[93m'
COLOR_NORMAL = '\033[0m'

# The set contains the list of all the possible run tests that are in art/test
# directory.
RUN_TEST_SET = set()

failed_tests = []
skipped_tests = []

# Flags
n_thread = 0
total_test_count = 0
verbose = False
dry_run = False
ignore_skips = False
build = False
dist = False
gdb = False
gdb_arg = ''
dump_cfg = ''
gdb_dex2oat = False
gdb_dex2oat_args = ''
csv_result = None
csv_writer = None
runtime_option = ''
with_agent: List[str] = []
run_test_option: List[str] = []
dex2oat_jobs = -1   # -1 corresponds to default threads for dex2oat
run_all_configs = False

# Dict containing extra arguments
extra_arguments: Dict[str, List[str]] = { "host" : [], "target" : [] }

# Dict to store user requested test variants.
# key: variant_type.
# value: set of variants user wants to run of type <key>.
_user_input_variants: collections.defaultdict = collections.defaultdict(set)


class ChildProcessTracker(object):
  """Keeps track of forked child processes to be able to kill them."""

  def __init__(self):
    self.procs = {}             # dict from pid to subprocess.Popen object
    self.mutex = threading.Lock()

  def wait(self, proc, timeout):
    """Waits on the given subprocess and makes it available to kill_all meanwhile.

    Args:
      proc: The subprocess.Popen object to wait on.
      timeout: Timeout passed on to proc.communicate.

    Returns: A tuple of the process stdout output and its return value.
    """
    with self.mutex:
      if self.procs is not None:
        self.procs[proc.pid] = proc
      else:
        os.killpg(proc.pid, signal.SIGKILL) # kill_all has already been called.
    try:
      output = proc.communicate(timeout=timeout)[0]
      return_value = proc.wait()
      return output, return_value
    finally:
      with self.mutex:
        if self.procs is not None:
          del self.procs[proc.pid]

  def kill_all(self):
    """Kills all currently running processes and any future ones."""
    with self.mutex:
      for pid in self.procs:
        os.killpg(pid, signal.SIGKILL)
      self.procs = None # Make future wait() calls kill their processes immediately.

child_process_tracker = ChildProcessTracker()

def setup_csv_result():
  """Set up the CSV output if required."""
  global csv_writer
  csv_writer = csv.writer(csv_result)
  # Write the header.
  csv_writer.writerow(['target', 'run', 'prebuild', 'compiler', 'relocate', 'trace', 'gc',
                       'jni', 'image', 'debuggable', 'jvmti', 'cdex_level', 'test', 'address_size', 'result'])


def send_csv_result(test, result):
  """
  Write a line into the CSV results file if one is available.
  """
  if csv_writer is not None:
    csv_writer.writerow(extract_test_name(test) + [result])

def close_csv_file():
  global csv_result
  global csv_writer
  if csv_result is not None:
    csv_writer = None
    csv_result.flush()
    csv_result.close()
    csv_result = None

def gather_test_info():
  """The method gathers test information about the test to be run which includes
  generating the list of total tests from the art/test directory and the list
  of disabled test. It also maps various variants to types.
  """
  global TOTAL_VARIANTS_SET
  # TODO: Avoid duplication of the variant names in different lists.
  VARIANT_TYPE_DICT['run'] = {'ndebug', 'debug'}
  VARIANT_TYPE_DICT['target'] = {'target', 'host', 'jvm'}
  VARIANT_TYPE_DICT['trace'] = {'trace', 'ntrace', 'stream'}
  VARIANT_TYPE_DICT['image'] = {'picimage', 'no-image'}
  VARIANT_TYPE_DICT['debuggable'] = {'ndebuggable', 'debuggable'}
  VARIANT_TYPE_DICT['gc'] = {'gcstress', 'gcverify', 'cms'}
  VARIANT_TYPE_DICT['prebuild'] = {'no-prebuild', 'prebuild'}
  VARIANT_TYPE_DICT['cdex_level'] = {'cdex-none', 'cdex-fast'}
  VARIANT_TYPE_DICT['relocate'] = {'relocate', 'no-relocate'}
  VARIANT_TYPE_DICT['jni'] = {'jni', 'forcecopy', 'checkjni'}
  VARIANT_TYPE_DICT['address_sizes'] = {'64', '32'}
  VARIANT_TYPE_DICT['jvmti'] = {'no-jvmti', 'jvmti-stress', 'redefine-stress', 'trace-stress',
                                'field-stress', 'step-stress'}
  VARIANT_TYPE_DICT['compiler'] = {'interp-ac', 'interpreter', 'jit', 'jit-on-first-use',
                                   'optimizing', 'speed-profile', 'baseline'}

  for v_type in VARIANT_TYPE_DICT:
    TOTAL_VARIANTS_SET = TOTAL_VARIANTS_SET.union(VARIANT_TYPE_DICT.get(v_type))

  test_dir = env.ANDROID_BUILD_TOP + '/art/test'
  for f in os.listdir(test_dir):
    if fnmatch.fnmatch(f, '[0-9]*'):
      RUN_TEST_SET.add(f)


def setup_test_env():
  """The method sets default value for the various variants of the tests if they
  are already not set.
  """
  if env.ART_TEST_BISECTION:
    env.ART_TEST_RUN_TEST_NO_PREBUILD = True
    env.ART_TEST_RUN_TEST_PREBUILD = False
    # Bisection search writes to standard output.
    env.ART_TEST_QUIET = False

  global _user_input_variants
  global run_all_configs
  # These are the default variant-options we will use if nothing in the group is specified.
  default_variants = {
      'target': {'host', 'target'},
      'prebuild': {'prebuild'},
      'cdex_level': {'cdex-fast'},
      'jvmti': { 'no-jvmti'},
      'compiler': {'optimizing',
                   'jit',
                   'interpreter',
                   'interp-ac',
                   'speed-profile'},
      'relocate': {'no-relocate'},
      'trace': {'ntrace'},
      'gc': {'cms'},
      'jni': {'checkjni'},
      'image': {'picimage'},
      'debuggable': {'ndebuggable'},
      'run': {'debug'},
      # address_sizes_target depends on the target so it is dealt with below.
  }
  # We want to pull these early since the full VARIANT_TYPE_DICT has a few additional ones we don't
  # want to pick up if we pass --all.
  default_variants_keys = default_variants.keys()
  if run_all_configs:
    default_variants = VARIANT_TYPE_DICT

  for key in default_variants_keys:
    if not _user_input_variants[key]:
      _user_input_variants[key] = default_variants[key]

  _user_input_variants['address_sizes_target'] = collections.defaultdict(set)
  if not _user_input_variants['address_sizes']:
    _user_input_variants['address_sizes_target']['target'].add(
        env.ART_PHONY_TEST_TARGET_SUFFIX)
    _user_input_variants['address_sizes_target']['host'].add(
        env.ART_PHONY_TEST_HOST_SUFFIX)
    if env.ART_TEST_RUN_TEST_2ND_ARCH:
      _user_input_variants['address_sizes_target']['host'].add(
          env.ART_2ND_PHONY_TEST_HOST_SUFFIX)
      _user_input_variants['address_sizes_target']['target'].add(
          env.ART_2ND_PHONY_TEST_TARGET_SUFFIX)
  else:
    _user_input_variants['address_sizes_target']['host'] = _user_input_variants['address_sizes']
    _user_input_variants['address_sizes_target']['target'] = _user_input_variants['address_sizes']

  global n_thread
  if 'target' in _user_input_variants['target']:
    device_name = get_device_name()
    if n_thread == 0:
      # Use only part of the cores since fully loading the device tends to lead to timeouts.
      n_thread = max(1, int(get_target_cpu_count() * 0.75))
      if device_name == 'fugu':
        n_thread = 1
  else:
    device_name = "host"
    if n_thread == 0:
      n_thread = get_host_cpu_count()
  print_text("Concurrency: {} ({})\n".format(n_thread, device_name))

  global extra_arguments
  for target in _user_input_variants['target']:
    extra_arguments[target] = find_extra_device_arguments(target)

  if not sys.stdout.isatty():
    global COLOR_ERROR
    global COLOR_PASS
    global COLOR_SKIP
    global COLOR_NORMAL
    COLOR_ERROR = ''
    COLOR_PASS = ''
    COLOR_SKIP = ''
    COLOR_NORMAL = ''

def find_extra_device_arguments(target):
  """
  Gets any extra arguments from the device_config.
  """
  device_name = target
  if target == 'target':
    device_name = get_device_name()
  return device_config.get(device_name, { 'run-test-args' : [] })['run-test-args']

def get_device_name():
  """
  Gets the value of ro.product.name from remote device (unless running on a VM).
  """
  if env.ART_TEST_ON_VM:
    return subprocess.Popen(f"{env.ART_SSH_CMD} uname -a".split(),
                            stdout = subprocess.PIPE,
                            universal_newlines=True).stdout.read().strip()

  proc = subprocess.Popen(['adb', 'shell', 'getprop', 'ro.product.name'],
                          stderr=subprocess.STDOUT,
                          stdout = subprocess.PIPE,
                          universal_newlines=True)
  # only wait 2 seconds.
  timeout_val = 2

  if env.ART_TEST_RUN_ON_ARM_FVP:
    # Increase timeout to 200 seconds due to the emulation overhead on FVP.
    timeout_val = 200

  output = proc.communicate(timeout = timeout_val)[0]
  success = not proc.wait()
  if success:
    return output.strip()
  else:
    print_text("Unable to determine device type!\n")
    print_text("Continuing anyway.\n")
    return "UNKNOWN_TARGET"

def run_tests(tests):
  """This method generates variants of the tests to be run and executes them.

  Args:
    tests: The set of tests to be run.
  """
  args_all = []

  # jvm does not run with all these combinations,
  # or at least it doesn't make sense for most of them.
  # TODO: support some jvm variants like jvmti ?
  target_input_variants = _user_input_variants['target']
  uncombinated_target_input_variants = []
  if 'jvm' in target_input_variants:
    _user_input_variants['target'].remove('jvm')
    uncombinated_target_input_variants.append('jvm')

  global total_test_count
  total_test_count = len(tests)
  if target_input_variants:
    for variant_type in VARIANT_TYPE_DICT:
      if not (variant_type == 'target' or 'address_sizes' in variant_type):
        total_test_count *= len(_user_input_variants[variant_type])
  target_address_combinations = 0
  for target in target_input_variants:
    for address_size in _user_input_variants['address_sizes_target'][target]:
      target_address_combinations += 1
  target_address_combinations += len(uncombinated_target_input_variants)
  total_test_count *= target_address_combinations

  if env.ART_TEST_WITH_STRACE:
    args_all += ['--strace']

  if env.ART_TEST_RUN_TEST_ALWAYS_CLEAN:
    args_all += ['--always-clean']

  if env.ART_TEST_BISECTION:
    args_all += ['--bisection-search']

  if gdb:
    args_all += ['--gdb']
    if gdb_arg:
      args_all += ['--gdb-arg', gdb_arg]

  if dump_cfg:
    args_all += ['--dump-cfg', dump_cfg]
  if gdb_dex2oat:
    args_all += ['--gdb-dex2oat']
    if gdb_dex2oat_args:
      args_all += ['--gdb-dex2oat-args', f'{gdb_dex2oat_args}']

  args_all += run_test_option

  if runtime_option:
    for opt in runtime_option:
      args_all += ['--runtime-option', opt]
  if with_agent:
    for opt in with_agent:
      args_all += ['--with-agent', opt]

  if dex2oat_jobs != -1:
    args_all += ['--dex2oat-jobs', str(dex2oat_jobs)]

  def iter_config(tests, input_variants, user_input_variants):
    config = itertools.product(tests, input_variants, user_input_variants['run'],
                                 user_input_variants['prebuild'], user_input_variants['compiler'],
                                 user_input_variants['relocate'], user_input_variants['trace'],
                                 user_input_variants['gc'], user_input_variants['jni'],
                                 user_input_variants['image'],
                                 user_input_variants['debuggable'], user_input_variants['jvmti'],
                                 user_input_variants['cdex_level'])
    return config

  # [--host, --target] combines with all the other user input variants.
  config = iter_config(tests, target_input_variants, _user_input_variants)
  # [--jvm] currently combines with nothing else. most of the extra flags we'd insert
  # would be unrecognizable by the 'java' binary, so avoid inserting any extra flags for now.
  uncombinated_config = iter_config(tests, uncombinated_target_input_variants, { 'run': [''],
      'prebuild': [''], 'compiler': [''],
      'relocate': [''], 'trace': [''],
      'gc': [''], 'jni': [''],
      'image': [''],
      'debuggable': [''], 'jvmti': [''],
      'cdex_level': ['']})

  def start_combination(executor, config_tuple, global_options, address_size):
      test, target, run, prebuild, compiler, relocate, trace, gc, \
      jni, image, debuggable, jvmti, cdex_level = config_tuple

      # NB The order of components here should match the order of
      # components in the regex parser in parse_test_name.
      test_name = 'test-art-'
      test_name += target + '-run-test-'
      test_name += run + '-'
      test_name += prebuild + '-'
      test_name += compiler + '-'
      test_name += relocate + '-'
      test_name += trace + '-'
      test_name += gc + '-'
      test_name += jni + '-'
      test_name += image + '-'
      test_name += debuggable + '-'
      test_name += jvmti + '-'
      test_name += cdex_level + '-'
      test_name += test
      test_name += address_size

      variant_set = {target, run, prebuild, compiler, relocate, trace, gc, jni,
                     image, debuggable, jvmti, cdex_level, address_size}

      args_test = global_options.copy()

      if target == 'host':
        args_test += ['--host']
      elif target == 'jvm':
        args_test += ['--jvm']

      # Honor ART_TEST_CHROOT, ART_TEST_ANDROID_ROOT, ART_TEST_ANDROID_ART_ROOT,
      # ART_TEST_ANDROID_I18N_ROOT, and ART_TEST_ANDROID_TZDATA_ROOT but only
      # for target tests.
      if target == 'target':
        if env.ART_TEST_CHROOT:
          args_test += ['--chroot', env.ART_TEST_CHROOT]
        if env.ART_TEST_ANDROID_ROOT:
          args_test += ['--android-root', env.ART_TEST_ANDROID_ROOT]
        if env.ART_TEST_ANDROID_I18N_ROOT:
            args_test += ['--android-i18n-root', env.ART_TEST_ANDROID_I18N_ROOT]
        if env.ART_TEST_ANDROID_ART_ROOT:
          args_test += ['--android-art-root', env.ART_TEST_ANDROID_ART_ROOT]
        if env.ART_TEST_ANDROID_TZDATA_ROOT:
          args_test += ['--android-tzdata-root', env.ART_TEST_ANDROID_TZDATA_ROOT]

      if run == 'ndebug':
        args_test += ['-O']

      if prebuild == 'prebuild':
        args_test += ['--prebuild']
      elif prebuild == 'no-prebuild':
        args_test += ['--no-prebuild']

      if cdex_level:
        # Add option and remove the cdex- prefix.
        args_test += ['--compact-dex-level', cdex_level.replace('cdex-','')]

      if compiler == 'optimizing':
        args_test += ['--optimizing']
      elif compiler == 'interpreter':
        args_test += ['--interpreter']
      elif compiler == 'interp-ac':
        args_test += ['--switch-interpreter', '--verify-soft-fail']
      elif compiler == 'jit':
        args_test += ['--jit']
      elif compiler == 'jit-on-first-use':
        args_test += ['--jit', '--runtime-option', '-Xjitthreshold:0']
      elif compiler == 'speed-profile':
        args_test += ['--random-profile']
      elif compiler == 'baseline':
        args_test += ['--baseline']

      if relocate == 'relocate':
        args_test += ['--relocate']
      elif relocate == 'no-relocate':
        args_test += ['--no-relocate']

      if trace == 'trace':
        args_test += ['--trace']
      elif trace == 'stream':
        args_test += ['--trace', '--stream']

      if gc == 'gcverify':
        args_test += ['--gcverify']
      elif gc == 'gcstress':
        args_test += ['--gcstress']

      if jni == 'forcecopy':
        args_test += ['--runtime-option', '-Xjniopts:forcecopy']
      elif jni == 'checkjni':
        args_test += ['--runtime-option', '-Xcheck:jni']

      if image == 'no-image':
        args_test += ['--no-image']

      if debuggable == 'debuggable':
        args_test += ['--debuggable', '--runtime-option', '-Xopaque-jni-ids:true']

      if jvmti == 'jvmti-stress':
        args_test += ['--jvmti-trace-stress', '--jvmti-redefine-stress', '--jvmti-field-stress']
      elif jvmti == 'field-stress':
        args_test += ['--jvmti-field-stress']
      elif jvmti == 'trace-stress':
        args_test += ['--jvmti-trace-stress']
      elif jvmti == 'redefine-stress':
        args_test += ['--jvmti-redefine-stress']
      elif jvmti == 'step-stress':
        args_test += ['--jvmti-step-stress']

      if address_size == '64':
        args_test += ['--64']

      # b/36039166: Note that the path lengths must kept reasonably short.
      temp_path = tempfile.mkdtemp(dir=env.ART_HOST_TEST_DIR)
      args_test = ['--temp-path', temp_path] + args_test

      # Run the run-test script using the prebuilt python.
      python3_bin = env.ANDROID_BUILD_TOP + "/prebuilts/build-tools/path/linux-x86/python3"
      run_test_sh = env.ANDROID_BUILD_TOP + '/art/test/run-test'
      args_test = [python3_bin, run_test_sh] + args_test + extra_arguments[target] + [test]
      return executor.submit(run_test, args_test, test, variant_set, test_name)

  global n_thread
  with concurrent.futures.ThreadPoolExecutor(max_workers=n_thread) as executor:
    test_futures = []
    for config_tuple in config:
      target = config_tuple[1]
      for address_size in _user_input_variants['address_sizes_target'][target]:
        test_futures.append(start_combination(executor, config_tuple, args_all, address_size))

    for config_tuple in uncombinated_config:
      test_futures.append(
          start_combination(executor, config_tuple, args_all, ""))  # no address size

    try:
      tests_done = 0
      for test_future in concurrent.futures.as_completed(f for f in test_futures if f):
        (test, status, failure_info, test_time) = test_future.result()
        tests_done += 1
        print_test_info(tests_done, test, status, failure_info, test_time)
        if failure_info and not env.ART_TEST_KEEP_GOING:
          for f in test_futures:
            f.cancel()
          break
    except KeyboardInterrupt:
      for f in test_futures:
        f.cancel()
      child_process_tracker.kill_all()
    executor.shutdown(True)

def _popen(**kwargs):
  if sys.version_info.major == 3 and sys.version_info.minor >= 6:
    return subprocess.Popen(encoding=sys.stdout.encoding, **kwargs)
  return subprocess.Popen(**kwargs)

def run_test(args, test, test_variant, test_name):
  """Runs the test.

  It invokes art/test/run-test script to run the test. The output of the script
  is checked, and if it ends with "Succeeded!", it assumes that the tests
  passed, otherwise, put it in the list of failed test. Before actually running
  the test, it also checks if the test is placed in the list of disabled tests,
  and if yes, it skips running it, and adds the test in the list of skipped
  tests.

  Args:
    args: The command to be used to invoke the script
    test: The name of the test without the variant information.
    test_variant: The set of variant for the test.
    test_name: The name of the test along with the variants.

  Returns: a tuple of testname, status, optional failure info, and test time.
  """
  try:
    command = ' '.join(args)

    if is_test_disabled(test, test_variant):
      test_skipped = True
      test_time = datetime.timedelta()
    else:
      test_skipped = False
      test_start_time = time.monotonic()
      if verbose:
        print_text("Starting %s at %s\n" % (test_name, test_start_time))
      environ = dict(os.environ)
      environ["FULL_TEST_NAME"] = test_name
      if gdb or gdb_dex2oat:
        proc = _popen(
          args=args,
          env=environ,
          stderr=subprocess.STDOUT,
          universal_newlines=True,
          start_new_session=True
        )
      else:
        proc = _popen(
          args=args,
          env=environ,
          stderr=subprocess.STDOUT,
          stdout = subprocess.PIPE,
          universal_newlines=True,
          start_new_session=True,
        )
      script_output, return_value = child_process_tracker.wait(proc, timeout)
      test_passed = not return_value
      test_time_seconds = time.monotonic() - test_start_time
      test_time = datetime.timedelta(seconds=test_time_seconds)

    if not test_skipped:
      if test_passed:
        return (test_name, 'PASS', None, test_time)
      else:
        failed_tests.append((test_name, str(command) + "\n" + script_output))
        return (test_name, 'FAIL', ('%s\n%s') % (command, script_output), test_time)
    elif not dry_run:
      skipped_tests.append(test_name)
      return (test_name, 'SKIP', None, test_time)
    else:
      return (test_name, 'PASS', None, test_time)
  except subprocess.TimeoutExpired as e:
    if verbose:
      print_text("Timeout of %s at %s\n" % (test_name, time.monotonic()))
    test_time_seconds = time.monotonic() - test_start_time
    test_time = datetime.timedelta(seconds=test_time_seconds)
    failed_tests.append((test_name, 'Timed out in %d seconds' % timeout))

    # HACK(b/142039427): Print extra backtraces on timeout.
    if "-target-" in test_name and not env.ART_TEST_ON_VM:
      for i in range(8):
        proc_name = "dalvikvm" + test_name[-2:]
        pidof = subprocess.run(["adb", "shell", "pidof", proc_name], stdout=subprocess.PIPE)
        for pid in pidof.stdout.decode("ascii").split():
          if i >= 4:
            print_text("Backtrace of %s at %s\n" % (pid, time.monotonic()))
            subprocess.run(["adb", "shell", "debuggerd", pid])
            time.sleep(10)
          task_dir = "/proc/%s/task" % pid
          tids = subprocess.run(["adb", "shell", "ls", task_dir], stdout=subprocess.PIPE)
          for tid in tids.stdout.decode("ascii").split():
            for status in ["stat", "status"]:
              filename = "%s/%s/%s" % (task_dir, tid, status)
              print_text("Content of %s\n" % (filename))
              subprocess.run(["adb", "shell", "cat", filename])
        time.sleep(60)

    # The python documentation states that it is necessary to actually kill the process.
    os.killpg(proc.pid, signal.SIGKILL)
    script_output = proc.communicate()

    return (test_name, 'TIMEOUT', 'Timed out in %d seconds\n%s' % (timeout, command), test_time)
  except Exception as e:
    failed_tests.append((test_name, str(e)))
    return (test_name, 'FAIL', ('%s\n%s\n\n') % (command, str(e)), datetime.timedelta())

@lru_cache
def get_console_width(default=100):
  # NB: The command may fail if we are running under 'nohup'.
  proc = subprocess.run(['stty', 'size'], capture_output=True)
  return int(proc.stdout.decode("utf8").split()[1]) if proc.returncode == 0 else default

def print_test_info(test_count, test_name, result, failed_test_info="",
                    test_time=datetime.timedelta()):
  """Print the continous test information

  If verbose is set to True, it continuously prints test status information
  on a new line.
  If verbose is set to False, it keeps on erasing test
  information by overriding it with the latest test information. Also,
  in this case it stictly makes sure that the information length doesn't
  exceed the console width. It does so by shortening the test_name.

  When a test fails, it prints the output of the run-test script and
  command used to invoke the script. It doesn't override the failing
  test information in either of the cases.
  """

  info = ''
  if not verbose:
    # Without --verbose, the testrunner erases passing test info. It
    # does that by overriding the printed text with white spaces all across
    # the console width.
    info = '\r' + ' ' * get_console_width() + '\r'
  try:
    percent = (test_count * 100) / total_test_count
    progress_info = ('[ %d%% %d/%d ]') % (
      percent,
      test_count,
      total_test_count)
    if test_time.total_seconds() != 0 and verbose:
      info += '(%s)' % str(test_time)


    if result == 'FAIL' or result == 'TIMEOUT':
      if not verbose:
        info += ('%s %s %s\n') % (
          progress_info,
          test_name,
          COLOR_ERROR + result + COLOR_NORMAL)
      else:
        info += ('%s %s %s\n%s\n') % (
          progress_info,
          test_name,
          COLOR_ERROR + result + COLOR_NORMAL,
          failed_test_info)
    else:
      result_text = ''
      if result == 'PASS':
        result_text += COLOR_PASS + 'PASS' + COLOR_NORMAL
      elif result == 'SKIP':
        result_text += COLOR_SKIP + 'SKIP' + COLOR_NORMAL

      if verbose:
        info += ('%s %s %s\n') % (
          progress_info,
          test_name,
          result_text)
      else:
        total_output_length = 2 # Two spaces
        total_output_length += len(progress_info)
        total_output_length += len(result)
        allowed_test_length = get_console_width() - total_output_length
        test_name_len = len(test_name)
        if allowed_test_length < test_name_len:
          test_name = ('...%s') % (
            test_name[-(allowed_test_length - 3):])
        info += ('%s %s %s') % (
          progress_info,
          test_name,
          result_text)
    send_csv_result(test_name, result)
    print_text(info)
  except Exception as e:
    print_text(('%s\n%s\n') % (test_name, str(e)))
    failed_tests.append(test_name)

def verify_knownfailure_entry(entry):
  supported_field = {
      'tests' : (list, str),
      'test_patterns' : (list,),
      'description' : (list, str),
      'bug' : (str,),
      'variant' : (str,),
      'devices': (list, str),
      'env_vars' : (dict,),
  }
  for field in entry:
    field_type = type(entry[field])
    if field_type not in supported_field[field]:
      raise ValueError('%s is not supported type for %s\n%s' % (
          str(field_type),
          field,
          str(entry)))

def get_disabled_test_info(device_name):
  """Generate set of known failures.

  It parses the art/test/knownfailures.json file to generate the list of
  disabled tests.

  Returns:
    The method returns a dict of tests mapped to the variants list
    for which the test should not be run.
  """
  known_failures_file = env.ANDROID_BUILD_TOP + '/art/test/knownfailures.json'
  with open(known_failures_file) as known_failures_json:
    known_failures_info = json.loads(known_failures_json.read())

  disabled_test_info = {}
  for failure in known_failures_info:
    verify_knownfailure_entry(failure)
    tests = failure.get('tests', [])
    if isinstance(tests, str):
      tests = [tests]
    patterns = failure.get("test_patterns", [])
    if (not isinstance(patterns, list)):
      raise ValueError("test_patterns is not a list in %s" % failure)

    tests += [f for f in RUN_TEST_SET if any(re.match(pat, f) is not None for pat in patterns)]
    variants = parse_variants(failure.get('variant'))

    # Treat a '"devices": "<foo>"' equivalent to 'target' variant if
    # "foo" is present in "devices".
    device_names = failure.get('devices', [])
    if isinstance(device_names, str):
      device_names = [device_names]
    if len(device_names) != 0:
      if device_name in device_names:
        variants.add('target')
      else:
        # Skip adding test info as device_name is not present in "devices" entry.
        continue

    env_vars = failure.get('env_vars')

    if check_env_vars(env_vars):
      for test in tests:
        if test not in RUN_TEST_SET:
          raise ValueError('%s is not a valid run-test' % (
              test))
        if test in disabled_test_info:
          disabled_test_info[test] = disabled_test_info[test].union(variants)
        else:
          disabled_test_info[test] = variants

  return disabled_test_info

def gather_disabled_test_info():
  global DISABLED_TEST_CONTAINER
  device_name = get_device_name() if 'target' in _user_input_variants['target'] else None
  DISABLED_TEST_CONTAINER = get_disabled_test_info(device_name)

def check_env_vars(env_vars):
  """Checks if the env variables are set as required to run the test.

  Returns:
    True if all the env variables are set as required, otherwise False.
  """

  if not env_vars:
    return True
  for key in env_vars:
    if env.get_env(key) != env_vars.get(key):
      return False
  return True


def is_test_disabled(test, variant_set):
  """Checks if the test along with the variant_set is disabled.

  Args:
    test: The name of the test as in art/test directory.
    variant_set: Variants to be used for the test.
  Returns:
    True, if the test is disabled.
  """
  if dry_run:
    return True
  if test in env.EXTRA_DISABLED_TESTS:
    return True
  if ignore_skips:
    return False
  variants_list = DISABLED_TEST_CONTAINER.get(test, {})
  for variants in variants_list:
    variants_present = True
    for variant in variants:
      if variant not in variant_set:
        variants_present = False
        break
    if variants_present:
      return True
  for bad_combo in NONFUNCTIONAL_VARIANT_SETS:
    if bad_combo.issubset(variant_set):
      return True
  return False


def parse_variants(variants):
  """Parse variants fetched from art/test/knownfailures.json.
  """
  if not variants:
    variants = ''
    for variant in TOTAL_VARIANTS_SET:
      variants += variant
      variants += '|'
    variants = variants[:-1]
  variant_list = set()
  or_variants = variants.split('|')
  for or_variant in or_variants:
    and_variants = or_variant.split('&')
    variant = set()
    for and_variant in and_variants:
      and_variant = and_variant.strip()
      if and_variant not in TOTAL_VARIANTS_SET:
        raise ValueError('%s is not a valid variant' % (
            and_variant))
      variant.add(and_variant)
    variant_list.add(frozenset(variant))
  return variant_list

def print_text(output):
  sys.stdout.write(output)
  sys.stdout.flush()

def print_analysis():
  if not verbose:
    # Without --verbose, the testrunner erases passing test info. It
    # does that by overriding the printed text with white spaces all across
    # the console width.
    eraser_text = '\r' + ' ' * get_console_width() + '\r'
    print_text(eraser_text)

  # Prints information about the total tests run.
  # E.g., "2/38 (5%) tests passed".
  passed_test_count = total_test_count - len(skipped_tests) - len(failed_tests)
  passed_test_information = ('%d/%d (%d%%) %s passed.\n') % (
      passed_test_count,
      total_test_count,
      (passed_test_count*100)/total_test_count,
      'tests' if passed_test_count > 1 else 'test')
  print_text(passed_test_information)

  # Prints the list of skipped tests, if any.
  if skipped_tests:
    print_text(COLOR_SKIP + 'SKIPPED TESTS: ' + COLOR_NORMAL + '\n')
    for test in skipped_tests:
      print_text(test + '\n')
    print_text('\n')

  # Prints the list of failed tests, if any.
  if failed_tests:
    print_text(COLOR_ERROR + 'FAILED: ' + COLOR_NORMAL + '\n')
    for test_info in failed_tests:
      print_text(('%s\n%s\n' % (test_info[0], test_info[1])))
    print_text(COLOR_ERROR + '----------' + COLOR_NORMAL + '\n')
    for failed_test in sorted([test_info[0] for test_info in failed_tests]):
      print_text(('%s\n' % (failed_test)))

test_name_matcher = None
def extract_test_name(test_name):
  """Parses the test name and returns all the parts"""
  global test_name_matcher
  if test_name_matcher is None:
    regex = '^test-art-'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['target']) + ')-'
    regex += 'run-test-'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['run']) + ')-'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['prebuild']) + ')-'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['compiler']) + ')-'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['relocate']) + ')-'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['trace']) + ')-'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['gc']) + ')-'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['jni']) + ')-'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['image']) + ')-'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['debuggable']) + ')-'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['jvmti']) + ')-'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['cdex_level']) + ')-'
    regex += '(' + '|'.join(RUN_TEST_SET) + ')'
    regex += '(' + '|'.join(VARIANT_TYPE_DICT['address_sizes']) + ')$'
    test_name_matcher = re.compile(regex)
  match = test_name_matcher.match(test_name)
  if match:
    return list(match.group(i) for i in range(1,15))
  raise ValueError(test_name + " is not a valid test")

def parse_test_name(test_name):
  """Parses the testname provided by the user.
  It supports two types of test_name:
  1) Like 001-HelloWorld. In this case, it will just verify if the test actually
  exists and if it does, it returns the testname.
  2) Like test-art-host-run-test-debug-prebuild-interpreter-no-relocate-ntrace-cms-checkjni-pointer-ids-picimage-ndebuggable-001-HelloWorld32
  In this case, it will parse all the variants and check if they are placed
  correctly. If yes, it will set the various VARIANT_TYPES to use the
  variants required to run the test. Again, it returns the test_name
  without the variant information like 001-HelloWorld.
  """
  test_set = set()
  for test in RUN_TEST_SET:
    if test.startswith(test_name):
      test_set.add(test)
  if test_set:
    return test_set

  parsed = extract_test_name(test_name)
  _user_input_variants['target'].add(parsed[0])
  _user_input_variants['run'].add(parsed[1])
  _user_input_variants['prebuild'].add(parsed[2])
  _user_input_variants['compiler'].add(parsed[3])
  _user_input_variants['relocate'].add(parsed[4])
  _user_input_variants['trace'].add(parsed[5])
  _user_input_variants['gc'].add(parsed[6])
  _user_input_variants['jni'].add(parsed[7])
  _user_input_variants['image'].add(parsed[8])
  _user_input_variants['debuggable'].add(parsed[9])
  _user_input_variants['jvmti'].add(parsed[10])
  _user_input_variants['cdex_level'].add(parsed[11])
  _user_input_variants['address_sizes'].add(parsed[13])
  return {parsed[12]}


def get_target_cpu_count():
  if env.ART_TEST_ON_VM:
    command = f"{env.ART_SSH_CMD} cat /sys/devices/system/cpu/present"
  else:
    command = 'adb shell cat /sys/devices/system/cpu/present'
  cpu_info_proc = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
  cpu_info = cpu_info_proc.stdout.read()
  if type(cpu_info) is bytes:
    cpu_info = cpu_info.decode('utf-8')
  cpu_info_regex = r'\d*-(\d*)'
  match = re.match(cpu_info_regex, cpu_info)
  if match:
    return int(match.group(1)) + 1  # Add one to convert from "last-index" to "count"
  else:
    raise ValueError('Unable to predict the concurrency for the target. '
                     'Is device connected?')


def get_host_cpu_count():
  return multiprocessing.cpu_count()


def parse_option():
  global verbose
  global dry_run
  global ignore_skips
  global n_thread
  global build
  global dist
  global gdb
  global gdb_arg
  global dump_cfg
  global gdb_dex2oat
  global gdb_dex2oat_args
  global runtime_option
  global run_test_option
  global timeout
  global dex2oat_jobs
  global run_all_configs
  global with_agent
  global csv_result

  parser = argparse.ArgumentParser(description="Runs all or a subset of the ART test suite.")
  parser.add_argument('tests', action='extend', nargs="*", help='name(s) of the test(s)')
  parser.add_argument('-t', '--test', action='append', dest='tests', help='name(s) of the test(s)'
      ' (deprecated: use positional arguments at the end without any option instead)')
  global_group = parser.add_argument_group('Global options',
                                           'Options that affect all tests being run')
  global_group.add_argument('-j', type=int, dest='n_thread', help="""Number of CPUs to use.
                            Defaults to half of CPUs on target and all CPUs on host.""")
  global_group.add_argument('--timeout', default=timeout, type=int, dest='timeout')
  global_group.add_argument('--verbose', '-v', action='store_true', dest='verbose')
  global_group.add_argument('--dry-run', action='store_true', dest='dry_run')
  global_group.add_argument("--skip", action='append', dest="skips", default=[],
                            help="Skip the given test in all circumstances.")
  global_group.add_argument("--no-skips", dest="ignore_skips", action='store_true', default=False,
                            help="""Don't skip any run-test configurations listed in
                            knownfailures.json.""")
  global_group.add_argument('--no-build-dependencies',
                            action='store_false', dest='build',
                            help="""Don't build dependencies under any circumstances. This is the
                            behavior if ART_TEST_RUN_TEST_ALWAYS_BUILD is not set to 'true'.""")
  global_group.add_argument('-b', '--build-dependencies',
                            action='store_true', dest='build',
                            help="""Build dependencies under all circumstances. By default we will
                            not build dependencies unless ART_TEST_RUN_TEST_BUILD=true.""")
  global_group.add_argument('--dist',
                            action='store_true', dest='dist',
                            help="""If dependencies are to be built, pass `dist` to the build
                            command line. You may want to also set the DIST_DIR environment
                            variable when using this flag.""")
  global_group.set_defaults(build = env.ART_TEST_RUN_TEST_BUILD)
  global_group.add_argument('--gdb', action='store_true', dest='gdb')
  global_group.add_argument('--gdb-arg', dest='gdb_arg')
  global_group.add_argument('--dump-cfg', dest='dump_cfg',
                            help="""Dump the CFG to the specified host path.
                            Example \"--dump-cfg <full-path>/graph.cfg\".""")
  global_group.add_argument('--gdb-dex2oat', action='store_true', dest='gdb_dex2oat')
  global_group.add_argument('--gdb-dex2oat-args', dest='gdb_dex2oat_args')
  global_group.add_argument('--run-test-option', action='append', dest='run_test_option',
                            default=[],
                            help="""Pass an option, unaltered, to the run-test script.
                            This should be enclosed in single-quotes to allow for spaces. The option
                            will be split using shlex.split() prior to invoking run-test.
                            Example \"--run-test-option='--with-agent libtifast.so=MethodExit'\".""")
  global_group.add_argument('--with-agent', action='append', dest='with_agent',
                            help="""Pass an agent to be attached to the runtime""")
  global_group.add_argument('--runtime-option', action='append', dest='runtime_option',
                            help="""Pass an option to the runtime. Runtime options
                            starting with a '-' must be separated by a '=', for
                            example '--runtime-option=-Xjitthreshold:0'.""")
  global_group.add_argument('--dex2oat-jobs', type=int, dest='dex2oat_jobs',
                            help='Number of dex2oat jobs')
  global_group.add_argument('-a', '--all', action='store_true', dest='run_all',
                            help="Run all the possible configurations for the input test set")
  global_group.add_argument('--csv-results', action='store', dest='csv_result', default=None,
                            type=argparse.FileType('w'), help='Store a CSV record of all results.')
  for variant_type, variant_set in VARIANT_TYPE_DICT.items():
    var_group = parser.add_argument_group(
        '{}-type Options'.format(variant_type),
        "Options that control the '{}' variants.".format(variant_type))
    var_group.add_argument('--all-' + variant_type,
                           action='store_true',
                           dest='all_' + variant_type,
                           help='Enable all variants of ' + variant_type)
    for variant in variant_set:
      flag = '--' + variant
      var_group.add_argument(flag, action='store_true', dest=variant)

  options = vars(parser.parse_args())
  if options['csv_result'] is not None:
    csv_result = options['csv_result']
    setup_csv_result()
  # Handle the --all-<type> meta-options
  for variant_type, variant_set in VARIANT_TYPE_DICT.items():
    if options['all_' + variant_type]:
      for variant in variant_set:
        options[variant] = True

  tests = None
  env.EXTRA_DISABLED_TESTS.update(set(options['skips']))
  if options['tests']:
    tests = set()
    for test_name in options['tests']:
      tests |= parse_test_name(test_name)

  for variant_type in VARIANT_TYPE_DICT:
    for variant in VARIANT_TYPE_DICT[variant_type]:
      if options.get(variant):
        _user_input_variants[variant_type].add(variant)

  if options['verbose']:
    verbose = True
  if options['n_thread']:
    n_thread = max(1, options['n_thread'])
  ignore_skips = options['ignore_skips']
  if options['dry_run']:
    dry_run = True
    verbose = True
  build = options['build']
  dist = options['dist']
  if options['gdb']:
    n_thread = 1
    gdb = True
    if options['gdb_arg']:
      gdb_arg = options['gdb_arg']
  if options['dump_cfg']:
    dump_cfg = options['dump_cfg']
  if options['gdb_dex2oat']:
    n_thread = 1
    gdb_dex2oat = True
    if options['gdb_dex2oat_args']:
      gdb_dex2oat_args = options['gdb_dex2oat_args']
  runtime_option = options['runtime_option'];
  with_agent = options['with_agent'];
  run_test_option = sum(map(shlex.split, options['run_test_option']), [])

  timeout = options['timeout']
  if options['dex2oat_jobs']:
    dex2oat_jobs = options['dex2oat_jobs']
  if options['run_all']:
    run_all_configs = True

  return tests or RUN_TEST_SET

def main():
  gather_test_info()
  tests = parse_option()
  setup_test_env()
  gather_disabled_test_info()
  if build:
    build_targets = []
    # Build only the needed shards (depending on the selected tests).
    shards = set(re.search("(\d\d)-", t).group(1) for t in tests)
    if any("hiddenapi" in t for t in tests):
      shards.add("HiddenApi")  # Include special HiddenApi shard.
    for mode in ['host', 'target', 'jvm']:
      if mode in _user_input_variants['target']:
        build_targets += ['test-art-{}-run-test-dependencies'.format(mode)]
        if len(shards) >= 100:
          build_targets += ["art-run-test-{}-data".format(mode)]  # Build all.
        else:
          build_targets += ["art-run-test-{}-data-shard{}".format(mode, s) for s in shards]
    build_command = env.ANDROID_BUILD_TOP + '/build/soong/soong_ui.bash --make-mode'
    build_command += ' D8='
    if dist:
      build_command += ' dist'
    build_command += ' ' + ' '.join(build_targets)
    print_text('Build command: %s\n' % build_command)
    if subprocess.call(build_command.split()):
      # Debugging for b/62653020
      if env.DIST_DIR:
        shutil.copyfile(env.SOONG_OUT_DIR + '/build.ninja', env.DIST_DIR + '/soong.ninja')
      sys.exit(1)

  run_tests(tests)

  print_analysis()
  close_csv_file()

  exit_code = 0 if len(failed_tests) == 0 else 1
  sys.exit(exit_code)

if __name__ == '__main__':
  main()