blob: 980363b1bbb79a6237190306a502924a1aeeae40 [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 Sehr9e734c72018-01-04 17:56:19 -080032#include "dex/dex_file-inl.h"
33#include "dex/dex_file_loader.h"
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070034#include "jit/profile_compilation_info.h"
35#include "method_reference.h"
36#include "runtime.h"
37#include "utils.h"
38
39namespace art {
40
41struct ImageSizes {
42 size_t art_size = 0;
43 size_t oat_size = 0;
44 size_t vdex_size = 0;
45};
46
47std::ostream& operator<<(std::ostream& os, const ImageSizes& sizes) {
48 os << "art=" << sizes.art_size << " oat=" << sizes.oat_size << " vdex=" << sizes.vdex_size;
49 return os;
50}
51
52class Dex2oatImageTest : public CommonRuntimeTest {
53 public:
54 virtual void TearDown() OVERRIDE {}
55
56 protected:
57 // Visitors take method and type references
58 template <typename MethodVisitor, typename ClassVisitor>
59 void VisitLibcoreDexes(const MethodVisitor& method_visitor,
60 const ClassVisitor& class_visitor,
61 size_t method_frequency = 1,
62 size_t class_frequency = 1) {
63 size_t method_counter = 0;
64 size_t class_counter = 0;
Andreas Gampe641a4732017-08-24 13:21:35 -070065 for (const std::string& dex : GetLibCoreDexFileNames()) {
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070066 std::vector<std::unique_ptr<const DexFile>> dex_files;
67 std::string error_msg;
Mathieu Chartier79c87da2017-10-10 11:54:29 -070068 CHECK(DexFileLoader::Open(dex.c_str(),
69 dex,
Nicolas Geoffray095c6c92017-10-19 13:59:55 +010070 /*verify*/ true,
Mathieu Chartier79c87da2017-10-10 11:54:29 -070071 /*verify_checksum*/ false,
72 &error_msg,
73 &dex_files))
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070074 << error_msg;
75 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
76 for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) {
77 if (++method_counter % method_frequency == 0) {
78 method_visitor(MethodReference(dex_file.get(), i));
79 }
80 }
81 for (size_t i = 0; i < dex_file->NumTypeIds(); ++i) {
82 if (++class_counter % class_frequency == 0) {
83 class_visitor(TypeReference(dex_file.get(), dex::TypeIndex(i)));
84 }
85 }
86 }
87 }
88 }
89
90 static void WriteLine(File* file, std::string line) {
91 line += '\n';
92 EXPECT_TRUE(file->WriteFully(&line[0], line.length()));
93 }
94
95 void GenerateClasses(File* out_file, size_t frequency = 1) {
96 VisitLibcoreDexes(VoidFunctor(),
97 [out_file](TypeReference ref) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070098 WriteLine(out_file, ref.dex_file->PrettyType(ref.TypeIndex()));
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -070099 }, frequency, frequency);
100 EXPECT_EQ(out_file->Flush(), 0);
101 }
102
103 void GenerateMethods(File* out_file, size_t frequency = 1) {
104 VisitLibcoreDexes([out_file](MethodReference ref) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700105 WriteLine(out_file, ref.PrettyMethod());
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700106 }, VoidFunctor(), frequency, frequency);
107 EXPECT_EQ(out_file->Flush(), 0);
108 }
109
110 void AddRuntimeArg(std::vector<std::string>& args, const std::string& arg) {
111 args.push_back("--runtime-arg");
112 args.push_back(arg);
113 }
114
115 ImageSizes CompileImageAndGetSizes(const std::vector<std::string>& extra_args) {
116 ImageSizes ret;
117 ScratchFile scratch;
118 std::string scratch_dir = scratch.GetFilename();
119 while (!scratch_dir.empty() && scratch_dir.back() != '/') {
120 scratch_dir.pop_back();
121 }
122 CHECK(!scratch_dir.empty()) << "No directory " << scratch.GetFilename();
123 std::string error_msg;
124 if (!CompileBootImage(extra_args, scratch.GetFilename(), &error_msg)) {
125 LOG(ERROR) << "Failed to compile image " << scratch.GetFilename() << error_msg;
126 }
127 std::string art_file = scratch.GetFilename() + ".art";
128 std::string oat_file = scratch.GetFilename() + ".oat";
129 std::string vdex_file = scratch.GetFilename() + ".vdex";
130 ret.art_size = GetFileSizeBytes(art_file);
131 ret.oat_size = GetFileSizeBytes(oat_file);
132 ret.vdex_size = GetFileSizeBytes(vdex_file);
133 CHECK_GT(ret.art_size, 0u) << art_file;
134 CHECK_GT(ret.oat_size, 0u) << oat_file;
135 CHECK_GT(ret.vdex_size, 0u) << vdex_file;
136 scratch.Close();
137 // Clear image files since we compile the image multiple times and don't want to leave any
138 // artifacts behind.
139 ClearDirectory(scratch_dir.c_str(), /*recursive*/ false);
140 return ret;
141 }
142
143 bool CompileBootImage(const std::vector<std::string>& extra_args,
144 const std::string& image_file_name_prefix,
145 std::string* error_msg) {
146 Runtime* const runtime = Runtime::Current();
147 std::vector<std::string> argv;
148 argv.push_back(runtime->GetCompilerExecutable());
149 AddRuntimeArg(argv, "-Xms64m");
150 AddRuntimeArg(argv, "-Xmx64m");
151 std::vector<std::string> dex_files = GetLibCoreDexFileNames();
152 for (const std::string& dex_file : dex_files) {
153 argv.push_back("--dex-file=" + dex_file);
154 argv.push_back("--dex-location=" + dex_file);
155 }
156 if (runtime->IsJavaDebuggable()) {
157 argv.push_back("--debuggable");
158 }
159 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
160
161 AddRuntimeArg(argv, "-Xverify:softfail");
162
163 if (!kIsTargetBuild) {
164 argv.push_back("--host");
165 }
166
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700167 argv.push_back("--image=" + image_file_name_prefix + ".art");
168 argv.push_back("--oat-file=" + image_file_name_prefix + ".oat");
169 argv.push_back("--oat-location=" + image_file_name_prefix + ".oat");
170 argv.push_back("--base=0x60000000");
171
172 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
173 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
174
175 // We must set --android-root.
176 const char* android_root = getenv("ANDROID_ROOT");
177 CHECK(android_root != nullptr);
178 argv.push_back("--android-root=" + std::string(android_root));
179 argv.insert(argv.end(), extra_args.begin(), extra_args.end());
180
181 return RunDex2Oat(argv, error_msg);
182 }
183
184 int RunDex2Oat(const std::vector<std::string>& args, std::string* error_msg) {
185 int link[2];
186
187 if (pipe(link) == -1) {
188 return false;
189 }
190
191 pid_t pid = fork();
192 if (pid == -1) {
193 return false;
194 }
195
196 if (pid == 0) {
197 // We need dex2oat to actually log things.
198 setenv("ANDROID_LOG_TAGS", "*:f", 1);
199 dup2(link[1], STDERR_FILENO);
200 close(link[0]);
201 close(link[1]);
202 std::vector<const char*> c_args;
203 for (const std::string& str : args) {
204 c_args.push_back(str.c_str());
205 }
206 c_args.push_back(nullptr);
207 execv(c_args[0], const_cast<char* const*>(c_args.data()));
208 exit(1);
209 UNREACHABLE();
210 } else {
211 close(link[1]);
212 char buffer[128];
213 memset(buffer, 0, 128);
214 ssize_t bytes_read = 0;
215
216 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
217 *error_msg += std::string(buffer, bytes_read);
218 }
219 close(link[0]);
220 int status = -1;
221 if (waitpid(pid, &status, 0) != -1) {
222 return (status == 0);
223 }
224 return false;
225 }
226 }
227};
228
229TEST_F(Dex2oatImageTest, TestModesAndFilters) {
230 if (kIsTargetBuild) {
231 // This test is too slow for target builds.
232 return;
233 }
234 ImageSizes base_sizes = CompileImageAndGetSizes({});
235 ImageSizes image_classes_sizes;
236 ImageSizes compiled_classes_sizes;
237 ImageSizes compiled_all_classes_sizes;
238 ImageSizes compiled_methods_sizes;
239 ImageSizes compiled_all_methods_sizes;
240 ImageSizes profile_sizes;
241 std::cout << "Base compile sizes " << base_sizes << std::endl;
242 // Test image classes
243 {
244 ScratchFile classes;
245 GenerateClasses(classes.GetFile(), /*frequency*/ 1u);
246 image_classes_sizes = CompileImageAndGetSizes(
247 {"--image-classes=" + classes.GetFilename()});
248 classes.Close();
249 std::cout << "Image classes sizes " << image_classes_sizes << std::endl;
250 // Putting all classes as image classes should increase art size
251 EXPECT_GE(image_classes_sizes.art_size, base_sizes.art_size);
252 // Sanity check that dex is the same size.
253 EXPECT_EQ(image_classes_sizes.vdex_size, base_sizes.vdex_size);
254 }
255 // Test compiled classes with all the classes.
256 {
257 ScratchFile classes;
258 // Only compile every even class.
259 GenerateClasses(classes.GetFile(), /*frequency*/ 1u);
260 compiled_all_classes_sizes = CompileImageAndGetSizes(
261 {"--compiled-classes=" + classes.GetFilename()});
262 classes.Close();
263 std::cout << "Compiled all classes sizes " << compiled_all_classes_sizes << std::endl;
264 // Check that oat size is smaller since we didn't compile everything.
265 EXPECT_EQ(compiled_all_classes_sizes.art_size, base_sizes.art_size);
Nicolas Geoffray6e5c0442017-07-17 11:29:41 +0100266 // TODO(mathieuc): Find a reliable way to check compiled code.
267 // EXPECT_EQ(compiled_all_classes_sizes.oat_size, base_sizes.oat_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700268 EXPECT_EQ(compiled_all_classes_sizes.vdex_size, base_sizes.vdex_size);
269 }
270 // Test compiled classes.
271 {
272 ScratchFile classes;
273 // Only compile every even class.
274 GenerateClasses(classes.GetFile(), /*frequency*/ 2u);
275 compiled_classes_sizes = CompileImageAndGetSizes(
276 {"--image-classes=" + classes.GetFilename(),
277 "--compiled-classes=" + classes.GetFilename()});
278 classes.Close();
279 std::cout << "Compiled classes sizes " << compiled_classes_sizes << std::endl;
280 // Check that oat size is smaller since we didn't compile everything.
Nicolas Geoffray6e5c0442017-07-17 11:29:41 +0100281 // TODO(mathieuc): Find a reliable way to check compiled code.
282 // EXPECT_LT(compiled_classes_sizes.oat_size, base_sizes.oat_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700283 // Art file should be smaller than image classes version since we included fewer classes in the
284 // list.
285 EXPECT_LT(compiled_classes_sizes.art_size, image_classes_sizes.art_size);
286 }
287 // Test compiled methods.
288 {
289 ScratchFile methods;
290 // Only compile every even class.
291 GenerateMethods(methods.GetFile(), /*frequency*/ 1u);
292 compiled_all_methods_sizes = CompileImageAndGetSizes(
293 {"--compiled-methods=" + methods.GetFilename()});
294 methods.Close();
295 std::cout << "Compiled all methods sizes " << compiled_all_methods_sizes << std::endl;
296 EXPECT_EQ(compiled_all_classes_sizes.art_size, base_sizes.art_size);
Nicolas Geoffray6e5c0442017-07-17 11:29:41 +0100297 // TODO(mathieuc): Find a reliable way to check compiled code. b/63746626
298 // EXPECT_EQ(compiled_all_classes_sizes.oat_size, base_sizes.oat_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700299 EXPECT_EQ(compiled_all_classes_sizes.vdex_size, base_sizes.vdex_size);
300 }
301 static size_t kMethodFrequency = 3;
302 static size_t kTypeFrequency = 4;
303 // Test compiling fewer methods and classes.
304 {
305 ScratchFile methods;
306 ScratchFile classes;
307 // Only compile every even class.
308 GenerateMethods(methods.GetFile(), kMethodFrequency);
309 GenerateClasses(classes.GetFile(), kTypeFrequency);
310 compiled_methods_sizes = CompileImageAndGetSizes(
311 {"--image-classes=" + classes.GetFilename(),
312 "--compiled-methods=" + methods.GetFilename()});
313 methods.Close();
314 classes.Close();
315 std::cout << "Compiled fewer methods sizes " << compiled_methods_sizes << std::endl;
316 }
317 // Cross verify profile based image against image-classes and compiled-methods to make sure it
318 // matches.
319 {
320 ProfileCompilationInfo profile;
321 VisitLibcoreDexes([&profile](MethodReference ref) {
Mathieu Chartierc46cf802017-09-28 11:52:19 -0700322 uint32_t flags = ProfileCompilationInfo::MethodHotness::kFlagHot |
323 ProfileCompilationInfo::MethodHotness::kFlagStartup;
324 EXPECT_TRUE(profile.AddMethodIndex(
325 static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags),
326 ref));
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700327 }, [&profile](TypeReference ref) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -0700328 EXPECT_TRUE(profile.AddClassForDex(ref));
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700329 }, kMethodFrequency, kTypeFrequency);
330 ScratchFile profile_file;
331 profile.Save(profile_file.GetFile()->Fd());
332 EXPECT_EQ(profile_file.GetFile()->Flush(), 0);
333 profile_sizes = CompileImageAndGetSizes(
334 {"--profile-file=" + profile_file.GetFilename(),
335 "--compiler-filter=speed-profile"});
336 profile_file.Close();
337 std::cout << "Profile sizes " << profile_sizes << std::endl;
338 // Since there is some difference between profile vs image + methods due to layout, check that
Mathieu Chartier44e9bac2017-10-06 09:50:18 -0700339 // the range is within expected margins (+-10%).
340 const double kRatio = 0.90;
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700341 EXPECT_LE(profile_sizes.art_size * kRatio, compiled_methods_sizes.art_size);
Nicolas Geoffray6e5c0442017-07-17 11:29:41 +0100342 // TODO(mathieuc): Find a reliable way to check compiled code. b/63746626
343 // EXPECT_LE(profile_sizes.oat_size * kRatio, compiled_methods_sizes.oat_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700344 EXPECT_LE(profile_sizes.vdex_size * kRatio, compiled_methods_sizes.vdex_size);
345 EXPECT_GE(profile_sizes.art_size / kRatio, compiled_methods_sizes.art_size);
Nicolas Geoffray6e5c0442017-07-17 11:29:41 +0100346 // TODO(mathieuc): Find a reliable way to check compiled code. b/63746626
347 // EXPECT_GE(profile_sizes.oat_size / kRatio, compiled_methods_sizes.oat_size);
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700348 EXPECT_GE(profile_sizes.vdex_size / kRatio, compiled_methods_sizes.vdex_size);
349 }
Jeff Haoc23b0c02017-07-27 18:19:38 -0700350 // Test dirty image objects.
351 {
352 ScratchFile classes;
353 GenerateClasses(classes.GetFile(), /*frequency*/ 1u);
354 image_classes_sizes = CompileImageAndGetSizes(
355 {"--dirty-image-objects=" + classes.GetFilename()});
356 classes.Close();
357 std::cout << "Dirty image object sizes " << image_classes_sizes << std::endl;
358 }
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700359}
360
361} // namespace art