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