• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
24 #include "runtime/experimental_flags.h"
25 
26 #define EXPECT_NULL(expected) EXPECT_EQ(reinterpret_cast<const void*>(expected), \
27                                         reinterpret_cast<void*>(nullptr));
28 
29 namespace art {
30   bool UsuallyEquals(double expected, double actual);
31 
32   // This has a gtest dependency, which is why it's in the gtest only.
operator ==(const TestProfilerOptions & lhs,const TestProfilerOptions & rhs)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 
UsuallyEquals(double expected,double actual)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>
UsuallyEquals(const T & expected,const T & actual,typename std::enable_if<detail::SupportsEqualityOperator<T>::value>::type * =0)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>
UsuallyEquals(const T & expected,const T & actual,const Ignore &...more ATTRIBUTE_UNUSED,typename std::enable_if<std::is_pod<T>::value>::type * =0,typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type * =0)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 
UsuallyEquals(const XGcOption & expected,const XGcOption & actual)80   bool UsuallyEquals(const XGcOption& expected, const XGcOption& actual) {
81     return memcmp(std::addressof(expected), std::addressof(actual), sizeof(expected)) == 0;
82   }
83 
UsuallyEquals(const char * expected,std::string actual)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>
IsExpectedKeyValue(const T & expected,const TMap & map,const TKey & key)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 
105   template <typename TMap, typename TKey, typename T>
IsExpectedDefaultKeyValue(const T & expected,const TMap & map,const TKey & key)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 
118 class 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 
SetUpTestCase()127   static void SetUpTestCase() {
128     art::InitLogging(nullptr);  // argv = null
129   }
130 
SetUp()131   virtual void SetUp() {
132     parser_ = ParsedOptions::MakeParser(false);  // do not ignore unrecognized options
133   }
134 
IsResultSuccessful(CmdlineResult result)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 
IsResultFailure(CmdlineResult result,CmdlineResult::Status failure_status)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))
162 #define EXPECT_DEFAULT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedDefaultKeyValue(expected, map, key))
163 
164 #define _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv)              \
165   do {                                                        \
166     EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv)));    \
167     EXPECT_EQ(0u, parser_->GetArgumentsMap().Size());         \
168 
169 #define EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv)               \
170   _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);                   \
171   } while (false)
172 
173 #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 
179 #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 
TEST_F(CmdlineParserTest,TestSimpleSuccesses)205 TEST_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);
220   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);
226   EXPECT_SINGLE_PARSE_EXISTS("-Xno-dex-file-fallback", M::NoDexFileFallback);
227 }  // TEST_F
228 
TEST_F(CmdlineParserTest,TestSimpleFailures)229 TEST_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 
TEST_F(CmdlineParserTest,TestLogVerbosity)243 TEST_F(CmdlineParserTest, TestLogVerbosity) {
244   {
245     const char* log_args = "-verbose:"
246         "class,compiler,gc,heap,jdwp,jni,monitor,profiler,signals,simulator,startup,"
247         "third-party-jni,threads,verifier";
248 
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;
259     log_verbosity.simulator = true;
260     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
285 
286   {
287     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   {
294     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   {
301     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   }
306 }  // TEST_F
307 
308 // TODO: Enable this b/19274810
TEST_F(CmdlineParserTest,DISABLED_TestXGcOption)309 TEST_F(CmdlineParserTest, DISABLED_TestXGcOption) {
310   /*
311    * Test success
312    */
313   {
314     XGcOption option_all_true{};  // NOLINT [readability/braces] [4]
315     option_all_true.collector_type_ = gc::CollectorType::kCollectorTypeCMS;
316     option_all_true.verify_pre_gc_heap_ = true;
317     option_all_true.verify_pre_sweeping_heap_ = true;
318     option_all_true.verify_post_gc_heap_ = true;
319     option_all_true.verify_pre_gc_rosalloc_ = true;
320     option_all_true.verify_pre_sweeping_rosalloc_ = true;
321     option_all_true.verify_post_gc_rosalloc_ = true;
322 
323     const char * xgc_args_all_true = "-Xgc:concurrent,"
324         "preverify,presweepingverify,postverify,"
325         "preverify_rosalloc,presweepingverify_rosalloc,"
326         "postverify_rosalloc,precise,"
327         "verifycardtable";
328 
329     EXPECT_SINGLE_PARSE_VALUE(option_all_true, xgc_args_all_true, M::GcOption);
330 
331     XGcOption option_all_false{};  // NOLINT [readability/braces] [4]
332     option_all_false.collector_type_ = gc::CollectorType::kCollectorTypeMS;
333     option_all_false.verify_pre_gc_heap_ = false;
334     option_all_false.verify_pre_sweeping_heap_ = false;
335     option_all_false.verify_post_gc_heap_ = false;
336     option_all_false.verify_pre_gc_rosalloc_ = false;
337     option_all_false.verify_pre_sweeping_rosalloc_ = false;
338     option_all_false.verify_post_gc_rosalloc_ = false;
339 
340     const char* xgc_args_all_false = "-Xgc:nonconcurrent,"
341         "nopreverify,nopresweepingverify,nopostverify,nopreverify_rosalloc,"
342         "nopresweepingverify_rosalloc,nopostverify_rosalloc,noprecise,noverifycardtable";
343 
344     EXPECT_SINGLE_PARSE_VALUE(option_all_false, xgc_args_all_false, M::GcOption);
345 
346     XGcOption option_all_default{};  // NOLINT [readability/braces] [4]
347 
348     const char* xgc_args_blank = "-Xgc:";
349     EXPECT_SINGLE_PARSE_VALUE(option_all_default, xgc_args_blank, M::GcOption);
350   }
351 
352   /*
353    * Test failures
354    */
355   EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage);  // invalid Xgc opt
356 }  // TEST_F
357 
358 /*
359  * {"-Xrunjdwp:_", "-agentlib:jdwp=_"}
360  */
TEST_F(CmdlineParserTest,TestJdwpOptions)361 TEST_F(CmdlineParserTest, TestJdwpOptions) {
362   /*
363    * Test success
364    */
365   {
366     /*
367      * "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n"
368      */
369     JDWP::JdwpOptions opt = JDWP::JdwpOptions();
370     opt.transport = JDWP::JdwpTransportType::kJdwpTransportSocket;
371     opt.port = 8000;
372     opt.server = true;
373 
374     const char *opt_args = "-Xrunjdwp:transport=dt_socket,address=8000,server=y";
375 
376     EXPECT_SINGLE_PARSE_VALUE(opt, opt_args, M::JdwpOptions);
377   }
378 
379   {
380     /*
381      * "Example: -agentlib:jdwp=transport=dt_socket,address=localhost:6500,server=n\n");
382      */
383     JDWP::JdwpOptions opt = JDWP::JdwpOptions();
384     opt.transport = JDWP::JdwpTransportType::kJdwpTransportSocket;
385     opt.host = "localhost";
386     opt.port = 6500;
387     opt.server = false;
388 
389     const char *opt_args = "-agentlib:jdwp=transport=dt_socket,address=localhost:6500,server=n";
390 
391     EXPECT_SINGLE_PARSE_VALUE(opt, opt_args, M::JdwpOptions);
392   }
393 
394   /*
395    * Test failures
396    */
397   EXPECT_SINGLE_PARSE_FAIL("-Xrunjdwp:help", CmdlineResult::kUsage);  // usage for help only
398   EXPECT_SINGLE_PARSE_FAIL("-Xrunjdwp:blabla", CmdlineResult::kFailure);  // invalid subarg
399   EXPECT_SINGLE_PARSE_FAIL("-agentlib:jdwp=help", CmdlineResult::kUsage);  // usage for help only
400   EXPECT_SINGLE_PARSE_FAIL("-agentlib:jdwp=blabla", CmdlineResult::kFailure);  // invalid subarg
401 }  // TEST_F
402 
403 /*
404  * -D_ -D_ -D_ ...
405  */
TEST_F(CmdlineParserTest,TestPropertiesList)406 TEST_F(CmdlineParserTest, TestPropertiesList) {
407   /*
408    * Test successes
409    */
410   {
411     std::vector<std::string> opt = {"hello"};
412 
413     EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello", M::PropertiesList);
414   }
415 
416   {
417     std::vector<std::string> opt = {"hello", "world"};
418 
419     EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello -Dworld", M::PropertiesList);
420   }
421 
422   {
423     std::vector<std::string> opt = {"one", "two", "three"};
424 
425     EXPECT_SINGLE_PARSE_VALUE(opt, "-Done -Dtwo -Dthree", M::PropertiesList);
426   }
427 }  // TEST_F
428 
429 /*
430 * -Xcompiler-option foo -Xcompiler-option bar ...
431 */
TEST_F(CmdlineParserTest,TestCompilerOption)432 TEST_F(CmdlineParserTest, TestCompilerOption) {
433  /*
434   * Test successes
435   */
436   {
437     std::vector<std::string> opt = {"hello"};
438     EXPECT_SINGLE_PARSE_VALUE(opt, "-Xcompiler-option hello", M::CompilerOptions);
439   }
440 
441   {
442     std::vector<std::string> opt = {"hello", "world"};
443     EXPECT_SINGLE_PARSE_VALUE(opt,
444                               "-Xcompiler-option hello -Xcompiler-option world",
445                               M::CompilerOptions);
446   }
447 
448   {
449     std::vector<std::string> opt = {"one", "two", "three"};
450     EXPECT_SINGLE_PARSE_VALUE(opt,
451                               "-Xcompiler-option one -Xcompiler-option two -Xcompiler-option three",
452                               M::CompilerOptions);
453   }
454 }  // TEST_F
455 
456 /*
457 * -Xjit, -Xnojit, -Xjitcodecachesize, Xjitcompilethreshold
458 */
TEST_F(CmdlineParserTest,TestJitOptions)459 TEST_F(CmdlineParserTest, TestJitOptions) {
460  /*
461   * Test successes
462   */
463   {
464     EXPECT_SINGLE_PARSE_VALUE(true, "-Xusejit:true", M::UseJitCompilation);
465     EXPECT_SINGLE_PARSE_VALUE(false, "-Xusejit:false", M::UseJitCompilation);
466   }
467   {
468     EXPECT_SINGLE_PARSE_VALUE(
469         MemoryKiB(16 * KB), "-Xjitinitialsize:16K", M::JITCodeCacheInitialCapacity);
470     EXPECT_SINGLE_PARSE_VALUE(
471         MemoryKiB(16 * MB), "-Xjitmaxsize:16M", M::JITCodeCacheMaxCapacity);
472   }
473   {
474     EXPECT_SINGLE_PARSE_VALUE(12345u, "-Xjitthreshold:12345", M::JITCompileThreshold);
475   }
476 }  // TEST_F
477 
478 /*
479 * -X-profile-*
480 */
TEST_F(CmdlineParserTest,TestProfilerOptions)481 TEST_F(CmdlineParserTest, TestProfilerOptions) {
482  /*
483   * Test successes
484   */
485 
486   {
487     TestProfilerOptions opt;
488     opt.enabled_ = true;
489 
490     EXPECT_SINGLE_PARSE_VALUE(opt,
491                               "-Xenable-profiler",
492                               M::ProfilerOpts);
493   }
494 
495   {
496     TestProfilerOptions opt;
497     // also need to test 'enabled'
498     opt.output_file_name_ = "hello_world.txt";
499 
500     EXPECT_SINGLE_PARSE_VALUE(opt,
501                               "-Xprofile-filename:hello_world.txt ",
502                               M::ProfilerOpts);
503   }
504 
505   {
506     TestProfilerOptions opt = TestProfilerOptions();
507     // also need to test 'enabled'
508     opt.output_file_name_ = "output.txt";
509     opt.period_s_ = 123u;
510     opt.duration_s_ = 456u;
511     opt.interval_us_ = 789u;
512     opt.backoff_coefficient_ = 2.0;
513     opt.start_immediately_ = true;
514     opt.top_k_threshold_ = 50.0;
515     opt.top_k_change_threshold_ = 60.0;
516     opt.profile_type_ = kProfilerMethod;
517     opt.max_stack_depth_ = 1337u;
518 
519     EXPECT_SINGLE_PARSE_VALUE(opt,
520                               "-Xprofile-filename:output.txt "
521                               "-Xprofile-period:123 "
522                               "-Xprofile-duration:456 "
523                               "-Xprofile-interval:789 "
524                               "-Xprofile-backoff:2.0 "
525                               "-Xprofile-start-immediately "
526                               "-Xprofile-top-k-threshold:50.0 "
527                               "-Xprofile-top-k-change-threshold:60.0 "
528                               "-Xprofile-type:method "
529                               "-Xprofile-max-stack-depth:1337",
530                               M::ProfilerOpts);
531   }
532 
533   {
534     TestProfilerOptions opt = TestProfilerOptions();
535     opt.profile_type_ = kProfilerBoundedStack;
536 
537     EXPECT_SINGLE_PARSE_VALUE(opt,
538                               "-Xprofile-type:stack",
539                               M::ProfilerOpts);
540   }
541 }  // TEST_F
542 
543 /* -Xexperimental:_ */
TEST_F(CmdlineParserTest,TestExperimentalFlags)544 TEST_F(CmdlineParserTest, TestExperimentalFlags) {
545   // Default
546   EXPECT_SINGLE_PARSE_DEFAULT_VALUE(ExperimentalFlags::kNone,
547                                     "",
548                                     M::Experimental);
549 
550   // Disabled explicitly
551   EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kNone,
552                             "-Xexperimental:none",
553                             M::Experimental);
554 
555   // Enabled explicitly
556   EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kLambdas,
557                             "-Xexperimental:lambdas",
558                             M::Experimental);
559 }
560 
561 // -Xverify:_
TEST_F(CmdlineParserTest,TestVerify)562 TEST_F(CmdlineParserTest, TestVerify) {
563   EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kNone,     "-Xverify:none",     M::Verify);
564   EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable,   "-Xverify:remote",   M::Verify);
565   EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable,   "-Xverify:all",      M::Verify);
566   EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kSoftFail, "-Xverify:softfail", M::Verify);
567 }
568 
TEST_F(CmdlineParserTest,TestIgnoreUnrecognized)569 TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) {
570   RuntimeParser::Builder parserBuilder;
571 
572   parserBuilder
573       .Define("-help")
574           .IntoKey(M::Help)
575       .IgnoreUnrecognized(true);
576 
577   parser_.reset(new RuntimeParser(parserBuilder.Build()));
578 
579   EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option");
580   EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option1 --non-existent-option-2");
581 }  //  TEST_F
582 
TEST_F(CmdlineParserTest,TestIgnoredArguments)583 TEST_F(CmdlineParserTest, TestIgnoredArguments) {
584   std::initializer_list<const char*> ignored_args = {
585       "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa",
586       "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:abdef",
587       "-Xdexopt:foobar", "-Xnoquithandler", "-Xjnigreflimit:ixnay", "-Xgenregmap", "-Xnogenregmap",
588       "-Xverifyopt:never", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:noop",
589       "-Xincludeselectedmethod", "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:nosuchluck",
590       "-Xjitoffset:none", "-Xjitconfig:yes", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile",
591       "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=1337"
592   };
593 
594   // Check they are ignored when parsed one at a time
595   for (auto&& arg : ignored_args) {
596     SCOPED_TRACE(arg);
597     EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(arg);
598   }
599 
600   // Check they are ignored when we pass it all together at once
601   std::vector<const char*> argv = ignored_args;
602   EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv);
603 }  //  TEST_F
604 
TEST_F(CmdlineParserTest,MultipleArguments)605 TEST_F(CmdlineParserTest, MultipleArguments) {
606   EXPECT_TRUE(IsResultSuccessful(parser_->Parse(
607       "-help -XX:ForegroundHeapGrowthMultiplier=0.5 "
608       "-Xnodex2oat -Xmethod-trace -XX:LargeObjectSpace=map")));
609 
610   auto&& map = parser_->ReleaseArgumentsMap();
611   EXPECT_EQ(5u, map.Size());
612   EXPECT_KEY_VALUE(map, M::Help, Unit{});  // NOLINT [whitespace/braces] [5]
613   EXPECT_KEY_VALUE(map, M::ForegroundHeapGrowthMultiplier, 0.5);
614   EXPECT_KEY_VALUE(map, M::Dex2Oat, false);
615   EXPECT_KEY_VALUE(map, M::MethodTrace, Unit{});  // NOLINT [whitespace/braces] [5]
616   EXPECT_KEY_VALUE(map, M::LargeObjectSpace, gc::space::LargeObjectSpaceType::kMap);
617 }  //  TEST_F
618 }  // namespace art
619