blob: 94f6e708fe4fb64860922c877ac760833f0706a7 [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 Juravlee0ac1152017-02-13 19:03:47 -080024#include "mirror/class-inl.h"
Mathieu Chartierd808e8b2017-03-21 13:37:41 -070025#include "obj_ptr-inl.h"
Calin Juravlee0ac1152017-02-13 19:03:47 -080026#include "profile_assistant.h"
27#include "scoped_thread_state_change-inl.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000028#include "utils.h"
29
30namespace art {
31
32class ProfileAssistantTest : public CommonRuntimeTest {
33 protected:
34 void SetupProfile(const std::string& id,
35 uint32_t checksum,
36 uint16_t number_of_methods,
Calin Juravlec824b512016-03-29 20:33:33 +010037 uint16_t number_of_classes,
Calin Juravle2e2db782016-02-23 12:00:03 +000038 const ScratchFile& profile,
39 ProfileCompilationInfo* info,
Calin Juravlecea9e9d2017-03-23 19:04:59 -070040 uint16_t start_method_index = 0,
41 bool reverse_dex_write_order = false) {
Calin Juravle2e2db782016-02-23 12:00:03 +000042 std::string dex_location1 = "location1" + id;
43 uint32_t dex_location_checksum1 = checksum;
44 std::string dex_location2 = "location2" + id;
45 uint32_t dex_location_checksum2 = 10 * checksum;
46 for (uint16_t i = start_method_index; i < start_method_index + number_of_methods; i++) {
Calin Juravlecea9e9d2017-03-23 19:04:59 -070047 // reverse_dex_write_order controls the order in which the dex files will be added to
48 // the profile and thus written to disk.
49 ProfileCompilationInfo::OfflineProfileMethodInfo pmi =
50 GetOfflineProfileMethodInfo(dex_location1, dex_location_checksum1,
51 dex_location2, dex_location_checksum2);
52 if (reverse_dex_write_order) {
53 ASSERT_TRUE(info->AddMethod(dex_location2, dex_location_checksum2, i, pmi));
54 ASSERT_TRUE(info->AddMethod(dex_location1, dex_location_checksum1, i, pmi));
55 } else {
56 ASSERT_TRUE(info->AddMethod(dex_location1, dex_location_checksum1, i, pmi));
57 ASSERT_TRUE(info->AddMethod(dex_location2, dex_location_checksum2, i, pmi));
58 }
Calin Juravle2e2db782016-02-23 12:00:03 +000059 }
Calin Juravlec824b512016-03-29 20:33:33 +010060 for (uint16_t i = 0; i < number_of_classes; i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -080061 ASSERT_TRUE(info->AddClassIndex(dex_location1, dex_location_checksum1, dex::TypeIndex(i)));
Calin Juravlec824b512016-03-29 20:33:33 +010062 }
63
Calin Juravle2e2db782016-02-23 12:00:03 +000064 ASSERT_TRUE(info->Save(GetFd(profile)));
65 ASSERT_EQ(0, profile.GetFile()->Flush());
66 ASSERT_TRUE(profile.GetFile()->ResetOffset());
67 }
68
Calin Juravlecea9e9d2017-03-23 19:04:59 -070069 ProfileCompilationInfo::OfflineProfileMethodInfo GetOfflineProfileMethodInfo(
70 const std::string& dex_location1, uint32_t dex_checksum1,
71 const std::string& dex_location2, uint32_t dex_checksum2) {
72 ProfileCompilationInfo::OfflineProfileMethodInfo pmi;
73 pmi.dex_references.emplace_back(dex_location1, dex_checksum1);
74 pmi.dex_references.emplace_back(dex_location2, dex_checksum2);
75
76 // Monomorphic
77 for (uint16_t dex_pc = 0; dex_pc < 11; dex_pc++) {
78 ProfileCompilationInfo::DexPcData dex_pc_data;
79 dex_pc_data.AddClass(0, dex::TypeIndex(0));
80 pmi.inline_caches.Put(dex_pc, dex_pc_data);
81 }
82 // Polymorphic
83 for (uint16_t dex_pc = 11; dex_pc < 22; dex_pc++) {
84 ProfileCompilationInfo::DexPcData dex_pc_data;
85 dex_pc_data.AddClass(0, dex::TypeIndex(0));
86 dex_pc_data.AddClass(1, dex::TypeIndex(1));
87
88 pmi.inline_caches.Put(dex_pc, dex_pc_data);
89 }
90 // Megamorphic
91 for (uint16_t dex_pc = 22; dex_pc < 33; dex_pc++) {
92 ProfileCompilationInfo::DexPcData dex_pc_data;
93 dex_pc_data.SetIsMegamorphic();
94 pmi.inline_caches.Put(dex_pc, dex_pc_data);
95 }
96 // Missing types
97 for (uint16_t dex_pc = 33; dex_pc < 44; dex_pc++) {
98 ProfileCompilationInfo::DexPcData dex_pc_data;
99 dex_pc_data.SetIsMissingTypes();
100 pmi.inline_caches.Put(dex_pc, dex_pc_data);
101 }
102
103 return pmi;
104 }
105
Calin Juravle2e2db782016-02-23 12:00:03 +0000106 int GetFd(const ScratchFile& file) const {
107 return static_cast<int>(file.GetFd());
108 }
109
110 void CheckProfileInfo(ScratchFile& file, const ProfileCompilationInfo& info) {
111 ProfileCompilationInfo file_info;
112 ASSERT_TRUE(file.GetFile()->ResetOffset());
113 ASSERT_TRUE(file_info.Load(GetFd(file)));
114 ASSERT_TRUE(file_info.Equals(info));
115 }
116
Calin Juravle7bcdb532016-06-07 16:14:47 +0100117 std::string GetProfmanCmd() {
Calin Juravle2e2db782016-02-23 12:00:03 +0000118 std::string file_path = GetTestAndroidRoot();
Calin Juravlede4fb632016-02-23 16:53:30 +0000119 file_path += "/bin/profman";
Calin Juravle2e2db782016-02-23 12:00:03 +0000120 if (kIsDebugBuild) {
121 file_path += "d";
122 }
Calin Juravle7bcdb532016-06-07 16:14:47 +0100123 EXPECT_TRUE(OS::FileExists(file_path.c_str()))
124 << file_path << " should be a valid file path";
125 return file_path;
126 }
127 // Runs test with given arguments.
128 int ProcessProfiles(const std::vector<int>& profiles_fd, int reference_profile_fd) {
129 std::string profman_cmd = GetProfmanCmd();
Calin Juravle2e2db782016-02-23 12:00:03 +0000130 std::vector<std::string> argv_str;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100131 argv_str.push_back(profman_cmd);
Calin Juravle2e2db782016-02-23 12:00:03 +0000132 for (size_t k = 0; k < profiles_fd.size(); k++) {
133 argv_str.push_back("--profile-file-fd=" + std::to_string(profiles_fd[k]));
134 }
135 argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile_fd));
136
137 std::string error;
138 return ExecAndReturnCode(argv_str, &error);
139 }
Calin Juravle7bcdb532016-06-07 16:14:47 +0100140
141 bool GenerateTestProfile(const std::string& filename) {
142 std::string profman_cmd = GetProfmanCmd();
143 std::vector<std::string> argv_str;
144 argv_str.push_back(profman_cmd);
145 argv_str.push_back("--generate-test-profile=" + filename);
146 std::string error;
147 return ExecAndReturnCode(argv_str, &error);
148 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800149
Jeff Haof0a31f82017-03-27 15:50:37 -0700150 bool GenerateTestProfileWithInputDex(const std::string& filename) {
151 std::string profman_cmd = GetProfmanCmd();
152 std::vector<std::string> argv_str;
153 argv_str.push_back(profman_cmd);
154 argv_str.push_back("--generate-test-profile=" + filename);
155 argv_str.push_back("--generate-test-profile-seed=0");
156 argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]);
157 argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]);
158 std::string error;
159 return ExecAndReturnCode(argv_str, &error);
160 }
161
Calin Juravlee0ac1152017-02-13 19:03:47 -0800162 bool CreateProfile(std::string profile_file_contents,
163 const std::string& filename,
164 const std::string& dex_location) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800165 ScratchFile class_names_file;
166 File* file = class_names_file.GetFile();
Calin Juravlee0ac1152017-02-13 19:03:47 -0800167 EXPECT_TRUE(file->WriteFully(profile_file_contents.c_str(), profile_file_contents.length()));
David Sehr7c80f2d2017-02-07 16:47:58 -0800168 EXPECT_EQ(0, file->Flush());
169 EXPECT_TRUE(file->ResetOffset());
170 std::string profman_cmd = GetProfmanCmd();
171 std::vector<std::string> argv_str;
172 argv_str.push_back(profman_cmd);
173 argv_str.push_back("--create-profile-from=" + class_names_file.GetFilename());
174 argv_str.push_back("--reference-profile-file=" + filename);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800175 argv_str.push_back("--apk=" + dex_location);
176 argv_str.push_back("--dex-location=" + dex_location);
David Sehr7c80f2d2017-02-07 16:47:58 -0800177 std::string error;
178 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0);
179 return true;
180 }
181
182 bool DumpClasses(const std::string& filename, std::string* file_contents) {
183 ScratchFile class_names_file;
184 std::string profman_cmd = GetProfmanCmd();
185 std::vector<std::string> argv_str;
186 argv_str.push_back(profman_cmd);
187 argv_str.push_back("--dump-classes");
188 argv_str.push_back("--profile-file=" + filename);
189 argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800190 argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]);
David Sehr7c80f2d2017-02-07 16:47:58 -0800191 argv_str.push_back("--dump-output-to-fd=" + std::to_string(GetFd(class_names_file)));
192 std::string error;
193 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0);
194 File* file = class_names_file.GetFile();
195 EXPECT_EQ(0, file->Flush());
196 EXPECT_TRUE(file->ResetOffset());
197 int64_t length = file->GetLength();
198 std::unique_ptr<char[]> buf(new char[length]);
199 EXPECT_EQ(file->Read(buf.get(), length, 0), length);
200 *file_contents = std::string(buf.get(), length);
201 return true;
202 }
203
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700204 bool CreateAndDump(const std::string& input_file_contents,
205 std::string* output_file_contents) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800206 ScratchFile profile_file;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800207 EXPECT_TRUE(CreateProfile(input_file_contents,
208 profile_file.GetFilename(),
209 GetLibCoreDexFileNames()[0]));
David Sehr7c80f2d2017-02-07 16:47:58 -0800210 profile_file.GetFile()->ResetOffset();
211 EXPECT_TRUE(DumpClasses(profile_file.GetFilename(), output_file_contents));
212 return true;
213 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800214
215 mirror::Class* GetClass(jobject class_loader, const std::string& clazz) {
216 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
217 Thread* self = Thread::Current();
218 ScopedObjectAccess soa(self);
219 StackHandleScope<1> hs(self);
220 Handle<mirror::ClassLoader> h_loader(
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700221 hs.NewHandle(ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(class_loader))));
Calin Juravlee0ac1152017-02-13 19:03:47 -0800222 return class_linker->FindClass(self, clazz.c_str(), h_loader);
223 }
224
225 ArtMethod* GetVirtualMethod(jobject class_loader,
226 const std::string& clazz,
227 const std::string& name) {
228 mirror::Class* klass = GetClass(class_loader, clazz);
229 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
230 const auto pointer_size = class_linker->GetImagePointerSize();
231 ArtMethod* method = nullptr;
232 Thread* self = Thread::Current();
233 ScopedObjectAccess soa(self);
234 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
235 if (name == m.GetName()) {
236 EXPECT_TRUE(method == nullptr);
237 method = &m;
238 }
239 }
240 return method;
241 }
242
243 // Verify that given method has the expected inline caches and nothing else.
244 void AssertInlineCaches(ArtMethod* method,
245 const std::set<mirror::Class*>& expected_clases,
246 const ProfileCompilationInfo& info,
Calin Juravle589e71e2017-03-03 16:05:05 -0800247 bool is_megamorphic,
248 bool is_missing_types)
Calin Juravlee0ac1152017-02-13 19:03:47 -0800249 REQUIRES_SHARED(Locks::mutator_lock_) {
250 ProfileCompilationInfo::OfflineProfileMethodInfo pmi;
251 ASSERT_TRUE(info.GetMethod(method->GetDexFile()->GetLocation(),
252 method->GetDexFile()->GetLocationChecksum(),
253 method->GetDexMethodIndex(),
254 &pmi));
255 ASSERT_EQ(pmi.inline_caches.size(), 1u);
256 ProfileCompilationInfo::DexPcData dex_pc_data = pmi.inline_caches.begin()->second;
257
Calin Juravle589e71e2017-03-03 16:05:05 -0800258 ASSERT_EQ(dex_pc_data.is_megamorphic, is_megamorphic);
259 ASSERT_EQ(dex_pc_data.is_missing_types, is_missing_types);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800260 ASSERT_EQ(expected_clases.size(), dex_pc_data.classes.size());
261 size_t found = 0;
262 for (mirror::Class* it : expected_clases) {
263 for (const auto& class_ref : dex_pc_data.classes) {
264 ProfileCompilationInfo::DexReference dex_ref =
265 pmi.dex_references[class_ref.dex_profile_index];
266 if (dex_ref.MatchesDex(&(it->GetDexFile())) &&
267 class_ref.type_index == it->GetDexTypeIndex()) {
268 found++;
269 }
270 }
271 }
272
273 ASSERT_EQ(expected_clases.size(), found);
274 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000275};
276
277TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferences) {
278 ScratchFile profile1;
279 ScratchFile profile2;
280 ScratchFile reference_profile;
281
282 std::vector<int> profile_fds({
283 GetFd(profile1),
284 GetFd(profile2)});
285 int reference_profile_fd = GetFd(reference_profile);
286
287 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
288 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100289 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000290 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100291 SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000292
293 // We should advise compilation.
294 ASSERT_EQ(ProfileAssistant::kCompile,
295 ProcessProfiles(profile_fds, reference_profile_fd));
296 // The resulting compilation info must be equal to the merge of the inputs.
297 ProfileCompilationInfo result;
298 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
299 ASSERT_TRUE(result.Load(reference_profile_fd));
300
301 ProfileCompilationInfo expected;
Calin Juravle67265462016-03-18 16:23:40 +0000302 ASSERT_TRUE(expected.MergeWith(info1));
303 ASSERT_TRUE(expected.MergeWith(info2));
Calin Juravle2e2db782016-02-23 12:00:03 +0000304 ASSERT_TRUE(expected.Equals(result));
305
306 // The information from profiles must remain the same.
307 CheckProfileInfo(profile1, info1);
308 CheckProfileInfo(profile2, info2);
309}
310
Calin Juravlec824b512016-03-29 20:33:33 +0100311// TODO(calin): Add more tests for classes.
312TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferencesBecauseOfClasses) {
313 ScratchFile profile1;
314 ScratchFile reference_profile;
315
316 std::vector<int> profile_fds({
317 GetFd(profile1)});
318 int reference_profile_fd = GetFd(reference_profile);
319
320 const uint16_t kNumberOfClassesToEnableCompilation = 100;
321 ProfileCompilationInfo info1;
322 SetupProfile("p1", 1, 0, kNumberOfClassesToEnableCompilation, profile1, &info1);
323
324 // We should advise compilation.
325 ASSERT_EQ(ProfileAssistant::kCompile,
326 ProcessProfiles(profile_fds, reference_profile_fd));
327 // The resulting compilation info must be equal to the merge of the inputs.
328 ProfileCompilationInfo result;
329 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
330 ASSERT_TRUE(result.Load(reference_profile_fd));
331
332 ProfileCompilationInfo expected;
333 ASSERT_TRUE(expected.MergeWith(info1));
334 ASSERT_TRUE(expected.Equals(result));
335
336 // The information from profiles must remain the same.
337 CheckProfileInfo(profile1, info1);
338}
339
Calin Juravle2e2db782016-02-23 12:00:03 +0000340TEST_F(ProfileAssistantTest, AdviseCompilationNonEmptyReferences) {
341 ScratchFile profile1;
342 ScratchFile profile2;
343 ScratchFile reference_profile;
344
345 std::vector<int> profile_fds({
346 GetFd(profile1),
347 GetFd(profile2)});
348 int reference_profile_fd = GetFd(reference_profile);
349
350 // The new profile info will contain the methods with indices 0-100.
351 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
352 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100353 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000354 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100355 SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000356
357
358 // The reference profile info will contain the methods with indices 50-150.
359 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
360 ProfileCompilationInfo reference_info;
Calin Juravlec824b512016-03-29 20:33:33 +0100361 SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
Calin Juravle2e2db782016-02-23 12:00:03 +0000362 &reference_info, kNumberOfMethodsToEnableCompilation / 2);
363
364 // We should advise compilation.
365 ASSERT_EQ(ProfileAssistant::kCompile,
366 ProcessProfiles(profile_fds, reference_profile_fd));
367
368 // The resulting compilation info must be equal to the merge of the inputs
369 ProfileCompilationInfo result;
370 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
371 ASSERT_TRUE(result.Load(reference_profile_fd));
372
373 ProfileCompilationInfo expected;
Calin Juravle67265462016-03-18 16:23:40 +0000374 ASSERT_TRUE(expected.MergeWith(info1));
375 ASSERT_TRUE(expected.MergeWith(info2));
376 ASSERT_TRUE(expected.MergeWith(reference_info));
Calin Juravle2e2db782016-02-23 12:00:03 +0000377 ASSERT_TRUE(expected.Equals(result));
378
379 // The information from profiles must remain the same.
380 CheckProfileInfo(profile1, info1);
381 CheckProfileInfo(profile2, info2);
382}
383
384TEST_F(ProfileAssistantTest, DoNotAdviseCompilation) {
385 ScratchFile profile1;
386 ScratchFile profile2;
387 ScratchFile reference_profile;
388
389 std::vector<int> profile_fds({
390 GetFd(profile1),
391 GetFd(profile2)});
392 int reference_profile_fd = GetFd(reference_profile);
393
394 const uint16_t kNumberOfMethodsToSkipCompilation = 1;
395 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100396 SetupProfile("p1", 1, kNumberOfMethodsToSkipCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000397 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100398 SetupProfile("p2", 2, kNumberOfMethodsToSkipCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000399
400 // We should not advise compilation.
401 ASSERT_EQ(ProfileAssistant::kSkipCompilation,
402 ProcessProfiles(profile_fds, reference_profile_fd));
403
404 // The information from profiles must remain the same.
405 ProfileCompilationInfo file_info1;
406 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
407 ASSERT_TRUE(file_info1.Load(GetFd(profile1)));
408 ASSERT_TRUE(file_info1.Equals(info1));
409
410 ProfileCompilationInfo file_info2;
411 ASSERT_TRUE(profile2.GetFile()->ResetOffset());
412 ASSERT_TRUE(file_info2.Load(GetFd(profile2)));
413 ASSERT_TRUE(file_info2.Equals(info2));
414
415 // Reference profile files must remain empty.
416 ASSERT_EQ(0, reference_profile.GetFile()->GetLength());
417
418 // The information from profiles must remain the same.
419 CheckProfileInfo(profile1, info1);
420 CheckProfileInfo(profile2, info2);
421}
422
423TEST_F(ProfileAssistantTest, FailProcessingBecauseOfProfiles) {
424 ScratchFile profile1;
425 ScratchFile profile2;
426 ScratchFile reference_profile;
427
428 std::vector<int> profile_fds({
429 GetFd(profile1),
430 GetFd(profile2)});
431 int reference_profile_fd = GetFd(reference_profile);
432
433 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
434 // Assign different hashes for the same dex file. This will make merging of information to fail.
435 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100436 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000437 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100438 SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000439
440 // We should fail processing.
441 ASSERT_EQ(ProfileAssistant::kErrorBadProfiles,
442 ProcessProfiles(profile_fds, reference_profile_fd));
443
444 // The information from profiles must remain the same.
445 CheckProfileInfo(profile1, info1);
446 CheckProfileInfo(profile2, info2);
447
448 // Reference profile files must still remain empty.
449 ASSERT_EQ(0, reference_profile.GetFile()->GetLength());
450}
451
452TEST_F(ProfileAssistantTest, FailProcessingBecauseOfReferenceProfiles) {
453 ScratchFile profile1;
454 ScratchFile reference_profile;
455
456 std::vector<int> profile_fds({
457 GetFd(profile1)});
458 int reference_profile_fd = GetFd(reference_profile);
459
460 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
461 // Assign different hashes for the same dex file. This will make merging of information to fail.
462 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100463 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000464 ProfileCompilationInfo reference_info;
Calin Juravlec824b512016-03-29 20:33:33 +0100465 SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, reference_profile, &reference_info);
Calin Juravle2e2db782016-02-23 12:00:03 +0000466
467 // We should not advise compilation.
468 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
469 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
470 ASSERT_EQ(ProfileAssistant::kErrorBadProfiles,
471 ProcessProfiles(profile_fds, reference_profile_fd));
472
473 // The information from profiles must remain the same.
474 CheckProfileInfo(profile1, info1);
475}
476
Calin Juravle7bcdb532016-06-07 16:14:47 +0100477TEST_F(ProfileAssistantTest, TestProfileGeneration) {
478 ScratchFile profile;
479 // Generate a test profile.
480 GenerateTestProfile(profile.GetFilename());
481
482 // Verify that the generated profile is valid and can be loaded.
483 ASSERT_TRUE(profile.GetFile()->ResetOffset());
484 ProfileCompilationInfo info;
485 ASSERT_TRUE(info.Load(GetFd(profile)));
486}
487
Jeff Haof0a31f82017-03-27 15:50:37 -0700488TEST_F(ProfileAssistantTest, TestProfileGenerationWithIndexDex) {
489 ScratchFile profile;
490 // Generate a test profile passing in a dex file as reference.
491 GenerateTestProfileWithInputDex(profile.GetFilename());
492
493 // Verify that the generated profile is valid and can be loaded.
494 ASSERT_TRUE(profile.GetFile()->ResetOffset());
495 ProfileCompilationInfo info;
496 ASSERT_TRUE(info.Load(GetFd(profile)));
497}
498
David Sehr7c80f2d2017-02-07 16:47:58 -0800499TEST_F(ProfileAssistantTest, TestProfileCreationAllMatch) {
500 // Class names put here need to be in sorted order.
501 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800502 "Ljava/lang/Comparable;",
503 "Ljava/lang/Math;",
504 "Ljava/lang/Object;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800505 };
506 std::string input_file_contents;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800507 std::string expected_contents;
David Sehr7c80f2d2017-02-07 16:47:58 -0800508 for (std::string& class_name : class_names) {
509 input_file_contents += class_name + std::string("\n");
Calin Juravlee0ac1152017-02-13 19:03:47 -0800510 expected_contents += DescriptorToDot(class_name.c_str()) +
511 std::string("\n");
David Sehr7c80f2d2017-02-07 16:47:58 -0800512 }
513 std::string output_file_contents;
514 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
Calin Juravlee0ac1152017-02-13 19:03:47 -0800515 ASSERT_EQ(output_file_contents, expected_contents);
David Sehr7c80f2d2017-02-07 16:47:58 -0800516}
517
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700518TEST_F(ProfileAssistantTest, TestProfileCreationGenerateMethods) {
519 // Class names put here need to be in sorted order.
520 std::vector<std::string> class_names = {
521 "Ljava/lang/Math;->*",
522 };
523 std::string input_file_contents;
524 std::string expected_contents;
525 for (std::string& class_name : class_names) {
526 input_file_contents += class_name + std::string("\n");
527 expected_contents += DescriptorToDot(class_name.c_str()) +
528 std::string("\n");
529 }
530 std::string output_file_contents;
531 ScratchFile profile_file;
532 EXPECT_TRUE(CreateProfile(input_file_contents,
533 profile_file.GetFilename(),
534 GetLibCoreDexFileNames()[0]));
535 ProfileCompilationInfo info;
536 profile_file.GetFile()->ResetOffset();
537 ASSERT_TRUE(info.Load(GetFd(profile_file)));
538 // Verify that the profile has matching methods.
539 ScopedObjectAccess soa(Thread::Current());
540 ObjPtr<mirror::Class> klass = GetClass(nullptr, "Ljava/lang/Math;");
541 ASSERT_TRUE(klass != nullptr);
542 size_t method_count = 0;
543 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
544 if (!method.IsCopied() && method.GetCodeItem() != nullptr) {
545 ++method_count;
546 ProfileCompilationInfo::OfflineProfileMethodInfo pmi;
547 ASSERT_TRUE(info.GetMethod(method.GetDexFile()->GetLocation(),
548 method.GetDexFile()->GetLocationChecksum(),
549 method.GetDexMethodIndex(),
550 &pmi));
551 }
552 }
553 EXPECT_GT(method_count, 0u);
554}
555
David Sehr7c80f2d2017-02-07 16:47:58 -0800556TEST_F(ProfileAssistantTest, TestProfileCreationOneNotMatched) {
557 // Class names put here need to be in sorted order.
558 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800559 "Ldoesnt/match/this/one;",
560 "Ljava/lang/Comparable;",
561 "Ljava/lang/Object;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800562 };
563 std::string input_file_contents;
564 for (std::string& class_name : class_names) {
565 input_file_contents += class_name + std::string("\n");
566 }
567 std::string output_file_contents;
568 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
569 std::string expected_contents =
Calin Juravlee0ac1152017-02-13 19:03:47 -0800570 DescriptorToDot(class_names[1].c_str()) + std::string("\n") +
571 DescriptorToDot(class_names[2].c_str()) + std::string("\n");
David Sehr7c80f2d2017-02-07 16:47:58 -0800572 ASSERT_EQ(output_file_contents, expected_contents);
573}
574
575TEST_F(ProfileAssistantTest, TestProfileCreationNoneMatched) {
576 // Class names put here need to be in sorted order.
577 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800578 "Ldoesnt/match/this/one;",
579 "Ldoesnt/match/this/one/either;",
580 "Lnor/this/one;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800581 };
582 std::string input_file_contents;
583 for (std::string& class_name : class_names) {
584 input_file_contents += class_name + std::string("\n");
585 }
586 std::string output_file_contents;
587 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
588 std::string expected_contents("");
589 ASSERT_EQ(output_file_contents, expected_contents);
590}
591
Calin Juravlee0ac1152017-02-13 19:03:47 -0800592TEST_F(ProfileAssistantTest, TestProfileCreateInlineCache) {
593 // Create the profile content.
594 std::vector<std::string> methods = {
595 "LTestInline;->inlineMonomorphic(LSuper;)I+LSubA;",
596 "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;",
597 "LTestInline;->inlineMegamorphic(LSuper;)I+LSubA;,LSubB;,LSubC;,LSubD;,LSubE;",
Calin Juravle589e71e2017-03-03 16:05:05 -0800598 "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types",
Calin Juravlee0ac1152017-02-13 19:03:47 -0800599 "LTestInline;->noInlineCache(LSuper;)I"
600 };
601 std::string input_file_contents;
602 for (std::string& m : methods) {
603 input_file_contents += m + std::string("\n");
604 }
605
606 // Create the profile and save it to disk.
607 ScratchFile profile_file;
608 ASSERT_TRUE(CreateProfile(input_file_contents,
609 profile_file.GetFilename(),
610 GetTestDexFileName("ProfileTestMultiDex")));
611
612 // Load the profile from disk.
613 ProfileCompilationInfo info;
614 profile_file.GetFile()->ResetOffset();
615 ASSERT_TRUE(info.Load(GetFd(profile_file)));
616
617 // Load the dex files and verify that the profile contains the expected methods info.
618 ScopedObjectAccess soa(Thread::Current());
619 jobject class_loader = LoadDex("ProfileTestMultiDex");
620 ASSERT_NE(class_loader, nullptr);
621
622 mirror::Class* sub_a = GetClass(class_loader, "LSubA;");
623 mirror::Class* sub_b = GetClass(class_loader, "LSubB;");
624 mirror::Class* sub_c = GetClass(class_loader, "LSubC;");
625
626 ASSERT_TRUE(sub_a != nullptr);
627 ASSERT_TRUE(sub_b != nullptr);
628 ASSERT_TRUE(sub_c != nullptr);
629
630 {
631 // Verify that method inlineMonomorphic has the expected inline caches and nothing else.
632 ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader,
633 "LTestInline;",
634 "inlineMonomorphic");
635 ASSERT_TRUE(inline_monomorphic != nullptr);
636 std::set<mirror::Class*> expected_monomorphic;
637 expected_monomorphic.insert(sub_a);
Calin Juravle589e71e2017-03-03 16:05:05 -0800638 AssertInlineCaches(inline_monomorphic,
639 expected_monomorphic,
640 info,
641 /*megamorphic*/false,
642 /*missing_types*/false);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800643 }
644
645 {
646 // Verify that method inlinePolymorphic has the expected inline caches and nothing else.
647 ArtMethod* inline_polymorhic = GetVirtualMethod(class_loader,
648 "LTestInline;",
649 "inlinePolymorphic");
650 ASSERT_TRUE(inline_polymorhic != nullptr);
651 std::set<mirror::Class*> expected_polymorphic;
652 expected_polymorphic.insert(sub_a);
653 expected_polymorphic.insert(sub_b);
654 expected_polymorphic.insert(sub_c);
Calin Juravle589e71e2017-03-03 16:05:05 -0800655 AssertInlineCaches(inline_polymorhic,
656 expected_polymorphic,
657 info,
658 /*megamorphic*/false,
659 /*missing_types*/false);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800660 }
661
662 {
663 // Verify that method inlineMegamorphic has the expected inline caches and nothing else.
664 ArtMethod* inline_megamorphic = GetVirtualMethod(class_loader,
665 "LTestInline;",
666 "inlineMegamorphic");
667 ASSERT_TRUE(inline_megamorphic != nullptr);
668 std::set<mirror::Class*> expected_megamorphic;
Calin Juravle589e71e2017-03-03 16:05:05 -0800669 AssertInlineCaches(inline_megamorphic,
670 expected_megamorphic,
671 info,
672 /*megamorphic*/true,
673 /*missing_types*/false);
674 }
675
676 {
677 // Verify that method inlineMegamorphic has the expected inline caches and nothing else.
678 ArtMethod* inline_missing_types = GetVirtualMethod(class_loader,
679 "LTestInline;",
680 "inlineMissingTypes");
681 ASSERT_TRUE(inline_missing_types != nullptr);
682 std::set<mirror::Class*> expected_missing_Types;
683 AssertInlineCaches(inline_missing_types,
684 expected_missing_Types,
685 info,
686 /*megamorphic*/false,
687 /*missing_types*/true);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800688 }
689
690 {
691 // Verify that method noInlineCache has no inline caches in the profile.
692 ArtMethod* no_inline_cache = GetVirtualMethod(class_loader, "LTestInline;", "noInlineCache");
693 ASSERT_TRUE(no_inline_cache != nullptr);
694 ProfileCompilationInfo::OfflineProfileMethodInfo pmi_no_inline_cache;
695 ASSERT_TRUE(info.GetMethod(no_inline_cache->GetDexFile()->GetLocation(),
696 no_inline_cache->GetDexFile()->GetLocationChecksum(),
697 no_inline_cache->GetDexMethodIndex(),
698 &pmi_no_inline_cache));
699 ASSERT_TRUE(pmi_no_inline_cache.inline_caches.empty());
700 }
701}
702
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700703TEST_F(ProfileAssistantTest, MergeProfilesWithDifferentDexOrder) {
704 ScratchFile profile1;
705 ScratchFile reference_profile;
706
707 std::vector<int> profile_fds({GetFd(profile1)});
708 int reference_profile_fd = GetFd(reference_profile);
709
710 // The new profile info will contain the methods with indices 0-100.
711 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
712 ProfileCompilationInfo info1;
713 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1,
714 /*start_method_index*/0, /*reverse_dex_write_order*/false);
715
716 // The reference profile info will contain the methods with indices 50-150.
717 // When setting up the profile reverse the order in which the dex files
718 // are added to the profile. This will verify that profman merges profiles
719 // with a different dex order correctly.
720 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
721 ProfileCompilationInfo reference_info;
722 SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
723 &reference_info, kNumberOfMethodsToEnableCompilation / 2, /*reverse_dex_write_order*/true);
724
725 // We should advise compilation.
726 ASSERT_EQ(ProfileAssistant::kCompile,
727 ProcessProfiles(profile_fds, reference_profile_fd));
728
729 // The resulting compilation info must be equal to the merge of the inputs.
730 ProfileCompilationInfo result;
731 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
732 ASSERT_TRUE(result.Load(reference_profile_fd));
733
734 ProfileCompilationInfo expected;
735 ASSERT_TRUE(expected.MergeWith(reference_info));
736 ASSERT_TRUE(expected.MergeWith(info1));
737 ASSERT_TRUE(expected.Equals(result));
738
739 // The information from profile must remain the same.
740 CheckProfileInfo(profile1, info1);
741}
742
Calin Juravle2e2db782016-02-23 12:00:03 +0000743} // namespace art