blob: e2f622db42ce8a55455dbd4bb1e7b0d476b97ccf [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,
40 uint16_t start_method_index = 0) {
41 std::string dex_location1 = "location1" + id;
42 uint32_t dex_location_checksum1 = checksum;
43 std::string dex_location2 = "location2" + id;
44 uint32_t dex_location_checksum2 = 10 * checksum;
45 for (uint16_t i = start_method_index; i < start_method_index + number_of_methods; i++) {
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080046 ASSERT_TRUE(info->AddMethodIndex(dex_location1, dex_location_checksum1, i));
47 ASSERT_TRUE(info->AddMethodIndex(dex_location2, dex_location_checksum2, i));
Calin Juravle2e2db782016-02-23 12:00:03 +000048 }
Calin Juravlec824b512016-03-29 20:33:33 +010049 for (uint16_t i = 0; i < number_of_classes; i++) {
Andreas Gampea5b09a62016-11-17 15:21:22 -080050 ASSERT_TRUE(info->AddClassIndex(dex_location1, dex_location_checksum1, dex::TypeIndex(i)));
Calin Juravlec824b512016-03-29 20:33:33 +010051 }
52
Calin Juravle2e2db782016-02-23 12:00:03 +000053 ASSERT_TRUE(info->Save(GetFd(profile)));
54 ASSERT_EQ(0, profile.GetFile()->Flush());
55 ASSERT_TRUE(profile.GetFile()->ResetOffset());
56 }
57
58 int GetFd(const ScratchFile& file) const {
59 return static_cast<int>(file.GetFd());
60 }
61
62 void CheckProfileInfo(ScratchFile& file, const ProfileCompilationInfo& info) {
63 ProfileCompilationInfo file_info;
64 ASSERT_TRUE(file.GetFile()->ResetOffset());
65 ASSERT_TRUE(file_info.Load(GetFd(file)));
66 ASSERT_TRUE(file_info.Equals(info));
67 }
68
Calin Juravle7bcdb532016-06-07 16:14:47 +010069 std::string GetProfmanCmd() {
Calin Juravle2e2db782016-02-23 12:00:03 +000070 std::string file_path = GetTestAndroidRoot();
Calin Juravlede4fb632016-02-23 16:53:30 +000071 file_path += "/bin/profman";
Calin Juravle2e2db782016-02-23 12:00:03 +000072 if (kIsDebugBuild) {
73 file_path += "d";
74 }
Calin Juravle7bcdb532016-06-07 16:14:47 +010075 EXPECT_TRUE(OS::FileExists(file_path.c_str()))
76 << file_path << " should be a valid file path";
77 return file_path;
78 }
79 // Runs test with given arguments.
80 int ProcessProfiles(const std::vector<int>& profiles_fd, int reference_profile_fd) {
81 std::string profman_cmd = GetProfmanCmd();
Calin Juravle2e2db782016-02-23 12:00:03 +000082 std::vector<std::string> argv_str;
Calin Juravle7bcdb532016-06-07 16:14:47 +010083 argv_str.push_back(profman_cmd);
Calin Juravle2e2db782016-02-23 12:00:03 +000084 for (size_t k = 0; k < profiles_fd.size(); k++) {
85 argv_str.push_back("--profile-file-fd=" + std::to_string(profiles_fd[k]));
86 }
87 argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile_fd));
88
89 std::string error;
90 return ExecAndReturnCode(argv_str, &error);
91 }
Calin Juravle7bcdb532016-06-07 16:14:47 +010092
93 bool GenerateTestProfile(const std::string& filename) {
94 std::string profman_cmd = GetProfmanCmd();
95 std::vector<std::string> argv_str;
96 argv_str.push_back(profman_cmd);
97 argv_str.push_back("--generate-test-profile=" + filename);
98 std::string error;
99 return ExecAndReturnCode(argv_str, &error);
100 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800101
Jeff Haof0a31f82017-03-27 15:50:37 -0700102 bool GenerateTestProfileWithInputDex(const std::string& filename) {
103 std::string profman_cmd = GetProfmanCmd();
104 std::vector<std::string> argv_str;
105 argv_str.push_back(profman_cmd);
106 argv_str.push_back("--generate-test-profile=" + filename);
107 argv_str.push_back("--generate-test-profile-seed=0");
108 argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]);
109 argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]);
110 std::string error;
111 return ExecAndReturnCode(argv_str, &error);
112 }
113
Calin Juravlee0ac1152017-02-13 19:03:47 -0800114 bool CreateProfile(std::string profile_file_contents,
115 const std::string& filename,
116 const std::string& dex_location) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800117 ScratchFile class_names_file;
118 File* file = class_names_file.GetFile();
Calin Juravlee0ac1152017-02-13 19:03:47 -0800119 EXPECT_TRUE(file->WriteFully(profile_file_contents.c_str(), profile_file_contents.length()));
David Sehr7c80f2d2017-02-07 16:47:58 -0800120 EXPECT_EQ(0, file->Flush());
121 EXPECT_TRUE(file->ResetOffset());
122 std::string profman_cmd = GetProfmanCmd();
123 std::vector<std::string> argv_str;
124 argv_str.push_back(profman_cmd);
125 argv_str.push_back("--create-profile-from=" + class_names_file.GetFilename());
126 argv_str.push_back("--reference-profile-file=" + filename);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800127 argv_str.push_back("--apk=" + dex_location);
128 argv_str.push_back("--dex-location=" + dex_location);
David Sehr7c80f2d2017-02-07 16:47:58 -0800129 std::string error;
130 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0);
131 return true;
132 }
133
134 bool DumpClasses(const std::string& filename, std::string* file_contents) {
135 ScratchFile class_names_file;
136 std::string profman_cmd = GetProfmanCmd();
137 std::vector<std::string> argv_str;
138 argv_str.push_back(profman_cmd);
139 argv_str.push_back("--dump-classes");
140 argv_str.push_back("--profile-file=" + filename);
141 argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800142 argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]);
David Sehr7c80f2d2017-02-07 16:47:58 -0800143 argv_str.push_back("--dump-output-to-fd=" + std::to_string(GetFd(class_names_file)));
144 std::string error;
145 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0);
146 File* file = class_names_file.GetFile();
147 EXPECT_EQ(0, file->Flush());
148 EXPECT_TRUE(file->ResetOffset());
149 int64_t length = file->GetLength();
150 std::unique_ptr<char[]> buf(new char[length]);
151 EXPECT_EQ(file->Read(buf.get(), length, 0), length);
152 *file_contents = std::string(buf.get(), length);
153 return true;
154 }
155
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700156 bool CreateAndDump(const std::string& input_file_contents,
157 std::string* output_file_contents) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800158 ScratchFile profile_file;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800159 EXPECT_TRUE(CreateProfile(input_file_contents,
160 profile_file.GetFilename(),
161 GetLibCoreDexFileNames()[0]));
David Sehr7c80f2d2017-02-07 16:47:58 -0800162 profile_file.GetFile()->ResetOffset();
163 EXPECT_TRUE(DumpClasses(profile_file.GetFilename(), output_file_contents));
164 return true;
165 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800166
167 mirror::Class* GetClass(jobject class_loader, const std::string& clazz) {
168 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
169 Thread* self = Thread::Current();
170 ScopedObjectAccess soa(self);
171 StackHandleScope<1> hs(self);
172 Handle<mirror::ClassLoader> h_loader(
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700173 hs.NewHandle(ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(class_loader))));
Calin Juravlee0ac1152017-02-13 19:03:47 -0800174 return class_linker->FindClass(self, clazz.c_str(), h_loader);
175 }
176
177 ArtMethod* GetVirtualMethod(jobject class_loader,
178 const std::string& clazz,
179 const std::string& name) {
180 mirror::Class* klass = GetClass(class_loader, clazz);
181 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
182 const auto pointer_size = class_linker->GetImagePointerSize();
183 ArtMethod* method = nullptr;
184 Thread* self = Thread::Current();
185 ScopedObjectAccess soa(self);
186 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
187 if (name == m.GetName()) {
188 EXPECT_TRUE(method == nullptr);
189 method = &m;
190 }
191 }
192 return method;
193 }
194
195 // Verify that given method has the expected inline caches and nothing else.
196 void AssertInlineCaches(ArtMethod* method,
197 const std::set<mirror::Class*>& expected_clases,
198 const ProfileCompilationInfo& info,
Calin Juravle589e71e2017-03-03 16:05:05 -0800199 bool is_megamorphic,
200 bool is_missing_types)
Calin Juravlee0ac1152017-02-13 19:03:47 -0800201 REQUIRES_SHARED(Locks::mutator_lock_) {
202 ProfileCompilationInfo::OfflineProfileMethodInfo pmi;
203 ASSERT_TRUE(info.GetMethod(method->GetDexFile()->GetLocation(),
204 method->GetDexFile()->GetLocationChecksum(),
205 method->GetDexMethodIndex(),
206 &pmi));
207 ASSERT_EQ(pmi.inline_caches.size(), 1u);
208 ProfileCompilationInfo::DexPcData dex_pc_data = pmi.inline_caches.begin()->second;
209
Calin Juravle589e71e2017-03-03 16:05:05 -0800210 ASSERT_EQ(dex_pc_data.is_megamorphic, is_megamorphic);
211 ASSERT_EQ(dex_pc_data.is_missing_types, is_missing_types);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800212 ASSERT_EQ(expected_clases.size(), dex_pc_data.classes.size());
213 size_t found = 0;
214 for (mirror::Class* it : expected_clases) {
215 for (const auto& class_ref : dex_pc_data.classes) {
216 ProfileCompilationInfo::DexReference dex_ref =
217 pmi.dex_references[class_ref.dex_profile_index];
218 if (dex_ref.MatchesDex(&(it->GetDexFile())) &&
219 class_ref.type_index == it->GetDexTypeIndex()) {
220 found++;
221 }
222 }
223 }
224
225 ASSERT_EQ(expected_clases.size(), found);
226 }
Calin Juravle2e2db782016-02-23 12:00:03 +0000227};
228
229TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferences) {
230 ScratchFile profile1;
231 ScratchFile profile2;
232 ScratchFile reference_profile;
233
234 std::vector<int> profile_fds({
235 GetFd(profile1),
236 GetFd(profile2)});
237 int reference_profile_fd = GetFd(reference_profile);
238
239 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
240 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100241 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000242 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100243 SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000244
245 // We should advise compilation.
246 ASSERT_EQ(ProfileAssistant::kCompile,
247 ProcessProfiles(profile_fds, reference_profile_fd));
248 // The resulting compilation info must be equal to the merge of the inputs.
249 ProfileCompilationInfo result;
250 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
251 ASSERT_TRUE(result.Load(reference_profile_fd));
252
253 ProfileCompilationInfo expected;
Calin Juravle67265462016-03-18 16:23:40 +0000254 ASSERT_TRUE(expected.MergeWith(info1));
255 ASSERT_TRUE(expected.MergeWith(info2));
Calin Juravle2e2db782016-02-23 12:00:03 +0000256 ASSERT_TRUE(expected.Equals(result));
257
258 // The information from profiles must remain the same.
259 CheckProfileInfo(profile1, info1);
260 CheckProfileInfo(profile2, info2);
261}
262
Calin Juravlec824b512016-03-29 20:33:33 +0100263// TODO(calin): Add more tests for classes.
264TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferencesBecauseOfClasses) {
265 ScratchFile profile1;
266 ScratchFile reference_profile;
267
268 std::vector<int> profile_fds({
269 GetFd(profile1)});
270 int reference_profile_fd = GetFd(reference_profile);
271
272 const uint16_t kNumberOfClassesToEnableCompilation = 100;
273 ProfileCompilationInfo info1;
274 SetupProfile("p1", 1, 0, kNumberOfClassesToEnableCompilation, profile1, &info1);
275
276 // We should advise compilation.
277 ASSERT_EQ(ProfileAssistant::kCompile,
278 ProcessProfiles(profile_fds, reference_profile_fd));
279 // The resulting compilation info must be equal to the merge of the inputs.
280 ProfileCompilationInfo result;
281 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
282 ASSERT_TRUE(result.Load(reference_profile_fd));
283
284 ProfileCompilationInfo expected;
285 ASSERT_TRUE(expected.MergeWith(info1));
286 ASSERT_TRUE(expected.Equals(result));
287
288 // The information from profiles must remain the same.
289 CheckProfileInfo(profile1, info1);
290}
291
Calin Juravle2e2db782016-02-23 12:00:03 +0000292TEST_F(ProfileAssistantTest, AdviseCompilationNonEmptyReferences) {
293 ScratchFile profile1;
294 ScratchFile profile2;
295 ScratchFile reference_profile;
296
297 std::vector<int> profile_fds({
298 GetFd(profile1),
299 GetFd(profile2)});
300 int reference_profile_fd = GetFd(reference_profile);
301
302 // The new profile info will contain the methods with indices 0-100.
303 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
304 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100305 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000306 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100307 SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000308
309
310 // The reference profile info will contain the methods with indices 50-150.
311 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
312 ProfileCompilationInfo reference_info;
Calin Juravlec824b512016-03-29 20:33:33 +0100313 SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
Calin Juravle2e2db782016-02-23 12:00:03 +0000314 &reference_info, kNumberOfMethodsToEnableCompilation / 2);
315
316 // We should advise compilation.
317 ASSERT_EQ(ProfileAssistant::kCompile,
318 ProcessProfiles(profile_fds, reference_profile_fd));
319
320 // The resulting compilation info must be equal to the merge of the inputs
321 ProfileCompilationInfo result;
322 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
323 ASSERT_TRUE(result.Load(reference_profile_fd));
324
325 ProfileCompilationInfo expected;
Calin Juravle67265462016-03-18 16:23:40 +0000326 ASSERT_TRUE(expected.MergeWith(info1));
327 ASSERT_TRUE(expected.MergeWith(info2));
328 ASSERT_TRUE(expected.MergeWith(reference_info));
Calin Juravle2e2db782016-02-23 12:00:03 +0000329 ASSERT_TRUE(expected.Equals(result));
330
331 // The information from profiles must remain the same.
332 CheckProfileInfo(profile1, info1);
333 CheckProfileInfo(profile2, info2);
334}
335
336TEST_F(ProfileAssistantTest, DoNotAdviseCompilation) {
337 ScratchFile profile1;
338 ScratchFile profile2;
339 ScratchFile reference_profile;
340
341 std::vector<int> profile_fds({
342 GetFd(profile1),
343 GetFd(profile2)});
344 int reference_profile_fd = GetFd(reference_profile);
345
346 const uint16_t kNumberOfMethodsToSkipCompilation = 1;
347 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100348 SetupProfile("p1", 1, kNumberOfMethodsToSkipCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000349 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100350 SetupProfile("p2", 2, kNumberOfMethodsToSkipCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000351
352 // We should not advise compilation.
353 ASSERT_EQ(ProfileAssistant::kSkipCompilation,
354 ProcessProfiles(profile_fds, reference_profile_fd));
355
356 // The information from profiles must remain the same.
357 ProfileCompilationInfo file_info1;
358 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
359 ASSERT_TRUE(file_info1.Load(GetFd(profile1)));
360 ASSERT_TRUE(file_info1.Equals(info1));
361
362 ProfileCompilationInfo file_info2;
363 ASSERT_TRUE(profile2.GetFile()->ResetOffset());
364 ASSERT_TRUE(file_info2.Load(GetFd(profile2)));
365 ASSERT_TRUE(file_info2.Equals(info2));
366
367 // Reference profile files must remain empty.
368 ASSERT_EQ(0, reference_profile.GetFile()->GetLength());
369
370 // The information from profiles must remain the same.
371 CheckProfileInfo(profile1, info1);
372 CheckProfileInfo(profile2, info2);
373}
374
375TEST_F(ProfileAssistantTest, FailProcessingBecauseOfProfiles) {
376 ScratchFile profile1;
377 ScratchFile profile2;
378 ScratchFile reference_profile;
379
380 std::vector<int> profile_fds({
381 GetFd(profile1),
382 GetFd(profile2)});
383 int reference_profile_fd = GetFd(reference_profile);
384
385 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
386 // Assign different hashes for the same dex file. This will make merging of information to fail.
387 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100388 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000389 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100390 SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000391
392 // We should fail processing.
393 ASSERT_EQ(ProfileAssistant::kErrorBadProfiles,
394 ProcessProfiles(profile_fds, reference_profile_fd));
395
396 // The information from profiles must remain the same.
397 CheckProfileInfo(profile1, info1);
398 CheckProfileInfo(profile2, info2);
399
400 // Reference profile files must still remain empty.
401 ASSERT_EQ(0, reference_profile.GetFile()->GetLength());
402}
403
404TEST_F(ProfileAssistantTest, FailProcessingBecauseOfReferenceProfiles) {
405 ScratchFile profile1;
406 ScratchFile reference_profile;
407
408 std::vector<int> profile_fds({
409 GetFd(profile1)});
410 int reference_profile_fd = GetFd(reference_profile);
411
412 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
413 // Assign different hashes for the same dex file. This will make merging of information to fail.
414 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100415 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000416 ProfileCompilationInfo reference_info;
Calin Juravlec824b512016-03-29 20:33:33 +0100417 SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, reference_profile, &reference_info);
Calin Juravle2e2db782016-02-23 12:00:03 +0000418
419 // We should not advise compilation.
420 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
421 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
422 ASSERT_EQ(ProfileAssistant::kErrorBadProfiles,
423 ProcessProfiles(profile_fds, reference_profile_fd));
424
425 // The information from profiles must remain the same.
426 CheckProfileInfo(profile1, info1);
427}
428
Calin Juravle7bcdb532016-06-07 16:14:47 +0100429TEST_F(ProfileAssistantTest, TestProfileGeneration) {
430 ScratchFile profile;
431 // Generate a test profile.
432 GenerateTestProfile(profile.GetFilename());
433
434 // Verify that the generated profile is valid and can be loaded.
435 ASSERT_TRUE(profile.GetFile()->ResetOffset());
436 ProfileCompilationInfo info;
437 ASSERT_TRUE(info.Load(GetFd(profile)));
438}
439
Jeff Haof0a31f82017-03-27 15:50:37 -0700440TEST_F(ProfileAssistantTest, TestProfileGenerationWithIndexDex) {
441 ScratchFile profile;
442 // Generate a test profile passing in a dex file as reference.
443 GenerateTestProfileWithInputDex(profile.GetFilename());
444
445 // Verify that the generated profile is valid and can be loaded.
446 ASSERT_TRUE(profile.GetFile()->ResetOffset());
447 ProfileCompilationInfo info;
448 ASSERT_TRUE(info.Load(GetFd(profile)));
449}
450
David Sehr7c80f2d2017-02-07 16:47:58 -0800451TEST_F(ProfileAssistantTest, TestProfileCreationAllMatch) {
452 // Class names put here need to be in sorted order.
453 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800454 "Ljava/lang/Comparable;",
455 "Ljava/lang/Math;",
456 "Ljava/lang/Object;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800457 };
458 std::string input_file_contents;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800459 std::string expected_contents;
David Sehr7c80f2d2017-02-07 16:47:58 -0800460 for (std::string& class_name : class_names) {
461 input_file_contents += class_name + std::string("\n");
Calin Juravlee0ac1152017-02-13 19:03:47 -0800462 expected_contents += DescriptorToDot(class_name.c_str()) +
463 std::string("\n");
David Sehr7c80f2d2017-02-07 16:47:58 -0800464 }
465 std::string output_file_contents;
466 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
Calin Juravlee0ac1152017-02-13 19:03:47 -0800467 ASSERT_EQ(output_file_contents, expected_contents);
David Sehr7c80f2d2017-02-07 16:47:58 -0800468}
469
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700470TEST_F(ProfileAssistantTest, TestProfileCreationGenerateMethods) {
471 // Class names put here need to be in sorted order.
472 std::vector<std::string> class_names = {
473 "Ljava/lang/Math;->*",
474 };
475 std::string input_file_contents;
476 std::string expected_contents;
477 for (std::string& class_name : class_names) {
478 input_file_contents += class_name + std::string("\n");
479 expected_contents += DescriptorToDot(class_name.c_str()) +
480 std::string("\n");
481 }
482 std::string output_file_contents;
483 ScratchFile profile_file;
484 EXPECT_TRUE(CreateProfile(input_file_contents,
485 profile_file.GetFilename(),
486 GetLibCoreDexFileNames()[0]));
487 ProfileCompilationInfo info;
488 profile_file.GetFile()->ResetOffset();
489 ASSERT_TRUE(info.Load(GetFd(profile_file)));
490 // Verify that the profile has matching methods.
491 ScopedObjectAccess soa(Thread::Current());
492 ObjPtr<mirror::Class> klass = GetClass(nullptr, "Ljava/lang/Math;");
493 ASSERT_TRUE(klass != nullptr);
494 size_t method_count = 0;
495 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
496 if (!method.IsCopied() && method.GetCodeItem() != nullptr) {
497 ++method_count;
498 ProfileCompilationInfo::OfflineProfileMethodInfo pmi;
499 ASSERT_TRUE(info.GetMethod(method.GetDexFile()->GetLocation(),
500 method.GetDexFile()->GetLocationChecksum(),
501 method.GetDexMethodIndex(),
502 &pmi));
503 }
504 }
505 EXPECT_GT(method_count, 0u);
506}
507
David Sehr7c80f2d2017-02-07 16:47:58 -0800508TEST_F(ProfileAssistantTest, TestProfileCreationOneNotMatched) {
509 // Class names put here need to be in sorted order.
510 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800511 "Ldoesnt/match/this/one;",
512 "Ljava/lang/Comparable;",
513 "Ljava/lang/Object;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800514 };
515 std::string input_file_contents;
516 for (std::string& class_name : class_names) {
517 input_file_contents += class_name + std::string("\n");
518 }
519 std::string output_file_contents;
520 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
521 std::string expected_contents =
Calin Juravlee0ac1152017-02-13 19:03:47 -0800522 DescriptorToDot(class_names[1].c_str()) + std::string("\n") +
523 DescriptorToDot(class_names[2].c_str()) + std::string("\n");
David Sehr7c80f2d2017-02-07 16:47:58 -0800524 ASSERT_EQ(output_file_contents, expected_contents);
525}
526
527TEST_F(ProfileAssistantTest, TestProfileCreationNoneMatched) {
528 // Class names put here need to be in sorted order.
529 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800530 "Ldoesnt/match/this/one;",
531 "Ldoesnt/match/this/one/either;",
532 "Lnor/this/one;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800533 };
534 std::string input_file_contents;
535 for (std::string& class_name : class_names) {
536 input_file_contents += class_name + std::string("\n");
537 }
538 std::string output_file_contents;
539 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
540 std::string expected_contents("");
541 ASSERT_EQ(output_file_contents, expected_contents);
542}
543
Calin Juravlee0ac1152017-02-13 19:03:47 -0800544TEST_F(ProfileAssistantTest, TestProfileCreateInlineCache) {
545 // Create the profile content.
546 std::vector<std::string> methods = {
547 "LTestInline;->inlineMonomorphic(LSuper;)I+LSubA;",
548 "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;",
549 "LTestInline;->inlineMegamorphic(LSuper;)I+LSubA;,LSubB;,LSubC;,LSubD;,LSubE;",
Calin Juravle589e71e2017-03-03 16:05:05 -0800550 "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types",
Calin Juravlee0ac1152017-02-13 19:03:47 -0800551 "LTestInline;->noInlineCache(LSuper;)I"
552 };
553 std::string input_file_contents;
554 for (std::string& m : methods) {
555 input_file_contents += m + std::string("\n");
556 }
557
558 // Create the profile and save it to disk.
559 ScratchFile profile_file;
560 ASSERT_TRUE(CreateProfile(input_file_contents,
561 profile_file.GetFilename(),
562 GetTestDexFileName("ProfileTestMultiDex")));
563
564 // Load the profile from disk.
565 ProfileCompilationInfo info;
566 profile_file.GetFile()->ResetOffset();
567 ASSERT_TRUE(info.Load(GetFd(profile_file)));
568
569 // Load the dex files and verify that the profile contains the expected methods info.
570 ScopedObjectAccess soa(Thread::Current());
571 jobject class_loader = LoadDex("ProfileTestMultiDex");
572 ASSERT_NE(class_loader, nullptr);
573
574 mirror::Class* sub_a = GetClass(class_loader, "LSubA;");
575 mirror::Class* sub_b = GetClass(class_loader, "LSubB;");
576 mirror::Class* sub_c = GetClass(class_loader, "LSubC;");
577
578 ASSERT_TRUE(sub_a != nullptr);
579 ASSERT_TRUE(sub_b != nullptr);
580 ASSERT_TRUE(sub_c != nullptr);
581
582 {
583 // Verify that method inlineMonomorphic has the expected inline caches and nothing else.
584 ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader,
585 "LTestInline;",
586 "inlineMonomorphic");
587 ASSERT_TRUE(inline_monomorphic != nullptr);
588 std::set<mirror::Class*> expected_monomorphic;
589 expected_monomorphic.insert(sub_a);
Calin Juravle589e71e2017-03-03 16:05:05 -0800590 AssertInlineCaches(inline_monomorphic,
591 expected_monomorphic,
592 info,
593 /*megamorphic*/false,
594 /*missing_types*/false);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800595 }
596
597 {
598 // Verify that method inlinePolymorphic has the expected inline caches and nothing else.
599 ArtMethod* inline_polymorhic = GetVirtualMethod(class_loader,
600 "LTestInline;",
601 "inlinePolymorphic");
602 ASSERT_TRUE(inline_polymorhic != nullptr);
603 std::set<mirror::Class*> expected_polymorphic;
604 expected_polymorphic.insert(sub_a);
605 expected_polymorphic.insert(sub_b);
606 expected_polymorphic.insert(sub_c);
Calin Juravle589e71e2017-03-03 16:05:05 -0800607 AssertInlineCaches(inline_polymorhic,
608 expected_polymorphic,
609 info,
610 /*megamorphic*/false,
611 /*missing_types*/false);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800612 }
613
614 {
615 // Verify that method inlineMegamorphic has the expected inline caches and nothing else.
616 ArtMethod* inline_megamorphic = GetVirtualMethod(class_loader,
617 "LTestInline;",
618 "inlineMegamorphic");
619 ASSERT_TRUE(inline_megamorphic != nullptr);
620 std::set<mirror::Class*> expected_megamorphic;
Calin Juravle589e71e2017-03-03 16:05:05 -0800621 AssertInlineCaches(inline_megamorphic,
622 expected_megamorphic,
623 info,
624 /*megamorphic*/true,
625 /*missing_types*/false);
626 }
627
628 {
629 // Verify that method inlineMegamorphic has the expected inline caches and nothing else.
630 ArtMethod* inline_missing_types = GetVirtualMethod(class_loader,
631 "LTestInline;",
632 "inlineMissingTypes");
633 ASSERT_TRUE(inline_missing_types != nullptr);
634 std::set<mirror::Class*> expected_missing_Types;
635 AssertInlineCaches(inline_missing_types,
636 expected_missing_Types,
637 info,
638 /*megamorphic*/false,
639 /*missing_types*/true);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800640 }
641
642 {
643 // Verify that method noInlineCache has no inline caches in the profile.
644 ArtMethod* no_inline_cache = GetVirtualMethod(class_loader, "LTestInline;", "noInlineCache");
645 ASSERT_TRUE(no_inline_cache != nullptr);
646 ProfileCompilationInfo::OfflineProfileMethodInfo pmi_no_inline_cache;
647 ASSERT_TRUE(info.GetMethod(no_inline_cache->GetDexFile()->GetLocation(),
648 no_inline_cache->GetDexFile()->GetLocationChecksum(),
649 no_inline_cache->GetDexMethodIndex(),
650 &pmi_no_inline_cache));
651 ASSERT_TRUE(pmi_no_inline_cache.inline_caches.empty());
652 }
653}
654
Calin Juravle2e2db782016-02-23 12:00:03 +0000655} // namespace art