blob: 15363395158620b66401f04c4341c594e819d9de [file] [log] [blame]
Igor Murashkinaaebaa02015-01-26 10:55:53 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "cmdline_parser.h"
Igor Murashkinaaebaa02015-01-26 10:55:53 -080018
Igor Murashkinaaebaa02015-01-26 10:55:53 -080019#include <numeric>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020
Igor Murashkinaaebaa02015-01-26 10:55:53 -080021#include "gtest/gtest.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070022
Andreas Gampe2c30e4a2017-08-23 11:31:32 -070023#include "experimental_flags.h"
24#include "parsed_options.h"
25#include "runtime.h"
26#include "runtime_options.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "utils.h"
Igor Murashkinaaebaa02015-01-26 10:55:53 -080028
29#define EXPECT_NULL(expected) EXPECT_EQ(reinterpret_cast<const void*>(expected), \
Mathieu Chartier2cebb242015-04-21 16:50:40 -070030 reinterpret_cast<void*>(nullptr));
Igor Murashkinaaebaa02015-01-26 10:55:53 -080031
32namespace art {
33 bool UsuallyEquals(double expected, double actual);
34
35 // This has a gtest dependency, which is why it's in the gtest only.
Calin Juravle138dbff2016-06-28 19:36:58 +010036 bool operator==(const ProfileSaverOptions& lhs, const ProfileSaverOptions& rhs) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -080037 return lhs.enabled_ == rhs.enabled_ &&
Calin Juravle138dbff2016-06-28 19:36:58 +010038 lhs.min_save_period_ms_ == rhs.min_save_period_ms_ &&
39 lhs.save_resolved_classes_delay_ms_ == rhs.save_resolved_classes_delay_ms_ &&
Mathieu Chartier7b135c82017-06-05 12:54:01 -070040 lhs.hot_startup_method_samples_ == rhs.hot_startup_method_samples_ &&
Calin Juravle138dbff2016-06-28 19:36:58 +010041 lhs.min_methods_to_save_ == rhs.min_methods_to_save_ &&
42 lhs.min_classes_to_save_ == rhs.min_classes_to_save_ &&
43 lhs.min_notification_before_wake_ == rhs.min_notification_before_wake_ &&
44 lhs.max_notification_before_wake_ == rhs.max_notification_before_wake_;
Igor Murashkinaaebaa02015-01-26 10:55:53 -080045 }
46
47 bool UsuallyEquals(double expected, double actual) {
48 using FloatingPoint = ::testing::internal::FloatingPoint<double>;
49
50 FloatingPoint exp(expected);
51 FloatingPoint act(actual);
52
53 // Compare with ULPs instead of comparing with ==
54 return exp.AlmostEquals(act);
55 }
56
57 template <typename T>
58 bool UsuallyEquals(const T& expected, const T& actual,
59 typename std::enable_if<
60 detail::SupportsEqualityOperator<T>::value>::type* = 0) {
61 return expected == actual;
62 }
63
64 // Try to use memcmp to compare simple plain-old-data structs.
65 //
66 // This should *not* generate false positives, but it can generate false negatives.
67 // This will mostly work except for fields like float which can have different bit patterns
68 // that are nevertheless equal.
69 // If a test is failing because the structs aren't "equal" when they really are
70 // then it's recommended to implement operator== for it instead.
71 template <typename T, typename ... Ignore>
72 bool UsuallyEquals(const T& expected, const T& actual,
73 const Ignore& ... more ATTRIBUTE_UNUSED,
74 typename std::enable_if<std::is_pod<T>::value>::type* = 0,
75 typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type* = 0
76 ) {
77 return memcmp(std::addressof(expected), std::addressof(actual), sizeof(T)) == 0;
78 }
79
80 bool UsuallyEquals(const XGcOption& expected, const XGcOption& actual) {
81 return memcmp(std::addressof(expected), std::addressof(actual), sizeof(expected)) == 0;
82 }
83
Andreas Gampeca620d72016-11-08 08:09:33 -080084 bool UsuallyEquals(const char* expected, const std::string& actual) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -080085 return std::string(expected) == actual;
86 }
87
88 template <typename TMap, typename TKey, typename T>
89 ::testing::AssertionResult IsExpectedKeyValue(const T& expected,
90 const TMap& map,
91 const TKey& key) {
92 auto* actual = map.Get(key);
93 if (actual != nullptr) {
94 if (!UsuallyEquals(expected, *actual)) {
95 return ::testing::AssertionFailure()
96 << "expected " << detail::ToStringAny(expected) << " but got "
97 << detail::ToStringAny(*actual);
98 }
99 return ::testing::AssertionSuccess();
100 }
101
102 return ::testing::AssertionFailure() << "key was not in the map";
103 }
104
Igor Murashkin158f35c2015-06-10 15:55:30 -0700105 template <typename TMap, typename TKey, typename T>
106 ::testing::AssertionResult IsExpectedDefaultKeyValue(const T& expected,
107 const TMap& map,
108 const TKey& key) {
109 const T& actual = map.GetOrDefault(key);
110 if (!UsuallyEquals(expected, actual)) {
111 return ::testing::AssertionFailure()
112 << "expected " << detail::ToStringAny(expected) << " but got "
113 << detail::ToStringAny(actual);
114 }
115 return ::testing::AssertionSuccess();
116 }
117
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800118class CmdlineParserTest : public ::testing::Test {
119 public:
120 CmdlineParserTest() = default;
121 ~CmdlineParserTest() = default;
122
123 protected:
124 using M = RuntimeArgumentMap;
125 using RuntimeParser = ParsedOptions::RuntimeParser;
126
127 static void SetUpTestCase() {
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700128 art::InitLogging(nullptr, art::Runtime::Abort); // argv = null
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800129 }
130
131 virtual void SetUp() {
132 parser_ = ParsedOptions::MakeParser(false); // do not ignore unrecognized options
133 }
134
Andreas Gampeca620d72016-11-08 08:09:33 -0800135 static ::testing::AssertionResult IsResultSuccessful(const CmdlineResult& result) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800136 if (result.IsSuccess()) {
137 return ::testing::AssertionSuccess();
138 } else {
139 return ::testing::AssertionFailure()
140 << result.GetStatus() << " with: " << result.GetMessage();
141 }
142 }
143
Andreas Gampeca620d72016-11-08 08:09:33 -0800144 static ::testing::AssertionResult IsResultFailure(const CmdlineResult& result,
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800145 CmdlineResult::Status failure_status) {
146 if (result.IsSuccess()) {
147 return ::testing::AssertionFailure() << " got success but expected failure: "
148 << failure_status;
149 } else if (result.GetStatus() == failure_status) {
150 return ::testing::AssertionSuccess();
151 }
152
153 return ::testing::AssertionFailure() << " expected failure " << failure_status
154 << " but got " << result.GetStatus();
155 }
156
157 std::unique_ptr<RuntimeParser> parser_;
158};
159
160#define EXPECT_KEY_EXISTS(map, key) EXPECT_TRUE((map).Exists(key))
161#define EXPECT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedKeyValue(expected, map, key))
Igor Murashkin158f35c2015-06-10 15:55:30 -0700162#define EXPECT_DEFAULT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedDefaultKeyValue(expected, map, key))
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800163
Igor Murashkin158f35c2015-06-10 15:55:30 -0700164#define _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800165 do { \
166 EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \
167 EXPECT_EQ(0u, parser_->GetArgumentsMap().Size()); \
Igor Murashkin158f35c2015-06-10 15:55:30 -0700168
169#define EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \
170 _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800171 } while (false)
172
Igor Murashkin158f35c2015-06-10 15:55:30 -0700173#define EXPECT_SINGLE_PARSE_DEFAULT_VALUE(expected, argv, key)\
174 _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \
175 RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \
176 EXPECT_DEFAULT_KEY_VALUE(args, key, expected); \
177 } while (false) // NOLINT [readability/namespace] [5]
178
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800179#define _EXPECT_SINGLE_PARSE_EXISTS(argv, key) \
180 do { \
181 EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \
182 RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \
183 EXPECT_EQ(1u, args.Size()); \
184 EXPECT_KEY_EXISTS(args, key); \
185
186#define EXPECT_SINGLE_PARSE_EXISTS(argv, key) \
187 _EXPECT_SINGLE_PARSE_EXISTS(argv, key); \
188 } while (false)
189
190#define EXPECT_SINGLE_PARSE_VALUE(expected, argv, key) \
191 _EXPECT_SINGLE_PARSE_EXISTS(argv, key); \
192 EXPECT_KEY_VALUE(args, key, expected); \
Igor Murashkin5573c372017-11-16 13:34:30 -0800193 } while (false)
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800194
195#define EXPECT_SINGLE_PARSE_VALUE_STR(expected, argv, key) \
196 EXPECT_SINGLE_PARSE_VALUE(std::string(expected), argv, key)
197
198#define EXPECT_SINGLE_PARSE_FAIL(argv, failure_status) \
199 do { \
200 EXPECT_TRUE(IsResultFailure(parser_->Parse(argv), failure_status));\
201 RuntimeArgumentMap args = parser_->ReleaseArgumentsMap();\
202 EXPECT_EQ(0u, args.Size()); \
203 } while (false)
204
205TEST_F(CmdlineParserTest, TestSimpleSuccesses) {
206 auto& parser = *parser_;
207
208 EXPECT_LT(0u, parser.CountDefinedArguments());
209
210 {
211 // Test case 1: No command line arguments
212 EXPECT_TRUE(IsResultSuccessful(parser.Parse("")));
213 RuntimeArgumentMap args = parser.ReleaseArgumentsMap();
214 EXPECT_EQ(0u, args.Size());
215 }
216
217 EXPECT_SINGLE_PARSE_EXISTS("-Xzygote", M::Zygote);
218 EXPECT_SINGLE_PARSE_VALUE_STR("/hello/world", "-Xbootclasspath:/hello/world", M::BootClassPath);
219 EXPECT_SINGLE_PARSE_VALUE("/hello/world", "-Xbootclasspath:/hello/world", M::BootClassPath);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800220 EXPECT_SINGLE_PARSE_VALUE(Memory<1>(234), "-Xss234", M::StackSize);
221 EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(1234*MB), "-Xms1234m", M::MemoryInitialSize);
222 EXPECT_SINGLE_PARSE_VALUE(true, "-XX:EnableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM);
223 EXPECT_SINGLE_PARSE_VALUE(false, "-XX:DisableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM);
224 EXPECT_SINGLE_PARSE_VALUE(0.5, "-XX:HeapTargetUtilization=0.5", M::HeapTargetUtilization);
225 EXPECT_SINGLE_PARSE_VALUE(5u, "-XX:ParallelGCThreads=5", M::ParallelGCThreads);
Jean Christophe Beyler24e04aa2014-09-12 12:03:25 -0700226 EXPECT_SINGLE_PARSE_EXISTS("-Xno-dex-file-fallback", M::NoDexFileFallback);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800227} // TEST_F
228
229TEST_F(CmdlineParserTest, TestSimpleFailures) {
230 // Test argument is unknown to the parser
231 EXPECT_SINGLE_PARSE_FAIL("abcdefg^%@#*(@#", CmdlineResult::kUnknown);
232 // Test value map substitution fails
233 EXPECT_SINGLE_PARSE_FAIL("-Xverify:whatever", CmdlineResult::kFailure);
234 // Test value type parsing failures
235 EXPECT_SINGLE_PARSE_FAIL("-Xsswhatever", CmdlineResult::kFailure); // invalid memory value
236 EXPECT_SINGLE_PARSE_FAIL("-Xms123", CmdlineResult::kFailure); // memory value too small
237 EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=0.0", CmdlineResult::kOutOfRange); // toosmal
238 EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=2.0", CmdlineResult::kOutOfRange); // toolarg
239 EXPECT_SINGLE_PARSE_FAIL("-XX:ParallelGCThreads=-5", CmdlineResult::kOutOfRange); // too small
240 EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // not a valid suboption
241} // TEST_F
242
243TEST_F(CmdlineParserTest, TestLogVerbosity) {
244 {
245 const char* log_args = "-verbose:"
Phil Wang751beff2015-08-28 15:17:15 +0800246 "class,compiler,gc,heap,jdwp,jni,monitor,profiler,signals,simulator,startup,"
247 "third-party-jni,threads,verifier";
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800248
249 LogVerbosity log_verbosity = LogVerbosity();
250 log_verbosity.class_linker = true;
251 log_verbosity.compiler = true;
252 log_verbosity.gc = true;
253 log_verbosity.heap = true;
254 log_verbosity.jdwp = true;
255 log_verbosity.jni = true;
256 log_verbosity.monitor = true;
257 log_verbosity.profiler = true;
258 log_verbosity.signals = true;
Phil Wang751beff2015-08-28 15:17:15 +0800259 log_verbosity.simulator = true;
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800260 log_verbosity.startup = true;
261 log_verbosity.third_party_jni = true;
262 log_verbosity.threads = true;
263 log_verbosity.verifier = true;
264
265 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
266 }
267
268 {
269 const char* log_args = "-verbose:"
270 "class,compiler,gc,heap,jdwp,jni,monitor";
271
272 LogVerbosity log_verbosity = LogVerbosity();
273 log_verbosity.class_linker = true;
274 log_verbosity.compiler = true;
275 log_verbosity.gc = true;
276 log_verbosity.heap = true;
277 log_verbosity.jdwp = true;
278 log_verbosity.jni = true;
279 log_verbosity.monitor = true;
280
281 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
282 }
283
284 EXPECT_SINGLE_PARSE_FAIL("-verbose:blablabla", CmdlineResult::kUsage); // invalid verbose opt
Richard Uhler66d874d2015-01-15 09:37:19 -0800285
286 {
Sebastien Hertzbba348e2015-06-01 08:28:18 +0200287 const char* log_args = "-verbose:deopt";
288 LogVerbosity log_verbosity = LogVerbosity();
289 log_verbosity.deopt = true;
290 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
291 }
292
293 {
Mathieu Chartier66a55392016-02-19 10:25:39 -0800294 const char* log_args = "-verbose:collector";
295 LogVerbosity log_verbosity = LogVerbosity();
296 log_verbosity.collector = true;
297 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
298 }
299
300 {
Richard Uhler66d874d2015-01-15 09:37:19 -0800301 const char* log_args = "-verbose:oat";
302 LogVerbosity log_verbosity = LogVerbosity();
303 log_verbosity.oat = true;
304 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
305 }
Andreas Gampebec07a02017-04-11 13:48:37 -0700306
307 {
308 const char* log_args = "-verbose:dex";
309 LogVerbosity log_verbosity = LogVerbosity();
310 log_verbosity.dex = true;
311 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
312 }
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800313} // TEST_F
314
Nicolas Geoffray8f4ee5c2015-02-05 10:14:10 +0000315// TODO: Enable this b/19274810
316TEST_F(CmdlineParserTest, DISABLED_TestXGcOption) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800317 /*
318 * Test success
319 */
320 {
Igor Murashkin5573c372017-11-16 13:34:30 -0800321 XGcOption option_all_true{};
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800322 option_all_true.collector_type_ = gc::CollectorType::kCollectorTypeCMS;
323 option_all_true.verify_pre_gc_heap_ = true;
324 option_all_true.verify_pre_sweeping_heap_ = true;
325 option_all_true.verify_post_gc_heap_ = true;
326 option_all_true.verify_pre_gc_rosalloc_ = true;
327 option_all_true.verify_pre_sweeping_rosalloc_ = true;
328 option_all_true.verify_post_gc_rosalloc_ = true;
329
330 const char * xgc_args_all_true = "-Xgc:concurrent,"
331 "preverify,presweepingverify,postverify,"
332 "preverify_rosalloc,presweepingverify_rosalloc,"
333 "postverify_rosalloc,precise,"
334 "verifycardtable";
335
336 EXPECT_SINGLE_PARSE_VALUE(option_all_true, xgc_args_all_true, M::GcOption);
337
Igor Murashkin5573c372017-11-16 13:34:30 -0800338 XGcOption option_all_false{};
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800339 option_all_false.collector_type_ = gc::CollectorType::kCollectorTypeMS;
340 option_all_false.verify_pre_gc_heap_ = false;
341 option_all_false.verify_pre_sweeping_heap_ = false;
342 option_all_false.verify_post_gc_heap_ = false;
343 option_all_false.verify_pre_gc_rosalloc_ = false;
344 option_all_false.verify_pre_sweeping_rosalloc_ = false;
345 option_all_false.verify_post_gc_rosalloc_ = false;
346
347 const char* xgc_args_all_false = "-Xgc:nonconcurrent,"
348 "nopreverify,nopresweepingverify,nopostverify,nopreverify_rosalloc,"
349 "nopresweepingverify_rosalloc,nopostverify_rosalloc,noprecise,noverifycardtable";
350
351 EXPECT_SINGLE_PARSE_VALUE(option_all_false, xgc_args_all_false, M::GcOption);
352
Igor Murashkin5573c372017-11-16 13:34:30 -0800353 XGcOption option_all_default{};
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800354
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800355 const char* xgc_args_blank = "-Xgc:";
356 EXPECT_SINGLE_PARSE_VALUE(option_all_default, xgc_args_blank, M::GcOption);
357 }
358
359 /*
360 * Test failures
361 */
362 EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // invalid Xgc opt
363} // TEST_F
364
365/*
366 * {"-Xrunjdwp:_", "-agentlib:jdwp=_"}
367 */
368TEST_F(CmdlineParserTest, TestJdwpOptions) {
369 /*
370 * Test success
371 */
372 {
373 /*
374 * "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
375 */
376 JDWP::JdwpOptions opt = JDWP::JdwpOptions();
377 opt.transport = JDWP::JdwpTransportType::kJdwpTransportSocket;
378 opt.port = 8000;
379 opt.server = true;
380
381 const char *opt_args = "-Xrunjdwp:transport=dt_socket,address=8000,server=y";
382
383 EXPECT_SINGLE_PARSE_VALUE(opt, opt_args, M::JdwpOptions);
384 }
385
386 {
387 /*
388 * "Example: -agentlib:jdwp=transport=dt_socket,address=localhost:6500,server=n\n");
389 */
390 JDWP::JdwpOptions opt = JDWP::JdwpOptions();
391 opt.transport = JDWP::JdwpTransportType::kJdwpTransportSocket;
392 opt.host = "localhost";
393 opt.port = 6500;
394 opt.server = false;
395
396 const char *opt_args = "-agentlib:jdwp=transport=dt_socket,address=localhost:6500,server=n";
397
398 EXPECT_SINGLE_PARSE_VALUE(opt, opt_args, M::JdwpOptions);
399 }
400
401 /*
402 * Test failures
403 */
404 EXPECT_SINGLE_PARSE_FAIL("-Xrunjdwp:help", CmdlineResult::kUsage); // usage for help only
405 EXPECT_SINGLE_PARSE_FAIL("-Xrunjdwp:blabla", CmdlineResult::kFailure); // invalid subarg
406 EXPECT_SINGLE_PARSE_FAIL("-agentlib:jdwp=help", CmdlineResult::kUsage); // usage for help only
407 EXPECT_SINGLE_PARSE_FAIL("-agentlib:jdwp=blabla", CmdlineResult::kFailure); // invalid subarg
408} // TEST_F
409
410/*
411 * -D_ -D_ -D_ ...
412 */
413TEST_F(CmdlineParserTest, TestPropertiesList) {
414 /*
415 * Test successes
416 */
417 {
418 std::vector<std::string> opt = {"hello"};
419
420 EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello", M::PropertiesList);
421 }
422
423 {
424 std::vector<std::string> opt = {"hello", "world"};
425
426 EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello -Dworld", M::PropertiesList);
427 }
428
429 {
430 std::vector<std::string> opt = {"one", "two", "three"};
431
432 EXPECT_SINGLE_PARSE_VALUE(opt, "-Done -Dtwo -Dthree", M::PropertiesList);
433 }
434} // TEST_F
435
436/*
437* -Xcompiler-option foo -Xcompiler-option bar ...
438*/
439TEST_F(CmdlineParserTest, TestCompilerOption) {
440 /*
441 * Test successes
442 */
443 {
444 std::vector<std::string> opt = {"hello"};
445 EXPECT_SINGLE_PARSE_VALUE(opt, "-Xcompiler-option hello", M::CompilerOptions);
446 }
447
448 {
449 std::vector<std::string> opt = {"hello", "world"};
450 EXPECT_SINGLE_PARSE_VALUE(opt,
451 "-Xcompiler-option hello -Xcompiler-option world",
452 M::CompilerOptions);
453 }
454
455 {
456 std::vector<std::string> opt = {"one", "two", "three"};
457 EXPECT_SINGLE_PARSE_VALUE(opt,
458 "-Xcompiler-option one -Xcompiler-option two -Xcompiler-option three",
459 M::CompilerOptions);
460 }
461} // TEST_F
462
463/*
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800464* -Xjit, -Xnojit, -Xjitcodecachesize, Xjitcompilethreshold
465*/
466TEST_F(CmdlineParserTest, TestJitOptions) {
467 /*
468 * Test successes
469 */
470 {
Calin Juravleffc87072016-04-20 14:22:09 +0100471 EXPECT_SINGLE_PARSE_VALUE(true, "-Xusejit:true", M::UseJitCompilation);
472 EXPECT_SINGLE_PARSE_VALUE(false, "-Xusejit:false", M::UseJitCompilation);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800473 }
474 {
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000475 EXPECT_SINGLE_PARSE_VALUE(
Nicolas Geoffray295a5962015-11-19 18:17:41 +0000476 MemoryKiB(16 * KB), "-Xjitinitialsize:16K", M::JITCodeCacheInitialCapacity);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000477 EXPECT_SINGLE_PARSE_VALUE(
Nicolas Geoffray295a5962015-11-19 18:17:41 +0000478 MemoryKiB(16 * MB), "-Xjitmaxsize:16M", M::JITCodeCacheMaxCapacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800479 }
480 {
481 EXPECT_SINGLE_PARSE_VALUE(12345u, "-Xjitthreshold:12345", M::JITCompileThreshold);
482 }
483} // TEST_F
484
485/*
Calin Juravle138dbff2016-06-28 19:36:58 +0100486* -Xps-*
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800487*/
Calin Juravle138dbff2016-06-28 19:36:58 +0100488TEST_F(CmdlineParserTest, ProfileSaverOptions) {
Mathieu Chartier885a7132017-06-10 14:35:11 -0700489 ProfileSaverOptions opt = ProfileSaverOptions(true, 1, 2, 3, 4, 5, 6, 7, "abc", true);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800490
Calin Juravle138dbff2016-06-28 19:36:58 +0100491 EXPECT_SINGLE_PARSE_VALUE(opt,
492 "-Xjitsaveprofilinginfo "
493 "-Xps-min-save-period-ms:1 "
494 "-Xps-save-resolved-classes-delay-ms:2 "
Mathieu Chartier7b135c82017-06-05 12:54:01 -0700495 "-Xps-hot-startup-method-samples:3 "
Calin Juravle138dbff2016-06-28 19:36:58 +0100496 "-Xps-min-methods-to-save:4 "
497 "-Xps-min-classes-to-save:5 "
498 "-Xps-min-notification-before-wake:6 "
Calin Juravle9545f6d2017-03-16 19:05:09 -0700499 "-Xps-max-notification-before-wake:7 "
Mathieu Chartier885a7132017-06-10 14:35:11 -0700500 "-Xps-profile-path:abc "
501 "-Xps-profile-boot-class-path",
Calin Juravle138dbff2016-06-28 19:36:58 +0100502 M::ProfileSaverOpts);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800503} // TEST_F
504
Alex Lighteb7c1442015-08-31 13:17:42 -0700505/* -Xexperimental:_ */
506TEST_F(CmdlineParserTest, TestExperimentalFlags) {
Neil Fuller9724c632016-01-07 15:42:47 +0000507 // Default
Alex Lighteb7c1442015-08-31 13:17:42 -0700508 EXPECT_SINGLE_PARSE_DEFAULT_VALUE(ExperimentalFlags::kNone,
Igor Murashkin158f35c2015-06-10 15:55:30 -0700509 "",
Alex Lighteb7c1442015-08-31 13:17:42 -0700510 M::Experimental);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700511
512 // Disabled explicitly
Alex Lighteb7c1442015-08-31 13:17:42 -0700513 EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kNone,
514 "-Xexperimental:none",
515 M::Experimental);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700516}
517
Igor Murashkin7617abd2015-07-10 18:27:47 -0700518// -Xverify:_
519TEST_F(CmdlineParserTest, TestVerify) {
520 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kNone, "-Xverify:none", M::Verify);
521 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:remote", M::Verify);
522 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:all", M::Verify);
523 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kSoftFail, "-Xverify:softfail", M::Verify);
524}
525
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800526TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) {
527 RuntimeParser::Builder parserBuilder;
528
529 parserBuilder
530 .Define("-help")
531 .IntoKey(M::Help)
532 .IgnoreUnrecognized(true);
533
534 parser_.reset(new RuntimeParser(parserBuilder.Build()));
535
536 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option");
537 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option1 --non-existent-option-2");
538} // TEST_F
539
540TEST_F(CmdlineParserTest, TestIgnoredArguments) {
541 std::initializer_list<const char*> ignored_args = {
542 "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa",
543 "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:abdef",
544 "-Xdexopt:foobar", "-Xnoquithandler", "-Xjnigreflimit:ixnay", "-Xgenregmap", "-Xnogenregmap",
545 "-Xverifyopt:never", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:noop",
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800546 "-Xincludeselectedmethod", "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:nosuchluck",
547 "-Xjitoffset:none", "-Xjitconfig:yes", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile",
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800548 "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=1337"
549 };
550
551 // Check they are ignored when parsed one at a time
552 for (auto&& arg : ignored_args) {
553 SCOPED_TRACE(arg);
554 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(arg);
555 }
556
557 // Check they are ignored when we pass it all together at once
558 std::vector<const char*> argv = ignored_args;
559 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);
560} // TEST_F
561
562TEST_F(CmdlineParserTest, MultipleArguments) {
563 EXPECT_TRUE(IsResultSuccessful(parser_->Parse(
564 "-help -XX:ForegroundHeapGrowthMultiplier=0.5 "
565 "-Xnodex2oat -Xmethod-trace -XX:LargeObjectSpace=map")));
566
567 auto&& map = parser_->ReleaseArgumentsMap();
568 EXPECT_EQ(5u, map.Size());
Igor Murashkin5573c372017-11-16 13:34:30 -0800569 EXPECT_KEY_VALUE(map, M::Help, Unit{});
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800570 EXPECT_KEY_VALUE(map, M::ForegroundHeapGrowthMultiplier, 0.5);
571 EXPECT_KEY_VALUE(map, M::Dex2Oat, false);
Igor Murashkin5573c372017-11-16 13:34:30 -0800572 EXPECT_KEY_VALUE(map, M::MethodTrace, Unit{});
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800573 EXPECT_KEY_VALUE(map, M::LargeObjectSpace, gc::space::LargeObjectSpaceType::kMap);
574} // TEST_F
575} // namespace art