blob: 336c5c93517b66294b1fe5ba641d4bde58bc506a [file] [log] [blame]
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -07001/*
2 * Copyright (C) 2017 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
Igor Murashkin5573c372017-11-16 13:34:30 -080017#include <regex>
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070018#include <sstream>
19#include <string>
20#include <vector>
21
22#include <sys/wait.h>
23#include <unistd.h>
24
Andreas Gampe57943812017-12-06 21:39:13 -080025#include <android-base/logging.h>
26
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070027#include "common_runtime_test.h"
28
David Sehr891a50e2017-10-27 17:01:07 -070029#include "base/file_utils.h"
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070030#include "base/macros.h"
31#include "base/unix_file/fd_file.h"
David Sehrc431b9d2018-03-02 12:01:51 -080032#include "base/utils.h"
David Sehr013fd802018-01-11 22:55:24 -080033#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file-inl.h"
35#include "dex/dex_file_loader.h"
David Sehr312f3b22018-03-19 08:39:26 -070036#include "dex/method_reference.h"
David Sehr82d046e2018-04-23 08:14:19 -070037#include "profile/profile_compilation_info.h"
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070038#include "runtime.h"
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070039
40namespace art {
41
42struct ImageSizes {
43 size_t art_size = 0;
44 size_t oat_size = 0;
45 size_t vdex_size = 0;
46};
47
48std::ostream& operator<<(std::ostream& os, const ImageSizes& sizes) {
49 os << "art=" << sizes.art_size << " oat=" << sizes.oat_size << " vdex=" << sizes.vdex_size;
50 return os;
51}
52
53class Dex2oatImageTest : public CommonRuntimeTest {
54 public:
Roland Levillainf73caca2018-08-24 17:19:07 +010055 void TearDown() override {}
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070056
57 protected:
58 // Visitors take method and type references
59 template <typename MethodVisitor, typename ClassVisitor>
60 void VisitLibcoreDexes(const MethodVisitor& method_visitor,
61 const ClassVisitor& class_visitor,
62 size_t method_frequency = 1,
63 size_t class_frequency = 1) {
64 size_t method_counter = 0;
65 size_t class_counter = 0;
Andreas Gampe641a4732017-08-24 13:21:35 -070066 for (const std::string& dex : GetLibCoreDexFileNames()) {
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070067 std::vector<std::unique_ptr<const DexFile>> dex_files;
68 std::string error_msg;
David Sehr013fd802018-01-11 22:55:24 -080069 const ArtDexFileLoader dex_file_loader;
70 CHECK(dex_file_loader.Open(dex.c_str(),
71 dex,
72 /*verify*/ true,
73 /*verify_checksum*/ false,
74 &error_msg,
75 &dex_files))
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070076 << error_msg;
77 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
78 for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) {
79 if (++method_counter % method_frequency == 0) {
80 method_visitor(MethodReference(dex_file.get(), i));
81 }
82 }
83 for (size_t i = 0; i < dex_file->NumTypeIds(); ++i) {
84 if (++class_counter % class_frequency == 0) {
85 class_visitor(TypeReference(dex_file.get(), dex::TypeIndex(i)));
86 }
87 }
88 }
89 }
90 }
91
92 static void WriteLine(File* file, std::string line) {
93 line += '\n';
94 EXPECT_TRUE(file->WriteFully(&line[0], line.length()));
95 }
96
Mathieu Chartierf68b6982019-06-27 09:40:20 -070097 void GenerateProfile(File* out_file, size_t method_frequency, size_t type_frequency) {
98 ProfileCompilationInfo profile;
99 VisitLibcoreDexes([&profile](MethodReference ref) {
100 uint32_t flags = ProfileCompilationInfo::MethodHotness::kFlagHot |
101 ProfileCompilationInfo::MethodHotness::kFlagStartup;
102 EXPECT_TRUE(profile.AddMethodIndex(
103 static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags),
104 ref));
105 }, [&profile](TypeReference ref) {
106 EXPECT_TRUE(profile.AddClassForDex(ref));
107 }, method_frequency, type_frequency);
108 ScratchFile profile_file;
109 profile.Save(out_file->Fd());
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700110 EXPECT_EQ(out_file->Flush(), 0);
111 }
112
113 void GenerateMethods(File* out_file, size_t frequency = 1) {
114 VisitLibcoreDexes([out_file](MethodReference ref) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700115 WriteLine(out_file, ref.PrettyMethod());
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700116 }, VoidFunctor(), frequency, frequency);
117 EXPECT_EQ(out_file->Flush(), 0);
118 }
119
120 void AddRuntimeArg(std::vector<std::string>& args, const std::string& arg) {
121 args.push_back("--runtime-arg");
122 args.push_back(arg);
123 }
124
125 ImageSizes CompileImageAndGetSizes(const std::vector<std::string>& extra_args) {
126 ImageSizes ret;
127 ScratchFile scratch;
128 std::string scratch_dir = scratch.GetFilename();
129 while (!scratch_dir.empty() && scratch_dir.back() != '/') {
130 scratch_dir.pop_back();
131 }
132 CHECK(!scratch_dir.empty()) << "No directory " << scratch.GetFilename();
133 std::string error_msg;
134 if (!CompileBootImage(extra_args, scratch.GetFilename(), &error_msg)) {
135 LOG(ERROR) << "Failed to compile image " << scratch.GetFilename() << error_msg;
136 }
137 std::string art_file = scratch.GetFilename() + ".art";
138 std::string oat_file = scratch.GetFilename() + ".oat";
139 std::string vdex_file = scratch.GetFilename() + ".vdex";
Vladimir Markoa5785a22018-03-08 14:25:47 +0000140 int64_t art_size = OS::GetFileSizeBytes(art_file.c_str());
141 int64_t oat_size = OS::GetFileSizeBytes(oat_file.c_str());
142 int64_t vdex_size = OS::GetFileSizeBytes(vdex_file.c_str());
143 CHECK_GT(art_size, 0u) << art_file;
144 CHECK_GT(oat_size, 0u) << oat_file;
145 CHECK_GT(vdex_size, 0u) << vdex_file;
146 ret.art_size = art_size;
147 ret.oat_size = oat_size;
148 ret.vdex_size = vdex_size;
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700149 scratch.Close();
150 // Clear image files since we compile the image multiple times and don't want to leave any
151 // artifacts behind.
152 ClearDirectory(scratch_dir.c_str(), /*recursive*/ false);
153 return ret;
154 }
155
156 bool CompileBootImage(const std::vector<std::string>& extra_args,
157 const std::string& image_file_name_prefix,
158 std::string* error_msg) {
159 Runtime* const runtime = Runtime::Current();
160 std::vector<std::string> argv;
161 argv.push_back(runtime->GetCompilerExecutable());
162 AddRuntimeArg(argv, "-Xms64m");
163 AddRuntimeArg(argv, "-Xmx64m");
164 std::vector<std::string> dex_files = GetLibCoreDexFileNames();
165 for (const std::string& dex_file : dex_files) {
166 argv.push_back("--dex-file=" + dex_file);
167 argv.push_back("--dex-location=" + dex_file);
168 }
169 if (runtime->IsJavaDebuggable()) {
170 argv.push_back("--debuggable");
171 }
172 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
173
174 AddRuntimeArg(argv, "-Xverify:softfail");
175
176 if (!kIsTargetBuild) {
177 argv.push_back("--host");
178 }
179
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700180 argv.push_back("--image=" + image_file_name_prefix + ".art");
181 argv.push_back("--oat-file=" + image_file_name_prefix + ".oat");
182 argv.push_back("--oat-location=" + image_file_name_prefix + ".oat");
183 argv.push_back("--base=0x60000000");
184
185 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
186 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
187
188 // We must set --android-root.
189 const char* android_root = getenv("ANDROID_ROOT");
190 CHECK(android_root != nullptr);
191 argv.push_back("--android-root=" + std::string(android_root));
192 argv.insert(argv.end(), extra_args.begin(), extra_args.end());
193
194 return RunDex2Oat(argv, error_msg);
195 }
196
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700197 bool RunDex2Oat(const std::vector<std::string>& args, std::string* error_msg) {
198 // We only want fatal logging for the error message.
199 auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:f", 1) == 0; };
200 ForkAndExecResult res = ForkAndExec(args, post_fork_fn, error_msg);
201 if (res.stage != ForkAndExecResult::kFinished) {
202 *error_msg = strerror(errno);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700203 return false;
204 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700205 return res.StandardSuccess();
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700206 }
207};
208
Vladimir Markoa5785a22018-03-08 14:25:47 +0000209TEST_F(Dex2oatImageTest, TestModesAndFilters) {
Roland Levillainf5dd1142018-07-03 13:29:18 +0100210 // This test crashes on the gtest-heap-poisoning configuration
211 // (AddressSanitizer + CMS/RosAlloc + heap-poisoning); see b/111061592.
212 // Temporarily disable this test on this configuration to keep
213 // our automated build/testing green while we work on a fix.
214 TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING_WITHOUT_READ_BARRIERS();
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700215 if (kIsTargetBuild) {
216 // This test is too slow for target builds.
217 return;
218 }
219 ImageSizes base_sizes = CompileImageAndGetSizes({});
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700220 ImageSizes everything_sizes;
221 ImageSizes filter_sizes;
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700222 std::cout << "Base compile sizes " << base_sizes << std::endl;
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700223 // Compile all methods and classes
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700224 {
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700225 ScratchFile profile_file;
226 GenerateProfile(profile_file.GetFile(), /*method_frequency=*/ 1u, /*type_frequency=*/ 1u);
227 everything_sizes = CompileImageAndGetSizes(
228 {"--profile-file=" + profile_file.GetFilename(),
229 "--compiler-filter=speed-profile"});
230 profile_file.Close();
231 std::cout << "All methods and classes sizes " << everything_sizes << std::endl;
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700232 // Putting all classes as image classes should increase art size
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700233 EXPECT_GE(everything_sizes.art_size, base_sizes.art_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700234 // Sanity check that dex is the same size.
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700235 EXPECT_EQ(everything_sizes.vdex_size, base_sizes.vdex_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700236 }
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700237 static size_t kMethodFrequency = 3;
238 static size_t kTypeFrequency = 4;
239 // Test compiling fewer methods and classes.
240 {
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700241 ScratchFile profile_file;
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700242 GenerateProfile(profile_file.GetFile(), kMethodFrequency, kTypeFrequency);
243 filter_sizes = CompileImageAndGetSizes(
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700244 {"--profile-file=" + profile_file.GetFilename(),
245 "--compiler-filter=speed-profile"});
246 profile_file.Close();
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700247 std::cout << "Fewer methods and classes sizes " << filter_sizes << std::endl;
248 EXPECT_LE(filter_sizes.art_size, everything_sizes.art_size);
249 EXPECT_LE(filter_sizes.oat_size, everything_sizes.oat_size);
250 EXPECT_LE(filter_sizes.vdex_size, everything_sizes.vdex_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700251 }
Jeff Haoc23b0c02017-07-27 18:19:38 -0700252 // Test dirty image objects.
253 {
254 ScratchFile classes;
Mathieu Chartierf68b6982019-06-27 09:40:20 -0700255 VisitLibcoreDexes(VoidFunctor(),
256 [&](TypeReference ref) {
257 WriteLine(classes.GetFile(), ref.dex_file->PrettyType(ref.TypeIndex()));
258 }, /*method_frequency=*/ 1u, /*class_frequency=*/ 1u);
259 ImageSizes image_classes_sizes = CompileImageAndGetSizes(
Jeff Haoc23b0c02017-07-27 18:19:38 -0700260 {"--dirty-image-objects=" + classes.GetFilename()});
261 classes.Close();
262 std::cout << "Dirty image object sizes " << image_classes_sizes << std::endl;
263 }
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700264}
265
266} // namespace art