blob: fe83ba9e144bf4f57afb8c797238f8f8bdcc143b [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"
18#include "runtime/runtime_options.h"
19#include "runtime/parsed_options.h"
20
21#include "utils.h"
22#include <numeric>
23#include "gtest/gtest.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070024#include "runtime/experimental_flags.h"
Igor Murashkinaaebaa02015-01-26 10:55:53 -080025
26#define EXPECT_NULL(expected) EXPECT_EQ(reinterpret_cast<const void*>(expected), \
Mathieu Chartier2cebb242015-04-21 16:50:40 -070027 reinterpret_cast<void*>(nullptr));
Igor Murashkinaaebaa02015-01-26 10:55:53 -080028
29namespace art {
30 bool UsuallyEquals(double expected, double actual);
31
32 // This has a gtest dependency, which is why it's in the gtest only.
33 bool operator==(const TestProfilerOptions& lhs, const TestProfilerOptions& rhs) {
34 return lhs.enabled_ == rhs.enabled_ &&
35 lhs.output_file_name_ == rhs.output_file_name_ &&
36 lhs.period_s_ == rhs.period_s_ &&
37 lhs.duration_s_ == rhs.duration_s_ &&
38 lhs.interval_us_ == rhs.interval_us_ &&
39 UsuallyEquals(lhs.backoff_coefficient_, rhs.backoff_coefficient_) &&
40 UsuallyEquals(lhs.start_immediately_, rhs.start_immediately_) &&
41 UsuallyEquals(lhs.top_k_threshold_, rhs.top_k_threshold_) &&
42 UsuallyEquals(lhs.top_k_change_threshold_, rhs.top_k_change_threshold_) &&
43 lhs.profile_type_ == rhs.profile_type_ &&
44 lhs.max_stack_depth_ == rhs.max_stack_depth_;
45 }
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
84 bool UsuallyEquals(const char* expected, std::string actual) {
85 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() {
128 art::InitLogging(nullptr); // argv = null
129 }
130
131 virtual void SetUp() {
132 parser_ = ParsedOptions::MakeParser(false); // do not ignore unrecognized options
133 }
134
135 static ::testing::AssertionResult IsResultSuccessful(CmdlineResult result) {
136 if (result.IsSuccess()) {
137 return ::testing::AssertionSuccess();
138 } else {
139 return ::testing::AssertionFailure()
140 << result.GetStatus() << " with: " << result.GetMessage();
141 }
142 }
143
144 static ::testing::AssertionResult IsResultFailure(CmdlineResult result,
145 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); \
193 } while (false) // NOLINT [readability/namespace] [5]
194
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 {
Richard Uhler66d874d2015-01-15 09:37:19 -0800294 const char* log_args = "-verbose:oat";
295 LogVerbosity log_verbosity = LogVerbosity();
296 log_verbosity.oat = true;
297 EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose);
298 }
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800299} // TEST_F
300
Nicolas Geoffray8f4ee5c2015-02-05 10:14:10 +0000301// TODO: Enable this b/19274810
302TEST_F(CmdlineParserTest, DISABLED_TestXGcOption) {
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800303 /*
304 * Test success
305 */
306 {
307 XGcOption option_all_true{}; // NOLINT [readability/braces] [4]
308 option_all_true.collector_type_ = gc::CollectorType::kCollectorTypeCMS;
309 option_all_true.verify_pre_gc_heap_ = true;
310 option_all_true.verify_pre_sweeping_heap_ = true;
311 option_all_true.verify_post_gc_heap_ = true;
312 option_all_true.verify_pre_gc_rosalloc_ = true;
313 option_all_true.verify_pre_sweeping_rosalloc_ = true;
314 option_all_true.verify_post_gc_rosalloc_ = true;
315
316 const char * xgc_args_all_true = "-Xgc:concurrent,"
317 "preverify,presweepingverify,postverify,"
318 "preverify_rosalloc,presweepingverify_rosalloc,"
319 "postverify_rosalloc,precise,"
320 "verifycardtable";
321
322 EXPECT_SINGLE_PARSE_VALUE(option_all_true, xgc_args_all_true, M::GcOption);
323
324 XGcOption option_all_false{}; // NOLINT [readability/braces] [4]
325 option_all_false.collector_type_ = gc::CollectorType::kCollectorTypeMS;
326 option_all_false.verify_pre_gc_heap_ = false;
327 option_all_false.verify_pre_sweeping_heap_ = false;
328 option_all_false.verify_post_gc_heap_ = false;
329 option_all_false.verify_pre_gc_rosalloc_ = false;
330 option_all_false.verify_pre_sweeping_rosalloc_ = false;
331 option_all_false.verify_post_gc_rosalloc_ = false;
332
333 const char* xgc_args_all_false = "-Xgc:nonconcurrent,"
334 "nopreverify,nopresweepingverify,nopostverify,nopreverify_rosalloc,"
335 "nopresweepingverify_rosalloc,nopostverify_rosalloc,noprecise,noverifycardtable";
336
337 EXPECT_SINGLE_PARSE_VALUE(option_all_false, xgc_args_all_false, M::GcOption);
338
339 XGcOption option_all_default{}; // NOLINT [readability/braces] [4]
340
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800341 const char* xgc_args_blank = "-Xgc:";
342 EXPECT_SINGLE_PARSE_VALUE(option_all_default, xgc_args_blank, M::GcOption);
343 }
344
345 /*
346 * Test failures
347 */
348 EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // invalid Xgc opt
349} // TEST_F
350
351/*
352 * {"-Xrunjdwp:_", "-agentlib:jdwp=_"}
353 */
354TEST_F(CmdlineParserTest, TestJdwpOptions) {
355 /*
356 * Test success
357 */
358 {
359 /*
360 * "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
361 */
362 JDWP::JdwpOptions opt = JDWP::JdwpOptions();
363 opt.transport = JDWP::JdwpTransportType::kJdwpTransportSocket;
364 opt.port = 8000;
365 opt.server = true;
366
367 const char *opt_args = "-Xrunjdwp:transport=dt_socket,address=8000,server=y";
368
369 EXPECT_SINGLE_PARSE_VALUE(opt, opt_args, M::JdwpOptions);
370 }
371
372 {
373 /*
374 * "Example: -agentlib:jdwp=transport=dt_socket,address=localhost:6500,server=n\n");
375 */
376 JDWP::JdwpOptions opt = JDWP::JdwpOptions();
377 opt.transport = JDWP::JdwpTransportType::kJdwpTransportSocket;
378 opt.host = "localhost";
379 opt.port = 6500;
380 opt.server = false;
381
382 const char *opt_args = "-agentlib:jdwp=transport=dt_socket,address=localhost:6500,server=n";
383
384 EXPECT_SINGLE_PARSE_VALUE(opt, opt_args, M::JdwpOptions);
385 }
386
387 /*
388 * Test failures
389 */
390 EXPECT_SINGLE_PARSE_FAIL("-Xrunjdwp:help", CmdlineResult::kUsage); // usage for help only
391 EXPECT_SINGLE_PARSE_FAIL("-Xrunjdwp:blabla", CmdlineResult::kFailure); // invalid subarg
392 EXPECT_SINGLE_PARSE_FAIL("-agentlib:jdwp=help", CmdlineResult::kUsage); // usage for help only
393 EXPECT_SINGLE_PARSE_FAIL("-agentlib:jdwp=blabla", CmdlineResult::kFailure); // invalid subarg
394} // TEST_F
395
396/*
397 * -D_ -D_ -D_ ...
398 */
399TEST_F(CmdlineParserTest, TestPropertiesList) {
400 /*
401 * Test successes
402 */
403 {
404 std::vector<std::string> opt = {"hello"};
405
406 EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello", M::PropertiesList);
407 }
408
409 {
410 std::vector<std::string> opt = {"hello", "world"};
411
412 EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello -Dworld", M::PropertiesList);
413 }
414
415 {
416 std::vector<std::string> opt = {"one", "two", "three"};
417
418 EXPECT_SINGLE_PARSE_VALUE(opt, "-Done -Dtwo -Dthree", M::PropertiesList);
419 }
420} // TEST_F
421
422/*
423* -Xcompiler-option foo -Xcompiler-option bar ...
424*/
425TEST_F(CmdlineParserTest, TestCompilerOption) {
426 /*
427 * Test successes
428 */
429 {
430 std::vector<std::string> opt = {"hello"};
431 EXPECT_SINGLE_PARSE_VALUE(opt, "-Xcompiler-option hello", M::CompilerOptions);
432 }
433
434 {
435 std::vector<std::string> opt = {"hello", "world"};
436 EXPECT_SINGLE_PARSE_VALUE(opt,
437 "-Xcompiler-option hello -Xcompiler-option world",
438 M::CompilerOptions);
439 }
440
441 {
442 std::vector<std::string> opt = {"one", "two", "three"};
443 EXPECT_SINGLE_PARSE_VALUE(opt,
444 "-Xcompiler-option one -Xcompiler-option two -Xcompiler-option three",
445 M::CompilerOptions);
446 }
447} // TEST_F
448
449/*
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800450* -Xjit, -Xnojit, -Xjitcodecachesize, Xjitcompilethreshold
451*/
452TEST_F(CmdlineParserTest, TestJitOptions) {
453 /*
454 * Test successes
455 */
456 {
Andreas Gampe26826992015-03-05 18:48:52 -0800457 EXPECT_SINGLE_PARSE_VALUE(true, "-Xusejit:true", M::UseJIT);
458 EXPECT_SINGLE_PARSE_VALUE(false, "-Xusejit:false", M::UseJIT);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800459 }
460 {
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000461 EXPECT_SINGLE_PARSE_VALUE(
Nicolas Geoffray295a5962015-11-19 18:17:41 +0000462 MemoryKiB(16 * KB), "-Xjitinitialsize:16K", M::JITCodeCacheInitialCapacity);
Nicolas Geoffray0a3be162015-11-18 11:15:22 +0000463 EXPECT_SINGLE_PARSE_VALUE(
Nicolas Geoffray295a5962015-11-19 18:17:41 +0000464 MemoryKiB(16 * MB), "-Xjitmaxsize:16M", M::JITCodeCacheMaxCapacity);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800465 }
466 {
467 EXPECT_SINGLE_PARSE_VALUE(12345u, "-Xjitthreshold:12345", M::JITCompileThreshold);
468 }
469} // TEST_F
470
471/*
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800472* -X-profile-*
473*/
474TEST_F(CmdlineParserTest, TestProfilerOptions) {
475 /*
476 * Test successes
477 */
478
479 {
480 TestProfilerOptions opt;
481 opt.enabled_ = true;
482
483 EXPECT_SINGLE_PARSE_VALUE(opt,
484 "-Xenable-profiler",
485 M::ProfilerOpts);
486 }
487
488 {
489 TestProfilerOptions opt;
490 // also need to test 'enabled'
491 opt.output_file_name_ = "hello_world.txt";
492
493 EXPECT_SINGLE_PARSE_VALUE(opt,
494 "-Xprofile-filename:hello_world.txt ",
495 M::ProfilerOpts);
496 }
497
498 {
499 TestProfilerOptions opt = TestProfilerOptions();
500 // also need to test 'enabled'
501 opt.output_file_name_ = "output.txt";
502 opt.period_s_ = 123u;
503 opt.duration_s_ = 456u;
504 opt.interval_us_ = 789u;
505 opt.backoff_coefficient_ = 2.0;
506 opt.start_immediately_ = true;
507 opt.top_k_threshold_ = 50.0;
508 opt.top_k_change_threshold_ = 60.0;
509 opt.profile_type_ = kProfilerMethod;
510 opt.max_stack_depth_ = 1337u;
511
512 EXPECT_SINGLE_PARSE_VALUE(opt,
513 "-Xprofile-filename:output.txt "
514 "-Xprofile-period:123 "
515 "-Xprofile-duration:456 "
516 "-Xprofile-interval:789 "
517 "-Xprofile-backoff:2.0 "
518 "-Xprofile-start-immediately "
519 "-Xprofile-top-k-threshold:50.0 "
520 "-Xprofile-top-k-change-threshold:60.0 "
521 "-Xprofile-type:method "
522 "-Xprofile-max-stack-depth:1337",
523 M::ProfilerOpts);
524 }
525
526 {
527 TestProfilerOptions opt = TestProfilerOptions();
528 opt.profile_type_ = kProfilerBoundedStack;
529
530 EXPECT_SINGLE_PARSE_VALUE(opt,
531 "-Xprofile-type:stack",
532 M::ProfilerOpts);
533 }
534} // TEST_F
535
Alex Lighteb7c1442015-08-31 13:17:42 -0700536/* -Xexperimental:_ */
537TEST_F(CmdlineParserTest, TestExperimentalFlags) {
Igor Murashkin158f35c2015-06-10 15:55:30 -0700538 // Off by default
Alex Lighteb7c1442015-08-31 13:17:42 -0700539 EXPECT_SINGLE_PARSE_DEFAULT_VALUE(ExperimentalFlags::kNone,
Igor Murashkin158f35c2015-06-10 15:55:30 -0700540 "",
Alex Lighteb7c1442015-08-31 13:17:42 -0700541 M::Experimental);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700542
543 // Disabled explicitly
Alex Lighteb7c1442015-08-31 13:17:42 -0700544 EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kNone,
545 "-Xexperimental:none",
546 M::Experimental);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700547
548 // Enabled explicitly
Alex Lighteb7c1442015-08-31 13:17:42 -0700549 EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kLambdas,
550 "-Xexperimental:lambdas",
551 M::Experimental);
552 // Enabled explicitly
553 EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kDefaultMethods,
554 "-Xexperimental:default-methods",
555 M::Experimental);
556
557 // Enabled both
558 EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kDefaultMethods | ExperimentalFlags::kLambdas,
559 "-Xexperimental:default-methods "
560 "-Xexperimental:lambdas",
561 M::Experimental);
Igor Murashkin158f35c2015-06-10 15:55:30 -0700562}
563
Igor Murashkin7617abd2015-07-10 18:27:47 -0700564// -Xverify:_
565TEST_F(CmdlineParserTest, TestVerify) {
566 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kNone, "-Xverify:none", M::Verify);
567 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:remote", M::Verify);
568 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:all", M::Verify);
569 EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kSoftFail, "-Xverify:softfail", M::Verify);
570}
571
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800572TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) {
573 RuntimeParser::Builder parserBuilder;
574
575 parserBuilder
576 .Define("-help")
577 .IntoKey(M::Help)
578 .IgnoreUnrecognized(true);
579
580 parser_.reset(new RuntimeParser(parserBuilder.Build()));
581
582 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option");
583 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option1 --non-existent-option-2");
584} // TEST_F
585
586TEST_F(CmdlineParserTest, TestIgnoredArguments) {
587 std::initializer_list<const char*> ignored_args = {
588 "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa",
589 "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:abdef",
590 "-Xdexopt:foobar", "-Xnoquithandler", "-Xjnigreflimit:ixnay", "-Xgenregmap", "-Xnogenregmap",
591 "-Xverifyopt:never", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:noop",
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800592 "-Xincludeselectedmethod", "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:nosuchluck",
593 "-Xjitoffset:none", "-Xjitconfig:yes", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile",
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800594 "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=1337"
595 };
596
597 // Check they are ignored when parsed one at a time
598 for (auto&& arg : ignored_args) {
599 SCOPED_TRACE(arg);
600 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(arg);
601 }
602
603 // Check they are ignored when we pass it all together at once
604 std::vector<const char*> argv = ignored_args;
605 EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);
606} // TEST_F
607
608TEST_F(CmdlineParserTest, MultipleArguments) {
609 EXPECT_TRUE(IsResultSuccessful(parser_->Parse(
610 "-help -XX:ForegroundHeapGrowthMultiplier=0.5 "
611 "-Xnodex2oat -Xmethod-trace -XX:LargeObjectSpace=map")));
612
613 auto&& map = parser_->ReleaseArgumentsMap();
614 EXPECT_EQ(5u, map.Size());
615 EXPECT_KEY_VALUE(map, M::Help, Unit{}); // NOLINT [whitespace/braces] [5]
616 EXPECT_KEY_VALUE(map, M::ForegroundHeapGrowthMultiplier, 0.5);
617 EXPECT_KEY_VALUE(map, M::Dex2Oat, false);
618 EXPECT_KEY_VALUE(map, M::MethodTrace, Unit{}); // NOLINT [whitespace/braces] [5]
619 EXPECT_KEY_VALUE(map, M::LargeObjectSpace, gc::space::LargeObjectSpaceType::kMap);
620} // TEST_F
621} // namespace art