blob: ccf9ac6ad5ae3a81bdc54ee07b041010487fefda [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
Mathieu Chartierea650f32017-05-24 12:04:13 -070033static constexpr size_t kMaxMethodIds = 65535;
34
Calin Juravle2e2db782016-02-23 12:00:03 +000035class ProfileAssistantTest : public CommonRuntimeTest {
Calin Juravlecc3171a2017-05-19 16:47:53 -070036 public:
Calin Juravlee6f87cc2017-05-24 17:41:05 -070037 void PostRuntimeCreate() OVERRIDE {
Calin Juravlecc3171a2017-05-19 16:47:53 -070038 arena_.reset(new ArenaAllocator(Runtime::Current()->GetArenaPool()));
39 }
40
Calin Juravle2e2db782016-02-23 12:00:03 +000041 protected:
42 void SetupProfile(const std::string& id,
43 uint32_t checksum,
44 uint16_t number_of_methods,
Calin Juravlec824b512016-03-29 20:33:33 +010045 uint16_t number_of_classes,
Calin Juravle2e2db782016-02-23 12:00:03 +000046 const ScratchFile& profile,
47 ProfileCompilationInfo* info,
Calin Juravlecea9e9d2017-03-23 19:04:59 -070048 uint16_t start_method_index = 0,
49 bool reverse_dex_write_order = false) {
Calin Juravle2e2db782016-02-23 12:00:03 +000050 std::string dex_location1 = "location1" + id;
51 uint32_t dex_location_checksum1 = checksum;
52 std::string dex_location2 = "location2" + id;
53 uint32_t dex_location_checksum2 = 10 * checksum;
54 for (uint16_t i = start_method_index; i < start_method_index + number_of_methods; i++) {
Calin Juravlecea9e9d2017-03-23 19:04:59 -070055 // reverse_dex_write_order controls the order in which the dex files will be added to
56 // the profile and thus written to disk.
57 ProfileCompilationInfo::OfflineProfileMethodInfo pmi =
58 GetOfflineProfileMethodInfo(dex_location1, dex_location_checksum1,
59 dex_location2, dex_location_checksum2);
60 if (reverse_dex_write_order) {
Mathieu Chartierea650f32017-05-24 12:04:13 -070061 ASSERT_TRUE(info->AddMethod(dex_location2, dex_location_checksum2, i, kMaxMethodIds, pmi));
62 ASSERT_TRUE(info->AddMethod(dex_location1, dex_location_checksum1, i, kMaxMethodIds, pmi));
Calin Juravlecea9e9d2017-03-23 19:04:59 -070063 } else {
Mathieu Chartierea650f32017-05-24 12:04:13 -070064 ASSERT_TRUE(info->AddMethod(dex_location1, dex_location_checksum1, i, kMaxMethodIds, pmi));
65 ASSERT_TRUE(info->AddMethod(dex_location2, dex_location_checksum2, i, kMaxMethodIds, pmi));
Calin Juravlecea9e9d2017-03-23 19:04:59 -070066 }
Calin Juravle2e2db782016-02-23 12:00:03 +000067 }
Calin Juravlec824b512016-03-29 20:33:33 +010068 for (uint16_t i = 0; i < number_of_classes; i++) {
Mathieu Chartierea650f32017-05-24 12:04:13 -070069 ASSERT_TRUE(info->AddClassIndex(dex_location1,
70 dex_location_checksum1,
71 dex::TypeIndex(i),
72 kMaxMethodIds));
Calin Juravlec824b512016-03-29 20:33:33 +010073 }
74
Calin Juravle2e2db782016-02-23 12:00:03 +000075 ASSERT_TRUE(info->Save(GetFd(profile)));
76 ASSERT_EQ(0, profile.GetFile()->Flush());
77 ASSERT_TRUE(profile.GetFile()->ResetOffset());
78 }
79
Mathieu Chartier28b5c582017-06-06 14:12:50 -070080 void SetupBasicProfile(const std::string& id,
81 uint32_t checksum,
82 uint16_t number_of_methods,
83 const std::vector<uint32_t> hot_methods,
84 const std::vector<uint32_t> startup_methods,
85 const std::vector<uint32_t> post_startup_methods,
86 const ScratchFile& profile,
87 ProfileCompilationInfo* info) {
88 std::string dex_location = "location1" + id;
89 for (uint32_t idx : hot_methods) {
90 info->AddMethodIndex(dex_location, checksum, idx, number_of_methods);
91 }
92 for (uint32_t idx : startup_methods) {
93 info->AddSampledMethod(/*startup*/true, dex_location, checksum, idx, number_of_methods);
94 }
95 for (uint32_t idx : post_startup_methods) {
96 info->AddSampledMethod(/*startup*/false, dex_location, checksum, idx, number_of_methods);
97 }
98 ASSERT_TRUE(info->Save(GetFd(profile)));
99 ASSERT_EQ(0, profile.GetFile()->Flush());
100 ASSERT_TRUE(profile.GetFile()->ResetOffset());
101 }
102
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700103 // Creates an inline cache which will be destructed at the end of the test.
104 ProfileCompilationInfo::InlineCacheMap* CreateInlineCacheMap() {
105 used_inline_caches.emplace_back(new ProfileCompilationInfo::InlineCacheMap(
106 std::less<uint16_t>(), arena_->Adapter(kArenaAllocProfile)));
107 return used_inline_caches.back().get();
108 }
109
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700110 ProfileCompilationInfo::OfflineProfileMethodInfo GetOfflineProfileMethodInfo(
111 const std::string& dex_location1, uint32_t dex_checksum1,
112 const std::string& dex_location2, uint32_t dex_checksum2) {
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700113 ProfileCompilationInfo::InlineCacheMap* ic_map = CreateInlineCacheMap();
114 ProfileCompilationInfo::OfflineProfileMethodInfo pmi(ic_map);
Mathieu Chartierea650f32017-05-24 12:04:13 -0700115 pmi.dex_references.emplace_back(dex_location1, dex_checksum1, kMaxMethodIds);
116 pmi.dex_references.emplace_back(dex_location2, dex_checksum2, kMaxMethodIds);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700117
118 // Monomorphic
119 for (uint16_t dex_pc = 0; dex_pc < 11; dex_pc++) {
Calin Juravlecc3171a2017-05-19 16:47:53 -0700120 ProfileCompilationInfo::DexPcData dex_pc_data(arena_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700121 dex_pc_data.AddClass(0, dex::TypeIndex(0));
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700122 ic_map->Put(dex_pc, dex_pc_data);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700123 }
124 // Polymorphic
125 for (uint16_t dex_pc = 11; dex_pc < 22; dex_pc++) {
Calin Juravlecc3171a2017-05-19 16:47:53 -0700126 ProfileCompilationInfo::DexPcData dex_pc_data(arena_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700127 dex_pc_data.AddClass(0, dex::TypeIndex(0));
128 dex_pc_data.AddClass(1, dex::TypeIndex(1));
129
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700130 ic_map->Put(dex_pc, dex_pc_data);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700131 }
132 // Megamorphic
133 for (uint16_t dex_pc = 22; dex_pc < 33; dex_pc++) {
Calin Juravlecc3171a2017-05-19 16:47:53 -0700134 ProfileCompilationInfo::DexPcData dex_pc_data(arena_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700135 dex_pc_data.SetIsMegamorphic();
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700136 ic_map->Put(dex_pc, dex_pc_data);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700137 }
138 // Missing types
139 for (uint16_t dex_pc = 33; dex_pc < 44; dex_pc++) {
Calin Juravlecc3171a2017-05-19 16:47:53 -0700140 ProfileCompilationInfo::DexPcData dex_pc_data(arena_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700141 dex_pc_data.SetIsMissingTypes();
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700142 ic_map->Put(dex_pc, dex_pc_data);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700143 }
144
145 return pmi;
146 }
147
Calin Juravle2e2db782016-02-23 12:00:03 +0000148 int GetFd(const ScratchFile& file) const {
149 return static_cast<int>(file.GetFd());
150 }
151
152 void CheckProfileInfo(ScratchFile& file, const ProfileCompilationInfo& info) {
153 ProfileCompilationInfo file_info;
154 ASSERT_TRUE(file.GetFile()->ResetOffset());
155 ASSERT_TRUE(file_info.Load(GetFd(file)));
156 ASSERT_TRUE(file_info.Equals(info));
157 }
158
Calin Juravle7bcdb532016-06-07 16:14:47 +0100159 std::string GetProfmanCmd() {
Calin Juravle2e2db782016-02-23 12:00:03 +0000160 std::string file_path = GetTestAndroidRoot();
Calin Juravlede4fb632016-02-23 16:53:30 +0000161 file_path += "/bin/profman";
Calin Juravle2e2db782016-02-23 12:00:03 +0000162 if (kIsDebugBuild) {
163 file_path += "d";
164 }
Calin Juravle7bcdb532016-06-07 16:14:47 +0100165 EXPECT_TRUE(OS::FileExists(file_path.c_str()))
166 << file_path << " should be a valid file path";
167 return file_path;
168 }
169 // Runs test with given arguments.
170 int ProcessProfiles(const std::vector<int>& profiles_fd, int reference_profile_fd) {
171 std::string profman_cmd = GetProfmanCmd();
Calin Juravle2e2db782016-02-23 12:00:03 +0000172 std::vector<std::string> argv_str;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100173 argv_str.push_back(profman_cmd);
Calin Juravle2e2db782016-02-23 12:00:03 +0000174 for (size_t k = 0; k < profiles_fd.size(); k++) {
175 argv_str.push_back("--profile-file-fd=" + std::to_string(profiles_fd[k]));
176 }
177 argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile_fd));
178
179 std::string error;
180 return ExecAndReturnCode(argv_str, &error);
181 }
Calin Juravle7bcdb532016-06-07 16:14:47 +0100182
183 bool GenerateTestProfile(const std::string& filename) {
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("--generate-test-profile=" + filename);
188 std::string error;
189 return ExecAndReturnCode(argv_str, &error);
190 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800191
Jeff Haof0a31f82017-03-27 15:50:37 -0700192 bool GenerateTestProfileWithInputDex(const std::string& filename) {
193 std::string profman_cmd = GetProfmanCmd();
194 std::vector<std::string> argv_str;
195 argv_str.push_back(profman_cmd);
196 argv_str.push_back("--generate-test-profile=" + filename);
197 argv_str.push_back("--generate-test-profile-seed=0");
198 argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]);
199 argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]);
200 std::string error;
201 return ExecAndReturnCode(argv_str, &error);
202 }
203
Calin Juravlee0ac1152017-02-13 19:03:47 -0800204 bool CreateProfile(std::string profile_file_contents,
205 const std::string& filename,
206 const std::string& dex_location) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800207 ScratchFile class_names_file;
208 File* file = class_names_file.GetFile();
Calin Juravlee0ac1152017-02-13 19:03:47 -0800209 EXPECT_TRUE(file->WriteFully(profile_file_contents.c_str(), profile_file_contents.length()));
David Sehr7c80f2d2017-02-07 16:47:58 -0800210 EXPECT_EQ(0, file->Flush());
211 EXPECT_TRUE(file->ResetOffset());
212 std::string profman_cmd = GetProfmanCmd();
213 std::vector<std::string> argv_str;
214 argv_str.push_back(profman_cmd);
215 argv_str.push_back("--create-profile-from=" + class_names_file.GetFilename());
216 argv_str.push_back("--reference-profile-file=" + filename);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800217 argv_str.push_back("--apk=" + dex_location);
218 argv_str.push_back("--dex-location=" + dex_location);
David Sehr7c80f2d2017-02-07 16:47:58 -0800219 std::string error;
220 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0);
221 return true;
222 }
223
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700224 bool RunProfman(const std::string& filename,
225 std::vector<std::string>& extra_args,
226 std::string* output) {
227 ScratchFile output_file;
David Sehr7c80f2d2017-02-07 16:47:58 -0800228 std::string profman_cmd = GetProfmanCmd();
229 std::vector<std::string> argv_str;
230 argv_str.push_back(profman_cmd);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700231 argv_str.insert(argv_str.end(), extra_args.begin(), extra_args.end());
David Sehr7c80f2d2017-02-07 16:47:58 -0800232 argv_str.push_back("--profile-file=" + filename);
233 argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800234 argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700235 argv_str.push_back("--dump-output-to-fd=" + std::to_string(GetFd(output_file)));
David Sehr7c80f2d2017-02-07 16:47:58 -0800236 std::string error;
237 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700238 File* file = output_file.GetFile();
David Sehr7c80f2d2017-02-07 16:47:58 -0800239 EXPECT_EQ(0, file->Flush());
240 EXPECT_TRUE(file->ResetOffset());
241 int64_t length = file->GetLength();
242 std::unique_ptr<char[]> buf(new char[length]);
243 EXPECT_EQ(file->Read(buf.get(), length, 0), length);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700244 *output = std::string(buf.get(), length);
David Sehr7c80f2d2017-02-07 16:47:58 -0800245 return true;
246 }
247
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700248 bool DumpClassesAndMethods(const std::string& filename, std::string* file_contents) {
249 std::vector<std::string> extra_args;
250 extra_args.push_back("--dump-classes-and-methods");
251 return RunProfman(filename, extra_args, file_contents);
252 }
253
254 bool DumpOnly(const std::string& filename, std::string* file_contents) {
255 std::vector<std::string> extra_args;
256 extra_args.push_back("--dump-only");
257 return RunProfman(filename, extra_args, file_contents);
258 }
259
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700260 bool CreateAndDump(const std::string& input_file_contents,
261 std::string* output_file_contents) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800262 ScratchFile profile_file;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800263 EXPECT_TRUE(CreateProfile(input_file_contents,
264 profile_file.GetFilename(),
265 GetLibCoreDexFileNames()[0]));
David Sehr7c80f2d2017-02-07 16:47:58 -0800266 profile_file.GetFile()->ResetOffset();
Mathieu Chartier34067262017-04-06 13:55:46 -0700267 EXPECT_TRUE(DumpClassesAndMethods(profile_file.GetFilename(), output_file_contents));
David Sehr7c80f2d2017-02-07 16:47:58 -0800268 return true;
269 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800270
271 mirror::Class* GetClass(jobject class_loader, const std::string& clazz) {
272 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
273 Thread* self = Thread::Current();
274 ScopedObjectAccess soa(self);
275 StackHandleScope<1> hs(self);
276 Handle<mirror::ClassLoader> h_loader(
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700277 hs.NewHandle(ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(class_loader))));
Calin Juravlee0ac1152017-02-13 19:03:47 -0800278 return class_linker->FindClass(self, clazz.c_str(), h_loader);
279 }
280
281 ArtMethod* GetVirtualMethod(jobject class_loader,
282 const std::string& clazz,
283 const std::string& name) {
284 mirror::Class* klass = GetClass(class_loader, clazz);
285 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
286 const auto pointer_size = class_linker->GetImagePointerSize();
287 ArtMethod* method = nullptr;
288 Thread* self = Thread::Current();
289 ScopedObjectAccess soa(self);
290 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
291 if (name == m.GetName()) {
292 EXPECT_TRUE(method == nullptr);
293 method = &m;
294 }
295 }
296 return method;
297 }
298
299 // Verify that given method has the expected inline caches and nothing else.
300 void AssertInlineCaches(ArtMethod* method,
301 const std::set<mirror::Class*>& expected_clases,
302 const ProfileCompilationInfo& info,
Calin Juravle589e71e2017-03-03 16:05:05 -0800303 bool is_megamorphic,
304 bool is_missing_types)
Calin Juravlee0ac1152017-02-13 19:03:47 -0800305 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravlecc3171a2017-05-19 16:47:53 -0700306 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi =
307 info.GetMethod(method->GetDexFile()->GetLocation(),
308 method->GetDexFile()->GetLocationChecksum(),
309 method->GetDexMethodIndex());
310 ASSERT_TRUE(pmi != nullptr);
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700311 ASSERT_EQ(pmi->inline_caches->size(), 1u);
312 const ProfileCompilationInfo::DexPcData& dex_pc_data = pmi->inline_caches->begin()->second;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800313
Calin Juravle589e71e2017-03-03 16:05:05 -0800314 ASSERT_EQ(dex_pc_data.is_megamorphic, is_megamorphic);
315 ASSERT_EQ(dex_pc_data.is_missing_types, is_missing_types);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800316 ASSERT_EQ(expected_clases.size(), dex_pc_data.classes.size());
317 size_t found = 0;
318 for (mirror::Class* it : expected_clases) {
319 for (const auto& class_ref : dex_pc_data.classes) {
320 ProfileCompilationInfo::DexReference dex_ref =
Calin Juravlecc3171a2017-05-19 16:47:53 -0700321 pmi->dex_references[class_ref.dex_profile_index];
Calin Juravlee0ac1152017-02-13 19:03:47 -0800322 if (dex_ref.MatchesDex(&(it->GetDexFile())) &&
323 class_ref.type_index == it->GetDexTypeIndex()) {
324 found++;
325 }
326 }
327 }
328
329 ASSERT_EQ(expected_clases.size(), found);
330 }
Calin Juravlecc3171a2017-05-19 16:47:53 -0700331
332 std::unique_ptr<ArenaAllocator> arena_;
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700333
334 // Cache of inline caches generated during tests.
335 // This makes it easier to pass data between different utilities and ensure that
336 // caches are destructed at the end of the test.
337 std::vector<std::unique_ptr<ProfileCompilationInfo::InlineCacheMap>> used_inline_caches;
Calin Juravle2e2db782016-02-23 12:00:03 +0000338};
339
340TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferences) {
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 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
351 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100352 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000353 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100354 SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000355
356 // We should advise compilation.
357 ASSERT_EQ(ProfileAssistant::kCompile,
358 ProcessProfiles(profile_fds, reference_profile_fd));
359 // The resulting compilation info must be equal to the merge of the inputs.
360 ProfileCompilationInfo result;
361 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
362 ASSERT_TRUE(result.Load(reference_profile_fd));
363
364 ProfileCompilationInfo expected;
Calin Juravle67265462016-03-18 16:23:40 +0000365 ASSERT_TRUE(expected.MergeWith(info1));
366 ASSERT_TRUE(expected.MergeWith(info2));
Calin Juravle2e2db782016-02-23 12:00:03 +0000367 ASSERT_TRUE(expected.Equals(result));
368
369 // The information from profiles must remain the same.
370 CheckProfileInfo(profile1, info1);
371 CheckProfileInfo(profile2, info2);
372}
373
Calin Juravlec824b512016-03-29 20:33:33 +0100374// TODO(calin): Add more tests for classes.
375TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferencesBecauseOfClasses) {
376 ScratchFile profile1;
377 ScratchFile reference_profile;
378
379 std::vector<int> profile_fds({
380 GetFd(profile1)});
381 int reference_profile_fd = GetFd(reference_profile);
382
383 const uint16_t kNumberOfClassesToEnableCompilation = 100;
384 ProfileCompilationInfo info1;
385 SetupProfile("p1", 1, 0, kNumberOfClassesToEnableCompilation, profile1, &info1);
386
387 // We should advise compilation.
388 ASSERT_EQ(ProfileAssistant::kCompile,
389 ProcessProfiles(profile_fds, reference_profile_fd));
390 // The resulting compilation info must be equal to the merge of the inputs.
391 ProfileCompilationInfo result;
392 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
393 ASSERT_TRUE(result.Load(reference_profile_fd));
394
395 ProfileCompilationInfo expected;
396 ASSERT_TRUE(expected.MergeWith(info1));
397 ASSERT_TRUE(expected.Equals(result));
398
399 // The information from profiles must remain the same.
400 CheckProfileInfo(profile1, info1);
401}
402
Calin Juravle2e2db782016-02-23 12:00:03 +0000403TEST_F(ProfileAssistantTest, AdviseCompilationNonEmptyReferences) {
404 ScratchFile profile1;
405 ScratchFile profile2;
406 ScratchFile reference_profile;
407
408 std::vector<int> profile_fds({
409 GetFd(profile1),
410 GetFd(profile2)});
411 int reference_profile_fd = GetFd(reference_profile);
412
413 // The new profile info will contain the methods with indices 0-100.
414 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
415 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100416 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000417 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100418 SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000419
420
421 // The reference profile info will contain the methods with indices 50-150.
422 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
423 ProfileCompilationInfo reference_info;
Calin Juravlec824b512016-03-29 20:33:33 +0100424 SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
Calin Juravle2e2db782016-02-23 12:00:03 +0000425 &reference_info, kNumberOfMethodsToEnableCompilation / 2);
426
427 // We should advise compilation.
428 ASSERT_EQ(ProfileAssistant::kCompile,
429 ProcessProfiles(profile_fds, reference_profile_fd));
430
431 // The resulting compilation info must be equal to the merge of the inputs
432 ProfileCompilationInfo result;
433 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
434 ASSERT_TRUE(result.Load(reference_profile_fd));
435
436 ProfileCompilationInfo expected;
Calin Juravle67265462016-03-18 16:23:40 +0000437 ASSERT_TRUE(expected.MergeWith(info1));
438 ASSERT_TRUE(expected.MergeWith(info2));
439 ASSERT_TRUE(expected.MergeWith(reference_info));
Calin Juravle2e2db782016-02-23 12:00:03 +0000440 ASSERT_TRUE(expected.Equals(result));
441
442 // The information from profiles must remain the same.
443 CheckProfileInfo(profile1, info1);
444 CheckProfileInfo(profile2, info2);
445}
446
447TEST_F(ProfileAssistantTest, DoNotAdviseCompilation) {
448 ScratchFile profile1;
449 ScratchFile profile2;
450 ScratchFile reference_profile;
451
452 std::vector<int> profile_fds({
453 GetFd(profile1),
454 GetFd(profile2)});
455 int reference_profile_fd = GetFd(reference_profile);
456
457 const uint16_t kNumberOfMethodsToSkipCompilation = 1;
458 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100459 SetupProfile("p1", 1, kNumberOfMethodsToSkipCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000460 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100461 SetupProfile("p2", 2, kNumberOfMethodsToSkipCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000462
463 // We should not advise compilation.
464 ASSERT_EQ(ProfileAssistant::kSkipCompilation,
465 ProcessProfiles(profile_fds, reference_profile_fd));
466
467 // The information from profiles must remain the same.
468 ProfileCompilationInfo file_info1;
469 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
470 ASSERT_TRUE(file_info1.Load(GetFd(profile1)));
471 ASSERT_TRUE(file_info1.Equals(info1));
472
473 ProfileCompilationInfo file_info2;
474 ASSERT_TRUE(profile2.GetFile()->ResetOffset());
475 ASSERT_TRUE(file_info2.Load(GetFd(profile2)));
476 ASSERT_TRUE(file_info2.Equals(info2));
477
478 // Reference profile files must remain empty.
479 ASSERT_EQ(0, reference_profile.GetFile()->GetLength());
480
481 // The information from profiles must remain the same.
482 CheckProfileInfo(profile1, info1);
483 CheckProfileInfo(profile2, info2);
484}
485
486TEST_F(ProfileAssistantTest, FailProcessingBecauseOfProfiles) {
487 ScratchFile profile1;
488 ScratchFile profile2;
489 ScratchFile reference_profile;
490
491 std::vector<int> profile_fds({
492 GetFd(profile1),
493 GetFd(profile2)});
494 int reference_profile_fd = GetFd(reference_profile);
495
496 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
497 // Assign different hashes for the same dex file. This will make merging of information to fail.
498 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100499 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000500 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100501 SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000502
503 // We should fail processing.
504 ASSERT_EQ(ProfileAssistant::kErrorBadProfiles,
505 ProcessProfiles(profile_fds, reference_profile_fd));
506
507 // The information from profiles must remain the same.
508 CheckProfileInfo(profile1, info1);
509 CheckProfileInfo(profile2, info2);
510
511 // Reference profile files must still remain empty.
512 ASSERT_EQ(0, reference_profile.GetFile()->GetLength());
513}
514
515TEST_F(ProfileAssistantTest, FailProcessingBecauseOfReferenceProfiles) {
516 ScratchFile profile1;
517 ScratchFile reference_profile;
518
519 std::vector<int> profile_fds({
520 GetFd(profile1)});
521 int reference_profile_fd = GetFd(reference_profile);
522
523 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
524 // Assign different hashes for the same dex file. This will make merging of information to fail.
525 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100526 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000527 ProfileCompilationInfo reference_info;
Calin Juravlec824b512016-03-29 20:33:33 +0100528 SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, reference_profile, &reference_info);
Calin Juravle2e2db782016-02-23 12:00:03 +0000529
530 // We should not advise compilation.
531 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
532 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
533 ASSERT_EQ(ProfileAssistant::kErrorBadProfiles,
534 ProcessProfiles(profile_fds, reference_profile_fd));
535
536 // The information from profiles must remain the same.
537 CheckProfileInfo(profile1, info1);
538}
539
Calin Juravle7bcdb532016-06-07 16:14:47 +0100540TEST_F(ProfileAssistantTest, TestProfileGeneration) {
541 ScratchFile profile;
542 // Generate a test profile.
543 GenerateTestProfile(profile.GetFilename());
544
545 // Verify that the generated profile is valid and can be loaded.
546 ASSERT_TRUE(profile.GetFile()->ResetOffset());
547 ProfileCompilationInfo info;
548 ASSERT_TRUE(info.Load(GetFd(profile)));
549}
550
Jeff Haof0a31f82017-03-27 15:50:37 -0700551TEST_F(ProfileAssistantTest, TestProfileGenerationWithIndexDex) {
552 ScratchFile profile;
553 // Generate a test profile passing in a dex file as reference.
554 GenerateTestProfileWithInputDex(profile.GetFilename());
555
556 // Verify that the generated profile is valid and can be loaded.
557 ASSERT_TRUE(profile.GetFile()->ResetOffset());
558 ProfileCompilationInfo info;
559 ASSERT_TRUE(info.Load(GetFd(profile)));
560}
561
David Sehr7c80f2d2017-02-07 16:47:58 -0800562TEST_F(ProfileAssistantTest, TestProfileCreationAllMatch) {
563 // Class names put here need to be in sorted order.
564 std::vector<std::string> class_names = {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700565 "HLjava/lang/Object;-><init>()V",
Calin Juravlee0ac1152017-02-13 19:03:47 -0800566 "Ljava/lang/Comparable;",
567 "Ljava/lang/Math;",
Mathieu Chartier34067262017-04-06 13:55:46 -0700568 "Ljava/lang/Object;",
Mathieu Chartierea650f32017-05-24 12:04:13 -0700569 "SPLjava/lang/Comparable;->compareTo(Ljava/lang/Object;)I",
David Sehr7c80f2d2017-02-07 16:47:58 -0800570 };
Mathieu Chartier34067262017-04-06 13:55:46 -0700571 std::string file_contents;
David Sehr7c80f2d2017-02-07 16:47:58 -0800572 for (std::string& class_name : class_names) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700573 file_contents += class_name + std::string("\n");
David Sehr7c80f2d2017-02-07 16:47:58 -0800574 }
575 std::string output_file_contents;
Mathieu Chartier34067262017-04-06 13:55:46 -0700576 ASSERT_TRUE(CreateAndDump(file_contents, &output_file_contents));
577 ASSERT_EQ(output_file_contents, file_contents);
David Sehr7c80f2d2017-02-07 16:47:58 -0800578}
579
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700580TEST_F(ProfileAssistantTest, TestProfileCreationGenerateMethods) {
581 // Class names put here need to be in sorted order.
582 std::vector<std::string> class_names = {
583 "Ljava/lang/Math;->*",
584 };
585 std::string input_file_contents;
586 std::string expected_contents;
587 for (std::string& class_name : class_names) {
588 input_file_contents += class_name + std::string("\n");
589 expected_contents += DescriptorToDot(class_name.c_str()) +
590 std::string("\n");
591 }
592 std::string output_file_contents;
593 ScratchFile profile_file;
594 EXPECT_TRUE(CreateProfile(input_file_contents,
595 profile_file.GetFilename(),
596 GetLibCoreDexFileNames()[0]));
597 ProfileCompilationInfo info;
598 profile_file.GetFile()->ResetOffset();
599 ASSERT_TRUE(info.Load(GetFd(profile_file)));
600 // Verify that the profile has matching methods.
601 ScopedObjectAccess soa(Thread::Current());
602 ObjPtr<mirror::Class> klass = GetClass(nullptr, "Ljava/lang/Math;");
603 ASSERT_TRUE(klass != nullptr);
604 size_t method_count = 0;
605 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
606 if (!method.IsCopied() && method.GetCodeItem() != nullptr) {
607 ++method_count;
Calin Juravlecc3171a2017-05-19 16:47:53 -0700608 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi =
609 info.GetMethod(method.GetDexFile()->GetLocation(),
610 method.GetDexFile()->GetLocationChecksum(),
611 method.GetDexMethodIndex());
612 ASSERT_TRUE(pmi != nullptr);
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700613 }
614 }
615 EXPECT_GT(method_count, 0u);
616}
617
David Sehr7c80f2d2017-02-07 16:47:58 -0800618TEST_F(ProfileAssistantTest, TestProfileCreationOneNotMatched) {
619 // Class names put here need to be in sorted order.
620 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800621 "Ldoesnt/match/this/one;",
622 "Ljava/lang/Comparable;",
623 "Ljava/lang/Object;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800624 };
625 std::string input_file_contents;
626 for (std::string& class_name : class_names) {
627 input_file_contents += class_name + std::string("\n");
628 }
629 std::string output_file_contents;
630 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
631 std::string expected_contents =
Mathieu Chartier34067262017-04-06 13:55:46 -0700632 class_names[1] + std::string("\n") +
633 class_names[2] + std::string("\n");
David Sehr7c80f2d2017-02-07 16:47:58 -0800634 ASSERT_EQ(output_file_contents, expected_contents);
635}
636
637TEST_F(ProfileAssistantTest, TestProfileCreationNoneMatched) {
638 // Class names put here need to be in sorted order.
639 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800640 "Ldoesnt/match/this/one;",
641 "Ldoesnt/match/this/one/either;",
642 "Lnor/this/one;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800643 };
644 std::string input_file_contents;
645 for (std::string& class_name : class_names) {
646 input_file_contents += class_name + std::string("\n");
647 }
648 std::string output_file_contents;
649 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
650 std::string expected_contents("");
651 ASSERT_EQ(output_file_contents, expected_contents);
652}
653
Calin Juravlee0ac1152017-02-13 19:03:47 -0800654TEST_F(ProfileAssistantTest, TestProfileCreateInlineCache) {
655 // Create the profile content.
656 std::vector<std::string> methods = {
657 "LTestInline;->inlineMonomorphic(LSuper;)I+LSubA;",
658 "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;",
659 "LTestInline;->inlineMegamorphic(LSuper;)I+LSubA;,LSubB;,LSubC;,LSubD;,LSubE;",
Calin Juravle589e71e2017-03-03 16:05:05 -0800660 "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types",
Calin Juravlee0ac1152017-02-13 19:03:47 -0800661 "LTestInline;->noInlineCache(LSuper;)I"
662 };
663 std::string input_file_contents;
664 for (std::string& m : methods) {
665 input_file_contents += m + std::string("\n");
666 }
667
668 // Create the profile and save it to disk.
669 ScratchFile profile_file;
670 ASSERT_TRUE(CreateProfile(input_file_contents,
671 profile_file.GetFilename(),
672 GetTestDexFileName("ProfileTestMultiDex")));
673
674 // Load the profile from disk.
675 ProfileCompilationInfo info;
676 profile_file.GetFile()->ResetOffset();
677 ASSERT_TRUE(info.Load(GetFd(profile_file)));
678
679 // Load the dex files and verify that the profile contains the expected methods info.
680 ScopedObjectAccess soa(Thread::Current());
681 jobject class_loader = LoadDex("ProfileTestMultiDex");
682 ASSERT_NE(class_loader, nullptr);
683
684 mirror::Class* sub_a = GetClass(class_loader, "LSubA;");
685 mirror::Class* sub_b = GetClass(class_loader, "LSubB;");
686 mirror::Class* sub_c = GetClass(class_loader, "LSubC;");
687
688 ASSERT_TRUE(sub_a != nullptr);
689 ASSERT_TRUE(sub_b != nullptr);
690 ASSERT_TRUE(sub_c != nullptr);
691
692 {
693 // Verify that method inlineMonomorphic has the expected inline caches and nothing else.
694 ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader,
695 "LTestInline;",
696 "inlineMonomorphic");
697 ASSERT_TRUE(inline_monomorphic != nullptr);
698 std::set<mirror::Class*> expected_monomorphic;
699 expected_monomorphic.insert(sub_a);
Calin Juravle589e71e2017-03-03 16:05:05 -0800700 AssertInlineCaches(inline_monomorphic,
701 expected_monomorphic,
702 info,
703 /*megamorphic*/false,
704 /*missing_types*/false);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800705 }
706
707 {
708 // Verify that method inlinePolymorphic has the expected inline caches and nothing else.
709 ArtMethod* inline_polymorhic = GetVirtualMethod(class_loader,
710 "LTestInline;",
711 "inlinePolymorphic");
712 ASSERT_TRUE(inline_polymorhic != nullptr);
713 std::set<mirror::Class*> expected_polymorphic;
714 expected_polymorphic.insert(sub_a);
715 expected_polymorphic.insert(sub_b);
716 expected_polymorphic.insert(sub_c);
Calin Juravle589e71e2017-03-03 16:05:05 -0800717 AssertInlineCaches(inline_polymorhic,
718 expected_polymorphic,
719 info,
720 /*megamorphic*/false,
721 /*missing_types*/false);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800722 }
723
724 {
725 // Verify that method inlineMegamorphic has the expected inline caches and nothing else.
726 ArtMethod* inline_megamorphic = GetVirtualMethod(class_loader,
727 "LTestInline;",
728 "inlineMegamorphic");
729 ASSERT_TRUE(inline_megamorphic != nullptr);
730 std::set<mirror::Class*> expected_megamorphic;
Calin Juravle589e71e2017-03-03 16:05:05 -0800731 AssertInlineCaches(inline_megamorphic,
732 expected_megamorphic,
733 info,
734 /*megamorphic*/true,
735 /*missing_types*/false);
736 }
737
738 {
739 // Verify that method inlineMegamorphic has the expected inline caches and nothing else.
740 ArtMethod* inline_missing_types = GetVirtualMethod(class_loader,
741 "LTestInline;",
742 "inlineMissingTypes");
743 ASSERT_TRUE(inline_missing_types != nullptr);
744 std::set<mirror::Class*> expected_missing_Types;
745 AssertInlineCaches(inline_missing_types,
746 expected_missing_Types,
747 info,
748 /*megamorphic*/false,
749 /*missing_types*/true);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800750 }
751
752 {
753 // Verify that method noInlineCache has no inline caches in the profile.
754 ArtMethod* no_inline_cache = GetVirtualMethod(class_loader, "LTestInline;", "noInlineCache");
755 ASSERT_TRUE(no_inline_cache != nullptr);
Calin Juravlecc3171a2017-05-19 16:47:53 -0700756 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi_no_inline_cache =
757 info.GetMethod(no_inline_cache->GetDexFile()->GetLocation(),
758 no_inline_cache->GetDexFile()->GetLocationChecksum(),
759 no_inline_cache->GetDexMethodIndex());
760 ASSERT_TRUE(pmi_no_inline_cache != nullptr);
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700761 ASSERT_TRUE(pmi_no_inline_cache->inline_caches->empty());
Calin Juravlee0ac1152017-02-13 19:03:47 -0800762 }
763}
764
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700765TEST_F(ProfileAssistantTest, MergeProfilesWithDifferentDexOrder) {
766 ScratchFile profile1;
767 ScratchFile reference_profile;
768
769 std::vector<int> profile_fds({GetFd(profile1)});
770 int reference_profile_fd = GetFd(reference_profile);
771
772 // The new profile info will contain the methods with indices 0-100.
773 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
774 ProfileCompilationInfo info1;
775 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1,
776 /*start_method_index*/0, /*reverse_dex_write_order*/false);
777
778 // The reference profile info will contain the methods with indices 50-150.
779 // When setting up the profile reverse the order in which the dex files
780 // are added to the profile. This will verify that profman merges profiles
781 // with a different dex order correctly.
782 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
783 ProfileCompilationInfo reference_info;
784 SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
785 &reference_info, kNumberOfMethodsToEnableCompilation / 2, /*reverse_dex_write_order*/true);
786
787 // We should advise compilation.
788 ASSERT_EQ(ProfileAssistant::kCompile,
789 ProcessProfiles(profile_fds, reference_profile_fd));
790
791 // The resulting compilation info must be equal to the merge of the inputs.
792 ProfileCompilationInfo result;
793 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
794 ASSERT_TRUE(result.Load(reference_profile_fd));
795
796 ProfileCompilationInfo expected;
797 ASSERT_TRUE(expected.MergeWith(reference_info));
798 ASSERT_TRUE(expected.MergeWith(info1));
799 ASSERT_TRUE(expected.Equals(result));
800
801 // The information from profile must remain the same.
802 CheckProfileInfo(profile1, info1);
803}
804
Calin Juravle08556882017-05-26 16:40:45 -0700805TEST_F(ProfileAssistantTest, TestProfileCreateWithInvalidData) {
806 // Create the profile content.
807 std::vector<std::string> profile_methods = {
808 "LTestInline;->inlineMonomorphic(LSuper;)I+invalid_class",
809 "LTestInline;->invalid_method",
810 "invalid_class"
811 };
812 std::string input_file_contents;
813 for (std::string& m : profile_methods) {
814 input_file_contents += m + std::string("\n");
815 }
816
817 // Create the profile and save it to disk.
818 ScratchFile profile_file;
819 std::string dex_filename = GetTestDexFileName("ProfileTestMultiDex");
820 ASSERT_TRUE(CreateProfile(input_file_contents,
821 profile_file.GetFilename(),
822 dex_filename));
823
824 // Load the profile from disk.
825 ProfileCompilationInfo info;
826 profile_file.GetFile()->ResetOffset();
827 ASSERT_TRUE(info.Load(GetFd(profile_file)));
828
829 // Load the dex files and verify that the profile contains the expected methods info.
830 ScopedObjectAccess soa(Thread::Current());
831 jobject class_loader = LoadDex("ProfileTestMultiDex");
832 ASSERT_NE(class_loader, nullptr);
833
834 ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader,
835 "LTestInline;",
836 "inlineMonomorphic");
837 const DexFile* dex_file = inline_monomorphic->GetDexFile();
838
839 // Verify that the inline cache contains the invalid type.
840 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi =
841 info.GetMethod(dex_file->GetLocation(),
842 dex_file->GetLocationChecksum(),
843 inline_monomorphic->GetDexMethodIndex());
844 ASSERT_TRUE(pmi != nullptr);
845 ASSERT_EQ(pmi->inline_caches->size(), 1u);
846 const ProfileCompilationInfo::DexPcData& dex_pc_data = pmi->inline_caches->begin()->second;
847 dex::TypeIndex invalid_class_index(std::numeric_limits<uint16_t>::max() - 1);
848 ASSERT_EQ(1u, dex_pc_data.classes.size());
849 ASSERT_EQ(invalid_class_index, dex_pc_data.classes.begin()->type_index);
850
851 // Verify that the start-up classes contain the invalid class.
852 std::set<dex::TypeIndex> classes;
Mathieu Chartierea650f32017-05-24 12:04:13 -0700853 std::set<uint16_t> hot_methods;
854 std::set<uint16_t> startup_methods;
855 std::set<uint16_t> post_start_methods;
856 ASSERT_TRUE(info.GetClassesAndMethods(*dex_file,
857 &classes,
858 &hot_methods,
859 &startup_methods,
860 &post_start_methods));
Calin Juravle08556882017-05-26 16:40:45 -0700861 ASSERT_EQ(1u, classes.size());
862 ASSERT_TRUE(classes.find(invalid_class_index) != classes.end());
863
864 // Verify that the invalid method is in the profile.
Mathieu Chartierea650f32017-05-24 12:04:13 -0700865 ASSERT_EQ(2u, hot_methods.size());
Calin Juravle08556882017-05-26 16:40:45 -0700866 uint16_t invalid_method_index = std::numeric_limits<uint16_t>::max() - 1;
Mathieu Chartierea650f32017-05-24 12:04:13 -0700867 ASSERT_TRUE(hot_methods.find(invalid_method_index) != hot_methods.end());
Calin Juravle08556882017-05-26 16:40:45 -0700868}
869
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700870TEST_F(ProfileAssistantTest, DumpOnly) {
871 ScratchFile profile;
872
873 const uint32_t kNumberOfMethods = 64;
874 std::vector<uint32_t> hot_methods;
875 std::vector<uint32_t> startup_methods;
876 std::vector<uint32_t> post_startup_methods;
877 for (size_t i = 0; i < kNumberOfMethods; ++i) {
878 if (i % 2 == 0) {
879 hot_methods.push_back(i);
880 }
881 if (i % 3 == 1) {
882 startup_methods.push_back(i);
883 }
884 if (i % 4 == 2) {
885 post_startup_methods.push_back(i);
886 }
887 }
888 EXPECT_GT(hot_methods.size(), 0u);
889 EXPECT_GT(startup_methods.size(), 0u);
890 EXPECT_GT(post_startup_methods.size(), 0u);
891 ProfileCompilationInfo info1;
892 SetupBasicProfile("p1",
893 1,
894 kNumberOfMethods,
895 hot_methods,
896 startup_methods,
897 post_startup_methods,
898 profile,
899 &info1);
900 std::string output;
901 DumpOnly(profile.GetFilename(), &output);
902 const size_t hot_offset = output.find("hot methods:");
903 const size_t startup_offset = output.find("startup methods:");
904 const size_t post_startup_offset = output.find("post startup methods:");
905 const size_t classes_offset = output.find("classes:");
906 ASSERT_NE(hot_offset, std::string::npos);
907 ASSERT_NE(startup_offset, std::string::npos);
908 ASSERT_NE(post_startup_offset, std::string::npos);
909 ASSERT_LT(hot_offset, startup_offset);
910 ASSERT_LT(startup_offset, post_startup_offset);
911 // Check the actual contents of the dump by looking at the offsets of the methods.
912 for (uint32_t m : hot_methods) {
913 const size_t pos = output.find(std::to_string(m) + "[],", hot_offset);
914 ASSERT_NE(pos, std::string::npos);
915 EXPECT_LT(pos, startup_offset);
916 }
917 for (uint32_t m : startup_methods) {
918 const size_t pos = output.find(std::to_string(m) + ",", startup_offset);
919 ASSERT_NE(pos, std::string::npos);
920 EXPECT_LT(pos, post_startup_offset);
921 }
922 for (uint32_t m : post_startup_methods) {
923 const size_t pos = output.find(std::to_string(m) + ",", post_startup_offset);
924 ASSERT_NE(pos, std::string::npos);
925 EXPECT_LT(pos, classes_offset);
926 }
927}
928
Calin Juravle2e2db782016-02-23 12:00:03 +0000929} // namespace art