blob: 188d0b0a2442a2bf7799d48c1d54583eb3402896 [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 Juravlee10c1e22018-01-26 20:10:15 -080019#include "android-base/strings.h"
Calin Juravlee0ac1152017-02-13 19:03:47 -080020#include "art_method-inl.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000021#include "base/unix_file/fd_file.h"
22#include "common_runtime_test.h"
David Sehr97c381e2017-02-01 15:09:58 -080023#include "exec_utils.h"
Calin Juravle33083d62017-01-18 15:29:12 -080024#include "jit/profile_compilation_info.h"
Calin Juravlecc3171a2017-05-19 16:47:53 -070025#include "linear_alloc.h"
Calin Juravlee0ac1152017-02-13 19:03:47 -080026#include "mirror/class-inl.h"
Mathieu Chartierd808e8b2017-03-21 13:37:41 -070027#include "obj_ptr-inl.h"
Calin Juravlee0ac1152017-02-13 19:03:47 -080028#include "profile_assistant.h"
29#include "scoped_thread_state_change-inl.h"
Calin Juravle2e2db782016-02-23 12:00:03 +000030#include "utils.h"
31
32namespace art {
33
Calin Juravleee9cb412018-02-13 20:32:35 -080034using Hotness = ProfileCompilationInfo::MethodHotness;
35
Mathieu Chartierea650f32017-05-24 12:04:13 -070036static constexpr size_t kMaxMethodIds = 65535;
37
Calin Juravle2e2db782016-02-23 12:00:03 +000038class ProfileAssistantTest : public CommonRuntimeTest {
Calin Juravlecc3171a2017-05-19 16:47:53 -070039 public:
Calin Juravlee6f87cc2017-05-24 17:41:05 -070040 void PostRuntimeCreate() OVERRIDE {
Vladimir Marko69d310e2017-10-09 14:12:23 +010041 allocator_.reset(new ArenaAllocator(Runtime::Current()->GetArenaPool()));
Calin Juravlecc3171a2017-05-19 16:47:53 -070042 }
43
Calin Juravle2e2db782016-02-23 12:00:03 +000044 protected:
45 void SetupProfile(const std::string& id,
46 uint32_t checksum,
47 uint16_t number_of_methods,
Calin Juravlec824b512016-03-29 20:33:33 +010048 uint16_t number_of_classes,
Calin Juravle2e2db782016-02-23 12:00:03 +000049 const ScratchFile& profile,
50 ProfileCompilationInfo* info,
Calin Juravlecea9e9d2017-03-23 19:04:59 -070051 uint16_t start_method_index = 0,
52 bool reverse_dex_write_order = false) {
Calin Juravle2e2db782016-02-23 12:00:03 +000053 std::string dex_location1 = "location1" + id;
54 uint32_t dex_location_checksum1 = checksum;
55 std::string dex_location2 = "location2" + id;
56 uint32_t dex_location_checksum2 = 10 * checksum;
Calin Juravlee10c1e22018-01-26 20:10:15 -080057 SetupProfile(dex_location1,
58 dex_location_checksum1,
59 dex_location2,
60 dex_location_checksum2,
61 number_of_methods,
62 number_of_classes,
63 profile,
64 info,
65 start_method_index,
66 reverse_dex_write_order);
67 }
68
69 void SetupProfile(const std::string& dex_location1,
70 uint32_t dex_location_checksum1,
71 const std::string& dex_location2,
72 uint32_t dex_location_checksum2,
73 uint16_t number_of_methods,
74 uint16_t number_of_classes,
75 const ScratchFile& profile,
76 ProfileCompilationInfo* info,
77 uint16_t start_method_index = 0,
78 bool reverse_dex_write_order = false) {
Calin Juravle2e2db782016-02-23 12:00:03 +000079 for (uint16_t i = start_method_index; i < start_method_index + number_of_methods; i++) {
Calin Juravlecea9e9d2017-03-23 19:04:59 -070080 // reverse_dex_write_order controls the order in which the dex files will be added to
81 // the profile and thus written to disk.
82 ProfileCompilationInfo::OfflineProfileMethodInfo pmi =
83 GetOfflineProfileMethodInfo(dex_location1, dex_location_checksum1,
84 dex_location2, dex_location_checksum2);
Calin Juravleee9cb412018-02-13 20:32:35 -080085 Hotness::Flag flags = Hotness::kFlagPostStartup;
Calin Juravlecea9e9d2017-03-23 19:04:59 -070086 if (reverse_dex_write_order) {
Calin Juravleee9cb412018-02-13 20:32:35 -080087 ASSERT_TRUE(info->AddMethod(
88 dex_location2, dex_location_checksum2, i, kMaxMethodIds, pmi, flags));
89 ASSERT_TRUE(info->AddMethod(
90 dex_location1, dex_location_checksum1, i, kMaxMethodIds, pmi, flags));
Calin Juravlecea9e9d2017-03-23 19:04:59 -070091 } else {
Calin Juravleee9cb412018-02-13 20:32:35 -080092 ASSERT_TRUE(info->AddMethod(
93 dex_location1, dex_location_checksum1, i, kMaxMethodIds, pmi, flags));
94 ASSERT_TRUE(info->AddMethod(
95 dex_location2, dex_location_checksum2, i, kMaxMethodIds, pmi, flags));
Calin Juravlecea9e9d2017-03-23 19:04:59 -070096 }
Calin Juravle2e2db782016-02-23 12:00:03 +000097 }
Calin Juravlec824b512016-03-29 20:33:33 +010098 for (uint16_t i = 0; i < number_of_classes; i++) {
Mathieu Chartierea650f32017-05-24 12:04:13 -070099 ASSERT_TRUE(info->AddClassIndex(dex_location1,
100 dex_location_checksum1,
101 dex::TypeIndex(i),
102 kMaxMethodIds));
Calin Juravlec824b512016-03-29 20:33:33 +0100103 }
104
Calin Juravle2e2db782016-02-23 12:00:03 +0000105 ASSERT_TRUE(info->Save(GetFd(profile)));
106 ASSERT_EQ(0, profile.GetFile()->Flush());
107 ASSERT_TRUE(profile.GetFile()->ResetOffset());
108 }
109
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700110 void SetupBasicProfile(const std::string& id,
111 uint32_t checksum,
112 uint16_t number_of_methods,
113 const std::vector<uint32_t> hot_methods,
114 const std::vector<uint32_t> startup_methods,
115 const std::vector<uint32_t> post_startup_methods,
116 const ScratchFile& profile,
117 ProfileCompilationInfo* info) {
118 std::string dex_location = "location1" + id;
119 for (uint32_t idx : hot_methods) {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700120 info->AddMethodIndex(Hotness::kFlagHot, dex_location, checksum, idx, number_of_methods);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700121 }
122 for (uint32_t idx : startup_methods) {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700123 info->AddMethodIndex(Hotness::kFlagStartup, dex_location, checksum, idx, number_of_methods);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700124 }
125 for (uint32_t idx : post_startup_methods) {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700126 info->AddMethodIndex(Hotness::kFlagPostStartup,
127 dex_location,
128 checksum,
129 idx,
130 number_of_methods);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700131 }
132 ASSERT_TRUE(info->Save(GetFd(profile)));
133 ASSERT_EQ(0, profile.GetFile()->Flush());
134 ASSERT_TRUE(profile.GetFile()->ResetOffset());
135 }
136
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700137 // Creates an inline cache which will be destructed at the end of the test.
138 ProfileCompilationInfo::InlineCacheMap* CreateInlineCacheMap() {
139 used_inline_caches.emplace_back(new ProfileCompilationInfo::InlineCacheMap(
Vladimir Marko69d310e2017-10-09 14:12:23 +0100140 std::less<uint16_t>(), allocator_->Adapter(kArenaAllocProfile)));
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700141 return used_inline_caches.back().get();
142 }
143
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700144 ProfileCompilationInfo::OfflineProfileMethodInfo GetOfflineProfileMethodInfo(
145 const std::string& dex_location1, uint32_t dex_checksum1,
146 const std::string& dex_location2, uint32_t dex_checksum2) {
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700147 ProfileCompilationInfo::InlineCacheMap* ic_map = CreateInlineCacheMap();
148 ProfileCompilationInfo::OfflineProfileMethodInfo pmi(ic_map);
Mathieu Chartierea650f32017-05-24 12:04:13 -0700149 pmi.dex_references.emplace_back(dex_location1, dex_checksum1, kMaxMethodIds);
150 pmi.dex_references.emplace_back(dex_location2, dex_checksum2, kMaxMethodIds);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700151
152 // Monomorphic
153 for (uint16_t dex_pc = 0; dex_pc < 11; dex_pc++) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100154 ProfileCompilationInfo::DexPcData dex_pc_data(allocator_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700155 dex_pc_data.AddClass(0, dex::TypeIndex(0));
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700156 ic_map->Put(dex_pc, dex_pc_data);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700157 }
158 // Polymorphic
159 for (uint16_t dex_pc = 11; dex_pc < 22; dex_pc++) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100160 ProfileCompilationInfo::DexPcData dex_pc_data(allocator_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700161 dex_pc_data.AddClass(0, dex::TypeIndex(0));
162 dex_pc_data.AddClass(1, dex::TypeIndex(1));
163
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700164 ic_map->Put(dex_pc, dex_pc_data);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700165 }
166 // Megamorphic
167 for (uint16_t dex_pc = 22; dex_pc < 33; dex_pc++) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100168 ProfileCompilationInfo::DexPcData dex_pc_data(allocator_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700169 dex_pc_data.SetIsMegamorphic();
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700170 ic_map->Put(dex_pc, dex_pc_data);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700171 }
172 // Missing types
173 for (uint16_t dex_pc = 33; dex_pc < 44; dex_pc++) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100174 ProfileCompilationInfo::DexPcData dex_pc_data(allocator_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700175 dex_pc_data.SetIsMissingTypes();
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700176 ic_map->Put(dex_pc, dex_pc_data);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700177 }
178
179 return pmi;
180 }
181
Calin Juravle2e2db782016-02-23 12:00:03 +0000182 int GetFd(const ScratchFile& file) const {
183 return static_cast<int>(file.GetFd());
184 }
185
186 void CheckProfileInfo(ScratchFile& file, const ProfileCompilationInfo& info) {
187 ProfileCompilationInfo file_info;
188 ASSERT_TRUE(file.GetFile()->ResetOffset());
189 ASSERT_TRUE(file_info.Load(GetFd(file)));
190 ASSERT_TRUE(file_info.Equals(info));
191 }
192
Calin Juravle7bcdb532016-06-07 16:14:47 +0100193 std::string GetProfmanCmd() {
Calin Juravle2e2db782016-02-23 12:00:03 +0000194 std::string file_path = GetTestAndroidRoot();
Calin Juravlede4fb632016-02-23 16:53:30 +0000195 file_path += "/bin/profman";
Calin Juravle2e2db782016-02-23 12:00:03 +0000196 if (kIsDebugBuild) {
197 file_path += "d";
198 }
Calin Juravle7bcdb532016-06-07 16:14:47 +0100199 EXPECT_TRUE(OS::FileExists(file_path.c_str()))
200 << file_path << " should be a valid file path";
201 return file_path;
202 }
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700203
Calin Juravle7bcdb532016-06-07 16:14:47 +0100204 // Runs test with given arguments.
205 int ProcessProfiles(const std::vector<int>& profiles_fd, int reference_profile_fd) {
206 std::string profman_cmd = GetProfmanCmd();
Calin Juravle2e2db782016-02-23 12:00:03 +0000207 std::vector<std::string> argv_str;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100208 argv_str.push_back(profman_cmd);
Calin Juravle2e2db782016-02-23 12:00:03 +0000209 for (size_t k = 0; k < profiles_fd.size(); k++) {
210 argv_str.push_back("--profile-file-fd=" + std::to_string(profiles_fd[k]));
211 }
212 argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile_fd));
213
214 std::string error;
215 return ExecAndReturnCode(argv_str, &error);
216 }
Calin Juravle7bcdb532016-06-07 16:14:47 +0100217
218 bool GenerateTestProfile(const std::string& filename) {
219 std::string profman_cmd = GetProfmanCmd();
220 std::vector<std::string> argv_str;
221 argv_str.push_back(profman_cmd);
222 argv_str.push_back("--generate-test-profile=" + filename);
223 std::string error;
224 return ExecAndReturnCode(argv_str, &error);
225 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800226
Jeff Haof0a31f82017-03-27 15:50:37 -0700227 bool GenerateTestProfileWithInputDex(const std::string& filename) {
228 std::string profman_cmd = GetProfmanCmd();
229 std::vector<std::string> argv_str;
230 argv_str.push_back(profman_cmd);
231 argv_str.push_back("--generate-test-profile=" + filename);
232 argv_str.push_back("--generate-test-profile-seed=0");
233 argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]);
234 argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]);
235 std::string error;
236 return ExecAndReturnCode(argv_str, &error);
237 }
238
Andreas Gampe641a4732017-08-24 13:21:35 -0700239 bool CreateProfile(const std::string& profile_file_contents,
Calin Juravlee0ac1152017-02-13 19:03:47 -0800240 const std::string& filename,
David Brazdilf13ac7c2018-01-30 10:09:08 +0000241 const std::string& dex_location,
242 bool skip_verification) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800243 ScratchFile class_names_file;
244 File* file = class_names_file.GetFile();
Calin Juravlee0ac1152017-02-13 19:03:47 -0800245 EXPECT_TRUE(file->WriteFully(profile_file_contents.c_str(), profile_file_contents.length()));
David Sehr7c80f2d2017-02-07 16:47:58 -0800246 EXPECT_EQ(0, file->Flush());
247 EXPECT_TRUE(file->ResetOffset());
248 std::string profman_cmd = GetProfmanCmd();
249 std::vector<std::string> argv_str;
250 argv_str.push_back(profman_cmd);
251 argv_str.push_back("--create-profile-from=" + class_names_file.GetFilename());
252 argv_str.push_back("--reference-profile-file=" + filename);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800253 argv_str.push_back("--apk=" + dex_location);
254 argv_str.push_back("--dex-location=" + dex_location);
David Brazdilf13ac7c2018-01-30 10:09:08 +0000255 if (skip_verification) {
256 argv_str.push_back("--skip-apk-verification");
257 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800258 std::string error;
259 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0);
260 return true;
261 }
262
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700263 bool RunProfman(const std::string& filename,
264 std::vector<std::string>& extra_args,
265 std::string* output) {
266 ScratchFile output_file;
David Sehr7c80f2d2017-02-07 16:47:58 -0800267 std::string profman_cmd = GetProfmanCmd();
268 std::vector<std::string> argv_str;
269 argv_str.push_back(profman_cmd);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700270 argv_str.insert(argv_str.end(), extra_args.begin(), extra_args.end());
David Sehr7c80f2d2017-02-07 16:47:58 -0800271 argv_str.push_back("--profile-file=" + filename);
272 argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800273 argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]);
David Brazdilf13ac7c2018-01-30 10:09:08 +0000274 argv_str.push_back("--skip-apk-verification");
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700275 argv_str.push_back("--dump-output-to-fd=" + std::to_string(GetFd(output_file)));
David Sehr7c80f2d2017-02-07 16:47:58 -0800276 std::string error;
277 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700278 File* file = output_file.GetFile();
David Sehr7c80f2d2017-02-07 16:47:58 -0800279 EXPECT_EQ(0, file->Flush());
280 EXPECT_TRUE(file->ResetOffset());
281 int64_t length = file->GetLength();
282 std::unique_ptr<char[]> buf(new char[length]);
283 EXPECT_EQ(file->Read(buf.get(), length, 0), length);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700284 *output = std::string(buf.get(), length);
David Sehr7c80f2d2017-02-07 16:47:58 -0800285 return true;
286 }
287
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700288 bool DumpClassesAndMethods(const std::string& filename, std::string* file_contents) {
289 std::vector<std::string> extra_args;
290 extra_args.push_back("--dump-classes-and-methods");
291 return RunProfman(filename, extra_args, file_contents);
292 }
293
294 bool DumpOnly(const std::string& filename, std::string* file_contents) {
295 std::vector<std::string> extra_args;
296 extra_args.push_back("--dump-only");
297 return RunProfman(filename, extra_args, file_contents);
298 }
299
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700300 bool CreateAndDump(const std::string& input_file_contents,
301 std::string* output_file_contents) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800302 ScratchFile profile_file;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800303 EXPECT_TRUE(CreateProfile(input_file_contents,
304 profile_file.GetFilename(),
David Brazdilf13ac7c2018-01-30 10:09:08 +0000305 GetLibCoreDexFileNames()[0],
306 /* skip_verification */ true));
David Sehr7c80f2d2017-02-07 16:47:58 -0800307 profile_file.GetFile()->ResetOffset();
Mathieu Chartier34067262017-04-06 13:55:46 -0700308 EXPECT_TRUE(DumpClassesAndMethods(profile_file.GetFilename(), output_file_contents));
David Sehr7c80f2d2017-02-07 16:47:58 -0800309 return true;
310 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800311
312 mirror::Class* GetClass(jobject class_loader, const std::string& clazz) {
313 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
314 Thread* self = Thread::Current();
315 ScopedObjectAccess soa(self);
316 StackHandleScope<1> hs(self);
317 Handle<mirror::ClassLoader> h_loader(
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700318 hs.NewHandle(ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(class_loader))));
Calin Juravlee0ac1152017-02-13 19:03:47 -0800319 return class_linker->FindClass(self, clazz.c_str(), h_loader);
320 }
321
322 ArtMethod* GetVirtualMethod(jobject class_loader,
323 const std::string& clazz,
324 const std::string& name) {
325 mirror::Class* klass = GetClass(class_loader, clazz);
326 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
327 const auto pointer_size = class_linker->GetImagePointerSize();
328 ArtMethod* method = nullptr;
329 Thread* self = Thread::Current();
330 ScopedObjectAccess soa(self);
331 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
332 if (name == m.GetName()) {
333 EXPECT_TRUE(method == nullptr);
334 method = &m;
335 }
336 }
337 return method;
338 }
339
340 // Verify that given method has the expected inline caches and nothing else.
341 void AssertInlineCaches(ArtMethod* method,
342 const std::set<mirror::Class*>& expected_clases,
343 const ProfileCompilationInfo& info,
Calin Juravle589e71e2017-03-03 16:05:05 -0800344 bool is_megamorphic,
345 bool is_missing_types)
Calin Juravlee0ac1152017-02-13 19:03:47 -0800346 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravlecc3171a2017-05-19 16:47:53 -0700347 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi =
348 info.GetMethod(method->GetDexFile()->GetLocation(),
349 method->GetDexFile()->GetLocationChecksum(),
350 method->GetDexMethodIndex());
351 ASSERT_TRUE(pmi != nullptr);
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700352 ASSERT_EQ(pmi->inline_caches->size(), 1u);
353 const ProfileCompilationInfo::DexPcData& dex_pc_data = pmi->inline_caches->begin()->second;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800354
Calin Juravle589e71e2017-03-03 16:05:05 -0800355 ASSERT_EQ(dex_pc_data.is_megamorphic, is_megamorphic);
356 ASSERT_EQ(dex_pc_data.is_missing_types, is_missing_types);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800357 ASSERT_EQ(expected_clases.size(), dex_pc_data.classes.size());
358 size_t found = 0;
359 for (mirror::Class* it : expected_clases) {
360 for (const auto& class_ref : dex_pc_data.classes) {
361 ProfileCompilationInfo::DexReference dex_ref =
Calin Juravlecc3171a2017-05-19 16:47:53 -0700362 pmi->dex_references[class_ref.dex_profile_index];
Calin Juravlee0ac1152017-02-13 19:03:47 -0800363 if (dex_ref.MatchesDex(&(it->GetDexFile())) &&
364 class_ref.type_index == it->GetDexTypeIndex()) {
365 found++;
366 }
367 }
368 }
369
370 ASSERT_EQ(expected_clases.size(), found);
371 }
Calin Juravlecc3171a2017-05-19 16:47:53 -0700372
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -0700373 int CheckCompilationMethodPercentChange(uint16_t methods_in_cur_profile,
374 uint16_t methods_in_ref_profile) {
375 ScratchFile profile;
376 ScratchFile reference_profile;
377 std::vector<int> profile_fds({ GetFd(profile)});
378 int reference_profile_fd = GetFd(reference_profile);
379 std::vector<uint32_t> hot_methods_cur;
380 std::vector<uint32_t> hot_methods_ref;
381 std::vector<uint32_t> empty_vector;
382 for (size_t i = 0; i < methods_in_cur_profile; ++i) {
383 hot_methods_cur.push_back(i);
384 }
385 for (size_t i = 0; i < methods_in_ref_profile; ++i) {
386 hot_methods_ref.push_back(i);
387 }
388 ProfileCompilationInfo info1;
389 uint16_t methods_in_profile = std::max(methods_in_cur_profile, methods_in_ref_profile);
390 SetupBasicProfile("p1", 1, methods_in_profile, hot_methods_cur, empty_vector, empty_vector,
391 profile, &info1);
392 ProfileCompilationInfo info2;
393 SetupBasicProfile("p1", 1, methods_in_profile, hot_methods_ref, empty_vector, empty_vector,
394 reference_profile, &info2);
395 return ProcessProfiles(profile_fds, reference_profile_fd);
396 }
397
398 int CheckCompilationClassPercentChange(uint16_t classes_in_cur_profile,
399 uint16_t classes_in_ref_profile) {
400 ScratchFile profile;
401 ScratchFile reference_profile;
402
403 std::vector<int> profile_fds({ GetFd(profile)});
404 int reference_profile_fd = GetFd(reference_profile);
405
406 ProfileCompilationInfo info1;
407 SetupProfile("p1", 1, 0, classes_in_cur_profile, profile, &info1);
408 ProfileCompilationInfo info2;
409 SetupProfile("p1", 1, 0, classes_in_ref_profile, reference_profile, &info2);
410 return ProcessProfiles(profile_fds, reference_profile_fd);
411 }
412
Vladimir Marko69d310e2017-10-09 14:12:23 +0100413 std::unique_ptr<ArenaAllocator> allocator_;
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700414
415 // Cache of inline caches generated during tests.
416 // This makes it easier to pass data between different utilities and ensure that
417 // caches are destructed at the end of the test.
418 std::vector<std::unique_ptr<ProfileCompilationInfo::InlineCacheMap>> used_inline_caches;
Calin Juravle2e2db782016-02-23 12:00:03 +0000419};
420
421TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferences) {
422 ScratchFile profile1;
423 ScratchFile profile2;
424 ScratchFile reference_profile;
425
426 std::vector<int> profile_fds({
427 GetFd(profile1),
428 GetFd(profile2)});
429 int reference_profile_fd = GetFd(reference_profile);
430
431 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
432 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100433 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000434 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100435 SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000436
437 // We should advise compilation.
438 ASSERT_EQ(ProfileAssistant::kCompile,
439 ProcessProfiles(profile_fds, reference_profile_fd));
440 // The resulting compilation info must be equal to the merge of the inputs.
441 ProfileCompilationInfo result;
442 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
443 ASSERT_TRUE(result.Load(reference_profile_fd));
444
445 ProfileCompilationInfo expected;
Calin Juravle67265462016-03-18 16:23:40 +0000446 ASSERT_TRUE(expected.MergeWith(info1));
447 ASSERT_TRUE(expected.MergeWith(info2));
Calin Juravle2e2db782016-02-23 12:00:03 +0000448 ASSERT_TRUE(expected.Equals(result));
449
450 // The information from profiles must remain the same.
451 CheckProfileInfo(profile1, info1);
452 CheckProfileInfo(profile2, info2);
453}
454
Calin Juravlec824b512016-03-29 20:33:33 +0100455// TODO(calin): Add more tests for classes.
456TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferencesBecauseOfClasses) {
457 ScratchFile profile1;
458 ScratchFile reference_profile;
459
460 std::vector<int> profile_fds({
461 GetFd(profile1)});
462 int reference_profile_fd = GetFd(reference_profile);
463
464 const uint16_t kNumberOfClassesToEnableCompilation = 100;
465 ProfileCompilationInfo info1;
466 SetupProfile("p1", 1, 0, kNumberOfClassesToEnableCompilation, profile1, &info1);
467
468 // We should advise compilation.
469 ASSERT_EQ(ProfileAssistant::kCompile,
470 ProcessProfiles(profile_fds, reference_profile_fd));
471 // The resulting compilation info must be equal to the merge of the inputs.
472 ProfileCompilationInfo result;
473 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
474 ASSERT_TRUE(result.Load(reference_profile_fd));
475
476 ProfileCompilationInfo expected;
477 ASSERT_TRUE(expected.MergeWith(info1));
478 ASSERT_TRUE(expected.Equals(result));
479
480 // The information from profiles must remain the same.
481 CheckProfileInfo(profile1, info1);
482}
483
Calin Juravle2e2db782016-02-23 12:00:03 +0000484TEST_F(ProfileAssistantTest, AdviseCompilationNonEmptyReferences) {
485 ScratchFile profile1;
486 ScratchFile profile2;
487 ScratchFile reference_profile;
488
489 std::vector<int> profile_fds({
490 GetFd(profile1),
491 GetFd(profile2)});
492 int reference_profile_fd = GetFd(reference_profile);
493
494 // The new profile info will contain the methods with indices 0-100.
495 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
496 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100497 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000498 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100499 SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000500
501
502 // The reference profile info will contain the methods with indices 50-150.
503 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
504 ProfileCompilationInfo reference_info;
Calin Juravlec824b512016-03-29 20:33:33 +0100505 SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
Calin Juravle2e2db782016-02-23 12:00:03 +0000506 &reference_info, kNumberOfMethodsToEnableCompilation / 2);
507
508 // We should advise compilation.
509 ASSERT_EQ(ProfileAssistant::kCompile,
510 ProcessProfiles(profile_fds, reference_profile_fd));
511
512 // The resulting compilation info must be equal to the merge of the inputs
513 ProfileCompilationInfo result;
514 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
515 ASSERT_TRUE(result.Load(reference_profile_fd));
516
517 ProfileCompilationInfo expected;
Calin Juravle67265462016-03-18 16:23:40 +0000518 ASSERT_TRUE(expected.MergeWith(info1));
519 ASSERT_TRUE(expected.MergeWith(info2));
520 ASSERT_TRUE(expected.MergeWith(reference_info));
Calin Juravle2e2db782016-02-23 12:00:03 +0000521 ASSERT_TRUE(expected.Equals(result));
522
523 // The information from profiles must remain the same.
524 CheckProfileInfo(profile1, info1);
525 CheckProfileInfo(profile2, info2);
526}
527
528TEST_F(ProfileAssistantTest, DoNotAdviseCompilation) {
529 ScratchFile profile1;
530 ScratchFile profile2;
531 ScratchFile reference_profile;
532
533 std::vector<int> profile_fds({
534 GetFd(profile1),
535 GetFd(profile2)});
536 int reference_profile_fd = GetFd(reference_profile);
537
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -0700538 const uint16_t kNumberOfMethodsToSkipCompilation = 24; // Threshold is 100.
Calin Juravle2e2db782016-02-23 12:00:03 +0000539 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100540 SetupProfile("p1", 1, kNumberOfMethodsToSkipCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000541 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100542 SetupProfile("p2", 2, kNumberOfMethodsToSkipCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000543
544 // We should not advise compilation.
545 ASSERT_EQ(ProfileAssistant::kSkipCompilation,
546 ProcessProfiles(profile_fds, reference_profile_fd));
547
548 // The information from profiles must remain the same.
549 ProfileCompilationInfo file_info1;
550 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
551 ASSERT_TRUE(file_info1.Load(GetFd(profile1)));
552 ASSERT_TRUE(file_info1.Equals(info1));
553
554 ProfileCompilationInfo file_info2;
555 ASSERT_TRUE(profile2.GetFile()->ResetOffset());
556 ASSERT_TRUE(file_info2.Load(GetFd(profile2)));
557 ASSERT_TRUE(file_info2.Equals(info2));
558
559 // Reference profile files must remain empty.
560 ASSERT_EQ(0, reference_profile.GetFile()->GetLength());
561
562 // The information from profiles must remain the same.
563 CheckProfileInfo(profile1, info1);
564 CheckProfileInfo(profile2, info2);
565}
566
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -0700567TEST_F(ProfileAssistantTest, DoNotAdviseCompilationMethodPercentage) {
568 const uint16_t kNumberOfMethodsInRefProfile = 6000;
569 const uint16_t kNumberOfMethodsInCurProfile = 6100; // Threshold is 2%.
570 // We should not advise compilation.
571 ASSERT_EQ(ProfileAssistant::kSkipCompilation,
572 CheckCompilationMethodPercentChange(kNumberOfMethodsInCurProfile,
573 kNumberOfMethodsInRefProfile));
574}
575
576TEST_F(ProfileAssistantTest, ShouldAdviseCompilationMethodPercentage) {
577 const uint16_t kNumberOfMethodsInRefProfile = 6000;
578 const uint16_t kNumberOfMethodsInCurProfile = 6200; // Threshold is 2%.
579 // We should advise compilation.
580 ASSERT_EQ(ProfileAssistant::kCompile,
581 CheckCompilationMethodPercentChange(kNumberOfMethodsInCurProfile,
582 kNumberOfMethodsInRefProfile));
583}
584
585TEST_F(ProfileAssistantTest, DoNotdviseCompilationClassPercentage) {
586 const uint16_t kNumberOfClassesInRefProfile = 6000;
587 const uint16_t kNumberOfClassesInCurProfile = 6110; // Threshold is 2%.
588 // We should not advise compilation.
589 ASSERT_EQ(ProfileAssistant::kSkipCompilation,
590 CheckCompilationClassPercentChange(kNumberOfClassesInCurProfile,
591 kNumberOfClassesInRefProfile));
592}
593
594TEST_F(ProfileAssistantTest, ShouldAdviseCompilationClassPercentage) {
595 const uint16_t kNumberOfClassesInRefProfile = 6000;
596 const uint16_t kNumberOfClassesInCurProfile = 6120; // Threshold is 2%.
597 // We should advise compilation.
598 ASSERT_EQ(ProfileAssistant::kCompile,
599 CheckCompilationClassPercentChange(kNumberOfClassesInCurProfile,
600 kNumberOfClassesInRefProfile));
601}
602
Calin Juravle2e2db782016-02-23 12:00:03 +0000603TEST_F(ProfileAssistantTest, FailProcessingBecauseOfProfiles) {
604 ScratchFile profile1;
605 ScratchFile profile2;
606 ScratchFile reference_profile;
607
608 std::vector<int> profile_fds({
609 GetFd(profile1),
610 GetFd(profile2)});
611 int reference_profile_fd = GetFd(reference_profile);
612
613 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
614 // Assign different hashes for the same dex file. This will make merging of information to fail.
615 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100616 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000617 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100618 SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000619
620 // We should fail processing.
621 ASSERT_EQ(ProfileAssistant::kErrorBadProfiles,
622 ProcessProfiles(profile_fds, reference_profile_fd));
623
624 // The information from profiles must remain the same.
625 CheckProfileInfo(profile1, info1);
626 CheckProfileInfo(profile2, info2);
627
628 // Reference profile files must still remain empty.
629 ASSERT_EQ(0, reference_profile.GetFile()->GetLength());
630}
631
632TEST_F(ProfileAssistantTest, FailProcessingBecauseOfReferenceProfiles) {
633 ScratchFile profile1;
634 ScratchFile reference_profile;
635
636 std::vector<int> profile_fds({
637 GetFd(profile1)});
638 int reference_profile_fd = GetFd(reference_profile);
639
640 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
641 // Assign different hashes for the same dex file. This will make merging of information to fail.
642 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100643 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000644 ProfileCompilationInfo reference_info;
Calin Juravlec824b512016-03-29 20:33:33 +0100645 SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, reference_profile, &reference_info);
Calin Juravle2e2db782016-02-23 12:00:03 +0000646
647 // We should not advise compilation.
648 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
649 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
650 ASSERT_EQ(ProfileAssistant::kErrorBadProfiles,
651 ProcessProfiles(profile_fds, reference_profile_fd));
652
653 // The information from profiles must remain the same.
654 CheckProfileInfo(profile1, info1);
655}
656
Calin Juravle7bcdb532016-06-07 16:14:47 +0100657TEST_F(ProfileAssistantTest, TestProfileGeneration) {
658 ScratchFile profile;
659 // Generate a test profile.
660 GenerateTestProfile(profile.GetFilename());
661
662 // Verify that the generated profile is valid and can be loaded.
663 ASSERT_TRUE(profile.GetFile()->ResetOffset());
664 ProfileCompilationInfo info;
665 ASSERT_TRUE(info.Load(GetFd(profile)));
666}
667
Jeff Haof0a31f82017-03-27 15:50:37 -0700668TEST_F(ProfileAssistantTest, TestProfileGenerationWithIndexDex) {
669 ScratchFile profile;
670 // Generate a test profile passing in a dex file as reference.
671 GenerateTestProfileWithInputDex(profile.GetFilename());
672
673 // Verify that the generated profile is valid and can be loaded.
674 ASSERT_TRUE(profile.GetFile()->ResetOffset());
675 ProfileCompilationInfo info;
676 ASSERT_TRUE(info.Load(GetFd(profile)));
677}
678
David Sehr7c80f2d2017-02-07 16:47:58 -0800679TEST_F(ProfileAssistantTest, TestProfileCreationAllMatch) {
680 // Class names put here need to be in sorted order.
681 std::vector<std::string> class_names = {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700682 "HLjava/lang/Object;-><init>()V",
Calin Juravlee0ac1152017-02-13 19:03:47 -0800683 "Ljava/lang/Comparable;",
684 "Ljava/lang/Math;",
Mathieu Chartier34067262017-04-06 13:55:46 -0700685 "Ljava/lang/Object;",
Mathieu Chartierea650f32017-05-24 12:04:13 -0700686 "SPLjava/lang/Comparable;->compareTo(Ljava/lang/Object;)I",
David Sehr7c80f2d2017-02-07 16:47:58 -0800687 };
Mathieu Chartier34067262017-04-06 13:55:46 -0700688 std::string file_contents;
David Sehr7c80f2d2017-02-07 16:47:58 -0800689 for (std::string& class_name : class_names) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700690 file_contents += class_name + std::string("\n");
David Sehr7c80f2d2017-02-07 16:47:58 -0800691 }
692 std::string output_file_contents;
Mathieu Chartier34067262017-04-06 13:55:46 -0700693 ASSERT_TRUE(CreateAndDump(file_contents, &output_file_contents));
694 ASSERT_EQ(output_file_contents, file_contents);
David Sehr7c80f2d2017-02-07 16:47:58 -0800695}
696
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700697TEST_F(ProfileAssistantTest, TestProfileCreationGenerateMethods) {
698 // Class names put here need to be in sorted order.
699 std::vector<std::string> class_names = {
700 "Ljava/lang/Math;->*",
701 };
702 std::string input_file_contents;
703 std::string expected_contents;
704 for (std::string& class_name : class_names) {
705 input_file_contents += class_name + std::string("\n");
706 expected_contents += DescriptorToDot(class_name.c_str()) +
707 std::string("\n");
708 }
709 std::string output_file_contents;
710 ScratchFile profile_file;
711 EXPECT_TRUE(CreateProfile(input_file_contents,
712 profile_file.GetFilename(),
David Brazdilf13ac7c2018-01-30 10:09:08 +0000713 GetLibCoreDexFileNames()[0],
714 /* skip_verification */ true));
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700715 ProfileCompilationInfo info;
716 profile_file.GetFile()->ResetOffset();
717 ASSERT_TRUE(info.Load(GetFd(profile_file)));
718 // Verify that the profile has matching methods.
719 ScopedObjectAccess soa(Thread::Current());
720 ObjPtr<mirror::Class> klass = GetClass(nullptr, "Ljava/lang/Math;");
721 ASSERT_TRUE(klass != nullptr);
722 size_t method_count = 0;
723 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
724 if (!method.IsCopied() && method.GetCodeItem() != nullptr) {
725 ++method_count;
Calin Juravlecc3171a2017-05-19 16:47:53 -0700726 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi =
727 info.GetMethod(method.GetDexFile()->GetLocation(),
728 method.GetDexFile()->GetLocationChecksum(),
729 method.GetDexMethodIndex());
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700730 ASSERT_TRUE(pmi != nullptr) << method.PrettyMethod();
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700731 }
732 }
733 EXPECT_GT(method_count, 0u);
734}
735
Mathieu Chartier2f794552017-06-19 10:58:08 -0700736TEST_F(ProfileAssistantTest, TestBootImageProfile) {
737 const std::string core_dex = GetLibCoreDexFileNames()[0];
738
739 std::vector<ScratchFile> profiles;
740
741 // In image with enough clean occurrences.
742 const std::string kCleanClass = "Ljava/lang/CharSequence;";
743 // In image with enough dirty occurrences.
744 const std::string kDirtyClass = "Ljava/lang/Object;";
745 // Not in image becauseof not enough occurrences.
746 const std::string kUncommonCleanClass = "Ljava/lang/Process;";
747 const std::string kUncommonDirtyClass = "Ljava/lang/Package;";
748 // Method that is hot.
749 // Also adds the class through inference since it is in each dex.
750 const std::string kHotMethod = "Ljava/lang/Comparable;->compareTo(Ljava/lang/Object;)I";
751 // Method that doesn't add the class since its only in one profile. Should still show up in the
752 // boot profile.
753 const std::string kOtherMethod = "Ljava/util/HashMap;-><init>()V";
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700754 // Method that gets marked as hot since it's in multiple profiles.
755 const std::string kMultiMethod = "Ljava/util/ArrayList;->clear()V";
Mathieu Chartier2f794552017-06-19 10:58:08 -0700756
757 // Thresholds for this test.
758 static const size_t kDirtyThreshold = 3;
759 static const size_t kCleanThreshold = 2;
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700760 static const size_t kMethodThreshold = 2;
Mathieu Chartier2f794552017-06-19 10:58:08 -0700761
762 // Create a bunch of boot profiles.
763 std::string dex1 =
764 kCleanClass + "\n" +
765 kDirtyClass + "\n" +
766 kUncommonCleanClass + "\n" +
767 "H" + kHotMethod + "\n" +
768 kUncommonDirtyClass;
769 profiles.emplace_back(ScratchFile());
David Brazdilf13ac7c2018-01-30 10:09:08 +0000770 EXPECT_TRUE(CreateProfile(
771 dex1, profiles.back().GetFilename(), core_dex, /* skip_verification */ true));
Mathieu Chartier2f794552017-06-19 10:58:08 -0700772
773 // Create a bunch of boot profiles.
774 std::string dex2 =
775 kCleanClass + "\n" +
776 kDirtyClass + "\n" +
777 "P" + kHotMethod + "\n" +
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700778 "P" + kMultiMethod + "\n" +
Mathieu Chartier2f794552017-06-19 10:58:08 -0700779 kUncommonDirtyClass;
780 profiles.emplace_back(ScratchFile());
David Brazdilf13ac7c2018-01-30 10:09:08 +0000781 EXPECT_TRUE(CreateProfile(
782 dex2, profiles.back().GetFilename(), core_dex, /* skip_verification */ true));
Mathieu Chartier2f794552017-06-19 10:58:08 -0700783
784 // Create a bunch of boot profiles.
785 std::string dex3 =
786 "S" + kHotMethod + "\n" +
787 "P" + kOtherMethod + "\n" +
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700788 "P" + kMultiMethod + "\n" +
Mathieu Chartier2f794552017-06-19 10:58:08 -0700789 kDirtyClass + "\n";
790 profiles.emplace_back(ScratchFile());
David Brazdilf13ac7c2018-01-30 10:09:08 +0000791 EXPECT_TRUE(CreateProfile(
792 dex3, profiles.back().GetFilename(), core_dex, /* skip_verification */ true));
Mathieu Chartier2f794552017-06-19 10:58:08 -0700793
794 // Generate the boot profile.
795 ScratchFile out_profile;
796 std::vector<std::string> args;
797 args.push_back(GetProfmanCmd());
798 args.push_back("--generate-boot-image-profile");
799 args.push_back("--boot-image-class-threshold=" + std::to_string(kDirtyThreshold));
800 args.push_back("--boot-image-clean-class-threshold=" + std::to_string(kCleanThreshold));
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700801 args.push_back("--boot-image-sampled-method-threshold=" + std::to_string(kMethodThreshold));
Mathieu Chartier2f794552017-06-19 10:58:08 -0700802 args.push_back("--reference-profile-file=" + out_profile.GetFilename());
803 args.push_back("--apk=" + core_dex);
804 args.push_back("--dex-location=" + core_dex);
David Brazdilf13ac7c2018-01-30 10:09:08 +0000805 args.push_back("--skip-apk-verification");
Mathieu Chartier2f794552017-06-19 10:58:08 -0700806 for (const ScratchFile& profile : profiles) {
807 args.push_back("--profile-file=" + profile.GetFilename());
808 }
809 std::string error;
810 EXPECT_EQ(ExecAndReturnCode(args, &error), 0) << error;
811 ASSERT_EQ(0, out_profile.GetFile()->Flush());
812 ASSERT_TRUE(out_profile.GetFile()->ResetOffset());
813
814 // Verify the boot profile contents.
815 std::string output_file_contents;
816 EXPECT_TRUE(DumpClassesAndMethods(out_profile.GetFilename(), &output_file_contents));
817 // Common classes, should be in the classes of the profile.
818 EXPECT_NE(output_file_contents.find(kCleanClass + "\n"), std::string::npos)
819 << output_file_contents;
820 EXPECT_NE(output_file_contents.find(kDirtyClass + "\n"), std::string::npos)
821 << output_file_contents;
822 // Uncommon classes, should not fit preloaded class criteria and should not be in the profile.
823 EXPECT_EQ(output_file_contents.find(kUncommonCleanClass + "\n"), std::string::npos)
824 << output_file_contents;
825 EXPECT_EQ(output_file_contents.find(kUncommonDirtyClass + "\n"), std::string::npos)
826 << output_file_contents;
827 // Inferred class from a method common to all three profiles.
828 EXPECT_NE(output_file_contents.find("Ljava/lang/Comparable;\n"), std::string::npos)
829 << output_file_contents;
830 // Aggregated methods hotness information.
831 EXPECT_NE(output_file_contents.find("HSP" + kHotMethod), std::string::npos)
832 << output_file_contents;
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700833 EXPECT_NE(output_file_contents.find("P" + kOtherMethod), std::string::npos)
Mathieu Chartier2f794552017-06-19 10:58:08 -0700834 << output_file_contents;
835 // Not inferred class, method is only in one profile.
836 EXPECT_EQ(output_file_contents.find("Ljava/util/HashMap;\n"), std::string::npos)
837 << output_file_contents;
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700838 // Test the sampled methods that became hot.
839 // Other method is in only one profile, it should not become hot.
840 EXPECT_EQ(output_file_contents.find("HP" + kOtherMethod), std::string::npos)
841 << output_file_contents;
842 // Multi method is in at least two profiles, it should become hot.
843 EXPECT_NE(output_file_contents.find("HP" + kMultiMethod), std::string::npos)
844 << output_file_contents;
Mathieu Chartier2f794552017-06-19 10:58:08 -0700845}
846
David Sehr7c80f2d2017-02-07 16:47:58 -0800847TEST_F(ProfileAssistantTest, TestProfileCreationOneNotMatched) {
848 // Class names put here need to be in sorted order.
849 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800850 "Ldoesnt/match/this/one;",
851 "Ljava/lang/Comparable;",
852 "Ljava/lang/Object;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800853 };
854 std::string input_file_contents;
855 for (std::string& class_name : class_names) {
856 input_file_contents += class_name + std::string("\n");
857 }
858 std::string output_file_contents;
859 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
860 std::string expected_contents =
Mathieu Chartier34067262017-04-06 13:55:46 -0700861 class_names[1] + std::string("\n") +
862 class_names[2] + std::string("\n");
David Sehr7c80f2d2017-02-07 16:47:58 -0800863 ASSERT_EQ(output_file_contents, expected_contents);
864}
865
866TEST_F(ProfileAssistantTest, TestProfileCreationNoneMatched) {
867 // Class names put here need to be in sorted order.
868 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800869 "Ldoesnt/match/this/one;",
870 "Ldoesnt/match/this/one/either;",
871 "Lnor/this/one;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800872 };
873 std::string input_file_contents;
874 for (std::string& class_name : class_names) {
875 input_file_contents += class_name + std::string("\n");
876 }
877 std::string output_file_contents;
878 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
879 std::string expected_contents("");
880 ASSERT_EQ(output_file_contents, expected_contents);
881}
882
Calin Juravlee0ac1152017-02-13 19:03:47 -0800883TEST_F(ProfileAssistantTest, TestProfileCreateInlineCache) {
884 // Create the profile content.
885 std::vector<std::string> methods = {
886 "LTestInline;->inlineMonomorphic(LSuper;)I+LSubA;",
887 "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;",
888 "LTestInline;->inlineMegamorphic(LSuper;)I+LSubA;,LSubB;,LSubC;,LSubD;,LSubE;",
Calin Juravle589e71e2017-03-03 16:05:05 -0800889 "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types",
Calin Juravlee0ac1152017-02-13 19:03:47 -0800890 "LTestInline;->noInlineCache(LSuper;)I"
891 };
892 std::string input_file_contents;
893 for (std::string& m : methods) {
894 input_file_contents += m + std::string("\n");
895 }
896
897 // Create the profile and save it to disk.
898 ScratchFile profile_file;
899 ASSERT_TRUE(CreateProfile(input_file_contents,
900 profile_file.GetFilename(),
David Brazdilf13ac7c2018-01-30 10:09:08 +0000901 GetTestDexFileName("ProfileTestMultiDex"),
902 /* skip_verification */ false));
Calin Juravlee0ac1152017-02-13 19:03:47 -0800903
904 // Load the profile from disk.
905 ProfileCompilationInfo info;
906 profile_file.GetFile()->ResetOffset();
907 ASSERT_TRUE(info.Load(GetFd(profile_file)));
908
909 // Load the dex files and verify that the profile contains the expected methods info.
910 ScopedObjectAccess soa(Thread::Current());
911 jobject class_loader = LoadDex("ProfileTestMultiDex");
912 ASSERT_NE(class_loader, nullptr);
913
914 mirror::Class* sub_a = GetClass(class_loader, "LSubA;");
915 mirror::Class* sub_b = GetClass(class_loader, "LSubB;");
916 mirror::Class* sub_c = GetClass(class_loader, "LSubC;");
917
918 ASSERT_TRUE(sub_a != nullptr);
919 ASSERT_TRUE(sub_b != nullptr);
920 ASSERT_TRUE(sub_c != nullptr);
921
922 {
923 // Verify that method inlineMonomorphic has the expected inline caches and nothing else.
924 ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader,
925 "LTestInline;",
926 "inlineMonomorphic");
927 ASSERT_TRUE(inline_monomorphic != nullptr);
928 std::set<mirror::Class*> expected_monomorphic;
929 expected_monomorphic.insert(sub_a);
Calin Juravle589e71e2017-03-03 16:05:05 -0800930 AssertInlineCaches(inline_monomorphic,
931 expected_monomorphic,
932 info,
933 /*megamorphic*/false,
934 /*missing_types*/false);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800935 }
936
937 {
938 // Verify that method inlinePolymorphic has the expected inline caches and nothing else.
939 ArtMethod* inline_polymorhic = GetVirtualMethod(class_loader,
940 "LTestInline;",
941 "inlinePolymorphic");
942 ASSERT_TRUE(inline_polymorhic != nullptr);
943 std::set<mirror::Class*> expected_polymorphic;
944 expected_polymorphic.insert(sub_a);
945 expected_polymorphic.insert(sub_b);
946 expected_polymorphic.insert(sub_c);
Calin Juravle589e71e2017-03-03 16:05:05 -0800947 AssertInlineCaches(inline_polymorhic,
948 expected_polymorphic,
949 info,
950 /*megamorphic*/false,
951 /*missing_types*/false);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800952 }
953
954 {
955 // Verify that method inlineMegamorphic has the expected inline caches and nothing else.
956 ArtMethod* inline_megamorphic = GetVirtualMethod(class_loader,
957 "LTestInline;",
958 "inlineMegamorphic");
959 ASSERT_TRUE(inline_megamorphic != nullptr);
960 std::set<mirror::Class*> expected_megamorphic;
Calin Juravle589e71e2017-03-03 16:05:05 -0800961 AssertInlineCaches(inline_megamorphic,
962 expected_megamorphic,
963 info,
964 /*megamorphic*/true,
965 /*missing_types*/false);
966 }
967
968 {
969 // Verify that method inlineMegamorphic has the expected inline caches and nothing else.
970 ArtMethod* inline_missing_types = GetVirtualMethod(class_loader,
971 "LTestInline;",
972 "inlineMissingTypes");
973 ASSERT_TRUE(inline_missing_types != nullptr);
974 std::set<mirror::Class*> expected_missing_Types;
975 AssertInlineCaches(inline_missing_types,
976 expected_missing_Types,
977 info,
978 /*megamorphic*/false,
979 /*missing_types*/true);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800980 }
981
982 {
983 // Verify that method noInlineCache has no inline caches in the profile.
984 ArtMethod* no_inline_cache = GetVirtualMethod(class_loader, "LTestInline;", "noInlineCache");
985 ASSERT_TRUE(no_inline_cache != nullptr);
Calin Juravlecc3171a2017-05-19 16:47:53 -0700986 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi_no_inline_cache =
987 info.GetMethod(no_inline_cache->GetDexFile()->GetLocation(),
988 no_inline_cache->GetDexFile()->GetLocationChecksum(),
989 no_inline_cache->GetDexMethodIndex());
990 ASSERT_TRUE(pmi_no_inline_cache != nullptr);
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700991 ASSERT_TRUE(pmi_no_inline_cache->inline_caches->empty());
Calin Juravlee0ac1152017-02-13 19:03:47 -0800992 }
993}
994
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700995TEST_F(ProfileAssistantTest, MergeProfilesWithDifferentDexOrder) {
996 ScratchFile profile1;
997 ScratchFile reference_profile;
998
999 std::vector<int> profile_fds({GetFd(profile1)});
1000 int reference_profile_fd = GetFd(reference_profile);
1001
1002 // The new profile info will contain the methods with indices 0-100.
1003 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
1004 ProfileCompilationInfo info1;
1005 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1,
1006 /*start_method_index*/0, /*reverse_dex_write_order*/false);
1007
1008 // The reference profile info will contain the methods with indices 50-150.
1009 // When setting up the profile reverse the order in which the dex files
1010 // are added to the profile. This will verify that profman merges profiles
1011 // with a different dex order correctly.
1012 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
1013 ProfileCompilationInfo reference_info;
1014 SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
1015 &reference_info, kNumberOfMethodsToEnableCompilation / 2, /*reverse_dex_write_order*/true);
1016
1017 // We should advise compilation.
1018 ASSERT_EQ(ProfileAssistant::kCompile,
1019 ProcessProfiles(profile_fds, reference_profile_fd));
1020
1021 // The resulting compilation info must be equal to the merge of the inputs.
1022 ProfileCompilationInfo result;
1023 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
1024 ASSERT_TRUE(result.Load(reference_profile_fd));
1025
1026 ProfileCompilationInfo expected;
1027 ASSERT_TRUE(expected.MergeWith(reference_info));
1028 ASSERT_TRUE(expected.MergeWith(info1));
1029 ASSERT_TRUE(expected.Equals(result));
1030
1031 // The information from profile must remain the same.
1032 CheckProfileInfo(profile1, info1);
1033}
1034
Calin Juravle08556882017-05-26 16:40:45 -07001035TEST_F(ProfileAssistantTest, TestProfileCreateWithInvalidData) {
1036 // Create the profile content.
1037 std::vector<std::string> profile_methods = {
1038 "LTestInline;->inlineMonomorphic(LSuper;)I+invalid_class",
1039 "LTestInline;->invalid_method",
1040 "invalid_class"
1041 };
1042 std::string input_file_contents;
1043 for (std::string& m : profile_methods) {
1044 input_file_contents += m + std::string("\n");
1045 }
1046
1047 // Create the profile and save it to disk.
1048 ScratchFile profile_file;
1049 std::string dex_filename = GetTestDexFileName("ProfileTestMultiDex");
1050 ASSERT_TRUE(CreateProfile(input_file_contents,
1051 profile_file.GetFilename(),
David Brazdilf13ac7c2018-01-30 10:09:08 +00001052 dex_filename,
1053 /* skip_verification */ false));
Calin Juravle08556882017-05-26 16:40:45 -07001054
1055 // Load the profile from disk.
1056 ProfileCompilationInfo info;
1057 profile_file.GetFile()->ResetOffset();
1058 ASSERT_TRUE(info.Load(GetFd(profile_file)));
1059
1060 // Load the dex files and verify that the profile contains the expected methods info.
1061 ScopedObjectAccess soa(Thread::Current());
1062 jobject class_loader = LoadDex("ProfileTestMultiDex");
1063 ASSERT_NE(class_loader, nullptr);
1064
1065 ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader,
1066 "LTestInline;",
1067 "inlineMonomorphic");
1068 const DexFile* dex_file = inline_monomorphic->GetDexFile();
1069
1070 // Verify that the inline cache contains the invalid type.
1071 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi =
1072 info.GetMethod(dex_file->GetLocation(),
1073 dex_file->GetLocationChecksum(),
1074 inline_monomorphic->GetDexMethodIndex());
1075 ASSERT_TRUE(pmi != nullptr);
1076 ASSERT_EQ(pmi->inline_caches->size(), 1u);
1077 const ProfileCompilationInfo::DexPcData& dex_pc_data = pmi->inline_caches->begin()->second;
1078 dex::TypeIndex invalid_class_index(std::numeric_limits<uint16_t>::max() - 1);
1079 ASSERT_EQ(1u, dex_pc_data.classes.size());
1080 ASSERT_EQ(invalid_class_index, dex_pc_data.classes.begin()->type_index);
1081
1082 // Verify that the start-up classes contain the invalid class.
1083 std::set<dex::TypeIndex> classes;
Mathieu Chartierea650f32017-05-24 12:04:13 -07001084 std::set<uint16_t> hot_methods;
1085 std::set<uint16_t> startup_methods;
1086 std::set<uint16_t> post_start_methods;
1087 ASSERT_TRUE(info.GetClassesAndMethods(*dex_file,
1088 &classes,
1089 &hot_methods,
1090 &startup_methods,
1091 &post_start_methods));
Calin Juravle08556882017-05-26 16:40:45 -07001092 ASSERT_EQ(1u, classes.size());
1093 ASSERT_TRUE(classes.find(invalid_class_index) != classes.end());
1094
Calin Juravleee9cb412018-02-13 20:32:35 -08001095 // Verify that the invalid method did not get in the profile.
1096 ASSERT_EQ(1u, hot_methods.size());
Calin Juravle08556882017-05-26 16:40:45 -07001097 uint16_t invalid_method_index = std::numeric_limits<uint16_t>::max() - 1;
Calin Juravleee9cb412018-02-13 20:32:35 -08001098 ASSERT_FALSE(hot_methods.find(invalid_method_index) != hot_methods.end());
Calin Juravle08556882017-05-26 16:40:45 -07001099}
1100
Mathieu Chartier28b5c582017-06-06 14:12:50 -07001101TEST_F(ProfileAssistantTest, DumpOnly) {
1102 ScratchFile profile;
1103
1104 const uint32_t kNumberOfMethods = 64;
1105 std::vector<uint32_t> hot_methods;
1106 std::vector<uint32_t> startup_methods;
1107 std::vector<uint32_t> post_startup_methods;
1108 for (size_t i = 0; i < kNumberOfMethods; ++i) {
1109 if (i % 2 == 0) {
1110 hot_methods.push_back(i);
1111 }
1112 if (i % 3 == 1) {
1113 startup_methods.push_back(i);
1114 }
1115 if (i % 4 == 2) {
1116 post_startup_methods.push_back(i);
1117 }
1118 }
1119 EXPECT_GT(hot_methods.size(), 0u);
1120 EXPECT_GT(startup_methods.size(), 0u);
1121 EXPECT_GT(post_startup_methods.size(), 0u);
1122 ProfileCompilationInfo info1;
1123 SetupBasicProfile("p1",
1124 1,
1125 kNumberOfMethods,
1126 hot_methods,
1127 startup_methods,
1128 post_startup_methods,
1129 profile,
1130 &info1);
1131 std::string output;
1132 DumpOnly(profile.GetFilename(), &output);
1133 const size_t hot_offset = output.find("hot methods:");
1134 const size_t startup_offset = output.find("startup methods:");
1135 const size_t post_startup_offset = output.find("post startup methods:");
1136 const size_t classes_offset = output.find("classes:");
1137 ASSERT_NE(hot_offset, std::string::npos);
1138 ASSERT_NE(startup_offset, std::string::npos);
1139 ASSERT_NE(post_startup_offset, std::string::npos);
1140 ASSERT_LT(hot_offset, startup_offset);
1141 ASSERT_LT(startup_offset, post_startup_offset);
1142 // Check the actual contents of the dump by looking at the offsets of the methods.
1143 for (uint32_t m : hot_methods) {
1144 const size_t pos = output.find(std::to_string(m) + "[],", hot_offset);
1145 ASSERT_NE(pos, std::string::npos);
1146 EXPECT_LT(pos, startup_offset);
1147 }
1148 for (uint32_t m : startup_methods) {
1149 const size_t pos = output.find(std::to_string(m) + ",", startup_offset);
1150 ASSERT_NE(pos, std::string::npos);
1151 EXPECT_LT(pos, post_startup_offset);
1152 }
1153 for (uint32_t m : post_startup_methods) {
1154 const size_t pos = output.find(std::to_string(m) + ",", post_startup_offset);
1155 ASSERT_NE(pos, std::string::npos);
1156 EXPECT_LT(pos, classes_offset);
1157 }
1158}
1159
Calin Juravlee10c1e22018-01-26 20:10:15 -08001160TEST_F(ProfileAssistantTest, MergeProfilesWithFilter) {
1161 ScratchFile profile1;
1162 ScratchFile profile2;
1163 ScratchFile reference_profile;
1164
1165 std::vector<int> profile_fds({
1166 GetFd(profile1),
1167 GetFd(profile2)});
1168 int reference_profile_fd = GetFd(reference_profile);
1169
1170 // Use a real dex file to generate profile test data.
1171 // The file will be used during merging to filter unwanted data.
1172 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("ProfileTestMultiDex");
1173 const DexFile& d1 = *dex_files[0];
1174 const DexFile& d2 = *dex_files[1];
1175 // The new profile info will contain the methods with indices 0-100.
1176 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
1177 ProfileCompilationInfo info1;
1178 SetupProfile(d1.GetLocation(), d1.GetLocationChecksum(), "p1", 1,
1179 kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
1180 ProfileCompilationInfo info2;
1181 SetupProfile(d2.GetLocation(), d2.GetLocationChecksum(), "p2", 2,
1182 kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
1183
1184
1185 // The reference profile info will contain the methods with indices 50-150.
1186 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
1187 ProfileCompilationInfo reference_info;
1188 SetupProfile(d1.GetLocation(), d1.GetLocationChecksum(), "p1", 1,
1189 kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
1190 &reference_info, kNumberOfMethodsToEnableCompilation / 2);
1191
1192 // Run profman and pass the dex file with --apk-fd.
1193 android::base::unique_fd apk_fd(
1194 open(GetTestDexFileName("ProfileTestMultiDex").c_str(), O_RDONLY));
1195 ASSERT_GE(apk_fd.get(), 0);
1196
1197 std::string profman_cmd = GetProfmanCmd();
1198 std::vector<std::string> argv_str;
1199 argv_str.push_back(profman_cmd);
1200 argv_str.push_back("--profile-file-fd=" + std::to_string(profile1.GetFd()));
1201 argv_str.push_back("--profile-file-fd=" + std::to_string(profile2.GetFd()));
1202 argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile.GetFd()));
1203 argv_str.push_back("--apk-fd=" + std::to_string(apk_fd.get()));
1204 std::string error;
1205
1206 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0) << error;
1207
1208 // Verify that we can load the result.
1209
1210 ProfileCompilationInfo result;
1211 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
1212 ASSERT_TRUE(result.Load(reference_profile_fd));
1213
1214
1215 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
1216 ASSERT_TRUE(profile2.GetFile()->ResetOffset());
1217 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
1218
1219 // Verify that the result filtered out data not belonging to the dex file.
1220 // This is equivalent to checking that the result is equal to the merging of
1221 // all profiles while filtering out data not belonging to the dex file.
1222
1223 ProfileCompilationInfo::ProfileLoadFilterFn filter_fn =
1224 [&d1, &d2](const std::string& dex_location, uint32_t checksum) -> bool {
1225 return (dex_location == ProfileCompilationInfo::GetProfileDexFileKey(d1.GetLocation())
1226 && checksum == d1.GetLocationChecksum())
1227 || (dex_location == ProfileCompilationInfo::GetProfileDexFileKey(d2.GetLocation())
1228 && checksum == d2.GetLocationChecksum());
1229 };
1230
1231 ProfileCompilationInfo info1_filter;
1232 ProfileCompilationInfo info2_filter;
1233 ProfileCompilationInfo expected;
1234
1235 info2_filter.Load(profile1.GetFd(), /*merge_classes*/ true, filter_fn);
1236 info2_filter.Load(profile2.GetFd(), /*merge_classes*/ true, filter_fn);
1237 expected.Load(reference_profile.GetFd(), /*merge_classes*/ true, filter_fn);
1238
1239 ASSERT_TRUE(expected.MergeWith(info1_filter));
1240 ASSERT_TRUE(expected.MergeWith(info2_filter));
1241
1242 ASSERT_TRUE(expected.Equals(result));
1243}
1244
Calin Juravle2e2db782016-02-23 12:00:03 +00001245} // namespace art