Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 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" |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 24 | #include "runtime/experimental_flags.h" |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 25 | |
| 26 | #define EXPECT_NULL(expected) EXPECT_EQ(reinterpret_cast<const void*>(expected), \ |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 27 | reinterpret_cast<void*>(nullptr)); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 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. |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 33 | bool operator==(const ProfileSaverOptions& lhs, const ProfileSaverOptions& rhs) { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 34 | return lhs.enabled_ == rhs.enabled_ && |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 35 | lhs.min_save_period_ms_ == rhs.min_save_period_ms_ && |
| 36 | lhs.save_resolved_classes_delay_ms_ == rhs.save_resolved_classes_delay_ms_ && |
| 37 | lhs.startup_method_samples_ == rhs.startup_method_samples_ && |
| 38 | lhs.min_methods_to_save_ == rhs.min_methods_to_save_ && |
| 39 | lhs.min_classes_to_save_ == rhs.min_classes_to_save_ && |
| 40 | lhs.min_notification_before_wake_ == rhs.min_notification_before_wake_ && |
| 41 | lhs.max_notification_before_wake_ == rhs.max_notification_before_wake_; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | bool UsuallyEquals(double expected, double actual) { |
| 45 | using FloatingPoint = ::testing::internal::FloatingPoint<double>; |
| 46 | |
| 47 | FloatingPoint exp(expected); |
| 48 | FloatingPoint act(actual); |
| 49 | |
| 50 | // Compare with ULPs instead of comparing with == |
| 51 | return exp.AlmostEquals(act); |
| 52 | } |
| 53 | |
| 54 | template <typename T> |
| 55 | bool UsuallyEquals(const T& expected, const T& actual, |
| 56 | typename std::enable_if< |
| 57 | detail::SupportsEqualityOperator<T>::value>::type* = 0) { |
| 58 | return expected == actual; |
| 59 | } |
| 60 | |
| 61 | // Try to use memcmp to compare simple plain-old-data structs. |
| 62 | // |
| 63 | // This should *not* generate false positives, but it can generate false negatives. |
| 64 | // This will mostly work except for fields like float which can have different bit patterns |
| 65 | // that are nevertheless equal. |
| 66 | // If a test is failing because the structs aren't "equal" when they really are |
| 67 | // then it's recommended to implement operator== for it instead. |
| 68 | template <typename T, typename ... Ignore> |
| 69 | bool UsuallyEquals(const T& expected, const T& actual, |
| 70 | const Ignore& ... more ATTRIBUTE_UNUSED, |
| 71 | typename std::enable_if<std::is_pod<T>::value>::type* = 0, |
| 72 | typename std::enable_if<!detail::SupportsEqualityOperator<T>::value>::type* = 0 |
| 73 | ) { |
| 74 | return memcmp(std::addressof(expected), std::addressof(actual), sizeof(T)) == 0; |
| 75 | } |
| 76 | |
| 77 | bool UsuallyEquals(const XGcOption& expected, const XGcOption& actual) { |
| 78 | return memcmp(std::addressof(expected), std::addressof(actual), sizeof(expected)) == 0; |
| 79 | } |
| 80 | |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 81 | bool UsuallyEquals(const char* expected, const std::string& actual) { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 82 | return std::string(expected) == actual; |
| 83 | } |
| 84 | |
| 85 | template <typename TMap, typename TKey, typename T> |
| 86 | ::testing::AssertionResult IsExpectedKeyValue(const T& expected, |
| 87 | const TMap& map, |
| 88 | const TKey& key) { |
| 89 | auto* actual = map.Get(key); |
| 90 | if (actual != nullptr) { |
| 91 | if (!UsuallyEquals(expected, *actual)) { |
| 92 | return ::testing::AssertionFailure() |
| 93 | << "expected " << detail::ToStringAny(expected) << " but got " |
| 94 | << detail::ToStringAny(*actual); |
| 95 | } |
| 96 | return ::testing::AssertionSuccess(); |
| 97 | } |
| 98 | |
| 99 | return ::testing::AssertionFailure() << "key was not in the map"; |
| 100 | } |
| 101 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 102 | template <typename TMap, typename TKey, typename T> |
| 103 | ::testing::AssertionResult IsExpectedDefaultKeyValue(const T& expected, |
| 104 | const TMap& map, |
| 105 | const TKey& key) { |
| 106 | const T& actual = map.GetOrDefault(key); |
| 107 | if (!UsuallyEquals(expected, actual)) { |
| 108 | return ::testing::AssertionFailure() |
| 109 | << "expected " << detail::ToStringAny(expected) << " but got " |
| 110 | << detail::ToStringAny(actual); |
| 111 | } |
| 112 | return ::testing::AssertionSuccess(); |
| 113 | } |
| 114 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 115 | class CmdlineParserTest : public ::testing::Test { |
| 116 | public: |
| 117 | CmdlineParserTest() = default; |
| 118 | ~CmdlineParserTest() = default; |
| 119 | |
| 120 | protected: |
| 121 | using M = RuntimeArgumentMap; |
| 122 | using RuntimeParser = ParsedOptions::RuntimeParser; |
| 123 | |
| 124 | static void SetUpTestCase() { |
David Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 125 | art::InitLogging(nullptr, art::Runtime::Aborter); // argv = null |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | virtual void SetUp() { |
| 129 | parser_ = ParsedOptions::MakeParser(false); // do not ignore unrecognized options |
| 130 | } |
| 131 | |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 132 | static ::testing::AssertionResult IsResultSuccessful(const CmdlineResult& result) { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 133 | if (result.IsSuccess()) { |
| 134 | return ::testing::AssertionSuccess(); |
| 135 | } else { |
| 136 | return ::testing::AssertionFailure() |
| 137 | << result.GetStatus() << " with: " << result.GetMessage(); |
| 138 | } |
| 139 | } |
| 140 | |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 141 | static ::testing::AssertionResult IsResultFailure(const CmdlineResult& result, |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 142 | CmdlineResult::Status failure_status) { |
| 143 | if (result.IsSuccess()) { |
| 144 | return ::testing::AssertionFailure() << " got success but expected failure: " |
| 145 | << failure_status; |
| 146 | } else if (result.GetStatus() == failure_status) { |
| 147 | return ::testing::AssertionSuccess(); |
| 148 | } |
| 149 | |
| 150 | return ::testing::AssertionFailure() << " expected failure " << failure_status |
| 151 | << " but got " << result.GetStatus(); |
| 152 | } |
| 153 | |
| 154 | std::unique_ptr<RuntimeParser> parser_; |
| 155 | }; |
| 156 | |
| 157 | #define EXPECT_KEY_EXISTS(map, key) EXPECT_TRUE((map).Exists(key)) |
| 158 | #define EXPECT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedKeyValue(expected, map, key)) |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 159 | #define EXPECT_DEFAULT_KEY_VALUE(map, key, expected) EXPECT_TRUE(IsExpectedDefaultKeyValue(expected, map, key)) |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 160 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 161 | #define _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \ |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 162 | do { \ |
| 163 | EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \ |
| 164 | EXPECT_EQ(0u, parser_->GetArgumentsMap().Size()); \ |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 165 | |
| 166 | #define EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \ |
| 167 | _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \ |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 168 | } while (false) |
| 169 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 170 | #define EXPECT_SINGLE_PARSE_DEFAULT_VALUE(expected, argv, key)\ |
| 171 | _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \ |
| 172 | RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \ |
| 173 | EXPECT_DEFAULT_KEY_VALUE(args, key, expected); \ |
| 174 | } while (false) // NOLINT [readability/namespace] [5] |
| 175 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 176 | #define _EXPECT_SINGLE_PARSE_EXISTS(argv, key) \ |
| 177 | do { \ |
| 178 | EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \ |
| 179 | RuntimeArgumentMap args = parser_->ReleaseArgumentsMap(); \ |
| 180 | EXPECT_EQ(1u, args.Size()); \ |
| 181 | EXPECT_KEY_EXISTS(args, key); \ |
| 182 | |
| 183 | #define EXPECT_SINGLE_PARSE_EXISTS(argv, key) \ |
| 184 | _EXPECT_SINGLE_PARSE_EXISTS(argv, key); \ |
| 185 | } while (false) |
| 186 | |
| 187 | #define EXPECT_SINGLE_PARSE_VALUE(expected, argv, key) \ |
| 188 | _EXPECT_SINGLE_PARSE_EXISTS(argv, key); \ |
| 189 | EXPECT_KEY_VALUE(args, key, expected); \ |
| 190 | } while (false) // NOLINT [readability/namespace] [5] |
| 191 | |
| 192 | #define EXPECT_SINGLE_PARSE_VALUE_STR(expected, argv, key) \ |
| 193 | EXPECT_SINGLE_PARSE_VALUE(std::string(expected), argv, key) |
| 194 | |
| 195 | #define EXPECT_SINGLE_PARSE_FAIL(argv, failure_status) \ |
| 196 | do { \ |
| 197 | EXPECT_TRUE(IsResultFailure(parser_->Parse(argv), failure_status));\ |
| 198 | RuntimeArgumentMap args = parser_->ReleaseArgumentsMap();\ |
| 199 | EXPECT_EQ(0u, args.Size()); \ |
| 200 | } while (false) |
| 201 | |
| 202 | TEST_F(CmdlineParserTest, TestSimpleSuccesses) { |
| 203 | auto& parser = *parser_; |
| 204 | |
| 205 | EXPECT_LT(0u, parser.CountDefinedArguments()); |
| 206 | |
| 207 | { |
| 208 | // Test case 1: No command line arguments |
| 209 | EXPECT_TRUE(IsResultSuccessful(parser.Parse(""))); |
| 210 | RuntimeArgumentMap args = parser.ReleaseArgumentsMap(); |
| 211 | EXPECT_EQ(0u, args.Size()); |
| 212 | } |
| 213 | |
| 214 | EXPECT_SINGLE_PARSE_EXISTS("-Xzygote", M::Zygote); |
| 215 | EXPECT_SINGLE_PARSE_VALUE_STR("/hello/world", "-Xbootclasspath:/hello/world", M::BootClassPath); |
| 216 | EXPECT_SINGLE_PARSE_VALUE("/hello/world", "-Xbootclasspath:/hello/world", M::BootClassPath); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 217 | EXPECT_SINGLE_PARSE_VALUE(Memory<1>(234), "-Xss234", M::StackSize); |
| 218 | EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(1234*MB), "-Xms1234m", M::MemoryInitialSize); |
| 219 | EXPECT_SINGLE_PARSE_VALUE(true, "-XX:EnableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM); |
| 220 | EXPECT_SINGLE_PARSE_VALUE(false, "-XX:DisableHSpaceCompactForOOM", M::EnableHSpaceCompactForOOM); |
| 221 | EXPECT_SINGLE_PARSE_VALUE(0.5, "-XX:HeapTargetUtilization=0.5", M::HeapTargetUtilization); |
| 222 | EXPECT_SINGLE_PARSE_VALUE(5u, "-XX:ParallelGCThreads=5", M::ParallelGCThreads); |
Jean Christophe Beyler | 24e04aa | 2014-09-12 12:03:25 -0700 | [diff] [blame] | 223 | EXPECT_SINGLE_PARSE_EXISTS("-Xno-dex-file-fallback", M::NoDexFileFallback); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 224 | } // TEST_F |
| 225 | |
| 226 | TEST_F(CmdlineParserTest, TestSimpleFailures) { |
| 227 | // Test argument is unknown to the parser |
| 228 | EXPECT_SINGLE_PARSE_FAIL("abcdefg^%@#*(@#", CmdlineResult::kUnknown); |
| 229 | // Test value map substitution fails |
| 230 | EXPECT_SINGLE_PARSE_FAIL("-Xverify:whatever", CmdlineResult::kFailure); |
| 231 | // Test value type parsing failures |
| 232 | EXPECT_SINGLE_PARSE_FAIL("-Xsswhatever", CmdlineResult::kFailure); // invalid memory value |
| 233 | EXPECT_SINGLE_PARSE_FAIL("-Xms123", CmdlineResult::kFailure); // memory value too small |
| 234 | EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=0.0", CmdlineResult::kOutOfRange); // toosmal |
| 235 | EXPECT_SINGLE_PARSE_FAIL("-XX:HeapTargetUtilization=2.0", CmdlineResult::kOutOfRange); // toolarg |
| 236 | EXPECT_SINGLE_PARSE_FAIL("-XX:ParallelGCThreads=-5", CmdlineResult::kOutOfRange); // too small |
| 237 | EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // not a valid suboption |
| 238 | } // TEST_F |
| 239 | |
| 240 | TEST_F(CmdlineParserTest, TestLogVerbosity) { |
| 241 | { |
| 242 | const char* log_args = "-verbose:" |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 243 | "class,compiler,gc,heap,jdwp,jni,monitor,profiler,signals,simulator,startup," |
| 244 | "third-party-jni,threads,verifier"; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 245 | |
| 246 | LogVerbosity log_verbosity = LogVerbosity(); |
| 247 | log_verbosity.class_linker = true; |
| 248 | log_verbosity.compiler = true; |
| 249 | log_verbosity.gc = true; |
| 250 | log_verbosity.heap = true; |
| 251 | log_verbosity.jdwp = true; |
| 252 | log_verbosity.jni = true; |
| 253 | log_verbosity.monitor = true; |
| 254 | log_verbosity.profiler = true; |
| 255 | log_verbosity.signals = true; |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame] | 256 | log_verbosity.simulator = true; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 257 | log_verbosity.startup = true; |
| 258 | log_verbosity.third_party_jni = true; |
| 259 | log_verbosity.threads = true; |
| 260 | log_verbosity.verifier = true; |
| 261 | |
| 262 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 263 | } |
| 264 | |
| 265 | { |
| 266 | const char* log_args = "-verbose:" |
| 267 | "class,compiler,gc,heap,jdwp,jni,monitor"; |
| 268 | |
| 269 | LogVerbosity log_verbosity = LogVerbosity(); |
| 270 | log_verbosity.class_linker = true; |
| 271 | log_verbosity.compiler = true; |
| 272 | log_verbosity.gc = true; |
| 273 | log_verbosity.heap = true; |
| 274 | log_verbosity.jdwp = true; |
| 275 | log_verbosity.jni = true; |
| 276 | log_verbosity.monitor = true; |
| 277 | |
| 278 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 279 | } |
| 280 | |
| 281 | EXPECT_SINGLE_PARSE_FAIL("-verbose:blablabla", CmdlineResult::kUsage); // invalid verbose opt |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 282 | |
| 283 | { |
Sebastien Hertz | bba348e | 2015-06-01 08:28:18 +0200 | [diff] [blame] | 284 | const char* log_args = "-verbose:deopt"; |
| 285 | LogVerbosity log_verbosity = LogVerbosity(); |
| 286 | log_verbosity.deopt = true; |
| 287 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 288 | } |
| 289 | |
| 290 | { |
Mathieu Chartier | 66a5539 | 2016-02-19 10:25:39 -0800 | [diff] [blame] | 291 | const char* log_args = "-verbose:collector"; |
| 292 | LogVerbosity log_verbosity = LogVerbosity(); |
| 293 | log_verbosity.collector = true; |
| 294 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 295 | } |
| 296 | |
| 297 | { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 298 | const char* log_args = "-verbose:oat"; |
| 299 | LogVerbosity log_verbosity = LogVerbosity(); |
| 300 | log_verbosity.oat = true; |
| 301 | EXPECT_SINGLE_PARSE_VALUE(log_verbosity, log_args, M::Verbose); |
| 302 | } |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 303 | } // TEST_F |
| 304 | |
Nicolas Geoffray | 8f4ee5c | 2015-02-05 10:14:10 +0000 | [diff] [blame] | 305 | // TODO: Enable this b/19274810 |
| 306 | TEST_F(CmdlineParserTest, DISABLED_TestXGcOption) { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 307 | /* |
| 308 | * Test success |
| 309 | */ |
| 310 | { |
| 311 | XGcOption option_all_true{}; // NOLINT [readability/braces] [4] |
| 312 | option_all_true.collector_type_ = gc::CollectorType::kCollectorTypeCMS; |
| 313 | option_all_true.verify_pre_gc_heap_ = true; |
| 314 | option_all_true.verify_pre_sweeping_heap_ = true; |
| 315 | option_all_true.verify_post_gc_heap_ = true; |
| 316 | option_all_true.verify_pre_gc_rosalloc_ = true; |
| 317 | option_all_true.verify_pre_sweeping_rosalloc_ = true; |
| 318 | option_all_true.verify_post_gc_rosalloc_ = true; |
| 319 | |
| 320 | const char * xgc_args_all_true = "-Xgc:concurrent," |
| 321 | "preverify,presweepingverify,postverify," |
| 322 | "preverify_rosalloc,presweepingverify_rosalloc," |
| 323 | "postverify_rosalloc,precise," |
| 324 | "verifycardtable"; |
| 325 | |
| 326 | EXPECT_SINGLE_PARSE_VALUE(option_all_true, xgc_args_all_true, M::GcOption); |
| 327 | |
| 328 | XGcOption option_all_false{}; // NOLINT [readability/braces] [4] |
| 329 | option_all_false.collector_type_ = gc::CollectorType::kCollectorTypeMS; |
| 330 | option_all_false.verify_pre_gc_heap_ = false; |
| 331 | option_all_false.verify_pre_sweeping_heap_ = false; |
| 332 | option_all_false.verify_post_gc_heap_ = false; |
| 333 | option_all_false.verify_pre_gc_rosalloc_ = false; |
| 334 | option_all_false.verify_pre_sweeping_rosalloc_ = false; |
| 335 | option_all_false.verify_post_gc_rosalloc_ = false; |
| 336 | |
| 337 | const char* xgc_args_all_false = "-Xgc:nonconcurrent," |
| 338 | "nopreverify,nopresweepingverify,nopostverify,nopreverify_rosalloc," |
| 339 | "nopresweepingverify_rosalloc,nopostverify_rosalloc,noprecise,noverifycardtable"; |
| 340 | |
| 341 | EXPECT_SINGLE_PARSE_VALUE(option_all_false, xgc_args_all_false, M::GcOption); |
| 342 | |
| 343 | XGcOption option_all_default{}; // NOLINT [readability/braces] [4] |
| 344 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 345 | const char* xgc_args_blank = "-Xgc:"; |
| 346 | EXPECT_SINGLE_PARSE_VALUE(option_all_default, xgc_args_blank, M::GcOption); |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Test failures |
| 351 | */ |
| 352 | EXPECT_SINGLE_PARSE_FAIL("-Xgc:blablabla", CmdlineResult::kUsage); // invalid Xgc opt |
| 353 | } // TEST_F |
| 354 | |
| 355 | /* |
| 356 | * {"-Xrunjdwp:_", "-agentlib:jdwp=_"} |
| 357 | */ |
| 358 | TEST_F(CmdlineParserTest, TestJdwpOptions) { |
| 359 | /* |
| 360 | * Test success |
| 361 | */ |
| 362 | { |
| 363 | /* |
| 364 | * "Example: -Xrunjdwp:transport=dt_socket,address=8000,server=y\n" |
| 365 | */ |
| 366 | JDWP::JdwpOptions opt = JDWP::JdwpOptions(); |
| 367 | opt.transport = JDWP::JdwpTransportType::kJdwpTransportSocket; |
| 368 | opt.port = 8000; |
| 369 | opt.server = true; |
| 370 | |
| 371 | const char *opt_args = "-Xrunjdwp:transport=dt_socket,address=8000,server=y"; |
| 372 | |
| 373 | EXPECT_SINGLE_PARSE_VALUE(opt, opt_args, M::JdwpOptions); |
| 374 | } |
| 375 | |
| 376 | { |
| 377 | /* |
| 378 | * "Example: -agentlib:jdwp=transport=dt_socket,address=localhost:6500,server=n\n"); |
| 379 | */ |
| 380 | JDWP::JdwpOptions opt = JDWP::JdwpOptions(); |
| 381 | opt.transport = JDWP::JdwpTransportType::kJdwpTransportSocket; |
| 382 | opt.host = "localhost"; |
| 383 | opt.port = 6500; |
| 384 | opt.server = false; |
| 385 | |
| 386 | const char *opt_args = "-agentlib:jdwp=transport=dt_socket,address=localhost:6500,server=n"; |
| 387 | |
| 388 | EXPECT_SINGLE_PARSE_VALUE(opt, opt_args, M::JdwpOptions); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Test failures |
| 393 | */ |
| 394 | EXPECT_SINGLE_PARSE_FAIL("-Xrunjdwp:help", CmdlineResult::kUsage); // usage for help only |
| 395 | EXPECT_SINGLE_PARSE_FAIL("-Xrunjdwp:blabla", CmdlineResult::kFailure); // invalid subarg |
| 396 | EXPECT_SINGLE_PARSE_FAIL("-agentlib:jdwp=help", CmdlineResult::kUsage); // usage for help only |
| 397 | EXPECT_SINGLE_PARSE_FAIL("-agentlib:jdwp=blabla", CmdlineResult::kFailure); // invalid subarg |
| 398 | } // TEST_F |
| 399 | |
| 400 | /* |
| 401 | * -D_ -D_ -D_ ... |
| 402 | */ |
| 403 | TEST_F(CmdlineParserTest, TestPropertiesList) { |
| 404 | /* |
| 405 | * Test successes |
| 406 | */ |
| 407 | { |
| 408 | std::vector<std::string> opt = {"hello"}; |
| 409 | |
| 410 | EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello", M::PropertiesList); |
| 411 | } |
| 412 | |
| 413 | { |
| 414 | std::vector<std::string> opt = {"hello", "world"}; |
| 415 | |
| 416 | EXPECT_SINGLE_PARSE_VALUE(opt, "-Dhello -Dworld", M::PropertiesList); |
| 417 | } |
| 418 | |
| 419 | { |
| 420 | std::vector<std::string> opt = {"one", "two", "three"}; |
| 421 | |
| 422 | EXPECT_SINGLE_PARSE_VALUE(opt, "-Done -Dtwo -Dthree", M::PropertiesList); |
| 423 | } |
| 424 | } // TEST_F |
| 425 | |
| 426 | /* |
| 427 | * -Xcompiler-option foo -Xcompiler-option bar ... |
| 428 | */ |
| 429 | TEST_F(CmdlineParserTest, TestCompilerOption) { |
| 430 | /* |
| 431 | * Test successes |
| 432 | */ |
| 433 | { |
| 434 | std::vector<std::string> opt = {"hello"}; |
| 435 | EXPECT_SINGLE_PARSE_VALUE(opt, "-Xcompiler-option hello", M::CompilerOptions); |
| 436 | } |
| 437 | |
| 438 | { |
| 439 | std::vector<std::string> opt = {"hello", "world"}; |
| 440 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 441 | "-Xcompiler-option hello -Xcompiler-option world", |
| 442 | M::CompilerOptions); |
| 443 | } |
| 444 | |
| 445 | { |
| 446 | std::vector<std::string> opt = {"one", "two", "three"}; |
| 447 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 448 | "-Xcompiler-option one -Xcompiler-option two -Xcompiler-option three", |
| 449 | M::CompilerOptions); |
| 450 | } |
| 451 | } // TEST_F |
| 452 | |
| 453 | /* |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 454 | * -Xjit, -Xnojit, -Xjitcodecachesize, Xjitcompilethreshold |
| 455 | */ |
| 456 | TEST_F(CmdlineParserTest, TestJitOptions) { |
| 457 | /* |
| 458 | * Test successes |
| 459 | */ |
| 460 | { |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 461 | EXPECT_SINGLE_PARSE_VALUE(true, "-Xusejit:true", M::UseJitCompilation); |
| 462 | EXPECT_SINGLE_PARSE_VALUE(false, "-Xusejit:false", M::UseJitCompilation); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 463 | } |
| 464 | { |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 465 | EXPECT_SINGLE_PARSE_VALUE( |
Nicolas Geoffray | 295a596 | 2015-11-19 18:17:41 +0000 | [diff] [blame] | 466 | MemoryKiB(16 * KB), "-Xjitinitialsize:16K", M::JITCodeCacheInitialCapacity); |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 467 | EXPECT_SINGLE_PARSE_VALUE( |
Nicolas Geoffray | 295a596 | 2015-11-19 18:17:41 +0000 | [diff] [blame] | 468 | MemoryKiB(16 * MB), "-Xjitmaxsize:16M", M::JITCodeCacheMaxCapacity); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 469 | } |
| 470 | { |
| 471 | EXPECT_SINGLE_PARSE_VALUE(12345u, "-Xjitthreshold:12345", M::JITCompileThreshold); |
| 472 | } |
| 473 | } // TEST_F |
| 474 | |
| 475 | /* |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 476 | * -Xps-* |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 477 | */ |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 478 | TEST_F(CmdlineParserTest, ProfileSaverOptions) { |
| 479 | ProfileSaverOptions opt = ProfileSaverOptions(true, 1, 2, 3, 4, 5, 6, 7); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 480 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 481 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 482 | "-Xjitsaveprofilinginfo " |
| 483 | "-Xps-min-save-period-ms:1 " |
| 484 | "-Xps-save-resolved-classes-delay-ms:2 " |
| 485 | "-Xps-startup-method-samples:3 " |
| 486 | "-Xps-min-methods-to-save:4 " |
| 487 | "-Xps-min-classes-to-save:5 " |
| 488 | "-Xps-min-notification-before-wake:6 " |
| 489 | "-Xps-max-notification-before-wake:7", |
| 490 | M::ProfileSaverOpts); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 491 | } // TEST_F |
| 492 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 493 | /* -Xexperimental:_ */ |
| 494 | TEST_F(CmdlineParserTest, TestExperimentalFlags) { |
Neil Fuller | 9724c63 | 2016-01-07 15:42:47 +0000 | [diff] [blame] | 495 | // Default |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 496 | EXPECT_SINGLE_PARSE_DEFAULT_VALUE(ExperimentalFlags::kNone, |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 497 | "", |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 498 | M::Experimental); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 499 | |
| 500 | // Disabled explicitly |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 501 | EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kNone, |
| 502 | "-Xexperimental:none", |
| 503 | M::Experimental); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Igor Murashkin | 7617abd | 2015-07-10 18:27:47 -0700 | [diff] [blame] | 506 | // -Xverify:_ |
| 507 | TEST_F(CmdlineParserTest, TestVerify) { |
| 508 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kNone, "-Xverify:none", M::Verify); |
| 509 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:remote", M::Verify); |
| 510 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:all", M::Verify); |
| 511 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kSoftFail, "-Xverify:softfail", M::Verify); |
| 512 | } |
| 513 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 514 | TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) { |
| 515 | RuntimeParser::Builder parserBuilder; |
| 516 | |
| 517 | parserBuilder |
| 518 | .Define("-help") |
| 519 | .IntoKey(M::Help) |
| 520 | .IgnoreUnrecognized(true); |
| 521 | |
| 522 | parser_.reset(new RuntimeParser(parserBuilder.Build())); |
| 523 | |
| 524 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option"); |
| 525 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option1 --non-existent-option-2"); |
| 526 | } // TEST_F |
| 527 | |
| 528 | TEST_F(CmdlineParserTest, TestIgnoredArguments) { |
| 529 | std::initializer_list<const char*> ignored_args = { |
| 530 | "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa", |
| 531 | "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:abdef", |
| 532 | "-Xdexopt:foobar", "-Xnoquithandler", "-Xjnigreflimit:ixnay", "-Xgenregmap", "-Xnogenregmap", |
| 533 | "-Xverifyopt:never", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:noop", |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 534 | "-Xincludeselectedmethod", "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:nosuchluck", |
| 535 | "-Xjitoffset:none", "-Xjitconfig:yes", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile", |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 536 | "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=1337" |
| 537 | }; |
| 538 | |
| 539 | // Check they are ignored when parsed one at a time |
| 540 | for (auto&& arg : ignored_args) { |
| 541 | SCOPED_TRACE(arg); |
| 542 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(arg); |
| 543 | } |
| 544 | |
| 545 | // Check they are ignored when we pass it all together at once |
| 546 | std::vector<const char*> argv = ignored_args; |
| 547 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); |
| 548 | } // TEST_F |
| 549 | |
| 550 | TEST_F(CmdlineParserTest, MultipleArguments) { |
| 551 | EXPECT_TRUE(IsResultSuccessful(parser_->Parse( |
| 552 | "-help -XX:ForegroundHeapGrowthMultiplier=0.5 " |
| 553 | "-Xnodex2oat -Xmethod-trace -XX:LargeObjectSpace=map"))); |
| 554 | |
| 555 | auto&& map = parser_->ReleaseArgumentsMap(); |
| 556 | EXPECT_EQ(5u, map.Size()); |
| 557 | EXPECT_KEY_VALUE(map, M::Help, Unit{}); // NOLINT [whitespace/braces] [5] |
| 558 | EXPECT_KEY_VALUE(map, M::ForegroundHeapGrowthMultiplier, 0.5); |
| 559 | EXPECT_KEY_VALUE(map, M::Dex2Oat, false); |
| 560 | EXPECT_KEY_VALUE(map, M::MethodTrace, Unit{}); // NOLINT [whitespace/braces] [5] |
| 561 | EXPECT_KEY_VALUE(map, M::LargeObjectSpace, gc::space::LargeObjectSpaceType::kMap); |
| 562 | } // TEST_F |
| 563 | } // namespace art |