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. |
| 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 Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 105 | 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 Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 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 | |
| 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 Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 162 | #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] | 163 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 164 | #define _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \ |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 165 | do { \ |
| 166 | EXPECT_TRUE(IsResultSuccessful(parser_->Parse(argv))); \ |
| 167 | EXPECT_EQ(0u, parser_->GetArgumentsMap().Size()); \ |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 168 | |
| 169 | #define EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv) \ |
| 170 | _EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); \ |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 171 | } while (false) |
| 172 | |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 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 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 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 | |
| 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); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 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); |
Jean Christophe Beyler | 24e04aa | 2014-09-12 12:03:25 -0700 | [diff] [blame] | 226 | EXPECT_SINGLE_PARSE_EXISTS("-Xno-dex-file-fallback", M::NoDexFileFallback); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 227 | } // TEST_F |
| 228 | |
| 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 | |
| 243 | TEST_F(CmdlineParserTest, TestLogVerbosity) { |
| 244 | { |
| 245 | const char* log_args = "-verbose:" |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame^] | 246 | "class,compiler,gc,heap,jdwp,jni,monitor,profiler,signals,simulator,startup," |
| 247 | "third-party-jni,threads,verifier"; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 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; |
Phil Wang | 751beff | 2015-08-28 15:17:15 +0800 | [diff] [blame^] | 259 | log_verbosity.simulator = true; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 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 |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 285 | |
| 286 | { |
Sebastien Hertz | bba348e | 2015-06-01 08:28:18 +0200 | [diff] [blame] | 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 | { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 294 | 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 Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 299 | } // TEST_F |
| 300 | |
Nicolas Geoffray | 8f4ee5c | 2015-02-05 10:14:10 +0000 | [diff] [blame] | 301 | // TODO: Enable this b/19274810 |
| 302 | TEST_F(CmdlineParserTest, DISABLED_TestXGcOption) { |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 303 | /* |
| 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 Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 341 | 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 | */ |
| 354 | TEST_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 | */ |
| 399 | TEST_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 | */ |
| 425 | TEST_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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 450 | * -Xjit, -Xnojit, -Xjitcodecachesize, Xjitcompilethreshold |
| 451 | */ |
| 452 | TEST_F(CmdlineParserTest, TestJitOptions) { |
| 453 | /* |
| 454 | * Test successes |
| 455 | */ |
| 456 | { |
Andreas Gampe | 2682699 | 2015-03-05 18:48:52 -0800 | [diff] [blame] | 457 | EXPECT_SINGLE_PARSE_VALUE(true, "-Xusejit:true", M::UseJIT); |
| 458 | EXPECT_SINGLE_PARSE_VALUE(false, "-Xusejit:false", M::UseJIT); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 459 | } |
| 460 | { |
| 461 | EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(16 * KB), "-Xjitcodecachesize:16K", M::JITCodeCacheCapacity); |
| 462 | EXPECT_SINGLE_PARSE_VALUE(MemoryKiB(16 * MB), "-Xjitcodecachesize:16M", M::JITCodeCacheCapacity); |
| 463 | } |
| 464 | { |
| 465 | EXPECT_SINGLE_PARSE_VALUE(12345u, "-Xjitthreshold:12345", M::JITCompileThreshold); |
| 466 | } |
| 467 | } // TEST_F |
| 468 | |
| 469 | /* |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 470 | * -X-profile-* |
| 471 | */ |
| 472 | TEST_F(CmdlineParserTest, TestProfilerOptions) { |
| 473 | /* |
| 474 | * Test successes |
| 475 | */ |
| 476 | |
| 477 | { |
| 478 | TestProfilerOptions opt; |
| 479 | opt.enabled_ = true; |
| 480 | |
| 481 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 482 | "-Xenable-profiler", |
| 483 | M::ProfilerOpts); |
| 484 | } |
| 485 | |
| 486 | { |
| 487 | TestProfilerOptions opt; |
| 488 | // also need to test 'enabled' |
| 489 | opt.output_file_name_ = "hello_world.txt"; |
| 490 | |
| 491 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 492 | "-Xprofile-filename:hello_world.txt ", |
| 493 | M::ProfilerOpts); |
| 494 | } |
| 495 | |
| 496 | { |
| 497 | TestProfilerOptions opt = TestProfilerOptions(); |
| 498 | // also need to test 'enabled' |
| 499 | opt.output_file_name_ = "output.txt"; |
| 500 | opt.period_s_ = 123u; |
| 501 | opt.duration_s_ = 456u; |
| 502 | opt.interval_us_ = 789u; |
| 503 | opt.backoff_coefficient_ = 2.0; |
| 504 | opt.start_immediately_ = true; |
| 505 | opt.top_k_threshold_ = 50.0; |
| 506 | opt.top_k_change_threshold_ = 60.0; |
| 507 | opt.profile_type_ = kProfilerMethod; |
| 508 | opt.max_stack_depth_ = 1337u; |
| 509 | |
| 510 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 511 | "-Xprofile-filename:output.txt " |
| 512 | "-Xprofile-period:123 " |
| 513 | "-Xprofile-duration:456 " |
| 514 | "-Xprofile-interval:789 " |
| 515 | "-Xprofile-backoff:2.0 " |
| 516 | "-Xprofile-start-immediately " |
| 517 | "-Xprofile-top-k-threshold:50.0 " |
| 518 | "-Xprofile-top-k-change-threshold:60.0 " |
| 519 | "-Xprofile-type:method " |
| 520 | "-Xprofile-max-stack-depth:1337", |
| 521 | M::ProfilerOpts); |
| 522 | } |
| 523 | |
| 524 | { |
| 525 | TestProfilerOptions opt = TestProfilerOptions(); |
| 526 | opt.profile_type_ = kProfilerBoundedStack; |
| 527 | |
| 528 | EXPECT_SINGLE_PARSE_VALUE(opt, |
| 529 | "-Xprofile-type:stack", |
| 530 | M::ProfilerOpts); |
| 531 | } |
| 532 | } // TEST_F |
| 533 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 534 | /* -Xexperimental:_ */ |
| 535 | TEST_F(CmdlineParserTest, TestExperimentalFlags) { |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 536 | // Off by default |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 537 | EXPECT_SINGLE_PARSE_DEFAULT_VALUE(ExperimentalFlags::kNone, |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 538 | "", |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 539 | M::Experimental); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 540 | |
| 541 | // Disabled explicitly |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 542 | EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kNone, |
| 543 | "-Xexperimental:none", |
| 544 | M::Experimental); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 545 | |
| 546 | // Enabled explicitly |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 547 | EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kLambdas, |
| 548 | "-Xexperimental:lambdas", |
| 549 | M::Experimental); |
| 550 | // Enabled explicitly |
| 551 | EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kDefaultMethods, |
| 552 | "-Xexperimental:default-methods", |
| 553 | M::Experimental); |
| 554 | |
| 555 | // Enabled both |
| 556 | EXPECT_SINGLE_PARSE_VALUE(ExperimentalFlags::kDefaultMethods | ExperimentalFlags::kLambdas, |
| 557 | "-Xexperimental:default-methods " |
| 558 | "-Xexperimental:lambdas", |
| 559 | M::Experimental); |
Igor Murashkin | 158f35c | 2015-06-10 15:55:30 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Igor Murashkin | 7617abd | 2015-07-10 18:27:47 -0700 | [diff] [blame] | 562 | // -Xverify:_ |
| 563 | TEST_F(CmdlineParserTest, TestVerify) { |
| 564 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kNone, "-Xverify:none", M::Verify); |
| 565 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:remote", M::Verify); |
| 566 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kEnable, "-Xverify:all", M::Verify); |
| 567 | EXPECT_SINGLE_PARSE_VALUE(verifier::VerifyMode::kSoftFail, "-Xverify:softfail", M::Verify); |
| 568 | } |
| 569 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 570 | TEST_F(CmdlineParserTest, TestIgnoreUnrecognized) { |
| 571 | RuntimeParser::Builder parserBuilder; |
| 572 | |
| 573 | parserBuilder |
| 574 | .Define("-help") |
| 575 | .IntoKey(M::Help) |
| 576 | .IgnoreUnrecognized(true); |
| 577 | |
| 578 | parser_.reset(new RuntimeParser(parserBuilder.Build())); |
| 579 | |
| 580 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option"); |
| 581 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS("-non-existent-option1 --non-existent-option-2"); |
| 582 | } // TEST_F |
| 583 | |
| 584 | TEST_F(CmdlineParserTest, TestIgnoredArguments) { |
| 585 | std::initializer_list<const char*> ignored_args = { |
| 586 | "-ea", "-da", "-enableassertions", "-disableassertions", "--runtime-arg", "-esa", |
| 587 | "-dsa", "-enablesystemassertions", "-disablesystemassertions", "-Xrs", "-Xint:abdef", |
| 588 | "-Xdexopt:foobar", "-Xnoquithandler", "-Xjnigreflimit:ixnay", "-Xgenregmap", "-Xnogenregmap", |
| 589 | "-Xverifyopt:never", "-Xcheckdexsum", "-Xincludeselectedop", "-Xjitop:noop", |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 590 | "-Xincludeselectedmethod", "-Xjitblocking", "-Xjitmethod:_", "-Xjitclass:nosuchluck", |
| 591 | "-Xjitoffset:none", "-Xjitconfig:yes", "-Xjitcheckcg", "-Xjitverbose", "-Xjitprofile", |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 592 | "-Xjitdisableopt", "-Xjitsuspendpoll", "-XX:mainThreadStackSize=1337" |
| 593 | }; |
| 594 | |
| 595 | // Check they are ignored when parsed one at a time |
| 596 | for (auto&& arg : ignored_args) { |
| 597 | SCOPED_TRACE(arg); |
| 598 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(arg); |
| 599 | } |
| 600 | |
| 601 | // Check they are ignored when we pass it all together at once |
| 602 | std::vector<const char*> argv = ignored_args; |
| 603 | EXPECT_SINGLE_PARSE_EMPTY_SUCCESS(argv); |
| 604 | } // TEST_F |
| 605 | |
| 606 | TEST_F(CmdlineParserTest, MultipleArguments) { |
| 607 | EXPECT_TRUE(IsResultSuccessful(parser_->Parse( |
| 608 | "-help -XX:ForegroundHeapGrowthMultiplier=0.5 " |
| 609 | "-Xnodex2oat -Xmethod-trace -XX:LargeObjectSpace=map"))); |
| 610 | |
| 611 | auto&& map = parser_->ReleaseArgumentsMap(); |
| 612 | EXPECT_EQ(5u, map.Size()); |
| 613 | EXPECT_KEY_VALUE(map, M::Help, Unit{}); // NOLINT [whitespace/braces] [5] |
| 614 | EXPECT_KEY_VALUE(map, M::ForegroundHeapGrowthMultiplier, 0.5); |
| 615 | EXPECT_KEY_VALUE(map, M::Dex2Oat, false); |
| 616 | EXPECT_KEY_VALUE(map, M::MethodTrace, Unit{}); // NOLINT [whitespace/braces] [5] |
| 617 | EXPECT_KEY_VALUE(map, M::LargeObjectSpace, gc::space::LargeObjectSpaceType::kMap); |
| 618 | } // TEST_F |
| 619 | } // namespace art |