1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "parsed_options.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <memory>
20*795d594fSAndroid Build Coastguard Worker
21*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/common_art_test.h"
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
25*795d594fSAndroid Build Coastguard Worker
26*795d594fSAndroid Build Coastguard Worker class ParsedOptionsTest : public CommonArtTest {
27*795d594fSAndroid Build Coastguard Worker public:
SetUpTestCase()28*795d594fSAndroid Build Coastguard Worker static void SetUpTestCase() {
29*795d594fSAndroid Build Coastguard Worker CommonArtTest::SetUpAndroidRootEnvVars();
30*795d594fSAndroid Build Coastguard Worker }
31*795d594fSAndroid Build Coastguard Worker };
32*795d594fSAndroid Build Coastguard Worker
test_vfprintf(FILE *,const char *,va_list)33*795d594fSAndroid Build Coastguard Worker static int test_vfprintf(FILE*, const char*, va_list) { return 0; }
test_abort()34*795d594fSAndroid Build Coastguard Worker static void test_abort() {}
test_exit(jint)35*795d594fSAndroid Build Coastguard Worker static void test_exit(jint) {}
36*795d594fSAndroid Build Coastguard Worker
TEST_F(ParsedOptionsTest,ParsedOptions)37*795d594fSAndroid Build Coastguard Worker TEST_F(ParsedOptionsTest, ParsedOptions) {
38*795d594fSAndroid Build Coastguard Worker std::string boot_class_path;
39*795d594fSAndroid Build Coastguard Worker std::string class_path;
40*795d594fSAndroid Build Coastguard Worker boot_class_path += "-Xbootclasspath:";
41*795d594fSAndroid Build Coastguard Worker
42*795d594fSAndroid Build Coastguard Worker bool first_dex_file = true;
43*795d594fSAndroid Build Coastguard Worker for (const std::string& dex_file_name : GetLibCoreDexFileNames()) {
44*795d594fSAndroid Build Coastguard Worker if (!first_dex_file) {
45*795d594fSAndroid Build Coastguard Worker class_path += ":";
46*795d594fSAndroid Build Coastguard Worker } else {
47*795d594fSAndroid Build Coastguard Worker first_dex_file = false;
48*795d594fSAndroid Build Coastguard Worker }
49*795d594fSAndroid Build Coastguard Worker class_path += dex_file_name;
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker boot_class_path += class_path;
52*795d594fSAndroid Build Coastguard Worker std::vector<std::string> expected_boot_class_path;
53*795d594fSAndroid Build Coastguard Worker Split(class_path, ':', &expected_boot_class_path);
54*795d594fSAndroid Build Coastguard Worker
55*795d594fSAndroid Build Coastguard Worker RuntimeOptions options;
56*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair(boot_class_path.c_str(), nullptr));
57*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-classpath", nullptr));
58*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair(class_path.c_str(), nullptr));
59*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-cp", nullptr));
60*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair(class_path.c_str(), nullptr));
61*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-Ximage:boot_image", nullptr));
62*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-Xcheck:jni", nullptr));
63*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-Xms2048", nullptr));
64*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-Xmx4k", nullptr));
65*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-Xss1m", nullptr));
66*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-XX:HeapTargetUtilization=0.75", nullptr));
67*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-XX:StopForNativeAllocs=200m", nullptr));
68*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-Dfoo=bar", nullptr));
69*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-Dbaz=qux", nullptr));
70*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-verbose:gc,class,jni", nullptr));
71*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("vfprintf", reinterpret_cast<void*>(test_vfprintf)));
72*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("abort", reinterpret_cast<void*>(test_abort)));
73*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("exit", reinterpret_cast<void*>(test_exit)));
74*795d594fSAndroid Build Coastguard Worker
75*795d594fSAndroid Build Coastguard Worker RuntimeArgumentMap map;
76*795d594fSAndroid Build Coastguard Worker bool parsed = ParsedOptions::Parse(options, false, &map);
77*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(parsed);
78*795d594fSAndroid Build Coastguard Worker ASSERT_NE(0u, map.Size());
79*795d594fSAndroid Build Coastguard Worker
80*795d594fSAndroid Build Coastguard Worker using Opt = RuntimeArgumentMap;
81*795d594fSAndroid Build Coastguard Worker
82*795d594fSAndroid Build Coastguard Worker #define EXPECT_PARSED_EQ(expected, actual_key) EXPECT_EQ(expected, map.GetOrDefault(actual_key))
83*795d594fSAndroid Build Coastguard Worker #define EXPECT_PARSED_EQ_AS_STRING_VECTOR(expected, actual_key) \
84*795d594fSAndroid Build Coastguard Worker EXPECT_EQ(expected, static_cast<std::vector<std::string>>(map.GetOrDefault(actual_key)))
85*795d594fSAndroid Build Coastguard Worker #define EXPECT_PARSED_EXISTS(actual_key) EXPECT_TRUE(map.Exists(actual_key))
86*795d594fSAndroid Build Coastguard Worker
87*795d594fSAndroid Build Coastguard Worker EXPECT_PARSED_EQ_AS_STRING_VECTOR(expected_boot_class_path, Opt::BootClassPath);
88*795d594fSAndroid Build Coastguard Worker EXPECT_PARSED_EQ(class_path, Opt::ClassPath);
89*795d594fSAndroid Build Coastguard Worker std::vector<std::string> boot_images = map.GetOrDefault(Opt::Image);
90*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(1U, boot_images.size());
91*795d594fSAndroid Build Coastguard Worker EXPECT_EQ(std::string("boot_image"), boot_images[0]);
92*795d594fSAndroid Build Coastguard Worker EXPECT_PARSED_EXISTS(Opt::CheckJni);
93*795d594fSAndroid Build Coastguard Worker EXPECT_PARSED_EQ(2048U, Opt::MemoryInitialSize);
94*795d594fSAndroid Build Coastguard Worker EXPECT_PARSED_EQ(4 * KB, Opt::MemoryMaximumSize);
95*795d594fSAndroid Build Coastguard Worker EXPECT_PARSED_EQ(1 * MB, Opt::StackSize);
96*795d594fSAndroid Build Coastguard Worker EXPECT_PARSED_EQ(200 * MB, Opt::StopForNativeAllocs);
97*795d594fSAndroid Build Coastguard Worker EXPECT_DOUBLE_EQ(0.75, map.GetOrDefault(Opt::HeapTargetUtilization));
98*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(reinterpret_cast<void*>(test_vfprintf) == map.GetOrDefault(Opt::HookVfprintf));
99*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(reinterpret_cast<void*>(test_exit) == map.GetOrDefault(Opt::HookExit));
100*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(reinterpret_cast<void*>(test_abort) == map.GetOrDefault(Opt::HookAbort));
101*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(VLOG_IS_ON(class_linker));
102*795d594fSAndroid Build Coastguard Worker EXPECT_FALSE(VLOG_IS_ON(compiler));
103*795d594fSAndroid Build Coastguard Worker EXPECT_FALSE(VLOG_IS_ON(heap));
104*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(VLOG_IS_ON(gc));
105*795d594fSAndroid Build Coastguard Worker EXPECT_FALSE(VLOG_IS_ON(interpreter));
106*795d594fSAndroid Build Coastguard Worker EXPECT_FALSE(VLOG_IS_ON(jdwp));
107*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(VLOG_IS_ON(jni));
108*795d594fSAndroid Build Coastguard Worker EXPECT_FALSE(VLOG_IS_ON(monitor));
109*795d594fSAndroid Build Coastguard Worker EXPECT_FALSE(VLOG_IS_ON(signals));
110*795d594fSAndroid Build Coastguard Worker EXPECT_FALSE(VLOG_IS_ON(simulator));
111*795d594fSAndroid Build Coastguard Worker EXPECT_FALSE(VLOG_IS_ON(startup));
112*795d594fSAndroid Build Coastguard Worker EXPECT_FALSE(VLOG_IS_ON(third_party_jni));
113*795d594fSAndroid Build Coastguard Worker EXPECT_FALSE(VLOG_IS_ON(threads));
114*795d594fSAndroid Build Coastguard Worker
115*795d594fSAndroid Build Coastguard Worker auto&& properties_list = map.GetOrDefault(Opt::PropertiesList);
116*795d594fSAndroid Build Coastguard Worker ASSERT_EQ(2U, properties_list.size());
117*795d594fSAndroid Build Coastguard Worker EXPECT_EQ("foo=bar", properties_list[0]);
118*795d594fSAndroid Build Coastguard Worker EXPECT_EQ("baz=qux", properties_list[1]);
119*795d594fSAndroid Build Coastguard Worker }
120*795d594fSAndroid Build Coastguard Worker
TEST_F(ParsedOptionsTest,ParsedOptionsGc)121*795d594fSAndroid Build Coastguard Worker TEST_F(ParsedOptionsTest, ParsedOptionsGc) {
122*795d594fSAndroid Build Coastguard Worker RuntimeOptions options;
123*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-Xgc:SS", nullptr));
124*795d594fSAndroid Build Coastguard Worker
125*795d594fSAndroid Build Coastguard Worker RuntimeArgumentMap map;
126*795d594fSAndroid Build Coastguard Worker bool parsed = ParsedOptions::Parse(options, false, &map);
127*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(parsed);
128*795d594fSAndroid Build Coastguard Worker ASSERT_NE(0u, map.Size());
129*795d594fSAndroid Build Coastguard Worker
130*795d594fSAndroid Build Coastguard Worker using Opt = RuntimeArgumentMap;
131*795d594fSAndroid Build Coastguard Worker
132*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(map.Exists(Opt::GcOption));
133*795d594fSAndroid Build Coastguard Worker
134*795d594fSAndroid Build Coastguard Worker XGcOption xgc = map.GetOrDefault(Opt::GcOption);
135*795d594fSAndroid Build Coastguard Worker EXPECT_EQ(gc::kCollectorTypeSS, xgc.collector_type_);
136*795d594fSAndroid Build Coastguard Worker }
137*795d594fSAndroid Build Coastguard Worker
TEST_F(ParsedOptionsTest,ParsedOptionsGenerationalCC)138*795d594fSAndroid Build Coastguard Worker TEST_F(ParsedOptionsTest, ParsedOptionsGenerationalCC) {
139*795d594fSAndroid Build Coastguard Worker RuntimeOptions options;
140*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("-Xgc:generational_cc", nullptr));
141*795d594fSAndroid Build Coastguard Worker
142*795d594fSAndroid Build Coastguard Worker RuntimeArgumentMap map;
143*795d594fSAndroid Build Coastguard Worker bool parsed = ParsedOptions::Parse(options, false, &map);
144*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(parsed);
145*795d594fSAndroid Build Coastguard Worker ASSERT_NE(0u, map.Size());
146*795d594fSAndroid Build Coastguard Worker
147*795d594fSAndroid Build Coastguard Worker using Opt = RuntimeArgumentMap;
148*795d594fSAndroid Build Coastguard Worker
149*795d594fSAndroid Build Coastguard Worker EXPECT_TRUE(map.Exists(Opt::GcOption));
150*795d594fSAndroid Build Coastguard Worker
151*795d594fSAndroid Build Coastguard Worker XGcOption xgc = map.GetOrDefault(Opt::GcOption);
152*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(xgc.generational_cc);
153*795d594fSAndroid Build Coastguard Worker }
154*795d594fSAndroid Build Coastguard Worker
TEST_F(ParsedOptionsTest,ParsedOptionsInstructionSet)155*795d594fSAndroid Build Coastguard Worker TEST_F(ParsedOptionsTest, ParsedOptionsInstructionSet) {
156*795d594fSAndroid Build Coastguard Worker using Opt = RuntimeArgumentMap;
157*795d594fSAndroid Build Coastguard Worker
158*795d594fSAndroid Build Coastguard Worker {
159*795d594fSAndroid Build Coastguard Worker // Nothing set, should be kRuntimeISA.
160*795d594fSAndroid Build Coastguard Worker RuntimeOptions options;
161*795d594fSAndroid Build Coastguard Worker RuntimeArgumentMap map;
162*795d594fSAndroid Build Coastguard Worker bool parsed = ParsedOptions::Parse(options, false, &map);
163*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(parsed);
164*795d594fSAndroid Build Coastguard Worker InstructionSet isa = map.GetOrDefault(Opt::ImageInstructionSet);
165*795d594fSAndroid Build Coastguard Worker EXPECT_EQ(kRuntimeISA, isa);
166*795d594fSAndroid Build Coastguard Worker }
167*795d594fSAndroid Build Coastguard Worker
168*795d594fSAndroid Build Coastguard Worker const char* isa_strings[] = { "arm", "arm64", "riscv64", "x86", "x86_64" };
169*795d594fSAndroid Build Coastguard Worker InstructionSet ISAs[] = { InstructionSet::kArm,
170*795d594fSAndroid Build Coastguard Worker InstructionSet::kArm64,
171*795d594fSAndroid Build Coastguard Worker InstructionSet::kRiscv64,
172*795d594fSAndroid Build Coastguard Worker InstructionSet::kX86,
173*795d594fSAndroid Build Coastguard Worker InstructionSet::kX86_64 };
174*795d594fSAndroid Build Coastguard Worker static_assert(arraysize(isa_strings) == arraysize(ISAs), "Need same amount.");
175*795d594fSAndroid Build Coastguard Worker
176*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < arraysize(isa_strings); ++i) {
177*795d594fSAndroid Build Coastguard Worker RuntimeOptions options;
178*795d594fSAndroid Build Coastguard Worker options.push_back(std::make_pair("imageinstructionset", isa_strings[i]));
179*795d594fSAndroid Build Coastguard Worker RuntimeArgumentMap map;
180*795d594fSAndroid Build Coastguard Worker bool parsed = ParsedOptions::Parse(options, false, &map);
181*795d594fSAndroid Build Coastguard Worker ASSERT_TRUE(parsed);
182*795d594fSAndroid Build Coastguard Worker InstructionSet isa = map.GetOrDefault(Opt::ImageInstructionSet);
183*795d594fSAndroid Build Coastguard Worker EXPECT_EQ(ISAs[i], isa);
184*795d594fSAndroid Build Coastguard Worker }
185*795d594fSAndroid Build Coastguard Worker }
186*795d594fSAndroid Build Coastguard Worker
187*795d594fSAndroid Build Coastguard Worker } // namespace art
188