blob: b8366323d6102cdd744971c0ba428d295e4a4c00 [file] [log] [blame]
Calin Juravle2e2db782016-02-23 12:00:03 +00001/*
2 * Copyright (C) 2016 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 <gtest/gtest.h>
18
Calin Juravlee0ac1152017-02-13 19:03:47 -080019#include "art_method-inl.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000020#include "base/unix_file/fd_file.h"
21#include "common_runtime_test.h"
David Sehr97c381e2017-02-01 15:09:58 -080022#include "exec_utils.h"
Calin Juravle33083d62017-01-18 15:29:12 -080023#include "jit/profile_compilation_info.h"
Calin Juravlecc3171a2017-05-19 16:47:53 -070024#include "linear_alloc.h"
Calin Juravlee0ac1152017-02-13 19:03:47 -080025#include "mirror/class-inl.h"
Mathieu Chartierd808e8b2017-03-21 13:37:41 -070026#include "obj_ptr-inl.h"
Calin Juravlee0ac1152017-02-13 19:03:47 -080027#include "profile_assistant.h"
28#include "scoped_thread_state_change-inl.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000029#include "utils.h"
30
31namespace art {
32
33class ProfileAssistantTest : public CommonRuntimeTest {
Calin Juravlecc3171a2017-05-19 16:47:53 -070034 public:
35 virtual void PostRuntimeCreate() {
36 arena_.reset(new ArenaAllocator(Runtime::Current()->GetArenaPool()));
37 }
38
Calin Juravle2e2db782016-02-23 12:00:03 +000039 protected:
40 void SetupProfile(const std::string& id,
41 uint32_t checksum,
42 uint16_t number_of_methods,
Calin Juravlec824b512016-03-29 20:33:33 +010043 uint16_t number_of_classes,
Calin Juravle2e2db782016-02-23 12:00:03 +000044 const ScratchFile& profile,
45 ProfileCompilationInfo* info,
Calin Juravlecea9e9d2017-03-23 19:04:59 -070046 uint16_t start_method_index = 0,
47 bool reverse_dex_write_order = false) {
Calin Juravle2e2db782016-02-23 12:00:03 +000048 std::string dex_location1 = "location1" + id;
49 uint32_t dex_location_checksum1 = checksum;
50 std::string dex_location2 = "location2" + id;
51 uint32_t dex_location_checksum2 = 10 * checksum;
52 for (uint16_t i = start_method_index; i < start_method_index + number_of_methods; i++) {
Calin Juravlecea9e9d2017-03-23 19:04:59 -070053 // reverse_dex_write_order controls the order in which the dex files will be added to
54 // the profile and thus written to disk.
55 ProfileCompilationInfo::OfflineProfileMethodInfo pmi =
56 GetOfflineProfileMethodInfo(dex_location1, dex_location_checksum1,
57 dex_location2, dex_location_checksum2);
58 if (reverse_dex_write_order) {
59 ASSERT_TRUE(info->AddMethod(dex_location2, dex_location_checksum2, i, pmi));
60 ASSERT_TRUE(info->AddMethod(dex_location1, dex_location_checksum1, i, pmi));
61 } else {
62 ASSERT_TRUE(info->AddMethod(dex_location1, dex_location_checksum1, i, pmi));
63 ASSERT_TRUE(info->AddMethod(dex_location2, dex_location_checksum2, i, pmi));
64 }
Calin Juravle2e2db782016-02-23 12:00:03 +000065 }
Calin Juravlec824b512016-03-29 20:33:33 +010066 for (uint16_t i = 0; i < number_of_classes; i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -080067 ASSERT_TRUE(info->AddClassIndex(dex_location1, dex_location_checksum1, dex::TypeIndex(i)));
Calin Juravlec824b512016-03-29 20:33:33 +010068 }
69
Calin Juravle2e2db782016-02-23 12:00:03 +000070 ASSERT_TRUE(info->Save(GetFd(profile)));
71 ASSERT_EQ(0, profile.GetFile()->Flush());
72 ASSERT_TRUE(profile.GetFile()->ResetOffset());
73 }
74
Calin Juravlecea9e9d2017-03-23 19:04:59 -070075 ProfileCompilationInfo::OfflineProfileMethodInfo GetOfflineProfileMethodInfo(
76 const std::string& dex_location1, uint32_t dex_checksum1,
77 const std::string& dex_location2, uint32_t dex_checksum2) {
Calin Juravlecc3171a2017-05-19 16:47:53 -070078 ProfileCompilationInfo::OfflineProfileMethodInfo pmi(arena_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -070079 pmi.dex_references.emplace_back(dex_location1, dex_checksum1);
80 pmi.dex_references.emplace_back(dex_location2, dex_checksum2);
81
82 // Monomorphic
83 for (uint16_t dex_pc = 0; dex_pc < 11; dex_pc++) {
Calin Juravlecc3171a2017-05-19 16:47:53 -070084 ProfileCompilationInfo::DexPcData dex_pc_data(arena_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -070085 dex_pc_data.AddClass(0, dex::TypeIndex(0));
86 pmi.inline_caches.Put(dex_pc, dex_pc_data);
87 }
88 // Polymorphic
89 for (uint16_t dex_pc = 11; dex_pc < 22; dex_pc++) {
Calin Juravlecc3171a2017-05-19 16:47:53 -070090 ProfileCompilationInfo::DexPcData dex_pc_data(arena_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -070091 dex_pc_data.AddClass(0, dex::TypeIndex(0));
92 dex_pc_data.AddClass(1, dex::TypeIndex(1));
93
94 pmi.inline_caches.Put(dex_pc, dex_pc_data);
95 }
96 // Megamorphic
97 for (uint16_t dex_pc = 22; dex_pc < 33; dex_pc++) {
Calin Juravlecc3171a2017-05-19 16:47:53 -070098 ProfileCompilationInfo::DexPcData dex_pc_data(arena_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -070099 dex_pc_data.SetIsMegamorphic();
100 pmi.inline_caches.Put(dex_pc, dex_pc_data);
101 }
102 // Missing types
103 for (uint16_t dex_pc = 33; dex_pc < 44; dex_pc++) {
Calin Juravlecc3171a2017-05-19 16:47:53 -0700104 ProfileCompilationInfo::DexPcData dex_pc_data(arena_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700105 dex_pc_data.SetIsMissingTypes();
106 pmi.inline_caches.Put(dex_pc, dex_pc_data);
107 }
108
109 return pmi;
110 }
111
Calin Juravle2e2db782016-02-23 12:00:03 +0000112 int GetFd(const ScratchFile& file) const {
113 return static_cast<int>(file.GetFd());
114 }
115
116 void CheckProfileInfo(ScratchFile& file, const ProfileCompilationInfo& info) {
117 ProfileCompilationInfo file_info;
118 ASSERT_TRUE(file.GetFile()->ResetOffset());
119 ASSERT_TRUE(file_info.Load(GetFd(file)));
120 ASSERT_TRUE(file_info.Equals(info));
121 }
122
Calin Juravle7bcdb532016-06-07 16:14:47 +0100123 std::string GetProfmanCmd() {
Calin Juravle2e2db782016-02-23 12:00:03 +0000124 std::string file_path = GetTestAndroidRoot();
Calin Juravlede4fb632016-02-23 16:53:30 +0000125 file_path += "/bin/profman";
Calin Juravle2e2db782016-02-23 12:00:03 +0000126 if (kIsDebugBuild) {
127 file_path += "d";
128 }
Calin Juravle7bcdb532016-06-07 16:14:47 +0100129 EXPECT_TRUE(OS::FileExists(file_path.c_str()))
130 << file_path << " should be a valid file path";
131 return file_path;
132 }
133 // Runs test with given arguments.
134 int ProcessProfiles(const std::vector<int>& profiles_fd, int reference_profile_fd) {
135 std::string profman_cmd = GetProfmanCmd();
Calin Juravle2e2db782016-02-23 12:00:03 +0000136 std::vector<std::string> argv_str;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100137 argv_str.push_back(profman_cmd);
Calin Juravle2e2db782016-02-23 12:00:03 +0000138 for (size_t k = 0; k < profiles_fd.size(); k++) {
139 argv_str.push_back("--profile-file-fd=" + std::to_string(profiles_fd[k]));
140 }
141 argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile_fd));
142
143 std::string error;
144 return ExecAndReturnCode(argv_str, &error);
145 }
Calin Juravle7bcdb532016-06-07 16:14:47 +0100146
147 bool GenerateTestProfile(const std::string& filename) {
148 std::string profman_cmd = GetProfmanCmd();
149 std::vector<std::string> argv_str;
150 argv_str.push_back(profman_cmd);
151 argv_str.push_back("--generate-test-profile=" + filename);
152 std::string error;
153 return ExecAndReturnCode(argv_str, &error);
154 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800155
Jeff Haof0a31f82017-03-27 15:50:37 -0700156 bool GenerateTestProfileWithInputDex(const std::string& filename) {
157 std::string profman_cmd = GetProfmanCmd();
158 std::vector<std::string> argv_str;
159 argv_str.push_back(profman_cmd);
160 argv_str.push_back("--generate-test-profile=" + filename);
161 argv_str.push_back("--generate-test-profile-seed=0");
162 argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]);
163 argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]);
164 std::string error;
165 return ExecAndReturnCode(argv_str, &error);
166 }
167
Calin Juravlee0ac1152017-02-13 19:03:47 -0800168 bool CreateProfile(std::string profile_file_contents,
169 const std::string& filename,
170 const std::string& dex_location) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800171 ScratchFile class_names_file;
172 File* file = class_names_file.GetFile();
Calin Juravlee0ac1152017-02-13 19:03:47 -0800173 EXPECT_TRUE(file->WriteFully(profile_file_contents.c_str(), profile_file_contents.length()));
David Sehr7c80f2d2017-02-07 16:47:58 -0800174 EXPECT_EQ(0, file->Flush());
175 EXPECT_TRUE(file->ResetOffset());
176 std::string profman_cmd = GetProfmanCmd();
177 std::vector<std::string> argv_str;
178 argv_str.push_back(profman_cmd);
179 argv_str.push_back("--create-profile-from=" + class_names_file.GetFilename());
180 argv_str.push_back("--reference-profile-file=" + filename);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800181 argv_str.push_back("--apk=" + dex_location);
182 argv_str.push_back("--dex-location=" + dex_location);
David Sehr7c80f2d2017-02-07 16:47:58 -0800183 std::string error;
184 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0);
185 return true;
186 }
187
Mathieu Chartier34067262017-04-06 13:55:46 -0700188 bool DumpClassesAndMethods(const std::string& filename, std::string* file_contents) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800189 ScratchFile class_names_file;
190 std::string profman_cmd = GetProfmanCmd();
191 std::vector<std::string> argv_str;
192 argv_str.push_back(profman_cmd);
Mathieu Chartier34067262017-04-06 13:55:46 -0700193 argv_str.push_back("--dump-classes-and-methods");
David Sehr7c80f2d2017-02-07 16:47:58 -0800194 argv_str.push_back("--profile-file=" + filename);
195 argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800196 argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]);
David Sehr7c80f2d2017-02-07 16:47:58 -0800197 argv_str.push_back("--dump-output-to-fd=" + std::to_string(GetFd(class_names_file)));
198 std::string error;
199 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0);
200 File* file = class_names_file.GetFile();
201 EXPECT_EQ(0, file->Flush());
202 EXPECT_TRUE(file->ResetOffset());
203 int64_t length = file->GetLength();
204 std::unique_ptr<char[]> buf(new char[length]);
205 EXPECT_EQ(file->Read(buf.get(), length, 0), length);
206 *file_contents = std::string(buf.get(), length);
207 return true;
208 }
209
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700210 bool CreateAndDump(const std::string& input_file_contents,
211 std::string* output_file_contents) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800212 ScratchFile profile_file;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800213 EXPECT_TRUE(CreateProfile(input_file_contents,
214 profile_file.GetFilename(),
215 GetLibCoreDexFileNames()[0]));
David Sehr7c80f2d2017-02-07 16:47:58 -0800216 profile_file.GetFile()->ResetOffset();
Mathieu Chartier34067262017-04-06 13:55:46 -0700217 EXPECT_TRUE(DumpClassesAndMethods(profile_file.GetFilename(), output_file_contents));
David Sehr7c80f2d2017-02-07 16:47:58 -0800218 return true;
219 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800220
221 mirror::Class* GetClass(jobject class_loader, const std::string& clazz) {
222 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
223 Thread* self = Thread::Current();
224 ScopedObjectAccess soa(self);
225 StackHandleScope<1> hs(self);
226 Handle<mirror::ClassLoader> h_loader(
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700227 hs.NewHandle(ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(class_loader))));
Calin Juravlee0ac1152017-02-13 19:03:47 -0800228 return class_linker->FindClass(self, clazz.c_str(), h_loader);
229 }
230
231 ArtMethod* GetVirtualMethod(jobject class_loader,
232 const std::string& clazz,
233 const std::string& name) {
234 mirror::Class* klass = GetClass(class_loader, clazz);
235 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
236 const auto pointer_size = class_linker->GetImagePointerSize();
237 ArtMethod* method = nullptr;
238 Thread* self = Thread::Current();
239 ScopedObjectAccess soa(self);
240 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
241 if (name == m.GetName()) {
242 EXPECT_TRUE(method == nullptr);
243 method = &m;
244 }
245 }
246 return method;
247 }
248
249 // Verify that given method has the expected inline caches and nothing else.
250 void AssertInlineCaches(ArtMethod* method,
251 const std::set<mirror::Class*>& expected_clases,
252 const ProfileCompilationInfo& info,
Calin Juravle589e71e2017-03-03 16:05:05 -0800253 bool is_megamorphic,
254 bool is_missing_types)
Calin Juravlee0ac1152017-02-13 19:03:47 -0800255 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravlecc3171a2017-05-19 16:47:53 -0700256 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi =
257 info.GetMethod(method->GetDexFile()->GetLocation(),
258 method->GetDexFile()->GetLocationChecksum(),
259 method->GetDexMethodIndex());
260 ASSERT_TRUE(pmi != nullptr);
261 ASSERT_EQ(pmi->inline_caches.size(), 1u);
262 ProfileCompilationInfo::DexPcData dex_pc_data = pmi->inline_caches.begin()->second;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800263
Calin Juravle589e71e2017-03-03 16:05:05 -0800264 ASSERT_EQ(dex_pc_data.is_megamorphic, is_megamorphic);
265 ASSERT_EQ(dex_pc_data.is_missing_types, is_missing_types);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800266 ASSERT_EQ(expected_clases.size(), dex_pc_data.classes.size());
267 size_t found = 0;
268 for (mirror::Class* it : expected_clases) {
269 for (const auto& class_ref : dex_pc_data.classes) {
270 ProfileCompilationInfo::DexReference dex_ref =
Calin Juravlecc3171a2017-05-19 16:47:53 -0700271 pmi->dex_references[class_ref.dex_profile_index];
Calin Juravlee0ac1152017-02-13 19:03:47 -0800272 if (dex_ref.MatchesDex(&(it->GetDexFile())) &&
273 class_ref.type_index == it->GetDexTypeIndex()) {
274 found++;
275 }
276 }
277 }
278
279 ASSERT_EQ(expected_clases.size(), found);
280 }
Calin Juravlecc3171a2017-05-19 16:47:53 -0700281
282 std::unique_ptr<ArenaAllocator> arena_;
Calin Juravle2e2db782016-02-23 12:00:03 +0000283};
284
285TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferences) {
286 ScratchFile profile1;
287 ScratchFile profile2;
288 ScratchFile reference_profile;
289
290 std::vector<int> profile_fds({
291 GetFd(profile1),
292 GetFd(profile2)});
293 int reference_profile_fd = GetFd(reference_profile);
294
295 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
296 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100297 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000298 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100299 SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000300
301 // We should advise compilation.
302 ASSERT_EQ(ProfileAssistant::kCompile,
303 ProcessProfiles(profile_fds, reference_profile_fd));
304 // The resulting compilation info must be equal to the merge of the inputs.
305 ProfileCompilationInfo result;
306 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
307 ASSERT_TRUE(result.Load(reference_profile_fd));
308
309 ProfileCompilationInfo expected;
Calin Juravle67265462016-03-18 16:23:40 +0000310 ASSERT_TRUE(expected.MergeWith(info1));
311 ASSERT_TRUE(expected.MergeWith(info2));
Calin Juravle2e2db782016-02-23 12:00:03 +0000312 ASSERT_TRUE(expected.Equals(result));
313
314 // The information from profiles must remain the same.
315 CheckProfileInfo(profile1, info1);
316 CheckProfileInfo(profile2, info2);
317}
318
Calin Juravlec824b512016-03-29 20:33:33 +0100319// TODO(calin): Add more tests for classes.
320TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferencesBecauseOfClasses) {
321 ScratchFile profile1;
322 ScratchFile reference_profile;
323
324 std::vector<int> profile_fds({
325 GetFd(profile1)});
326 int reference_profile_fd = GetFd(reference_profile);
327
328 const uint16_t kNumberOfClassesToEnableCompilation = 100;
329 ProfileCompilationInfo info1;
330 SetupProfile("p1", 1, 0, kNumberOfClassesToEnableCompilation, profile1, &info1);
331
332 // We should advise compilation.
333 ASSERT_EQ(ProfileAssistant::kCompile,
334 ProcessProfiles(profile_fds, reference_profile_fd));
335 // The resulting compilation info must be equal to the merge of the inputs.
336 ProfileCompilationInfo result;
337 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
338 ASSERT_TRUE(result.Load(reference_profile_fd));
339
340 ProfileCompilationInfo expected;
341 ASSERT_TRUE(expected.MergeWith(info1));
342 ASSERT_TRUE(expected.Equals(result));
343
344 // The information from profiles must remain the same.
345 CheckProfileInfo(profile1, info1);
346}
347
Calin Juravle2e2db782016-02-23 12:00:03 +0000348TEST_F(ProfileAssistantTest, AdviseCompilationNonEmptyReferences) {
349 ScratchFile profile1;
350 ScratchFile profile2;
351 ScratchFile reference_profile;
352
353 std::vector<int> profile_fds({
354 GetFd(profile1),
355 GetFd(profile2)});
356 int reference_profile_fd = GetFd(reference_profile);
357
358 // The new profile info will contain the methods with indices 0-100.
359 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
360 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100361 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000362 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100363 SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000364
365
366 // The reference profile info will contain the methods with indices 50-150.
367 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
368 ProfileCompilationInfo reference_info;
Calin Juravlec824b512016-03-29 20:33:33 +0100369 SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
Calin Juravle2e2db782016-02-23 12:00:03 +0000370 &reference_info, kNumberOfMethodsToEnableCompilation / 2);
371
372 // We should advise compilation.
373 ASSERT_EQ(ProfileAssistant::kCompile,
374 ProcessProfiles(profile_fds, reference_profile_fd));
375
376 // The resulting compilation info must be equal to the merge of the inputs
377 ProfileCompilationInfo result;
378 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
379 ASSERT_TRUE(result.Load(reference_profile_fd));
380
381 ProfileCompilationInfo expected;
Calin Juravle67265462016-03-18 16:23:40 +0000382 ASSERT_TRUE(expected.MergeWith(info1));
383 ASSERT_TRUE(expected.MergeWith(info2));
384 ASSERT_TRUE(expected.MergeWith(reference_info));
Calin Juravle2e2db782016-02-23 12:00:03 +0000385 ASSERT_TRUE(expected.Equals(result));
386
387 // The information from profiles must remain the same.
388 CheckProfileInfo(profile1, info1);
389 CheckProfileInfo(profile2, info2);
390}
391
392TEST_F(ProfileAssistantTest, DoNotAdviseCompilation) {
393 ScratchFile profile1;
394 ScratchFile profile2;
395 ScratchFile reference_profile;
396
397 std::vector<int> profile_fds({
398 GetFd(profile1),
399 GetFd(profile2)});
400 int reference_profile_fd = GetFd(reference_profile);
401
402 const uint16_t kNumberOfMethodsToSkipCompilation = 1;
403 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100404 SetupProfile("p1", 1, kNumberOfMethodsToSkipCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000405 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100406 SetupProfile("p2", 2, kNumberOfMethodsToSkipCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000407
408 // We should not advise compilation.
409 ASSERT_EQ(ProfileAssistant::kSkipCompilation,
410 ProcessProfiles(profile_fds, reference_profile_fd));
411
412 // The information from profiles must remain the same.
413 ProfileCompilationInfo file_info1;
414 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
415 ASSERT_TRUE(file_info1.Load(GetFd(profile1)));
416 ASSERT_TRUE(file_info1.Equals(info1));
417
418 ProfileCompilationInfo file_info2;
419 ASSERT_TRUE(profile2.GetFile()->ResetOffset());
420 ASSERT_TRUE(file_info2.Load(GetFd(profile2)));
421 ASSERT_TRUE(file_info2.Equals(info2));
422
423 // Reference profile files must remain empty.
424 ASSERT_EQ(0, reference_profile.GetFile()->GetLength());
425
426 // The information from profiles must remain the same.
427 CheckProfileInfo(profile1, info1);
428 CheckProfileInfo(profile2, info2);
429}
430
431TEST_F(ProfileAssistantTest, FailProcessingBecauseOfProfiles) {
432 ScratchFile profile1;
433 ScratchFile profile2;
434 ScratchFile reference_profile;
435
436 std::vector<int> profile_fds({
437 GetFd(profile1),
438 GetFd(profile2)});
439 int reference_profile_fd = GetFd(reference_profile);
440
441 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
442 // Assign different hashes for the same dex file. This will make merging of information to fail.
443 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100444 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000445 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100446 SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000447
448 // We should fail processing.
449 ASSERT_EQ(ProfileAssistant::kErrorBadProfiles,
450 ProcessProfiles(profile_fds, reference_profile_fd));
451
452 // The information from profiles must remain the same.
453 CheckProfileInfo(profile1, info1);
454 CheckProfileInfo(profile2, info2);
455
456 // Reference profile files must still remain empty.
457 ASSERT_EQ(0, reference_profile.GetFile()->GetLength());
458}
459
460TEST_F(ProfileAssistantTest, FailProcessingBecauseOfReferenceProfiles) {
461 ScratchFile profile1;
462 ScratchFile reference_profile;
463
464 std::vector<int> profile_fds({
465 GetFd(profile1)});
466 int reference_profile_fd = GetFd(reference_profile);
467
468 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
469 // Assign different hashes for the same dex file. This will make merging of information to fail.
470 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100471 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000472 ProfileCompilationInfo reference_info;
Calin Juravlec824b512016-03-29 20:33:33 +0100473 SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, reference_profile, &reference_info);
Calin Juravle2e2db782016-02-23 12:00:03 +0000474
475 // We should not advise compilation.
476 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
477 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
478 ASSERT_EQ(ProfileAssistant::kErrorBadProfiles,
479 ProcessProfiles(profile_fds, reference_profile_fd));
480
481 // The information from profiles must remain the same.
482 CheckProfileInfo(profile1, info1);
483}
484
Calin Juravle7bcdb532016-06-07 16:14:47 +0100485TEST_F(ProfileAssistantTest, TestProfileGeneration) {
486 ScratchFile profile;
487 // Generate a test profile.
488 GenerateTestProfile(profile.GetFilename());
489
490 // Verify that the generated profile is valid and can be loaded.
491 ASSERT_TRUE(profile.GetFile()->ResetOffset());
492 ProfileCompilationInfo info;
493 ASSERT_TRUE(info.Load(GetFd(profile)));
494}
495
Jeff Haof0a31f82017-03-27 15:50:37 -0700496TEST_F(ProfileAssistantTest, TestProfileGenerationWithIndexDex) {
497 ScratchFile profile;
498 // Generate a test profile passing in a dex file as reference.
499 GenerateTestProfileWithInputDex(profile.GetFilename());
500
501 // Verify that the generated profile is valid and can be loaded.
502 ASSERT_TRUE(profile.GetFile()->ResetOffset());
503 ProfileCompilationInfo info;
504 ASSERT_TRUE(info.Load(GetFd(profile)));
505}
506
David Sehr7c80f2d2017-02-07 16:47:58 -0800507TEST_F(ProfileAssistantTest, TestProfileCreationAllMatch) {
508 // Class names put here need to be in sorted order.
509 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800510 "Ljava/lang/Comparable;",
511 "Ljava/lang/Math;",
Mathieu Chartier34067262017-04-06 13:55:46 -0700512 "Ljava/lang/Object;",
513 "Ljava/lang/Object;-><init>()V"
David Sehr7c80f2d2017-02-07 16:47:58 -0800514 };
Mathieu Chartier34067262017-04-06 13:55:46 -0700515 std::string file_contents;
David Sehr7c80f2d2017-02-07 16:47:58 -0800516 for (std::string& class_name : class_names) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700517 file_contents += class_name + std::string("\n");
David Sehr7c80f2d2017-02-07 16:47:58 -0800518 }
519 std::string output_file_contents;
Mathieu Chartier34067262017-04-06 13:55:46 -0700520 ASSERT_TRUE(CreateAndDump(file_contents, &output_file_contents));
521 ASSERT_EQ(output_file_contents, file_contents);
David Sehr7c80f2d2017-02-07 16:47:58 -0800522}
523
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700524TEST_F(ProfileAssistantTest, TestProfileCreationGenerateMethods) {
525 // Class names put here need to be in sorted order.
526 std::vector<std::string> class_names = {
527 "Ljava/lang/Math;->*",
528 };
529 std::string input_file_contents;
530 std::string expected_contents;
531 for (std::string& class_name : class_names) {
532 input_file_contents += class_name + std::string("\n");
533 expected_contents += DescriptorToDot(class_name.c_str()) +
534 std::string("\n");
535 }
536 std::string output_file_contents;
537 ScratchFile profile_file;
538 EXPECT_TRUE(CreateProfile(input_file_contents,
539 profile_file.GetFilename(),
540 GetLibCoreDexFileNames()[0]));
541 ProfileCompilationInfo info;
542 profile_file.GetFile()->ResetOffset();
543 ASSERT_TRUE(info.Load(GetFd(profile_file)));
544 // Verify that the profile has matching methods.
545 ScopedObjectAccess soa(Thread::Current());
546 ObjPtr<mirror::Class> klass = GetClass(nullptr, "Ljava/lang/Math;");
547 ASSERT_TRUE(klass != nullptr);
548 size_t method_count = 0;
549 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
550 if (!method.IsCopied() && method.GetCodeItem() != nullptr) {
551 ++method_count;
Calin Juravlecc3171a2017-05-19 16:47:53 -0700552 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi =
553 info.GetMethod(method.GetDexFile()->GetLocation(),
554 method.GetDexFile()->GetLocationChecksum(),
555 method.GetDexMethodIndex());
556 ASSERT_TRUE(pmi != nullptr);
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700557 }
558 }
559 EXPECT_GT(method_count, 0u);
560}
561
David Sehr7c80f2d2017-02-07 16:47:58 -0800562TEST_F(ProfileAssistantTest, TestProfileCreationOneNotMatched) {
563 // Class names put here need to be in sorted order.
564 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800565 "Ldoesnt/match/this/one;",
566 "Ljava/lang/Comparable;",
567 "Ljava/lang/Object;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800568 };
569 std::string input_file_contents;
570 for (std::string& class_name : class_names) {
571 input_file_contents += class_name + std::string("\n");
572 }
573 std::string output_file_contents;
574 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
575 std::string expected_contents =
Mathieu Chartier34067262017-04-06 13:55:46 -0700576 class_names[1] + std::string("\n") +
577 class_names[2] + std::string("\n");
David Sehr7c80f2d2017-02-07 16:47:58 -0800578 ASSERT_EQ(output_file_contents, expected_contents);
579}
580
581TEST_F(ProfileAssistantTest, TestProfileCreationNoneMatched) {
582 // Class names put here need to be in sorted order.
583 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800584 "Ldoesnt/match/this/one;",
585 "Ldoesnt/match/this/one/either;",
586 "Lnor/this/one;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800587 };
588 std::string input_file_contents;
589 for (std::string& class_name : class_names) {
590 input_file_contents += class_name + std::string("\n");
591 }
592 std::string output_file_contents;
593 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
594 std::string expected_contents("");
595 ASSERT_EQ(output_file_contents, expected_contents);
596}
597
Calin Juravlee0ac1152017-02-13 19:03:47 -0800598TEST_F(ProfileAssistantTest, TestProfileCreateInlineCache) {
599 // Create the profile content.
600 std::vector<std::string> methods = {
601 "LTestInline;->inlineMonomorphic(LSuper;)I+LSubA;",
602 "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;",
603 "LTestInline;->inlineMegamorphic(LSuper;)I+LSubA;,LSubB;,LSubC;,LSubD;,LSubE;",
Calin Juravle589e71e2017-03-03 16:05:05 -0800604 "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types",
Calin Juravlee0ac1152017-02-13 19:03:47 -0800605 "LTestInline;->noInlineCache(LSuper;)I"
606 };
607 std::string input_file_contents;
608 for (std::string& m : methods) {
609 input_file_contents += m + std::string("\n");
610 }
611
612 // Create the profile and save it to disk.
613 ScratchFile profile_file;
614 ASSERT_TRUE(CreateProfile(input_file_contents,
615 profile_file.GetFilename(),
616 GetTestDexFileName("ProfileTestMultiDex")));
617
618 // Load the profile from disk.
619 ProfileCompilationInfo info;
620 profile_file.GetFile()->ResetOffset();
621 ASSERT_TRUE(info.Load(GetFd(profile_file)));
622
623 // Load the dex files and verify that the profile contains the expected methods info.
624 ScopedObjectAccess soa(Thread::Current());
625 jobject class_loader = LoadDex("ProfileTestMultiDex");
626 ASSERT_NE(class_loader, nullptr);
627
628 mirror::Class* sub_a = GetClass(class_loader, "LSubA;");
629 mirror::Class* sub_b = GetClass(class_loader, "LSubB;");
630 mirror::Class* sub_c = GetClass(class_loader, "LSubC;");
631
632 ASSERT_TRUE(sub_a != nullptr);
633 ASSERT_TRUE(sub_b != nullptr);
634 ASSERT_TRUE(sub_c != nullptr);
635
636 {
637 // Verify that method inlineMonomorphic has the expected inline caches and nothing else.
638 ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader,
639 "LTestInline;",
640 "inlineMonomorphic");
641 ASSERT_TRUE(inline_monomorphic != nullptr);
642 std::set<mirror::Class*> expected_monomorphic;
643 expected_monomorphic.insert(sub_a);
Calin Juravle589e71e2017-03-03 16:05:05 -0800644 AssertInlineCaches(inline_monomorphic,
645 expected_monomorphic,
646 info,
647 /*megamorphic*/false,
648 /*missing_types*/false);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800649 }
650
651 {
652 // Verify that method inlinePolymorphic has the expected inline caches and nothing else.
653 ArtMethod* inline_polymorhic = GetVirtualMethod(class_loader,
654 "LTestInline;",
655 "inlinePolymorphic");
656 ASSERT_TRUE(inline_polymorhic != nullptr);
657 std::set<mirror::Class*> expected_polymorphic;
658 expected_polymorphic.insert(sub_a);
659 expected_polymorphic.insert(sub_b);
660 expected_polymorphic.insert(sub_c);
Calin Juravle589e71e2017-03-03 16:05:05 -0800661 AssertInlineCaches(inline_polymorhic,
662 expected_polymorphic,
663 info,
664 /*megamorphic*/false,
665 /*missing_types*/false);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800666 }
667
668 {
669 // Verify that method inlineMegamorphic has the expected inline caches and nothing else.
670 ArtMethod* inline_megamorphic = GetVirtualMethod(class_loader,
671 "LTestInline;",
672 "inlineMegamorphic");
673 ASSERT_TRUE(inline_megamorphic != nullptr);
674 std::set<mirror::Class*> expected_megamorphic;
Calin Juravle589e71e2017-03-03 16:05:05 -0800675 AssertInlineCaches(inline_megamorphic,
676 expected_megamorphic,
677 info,
678 /*megamorphic*/true,
679 /*missing_types*/false);
680 }
681
682 {
683 // Verify that method inlineMegamorphic has the expected inline caches and nothing else.
684 ArtMethod* inline_missing_types = GetVirtualMethod(class_loader,
685 "LTestInline;",
686 "inlineMissingTypes");
687 ASSERT_TRUE(inline_missing_types != nullptr);
688 std::set<mirror::Class*> expected_missing_Types;
689 AssertInlineCaches(inline_missing_types,
690 expected_missing_Types,
691 info,
692 /*megamorphic*/false,
693 /*missing_types*/true);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800694 }
695
696 {
697 // Verify that method noInlineCache has no inline caches in the profile.
698 ArtMethod* no_inline_cache = GetVirtualMethod(class_loader, "LTestInline;", "noInlineCache");
699 ASSERT_TRUE(no_inline_cache != nullptr);
Calin Juravlecc3171a2017-05-19 16:47:53 -0700700 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi_no_inline_cache =
701 info.GetMethod(no_inline_cache->GetDexFile()->GetLocation(),
702 no_inline_cache->GetDexFile()->GetLocationChecksum(),
703 no_inline_cache->GetDexMethodIndex());
704 ASSERT_TRUE(pmi_no_inline_cache != nullptr);
705 ASSERT_TRUE(pmi_no_inline_cache->inline_caches.empty());
Calin Juravlee0ac1152017-02-13 19:03:47 -0800706 }
707}
708
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700709TEST_F(ProfileAssistantTest, MergeProfilesWithDifferentDexOrder) {
710 ScratchFile profile1;
711 ScratchFile reference_profile;
712
713 std::vector<int> profile_fds({GetFd(profile1)});
714 int reference_profile_fd = GetFd(reference_profile);
715
716 // The new profile info will contain the methods with indices 0-100.
717 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
718 ProfileCompilationInfo info1;
719 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1,
720 /*start_method_index*/0, /*reverse_dex_write_order*/false);
721
722 // The reference profile info will contain the methods with indices 50-150.
723 // When setting up the profile reverse the order in which the dex files
724 // are added to the profile. This will verify that profman merges profiles
725 // with a different dex order correctly.
726 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
727 ProfileCompilationInfo reference_info;
728 SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
729 &reference_info, kNumberOfMethodsToEnableCompilation / 2, /*reverse_dex_write_order*/true);
730
731 // We should advise compilation.
732 ASSERT_EQ(ProfileAssistant::kCompile,
733 ProcessProfiles(profile_fds, reference_profile_fd));
734
735 // The resulting compilation info must be equal to the merge of the inputs.
736 ProfileCompilationInfo result;
737 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
738 ASSERT_TRUE(result.Load(reference_profile_fd));
739
740 ProfileCompilationInfo expected;
741 ASSERT_TRUE(expected.MergeWith(reference_info));
742 ASSERT_TRUE(expected.MergeWith(info1));
743 ASSERT_TRUE(expected.Equals(result));
744
745 // The information from profile must remain the same.
746 CheckProfileInfo(profile1, info1);
747}
748
Calin Juravle2e2db782016-02-23 12:00:03 +0000749} // namespace art