blob: 79310ac1667a23922514c5c8804b12a4086ba05a [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
Mathieu Chartierea650f32017-05-24 12:04:13 -070034static constexpr size_t kMaxMethodIds = 65535;
35
Calin Juravle2e2db782016-02-23 12:00:03 +000036class ProfileAssistantTest : public CommonRuntimeTest {
Calin Juravlecc3171a2017-05-19 16:47:53 -070037 public:
Calin Juravlee6f87cc2017-05-24 17:41:05 -070038 void PostRuntimeCreate() OVERRIDE {
Vladimir Marko69d310e2017-10-09 14:12:23 +010039 allocator_.reset(new ArenaAllocator(Runtime::Current()->GetArenaPool()));
Calin Juravlecc3171a2017-05-19 16:47:53 -070040 }
41
Calin Juravle2e2db782016-02-23 12:00:03 +000042 protected:
43 void SetupProfile(const std::string& id,
44 uint32_t checksum,
45 uint16_t number_of_methods,
Calin Juravlec824b512016-03-29 20:33:33 +010046 uint16_t number_of_classes,
Calin Juravle2e2db782016-02-23 12:00:03 +000047 const ScratchFile& profile,
48 ProfileCompilationInfo* info,
Calin Juravlecea9e9d2017-03-23 19:04:59 -070049 uint16_t start_method_index = 0,
50 bool reverse_dex_write_order = false) {
Calin Juravle2e2db782016-02-23 12:00:03 +000051 std::string dex_location1 = "location1" + id;
52 uint32_t dex_location_checksum1 = checksum;
53 std::string dex_location2 = "location2" + id;
54 uint32_t dex_location_checksum2 = 10 * checksum;
Calin Juravlee10c1e22018-01-26 20:10:15 -080055 SetupProfile(dex_location1,
56 dex_location_checksum1,
57 dex_location2,
58 dex_location_checksum2,
59 number_of_methods,
60 number_of_classes,
61 profile,
62 info,
63 start_method_index,
64 reverse_dex_write_order);
65 }
66
67 void SetupProfile(const std::string& dex_location1,
68 uint32_t dex_location_checksum1,
69 const std::string& dex_location2,
70 uint32_t dex_location_checksum2,
71 uint16_t number_of_methods,
72 uint16_t number_of_classes,
73 const ScratchFile& profile,
74 ProfileCompilationInfo* info,
75 uint16_t start_method_index = 0,
76 bool reverse_dex_write_order = false) {
Calin Juravle2e2db782016-02-23 12:00:03 +000077 for (uint16_t i = start_method_index; i < start_method_index + number_of_methods; i++) {
Calin Juravlecea9e9d2017-03-23 19:04:59 -070078 // reverse_dex_write_order controls the order in which the dex files will be added to
79 // the profile and thus written to disk.
80 ProfileCompilationInfo::OfflineProfileMethodInfo pmi =
81 GetOfflineProfileMethodInfo(dex_location1, dex_location_checksum1,
82 dex_location2, dex_location_checksum2);
83 if (reverse_dex_write_order) {
Mathieu Chartierea650f32017-05-24 12:04:13 -070084 ASSERT_TRUE(info->AddMethod(dex_location2, dex_location_checksum2, i, kMaxMethodIds, pmi));
85 ASSERT_TRUE(info->AddMethod(dex_location1, dex_location_checksum1, i, kMaxMethodIds, pmi));
Calin Juravlecea9e9d2017-03-23 19:04:59 -070086 } else {
Mathieu Chartierea650f32017-05-24 12:04:13 -070087 ASSERT_TRUE(info->AddMethod(dex_location1, dex_location_checksum1, i, kMaxMethodIds, pmi));
88 ASSERT_TRUE(info->AddMethod(dex_location2, dex_location_checksum2, i, kMaxMethodIds, pmi));
Calin Juravlecea9e9d2017-03-23 19:04:59 -070089 }
Calin Juravle2e2db782016-02-23 12:00:03 +000090 }
Calin Juravlec824b512016-03-29 20:33:33 +010091 for (uint16_t i = 0; i < number_of_classes; i++) {
Mathieu Chartierea650f32017-05-24 12:04:13 -070092 ASSERT_TRUE(info->AddClassIndex(dex_location1,
93 dex_location_checksum1,
94 dex::TypeIndex(i),
95 kMaxMethodIds));
Calin Juravlec824b512016-03-29 20:33:33 +010096 }
97
Calin Juravle2e2db782016-02-23 12:00:03 +000098 ASSERT_TRUE(info->Save(GetFd(profile)));
99 ASSERT_EQ(0, profile.GetFile()->Flush());
100 ASSERT_TRUE(profile.GetFile()->ResetOffset());
101 }
102
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700103 void SetupBasicProfile(const std::string& id,
104 uint32_t checksum,
105 uint16_t number_of_methods,
106 const std::vector<uint32_t> hot_methods,
107 const std::vector<uint32_t> startup_methods,
108 const std::vector<uint32_t> post_startup_methods,
109 const ScratchFile& profile,
110 ProfileCompilationInfo* info) {
111 std::string dex_location = "location1" + id;
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700112 using Hotness = ProfileCompilationInfo::MethodHotness;
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700113 for (uint32_t idx : hot_methods) {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700114 info->AddMethodIndex(Hotness::kFlagHot, dex_location, checksum, idx, number_of_methods);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700115 }
116 for (uint32_t idx : startup_methods) {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700117 info->AddMethodIndex(Hotness::kFlagStartup, dex_location, checksum, idx, number_of_methods);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700118 }
119 for (uint32_t idx : post_startup_methods) {
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700120 info->AddMethodIndex(Hotness::kFlagPostStartup,
121 dex_location,
122 checksum,
123 idx,
124 number_of_methods);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700125 }
126 ASSERT_TRUE(info->Save(GetFd(profile)));
127 ASSERT_EQ(0, profile.GetFile()->Flush());
128 ASSERT_TRUE(profile.GetFile()->ResetOffset());
129 }
130
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700131 // Creates an inline cache which will be destructed at the end of the test.
132 ProfileCompilationInfo::InlineCacheMap* CreateInlineCacheMap() {
133 used_inline_caches.emplace_back(new ProfileCompilationInfo::InlineCacheMap(
Vladimir Marko69d310e2017-10-09 14:12:23 +0100134 std::less<uint16_t>(), allocator_->Adapter(kArenaAllocProfile)));
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700135 return used_inline_caches.back().get();
136 }
137
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700138 ProfileCompilationInfo::OfflineProfileMethodInfo GetOfflineProfileMethodInfo(
139 const std::string& dex_location1, uint32_t dex_checksum1,
140 const std::string& dex_location2, uint32_t dex_checksum2) {
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700141 ProfileCompilationInfo::InlineCacheMap* ic_map = CreateInlineCacheMap();
142 ProfileCompilationInfo::OfflineProfileMethodInfo pmi(ic_map);
Mathieu Chartierea650f32017-05-24 12:04:13 -0700143 pmi.dex_references.emplace_back(dex_location1, dex_checksum1, kMaxMethodIds);
144 pmi.dex_references.emplace_back(dex_location2, dex_checksum2, kMaxMethodIds);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700145
146 // Monomorphic
147 for (uint16_t dex_pc = 0; dex_pc < 11; dex_pc++) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100148 ProfileCompilationInfo::DexPcData dex_pc_data(allocator_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700149 dex_pc_data.AddClass(0, dex::TypeIndex(0));
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700150 ic_map->Put(dex_pc, dex_pc_data);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700151 }
152 // Polymorphic
153 for (uint16_t dex_pc = 11; dex_pc < 22; 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));
156 dex_pc_data.AddClass(1, dex::TypeIndex(1));
157
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700158 ic_map->Put(dex_pc, dex_pc_data);
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700159 }
160 // Megamorphic
161 for (uint16_t dex_pc = 22; dex_pc < 33; dex_pc++) {
Vladimir Marko69d310e2017-10-09 14:12:23 +0100162 ProfileCompilationInfo::DexPcData dex_pc_data(allocator_.get());
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700163 dex_pc_data.SetIsMegamorphic();
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 // Missing types
167 for (uint16_t dex_pc = 33; dex_pc < 44; 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.SetIsMissingTypes();
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
173 return pmi;
174 }
175
Calin Juravle2e2db782016-02-23 12:00:03 +0000176 int GetFd(const ScratchFile& file) const {
177 return static_cast<int>(file.GetFd());
178 }
179
180 void CheckProfileInfo(ScratchFile& file, const ProfileCompilationInfo& info) {
181 ProfileCompilationInfo file_info;
182 ASSERT_TRUE(file.GetFile()->ResetOffset());
183 ASSERT_TRUE(file_info.Load(GetFd(file)));
184 ASSERT_TRUE(file_info.Equals(info));
185 }
186
Calin Juravle7bcdb532016-06-07 16:14:47 +0100187 std::string GetProfmanCmd() {
Calin Juravle2e2db782016-02-23 12:00:03 +0000188 std::string file_path = GetTestAndroidRoot();
Calin Juravlede4fb632016-02-23 16:53:30 +0000189 file_path += "/bin/profman";
Calin Juravle2e2db782016-02-23 12:00:03 +0000190 if (kIsDebugBuild) {
191 file_path += "d";
192 }
Calin Juravle7bcdb532016-06-07 16:14:47 +0100193 EXPECT_TRUE(OS::FileExists(file_path.c_str()))
194 << file_path << " should be a valid file path";
195 return file_path;
196 }
Mathieu Chartierf70fe3d2017-06-21 15:24:02 -0700197
Calin Juravle7bcdb532016-06-07 16:14:47 +0100198 // Runs test with given arguments.
199 int ProcessProfiles(const std::vector<int>& profiles_fd, int reference_profile_fd) {
200 std::string profman_cmd = GetProfmanCmd();
Calin Juravle2e2db782016-02-23 12:00:03 +0000201 std::vector<std::string> argv_str;
Calin Juravle7bcdb532016-06-07 16:14:47 +0100202 argv_str.push_back(profman_cmd);
Calin Juravle2e2db782016-02-23 12:00:03 +0000203 for (size_t k = 0; k < profiles_fd.size(); k++) {
204 argv_str.push_back("--profile-file-fd=" + std::to_string(profiles_fd[k]));
205 }
206 argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile_fd));
207
208 std::string error;
209 return ExecAndReturnCode(argv_str, &error);
210 }
Calin Juravle7bcdb532016-06-07 16:14:47 +0100211
212 bool GenerateTestProfile(const std::string& filename) {
213 std::string profman_cmd = GetProfmanCmd();
214 std::vector<std::string> argv_str;
215 argv_str.push_back(profman_cmd);
216 argv_str.push_back("--generate-test-profile=" + filename);
217 std::string error;
218 return ExecAndReturnCode(argv_str, &error);
219 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800220
Jeff Haof0a31f82017-03-27 15:50:37 -0700221 bool GenerateTestProfileWithInputDex(const std::string& filename) {
222 std::string profman_cmd = GetProfmanCmd();
223 std::vector<std::string> argv_str;
224 argv_str.push_back(profman_cmd);
225 argv_str.push_back("--generate-test-profile=" + filename);
226 argv_str.push_back("--generate-test-profile-seed=0");
227 argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]);
228 argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]);
229 std::string error;
230 return ExecAndReturnCode(argv_str, &error);
231 }
232
Andreas Gampe641a4732017-08-24 13:21:35 -0700233 bool CreateProfile(const std::string& profile_file_contents,
Calin Juravlee0ac1152017-02-13 19:03:47 -0800234 const std::string& filename,
David Brazdilf13ac7c2018-01-30 10:09:08 +0000235 const std::string& dex_location,
236 bool skip_verification) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800237 ScratchFile class_names_file;
238 File* file = class_names_file.GetFile();
Calin Juravlee0ac1152017-02-13 19:03:47 -0800239 EXPECT_TRUE(file->WriteFully(profile_file_contents.c_str(), profile_file_contents.length()));
David Sehr7c80f2d2017-02-07 16:47:58 -0800240 EXPECT_EQ(0, file->Flush());
241 EXPECT_TRUE(file->ResetOffset());
242 std::string profman_cmd = GetProfmanCmd();
243 std::vector<std::string> argv_str;
244 argv_str.push_back(profman_cmd);
245 argv_str.push_back("--create-profile-from=" + class_names_file.GetFilename());
246 argv_str.push_back("--reference-profile-file=" + filename);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800247 argv_str.push_back("--apk=" + dex_location);
248 argv_str.push_back("--dex-location=" + dex_location);
David Brazdilf13ac7c2018-01-30 10:09:08 +0000249 if (skip_verification) {
250 argv_str.push_back("--skip-apk-verification");
251 }
David Sehr7c80f2d2017-02-07 16:47:58 -0800252 std::string error;
253 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0);
254 return true;
255 }
256
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700257 bool RunProfman(const std::string& filename,
258 std::vector<std::string>& extra_args,
259 std::string* output) {
260 ScratchFile output_file;
David Sehr7c80f2d2017-02-07 16:47:58 -0800261 std::string profman_cmd = GetProfmanCmd();
262 std::vector<std::string> argv_str;
263 argv_str.push_back(profman_cmd);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700264 argv_str.insert(argv_str.end(), extra_args.begin(), extra_args.end());
David Sehr7c80f2d2017-02-07 16:47:58 -0800265 argv_str.push_back("--profile-file=" + filename);
266 argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800267 argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]);
David Brazdilf13ac7c2018-01-30 10:09:08 +0000268 argv_str.push_back("--skip-apk-verification");
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700269 argv_str.push_back("--dump-output-to-fd=" + std::to_string(GetFd(output_file)));
David Sehr7c80f2d2017-02-07 16:47:58 -0800270 std::string error;
271 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700272 File* file = output_file.GetFile();
David Sehr7c80f2d2017-02-07 16:47:58 -0800273 EXPECT_EQ(0, file->Flush());
274 EXPECT_TRUE(file->ResetOffset());
275 int64_t length = file->GetLength();
276 std::unique_ptr<char[]> buf(new char[length]);
277 EXPECT_EQ(file->Read(buf.get(), length, 0), length);
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700278 *output = std::string(buf.get(), length);
David Sehr7c80f2d2017-02-07 16:47:58 -0800279 return true;
280 }
281
Mathieu Chartier28b5c582017-06-06 14:12:50 -0700282 bool DumpClassesAndMethods(const std::string& filename, std::string* file_contents) {
283 std::vector<std::string> extra_args;
284 extra_args.push_back("--dump-classes-and-methods");
285 return RunProfman(filename, extra_args, file_contents);
286 }
287
288 bool DumpOnly(const std::string& filename, std::string* file_contents) {
289 std::vector<std::string> extra_args;
290 extra_args.push_back("--dump-only");
291 return RunProfman(filename, extra_args, file_contents);
292 }
293
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700294 bool CreateAndDump(const std::string& input_file_contents,
295 std::string* output_file_contents) {
David Sehr7c80f2d2017-02-07 16:47:58 -0800296 ScratchFile profile_file;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800297 EXPECT_TRUE(CreateProfile(input_file_contents,
298 profile_file.GetFilename(),
David Brazdilf13ac7c2018-01-30 10:09:08 +0000299 GetLibCoreDexFileNames()[0],
300 /* skip_verification */ true));
David Sehr7c80f2d2017-02-07 16:47:58 -0800301 profile_file.GetFile()->ResetOffset();
Mathieu Chartier34067262017-04-06 13:55:46 -0700302 EXPECT_TRUE(DumpClassesAndMethods(profile_file.GetFilename(), output_file_contents));
David Sehr7c80f2d2017-02-07 16:47:58 -0800303 return true;
304 }
Calin Juravlee0ac1152017-02-13 19:03:47 -0800305
306 mirror::Class* GetClass(jobject class_loader, const std::string& clazz) {
307 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
308 Thread* self = Thread::Current();
309 ScopedObjectAccess soa(self);
310 StackHandleScope<1> hs(self);
311 Handle<mirror::ClassLoader> h_loader(
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700312 hs.NewHandle(ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(class_loader))));
Calin Juravlee0ac1152017-02-13 19:03:47 -0800313 return class_linker->FindClass(self, clazz.c_str(), h_loader);
314 }
315
316 ArtMethod* GetVirtualMethod(jobject class_loader,
317 const std::string& clazz,
318 const std::string& name) {
319 mirror::Class* klass = GetClass(class_loader, clazz);
320 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
321 const auto pointer_size = class_linker->GetImagePointerSize();
322 ArtMethod* method = nullptr;
323 Thread* self = Thread::Current();
324 ScopedObjectAccess soa(self);
325 for (auto& m : klass->GetVirtualMethods(pointer_size)) {
326 if (name == m.GetName()) {
327 EXPECT_TRUE(method == nullptr);
328 method = &m;
329 }
330 }
331 return method;
332 }
333
334 // Verify that given method has the expected inline caches and nothing else.
335 void AssertInlineCaches(ArtMethod* method,
336 const std::set<mirror::Class*>& expected_clases,
337 const ProfileCompilationInfo& info,
Calin Juravle589e71e2017-03-03 16:05:05 -0800338 bool is_megamorphic,
339 bool is_missing_types)
Calin Juravlee0ac1152017-02-13 19:03:47 -0800340 REQUIRES_SHARED(Locks::mutator_lock_) {
Calin Juravlecc3171a2017-05-19 16:47:53 -0700341 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi =
342 info.GetMethod(method->GetDexFile()->GetLocation(),
343 method->GetDexFile()->GetLocationChecksum(),
344 method->GetDexMethodIndex());
345 ASSERT_TRUE(pmi != nullptr);
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700346 ASSERT_EQ(pmi->inline_caches->size(), 1u);
347 const ProfileCompilationInfo::DexPcData& dex_pc_data = pmi->inline_caches->begin()->second;
Calin Juravlee0ac1152017-02-13 19:03:47 -0800348
Calin Juravle589e71e2017-03-03 16:05:05 -0800349 ASSERT_EQ(dex_pc_data.is_megamorphic, is_megamorphic);
350 ASSERT_EQ(dex_pc_data.is_missing_types, is_missing_types);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800351 ASSERT_EQ(expected_clases.size(), dex_pc_data.classes.size());
352 size_t found = 0;
353 for (mirror::Class* it : expected_clases) {
354 for (const auto& class_ref : dex_pc_data.classes) {
355 ProfileCompilationInfo::DexReference dex_ref =
Calin Juravlecc3171a2017-05-19 16:47:53 -0700356 pmi->dex_references[class_ref.dex_profile_index];
Calin Juravlee0ac1152017-02-13 19:03:47 -0800357 if (dex_ref.MatchesDex(&(it->GetDexFile())) &&
358 class_ref.type_index == it->GetDexTypeIndex()) {
359 found++;
360 }
361 }
362 }
363
364 ASSERT_EQ(expected_clases.size(), found);
365 }
Calin Juravlecc3171a2017-05-19 16:47:53 -0700366
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -0700367 int CheckCompilationMethodPercentChange(uint16_t methods_in_cur_profile,
368 uint16_t methods_in_ref_profile) {
369 ScratchFile profile;
370 ScratchFile reference_profile;
371 std::vector<int> profile_fds({ GetFd(profile)});
372 int reference_profile_fd = GetFd(reference_profile);
373 std::vector<uint32_t> hot_methods_cur;
374 std::vector<uint32_t> hot_methods_ref;
375 std::vector<uint32_t> empty_vector;
376 for (size_t i = 0; i < methods_in_cur_profile; ++i) {
377 hot_methods_cur.push_back(i);
378 }
379 for (size_t i = 0; i < methods_in_ref_profile; ++i) {
380 hot_methods_ref.push_back(i);
381 }
382 ProfileCompilationInfo info1;
383 uint16_t methods_in_profile = std::max(methods_in_cur_profile, methods_in_ref_profile);
384 SetupBasicProfile("p1", 1, methods_in_profile, hot_methods_cur, empty_vector, empty_vector,
385 profile, &info1);
386 ProfileCompilationInfo info2;
387 SetupBasicProfile("p1", 1, methods_in_profile, hot_methods_ref, empty_vector, empty_vector,
388 reference_profile, &info2);
389 return ProcessProfiles(profile_fds, reference_profile_fd);
390 }
391
392 int CheckCompilationClassPercentChange(uint16_t classes_in_cur_profile,
393 uint16_t classes_in_ref_profile) {
394 ScratchFile profile;
395 ScratchFile reference_profile;
396
397 std::vector<int> profile_fds({ GetFd(profile)});
398 int reference_profile_fd = GetFd(reference_profile);
399
400 ProfileCompilationInfo info1;
401 SetupProfile("p1", 1, 0, classes_in_cur_profile, profile, &info1);
402 ProfileCompilationInfo info2;
403 SetupProfile("p1", 1, 0, classes_in_ref_profile, reference_profile, &info2);
404 return ProcessProfiles(profile_fds, reference_profile_fd);
405 }
406
Vladimir Marko69d310e2017-10-09 14:12:23 +0100407 std::unique_ptr<ArenaAllocator> allocator_;
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700408
409 // Cache of inline caches generated during tests.
410 // This makes it easier to pass data between different utilities and ensure that
411 // caches are destructed at the end of the test.
412 std::vector<std::unique_ptr<ProfileCompilationInfo::InlineCacheMap>> used_inline_caches;
Calin Juravle2e2db782016-02-23 12:00:03 +0000413};
414
415TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferences) {
416 ScratchFile profile1;
417 ScratchFile profile2;
418 ScratchFile reference_profile;
419
420 std::vector<int> profile_fds({
421 GetFd(profile1),
422 GetFd(profile2)});
423 int reference_profile_fd = GetFd(reference_profile);
424
425 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
426 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100427 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000428 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100429 SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000430
431 // We should advise compilation.
432 ASSERT_EQ(ProfileAssistant::kCompile,
433 ProcessProfiles(profile_fds, reference_profile_fd));
434 // The resulting compilation info must be equal to the merge of the inputs.
435 ProfileCompilationInfo result;
436 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
437 ASSERT_TRUE(result.Load(reference_profile_fd));
438
439 ProfileCompilationInfo expected;
Calin Juravle67265462016-03-18 16:23:40 +0000440 ASSERT_TRUE(expected.MergeWith(info1));
441 ASSERT_TRUE(expected.MergeWith(info2));
Calin Juravle2e2db782016-02-23 12:00:03 +0000442 ASSERT_TRUE(expected.Equals(result));
443
444 // The information from profiles must remain the same.
445 CheckProfileInfo(profile1, info1);
446 CheckProfileInfo(profile2, info2);
447}
448
Calin Juravlec824b512016-03-29 20:33:33 +0100449// TODO(calin): Add more tests for classes.
450TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferencesBecauseOfClasses) {
451 ScratchFile profile1;
452 ScratchFile reference_profile;
453
454 std::vector<int> profile_fds({
455 GetFd(profile1)});
456 int reference_profile_fd = GetFd(reference_profile);
457
458 const uint16_t kNumberOfClassesToEnableCompilation = 100;
459 ProfileCompilationInfo info1;
460 SetupProfile("p1", 1, 0, kNumberOfClassesToEnableCompilation, profile1, &info1);
461
462 // We should advise compilation.
463 ASSERT_EQ(ProfileAssistant::kCompile,
464 ProcessProfiles(profile_fds, reference_profile_fd));
465 // The resulting compilation info must be equal to the merge of the inputs.
466 ProfileCompilationInfo result;
467 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
468 ASSERT_TRUE(result.Load(reference_profile_fd));
469
470 ProfileCompilationInfo expected;
471 ASSERT_TRUE(expected.MergeWith(info1));
472 ASSERT_TRUE(expected.Equals(result));
473
474 // The information from profiles must remain the same.
475 CheckProfileInfo(profile1, info1);
476}
477
Calin Juravle2e2db782016-02-23 12:00:03 +0000478TEST_F(ProfileAssistantTest, AdviseCompilationNonEmptyReferences) {
479 ScratchFile profile1;
480 ScratchFile profile2;
481 ScratchFile reference_profile;
482
483 std::vector<int> profile_fds({
484 GetFd(profile1),
485 GetFd(profile2)});
486 int reference_profile_fd = GetFd(reference_profile);
487
488 // The new profile info will contain the methods with indices 0-100.
489 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
490 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100491 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000492 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100493 SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000494
495
496 // The reference profile info will contain the methods with indices 50-150.
497 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
498 ProfileCompilationInfo reference_info;
Calin Juravlec824b512016-03-29 20:33:33 +0100499 SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
Calin Juravle2e2db782016-02-23 12:00:03 +0000500 &reference_info, kNumberOfMethodsToEnableCompilation / 2);
501
502 // We should advise compilation.
503 ASSERT_EQ(ProfileAssistant::kCompile,
504 ProcessProfiles(profile_fds, reference_profile_fd));
505
506 // The resulting compilation info must be equal to the merge of the inputs
507 ProfileCompilationInfo result;
508 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
509 ASSERT_TRUE(result.Load(reference_profile_fd));
510
511 ProfileCompilationInfo expected;
Calin Juravle67265462016-03-18 16:23:40 +0000512 ASSERT_TRUE(expected.MergeWith(info1));
513 ASSERT_TRUE(expected.MergeWith(info2));
514 ASSERT_TRUE(expected.MergeWith(reference_info));
Calin Juravle2e2db782016-02-23 12:00:03 +0000515 ASSERT_TRUE(expected.Equals(result));
516
517 // The information from profiles must remain the same.
518 CheckProfileInfo(profile1, info1);
519 CheckProfileInfo(profile2, info2);
520}
521
522TEST_F(ProfileAssistantTest, DoNotAdviseCompilation) {
523 ScratchFile profile1;
524 ScratchFile profile2;
525 ScratchFile reference_profile;
526
527 std::vector<int> profile_fds({
528 GetFd(profile1),
529 GetFd(profile2)});
530 int reference_profile_fd = GetFd(reference_profile);
531
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -0700532 const uint16_t kNumberOfMethodsToSkipCompilation = 24; // Threshold is 100.
Calin Juravle2e2db782016-02-23 12:00:03 +0000533 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100534 SetupProfile("p1", 1, kNumberOfMethodsToSkipCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000535 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100536 SetupProfile("p2", 2, kNumberOfMethodsToSkipCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000537
538 // We should not advise compilation.
539 ASSERT_EQ(ProfileAssistant::kSkipCompilation,
540 ProcessProfiles(profile_fds, reference_profile_fd));
541
542 // The information from profiles must remain the same.
543 ProfileCompilationInfo file_info1;
544 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
545 ASSERT_TRUE(file_info1.Load(GetFd(profile1)));
546 ASSERT_TRUE(file_info1.Equals(info1));
547
548 ProfileCompilationInfo file_info2;
549 ASSERT_TRUE(profile2.GetFile()->ResetOffset());
550 ASSERT_TRUE(file_info2.Load(GetFd(profile2)));
551 ASSERT_TRUE(file_info2.Equals(info2));
552
553 // Reference profile files must remain empty.
554 ASSERT_EQ(0, reference_profile.GetFile()->GetLength());
555
556 // The information from profiles must remain the same.
557 CheckProfileInfo(profile1, info1);
558 CheckProfileInfo(profile2, info2);
559}
560
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -0700561TEST_F(ProfileAssistantTest, DoNotAdviseCompilationMethodPercentage) {
562 const uint16_t kNumberOfMethodsInRefProfile = 6000;
563 const uint16_t kNumberOfMethodsInCurProfile = 6100; // Threshold is 2%.
564 // We should not advise compilation.
565 ASSERT_EQ(ProfileAssistant::kSkipCompilation,
566 CheckCompilationMethodPercentChange(kNumberOfMethodsInCurProfile,
567 kNumberOfMethodsInRefProfile));
568}
569
570TEST_F(ProfileAssistantTest, ShouldAdviseCompilationMethodPercentage) {
571 const uint16_t kNumberOfMethodsInRefProfile = 6000;
572 const uint16_t kNumberOfMethodsInCurProfile = 6200; // Threshold is 2%.
573 // We should advise compilation.
574 ASSERT_EQ(ProfileAssistant::kCompile,
575 CheckCompilationMethodPercentChange(kNumberOfMethodsInCurProfile,
576 kNumberOfMethodsInRefProfile));
577}
578
579TEST_F(ProfileAssistantTest, DoNotdviseCompilationClassPercentage) {
580 const uint16_t kNumberOfClassesInRefProfile = 6000;
581 const uint16_t kNumberOfClassesInCurProfile = 6110; // Threshold is 2%.
582 // We should not advise compilation.
583 ASSERT_EQ(ProfileAssistant::kSkipCompilation,
584 CheckCompilationClassPercentChange(kNumberOfClassesInCurProfile,
585 kNumberOfClassesInRefProfile));
586}
587
588TEST_F(ProfileAssistantTest, ShouldAdviseCompilationClassPercentage) {
589 const uint16_t kNumberOfClassesInRefProfile = 6000;
590 const uint16_t kNumberOfClassesInCurProfile = 6120; // Threshold is 2%.
591 // We should advise compilation.
592 ASSERT_EQ(ProfileAssistant::kCompile,
593 CheckCompilationClassPercentChange(kNumberOfClassesInCurProfile,
594 kNumberOfClassesInRefProfile));
595}
596
Calin Juravle2e2db782016-02-23 12:00:03 +0000597TEST_F(ProfileAssistantTest, FailProcessingBecauseOfProfiles) {
598 ScratchFile profile1;
599 ScratchFile profile2;
600 ScratchFile reference_profile;
601
602 std::vector<int> profile_fds({
603 GetFd(profile1),
604 GetFd(profile2)});
605 int reference_profile_fd = GetFd(reference_profile);
606
607 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
608 // Assign different hashes for the same dex file. This will make merging of information to fail.
609 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100610 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000611 ProfileCompilationInfo info2;
Calin Juravlec824b512016-03-29 20:33:33 +0100612 SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
Calin Juravle2e2db782016-02-23 12:00:03 +0000613
614 // We should fail processing.
615 ASSERT_EQ(ProfileAssistant::kErrorBadProfiles,
616 ProcessProfiles(profile_fds, reference_profile_fd));
617
618 // The information from profiles must remain the same.
619 CheckProfileInfo(profile1, info1);
620 CheckProfileInfo(profile2, info2);
621
622 // Reference profile files must still remain empty.
623 ASSERT_EQ(0, reference_profile.GetFile()->GetLength());
624}
625
626TEST_F(ProfileAssistantTest, FailProcessingBecauseOfReferenceProfiles) {
627 ScratchFile profile1;
628 ScratchFile reference_profile;
629
630 std::vector<int> profile_fds({
631 GetFd(profile1)});
632 int reference_profile_fd = GetFd(reference_profile);
633
634 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
635 // Assign different hashes for the same dex file. This will make merging of information to fail.
636 ProfileCompilationInfo info1;
Calin Juravlec824b512016-03-29 20:33:33 +0100637 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
Calin Juravle2e2db782016-02-23 12:00:03 +0000638 ProfileCompilationInfo reference_info;
Calin Juravlec824b512016-03-29 20:33:33 +0100639 SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, reference_profile, &reference_info);
Calin Juravle2e2db782016-02-23 12:00:03 +0000640
641 // We should not advise compilation.
642 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
643 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
644 ASSERT_EQ(ProfileAssistant::kErrorBadProfiles,
645 ProcessProfiles(profile_fds, reference_profile_fd));
646
647 // The information from profiles must remain the same.
648 CheckProfileInfo(profile1, info1);
649}
650
Calin Juravle7bcdb532016-06-07 16:14:47 +0100651TEST_F(ProfileAssistantTest, TestProfileGeneration) {
652 ScratchFile profile;
653 // Generate a test profile.
654 GenerateTestProfile(profile.GetFilename());
655
656 // Verify that the generated profile is valid and can be loaded.
657 ASSERT_TRUE(profile.GetFile()->ResetOffset());
658 ProfileCompilationInfo info;
659 ASSERT_TRUE(info.Load(GetFd(profile)));
660}
661
Jeff Haof0a31f82017-03-27 15:50:37 -0700662TEST_F(ProfileAssistantTest, TestProfileGenerationWithIndexDex) {
663 ScratchFile profile;
664 // Generate a test profile passing in a dex file as reference.
665 GenerateTestProfileWithInputDex(profile.GetFilename());
666
667 // Verify that the generated profile is valid and can be loaded.
668 ASSERT_TRUE(profile.GetFile()->ResetOffset());
669 ProfileCompilationInfo info;
670 ASSERT_TRUE(info.Load(GetFd(profile)));
671}
672
David Sehr7c80f2d2017-02-07 16:47:58 -0800673TEST_F(ProfileAssistantTest, TestProfileCreationAllMatch) {
674 // Class names put here need to be in sorted order.
675 std::vector<std::string> class_names = {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700676 "HLjava/lang/Object;-><init>()V",
Calin Juravlee0ac1152017-02-13 19:03:47 -0800677 "Ljava/lang/Comparable;",
678 "Ljava/lang/Math;",
Mathieu Chartier34067262017-04-06 13:55:46 -0700679 "Ljava/lang/Object;",
Mathieu Chartierea650f32017-05-24 12:04:13 -0700680 "SPLjava/lang/Comparable;->compareTo(Ljava/lang/Object;)I",
David Sehr7c80f2d2017-02-07 16:47:58 -0800681 };
Mathieu Chartier34067262017-04-06 13:55:46 -0700682 std::string file_contents;
David Sehr7c80f2d2017-02-07 16:47:58 -0800683 for (std::string& class_name : class_names) {
Mathieu Chartier34067262017-04-06 13:55:46 -0700684 file_contents += class_name + std::string("\n");
David Sehr7c80f2d2017-02-07 16:47:58 -0800685 }
686 std::string output_file_contents;
Mathieu Chartier34067262017-04-06 13:55:46 -0700687 ASSERT_TRUE(CreateAndDump(file_contents, &output_file_contents));
688 ASSERT_EQ(output_file_contents, file_contents);
David Sehr7c80f2d2017-02-07 16:47:58 -0800689}
690
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700691TEST_F(ProfileAssistantTest, TestProfileCreationGenerateMethods) {
692 // Class names put here need to be in sorted order.
693 std::vector<std::string> class_names = {
694 "Ljava/lang/Math;->*",
695 };
696 std::string input_file_contents;
697 std::string expected_contents;
698 for (std::string& class_name : class_names) {
699 input_file_contents += class_name + std::string("\n");
700 expected_contents += DescriptorToDot(class_name.c_str()) +
701 std::string("\n");
702 }
703 std::string output_file_contents;
704 ScratchFile profile_file;
705 EXPECT_TRUE(CreateProfile(input_file_contents,
706 profile_file.GetFilename(),
David Brazdilf13ac7c2018-01-30 10:09:08 +0000707 GetLibCoreDexFileNames()[0],
708 /* skip_verification */ true));
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700709 ProfileCompilationInfo info;
710 profile_file.GetFile()->ResetOffset();
711 ASSERT_TRUE(info.Load(GetFd(profile_file)));
712 // Verify that the profile has matching methods.
713 ScopedObjectAccess soa(Thread::Current());
714 ObjPtr<mirror::Class> klass = GetClass(nullptr, "Ljava/lang/Math;");
715 ASSERT_TRUE(klass != nullptr);
716 size_t method_count = 0;
717 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
718 if (!method.IsCopied() && method.GetCodeItem() != nullptr) {
719 ++method_count;
Calin Juravlecc3171a2017-05-19 16:47:53 -0700720 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi =
721 info.GetMethod(method.GetDexFile()->GetLocation(),
722 method.GetDexFile()->GetLocationChecksum(),
723 method.GetDexMethodIndex());
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700724 ASSERT_TRUE(pmi != nullptr) << method.PrettyMethod();
Mathieu Chartierd808e8b2017-03-21 13:37:41 -0700725 }
726 }
727 EXPECT_GT(method_count, 0u);
728}
729
Mathieu Chartier2f794552017-06-19 10:58:08 -0700730TEST_F(ProfileAssistantTest, TestBootImageProfile) {
731 const std::string core_dex = GetLibCoreDexFileNames()[0];
732
733 std::vector<ScratchFile> profiles;
734
735 // In image with enough clean occurrences.
736 const std::string kCleanClass = "Ljava/lang/CharSequence;";
737 // In image with enough dirty occurrences.
738 const std::string kDirtyClass = "Ljava/lang/Object;";
739 // Not in image becauseof not enough occurrences.
740 const std::string kUncommonCleanClass = "Ljava/lang/Process;";
741 const std::string kUncommonDirtyClass = "Ljava/lang/Package;";
742 // Method that is hot.
743 // Also adds the class through inference since it is in each dex.
744 const std::string kHotMethod = "Ljava/lang/Comparable;->compareTo(Ljava/lang/Object;)I";
745 // Method that doesn't add the class since its only in one profile. Should still show up in the
746 // boot profile.
747 const std::string kOtherMethod = "Ljava/util/HashMap;-><init>()V";
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700748 // Method that gets marked as hot since it's in multiple profiles.
749 const std::string kMultiMethod = "Ljava/util/ArrayList;->clear()V";
Mathieu Chartier2f794552017-06-19 10:58:08 -0700750
751 // Thresholds for this test.
752 static const size_t kDirtyThreshold = 3;
753 static const size_t kCleanThreshold = 2;
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700754 static const size_t kMethodThreshold = 2;
Mathieu Chartier2f794552017-06-19 10:58:08 -0700755
756 // Create a bunch of boot profiles.
757 std::string dex1 =
758 kCleanClass + "\n" +
759 kDirtyClass + "\n" +
760 kUncommonCleanClass + "\n" +
761 "H" + kHotMethod + "\n" +
762 kUncommonDirtyClass;
763 profiles.emplace_back(ScratchFile());
David Brazdilf13ac7c2018-01-30 10:09:08 +0000764 EXPECT_TRUE(CreateProfile(
765 dex1, profiles.back().GetFilename(), core_dex, /* skip_verification */ true));
Mathieu Chartier2f794552017-06-19 10:58:08 -0700766
767 // Create a bunch of boot profiles.
768 std::string dex2 =
769 kCleanClass + "\n" +
770 kDirtyClass + "\n" +
771 "P" + kHotMethod + "\n" +
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700772 "P" + kMultiMethod + "\n" +
Mathieu Chartier2f794552017-06-19 10:58:08 -0700773 kUncommonDirtyClass;
774 profiles.emplace_back(ScratchFile());
David Brazdilf13ac7c2018-01-30 10:09:08 +0000775 EXPECT_TRUE(CreateProfile(
776 dex2, profiles.back().GetFilename(), core_dex, /* skip_verification */ true));
Mathieu Chartier2f794552017-06-19 10:58:08 -0700777
778 // Create a bunch of boot profiles.
779 std::string dex3 =
780 "S" + kHotMethod + "\n" +
781 "P" + kOtherMethod + "\n" +
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700782 "P" + kMultiMethod + "\n" +
Mathieu Chartier2f794552017-06-19 10:58:08 -0700783 kDirtyClass + "\n";
784 profiles.emplace_back(ScratchFile());
David Brazdilf13ac7c2018-01-30 10:09:08 +0000785 EXPECT_TRUE(CreateProfile(
786 dex3, profiles.back().GetFilename(), core_dex, /* skip_verification */ true));
Mathieu Chartier2f794552017-06-19 10:58:08 -0700787
788 // Generate the boot profile.
789 ScratchFile out_profile;
790 std::vector<std::string> args;
791 args.push_back(GetProfmanCmd());
792 args.push_back("--generate-boot-image-profile");
793 args.push_back("--boot-image-class-threshold=" + std::to_string(kDirtyThreshold));
794 args.push_back("--boot-image-clean-class-threshold=" + std::to_string(kCleanThreshold));
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700795 args.push_back("--boot-image-sampled-method-threshold=" + std::to_string(kMethodThreshold));
Mathieu Chartier2f794552017-06-19 10:58:08 -0700796 args.push_back("--reference-profile-file=" + out_profile.GetFilename());
797 args.push_back("--apk=" + core_dex);
798 args.push_back("--dex-location=" + core_dex);
David Brazdilf13ac7c2018-01-30 10:09:08 +0000799 args.push_back("--skip-apk-verification");
Mathieu Chartier2f794552017-06-19 10:58:08 -0700800 for (const ScratchFile& profile : profiles) {
801 args.push_back("--profile-file=" + profile.GetFilename());
802 }
803 std::string error;
804 EXPECT_EQ(ExecAndReturnCode(args, &error), 0) << error;
805 ASSERT_EQ(0, out_profile.GetFile()->Flush());
806 ASSERT_TRUE(out_profile.GetFile()->ResetOffset());
807
808 // Verify the boot profile contents.
809 std::string output_file_contents;
810 EXPECT_TRUE(DumpClassesAndMethods(out_profile.GetFilename(), &output_file_contents));
811 // Common classes, should be in the classes of the profile.
812 EXPECT_NE(output_file_contents.find(kCleanClass + "\n"), std::string::npos)
813 << output_file_contents;
814 EXPECT_NE(output_file_contents.find(kDirtyClass + "\n"), std::string::npos)
815 << output_file_contents;
816 // Uncommon classes, should not fit preloaded class criteria and should not be in the profile.
817 EXPECT_EQ(output_file_contents.find(kUncommonCleanClass + "\n"), std::string::npos)
818 << output_file_contents;
819 EXPECT_EQ(output_file_contents.find(kUncommonDirtyClass + "\n"), std::string::npos)
820 << output_file_contents;
821 // Inferred class from a method common to all three profiles.
822 EXPECT_NE(output_file_contents.find("Ljava/lang/Comparable;\n"), std::string::npos)
823 << output_file_contents;
824 // Aggregated methods hotness information.
825 EXPECT_NE(output_file_contents.find("HSP" + kHotMethod), std::string::npos)
826 << output_file_contents;
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700827 EXPECT_NE(output_file_contents.find("P" + kOtherMethod), std::string::npos)
Mathieu Chartier2f794552017-06-19 10:58:08 -0700828 << output_file_contents;
829 // Not inferred class, method is only in one profile.
830 EXPECT_EQ(output_file_contents.find("Ljava/util/HashMap;\n"), std::string::npos)
831 << output_file_contents;
Mathieu Chartier8eecddf2017-07-12 16:05:54 -0700832 // Test the sampled methods that became hot.
833 // Other method is in only one profile, it should not become hot.
834 EXPECT_EQ(output_file_contents.find("HP" + kOtherMethod), std::string::npos)
835 << output_file_contents;
836 // Multi method is in at least two profiles, it should become hot.
837 EXPECT_NE(output_file_contents.find("HP" + kMultiMethod), std::string::npos)
838 << output_file_contents;
Mathieu Chartier2f794552017-06-19 10:58:08 -0700839}
840
David Sehr7c80f2d2017-02-07 16:47:58 -0800841TEST_F(ProfileAssistantTest, TestProfileCreationOneNotMatched) {
842 // Class names put here need to be in sorted order.
843 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800844 "Ldoesnt/match/this/one;",
845 "Ljava/lang/Comparable;",
846 "Ljava/lang/Object;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800847 };
848 std::string input_file_contents;
849 for (std::string& class_name : class_names) {
850 input_file_contents += class_name + std::string("\n");
851 }
852 std::string output_file_contents;
853 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
854 std::string expected_contents =
Mathieu Chartier34067262017-04-06 13:55:46 -0700855 class_names[1] + std::string("\n") +
856 class_names[2] + std::string("\n");
David Sehr7c80f2d2017-02-07 16:47:58 -0800857 ASSERT_EQ(output_file_contents, expected_contents);
858}
859
860TEST_F(ProfileAssistantTest, TestProfileCreationNoneMatched) {
861 // Class names put here need to be in sorted order.
862 std::vector<std::string> class_names = {
Calin Juravlee0ac1152017-02-13 19:03:47 -0800863 "Ldoesnt/match/this/one;",
864 "Ldoesnt/match/this/one/either;",
865 "Lnor/this/one;"
David Sehr7c80f2d2017-02-07 16:47:58 -0800866 };
867 std::string input_file_contents;
868 for (std::string& class_name : class_names) {
869 input_file_contents += class_name + std::string("\n");
870 }
871 std::string output_file_contents;
872 ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents));
873 std::string expected_contents("");
874 ASSERT_EQ(output_file_contents, expected_contents);
875}
876
Calin Juravlee0ac1152017-02-13 19:03:47 -0800877TEST_F(ProfileAssistantTest, TestProfileCreateInlineCache) {
878 // Create the profile content.
879 std::vector<std::string> methods = {
880 "LTestInline;->inlineMonomorphic(LSuper;)I+LSubA;",
881 "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;",
882 "LTestInline;->inlineMegamorphic(LSuper;)I+LSubA;,LSubB;,LSubC;,LSubD;,LSubE;",
Calin Juravle589e71e2017-03-03 16:05:05 -0800883 "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types",
Calin Juravlee0ac1152017-02-13 19:03:47 -0800884 "LTestInline;->noInlineCache(LSuper;)I"
885 };
886 std::string input_file_contents;
887 for (std::string& m : methods) {
888 input_file_contents += m + std::string("\n");
889 }
890
891 // Create the profile and save it to disk.
892 ScratchFile profile_file;
893 ASSERT_TRUE(CreateProfile(input_file_contents,
894 profile_file.GetFilename(),
David Brazdilf13ac7c2018-01-30 10:09:08 +0000895 GetTestDexFileName("ProfileTestMultiDex"),
896 /* skip_verification */ false));
Calin Juravlee0ac1152017-02-13 19:03:47 -0800897
898 // Load the profile from disk.
899 ProfileCompilationInfo info;
900 profile_file.GetFile()->ResetOffset();
901 ASSERT_TRUE(info.Load(GetFd(profile_file)));
902
903 // Load the dex files and verify that the profile contains the expected methods info.
904 ScopedObjectAccess soa(Thread::Current());
905 jobject class_loader = LoadDex("ProfileTestMultiDex");
906 ASSERT_NE(class_loader, nullptr);
907
908 mirror::Class* sub_a = GetClass(class_loader, "LSubA;");
909 mirror::Class* sub_b = GetClass(class_loader, "LSubB;");
910 mirror::Class* sub_c = GetClass(class_loader, "LSubC;");
911
912 ASSERT_TRUE(sub_a != nullptr);
913 ASSERT_TRUE(sub_b != nullptr);
914 ASSERT_TRUE(sub_c != nullptr);
915
916 {
917 // Verify that method inlineMonomorphic has the expected inline caches and nothing else.
918 ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader,
919 "LTestInline;",
920 "inlineMonomorphic");
921 ASSERT_TRUE(inline_monomorphic != nullptr);
922 std::set<mirror::Class*> expected_monomorphic;
923 expected_monomorphic.insert(sub_a);
Calin Juravle589e71e2017-03-03 16:05:05 -0800924 AssertInlineCaches(inline_monomorphic,
925 expected_monomorphic,
926 info,
927 /*megamorphic*/false,
928 /*missing_types*/false);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800929 }
930
931 {
932 // Verify that method inlinePolymorphic has the expected inline caches and nothing else.
933 ArtMethod* inline_polymorhic = GetVirtualMethod(class_loader,
934 "LTestInline;",
935 "inlinePolymorphic");
936 ASSERT_TRUE(inline_polymorhic != nullptr);
937 std::set<mirror::Class*> expected_polymorphic;
938 expected_polymorphic.insert(sub_a);
939 expected_polymorphic.insert(sub_b);
940 expected_polymorphic.insert(sub_c);
Calin Juravle589e71e2017-03-03 16:05:05 -0800941 AssertInlineCaches(inline_polymorhic,
942 expected_polymorphic,
943 info,
944 /*megamorphic*/false,
945 /*missing_types*/false);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800946 }
947
948 {
949 // Verify that method inlineMegamorphic has the expected inline caches and nothing else.
950 ArtMethod* inline_megamorphic = GetVirtualMethod(class_loader,
951 "LTestInline;",
952 "inlineMegamorphic");
953 ASSERT_TRUE(inline_megamorphic != nullptr);
954 std::set<mirror::Class*> expected_megamorphic;
Calin Juravle589e71e2017-03-03 16:05:05 -0800955 AssertInlineCaches(inline_megamorphic,
956 expected_megamorphic,
957 info,
958 /*megamorphic*/true,
959 /*missing_types*/false);
960 }
961
962 {
963 // Verify that method inlineMegamorphic has the expected inline caches and nothing else.
964 ArtMethod* inline_missing_types = GetVirtualMethod(class_loader,
965 "LTestInline;",
966 "inlineMissingTypes");
967 ASSERT_TRUE(inline_missing_types != nullptr);
968 std::set<mirror::Class*> expected_missing_Types;
969 AssertInlineCaches(inline_missing_types,
970 expected_missing_Types,
971 info,
972 /*megamorphic*/false,
973 /*missing_types*/true);
Calin Juravlee0ac1152017-02-13 19:03:47 -0800974 }
975
976 {
977 // Verify that method noInlineCache has no inline caches in the profile.
978 ArtMethod* no_inline_cache = GetVirtualMethod(class_loader, "LTestInline;", "noInlineCache");
979 ASSERT_TRUE(no_inline_cache != nullptr);
Calin Juravlecc3171a2017-05-19 16:47:53 -0700980 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi_no_inline_cache =
981 info.GetMethod(no_inline_cache->GetDexFile()->GetLocation(),
982 no_inline_cache->GetDexFile()->GetLocationChecksum(),
983 no_inline_cache->GetDexMethodIndex());
984 ASSERT_TRUE(pmi_no_inline_cache != nullptr);
Calin Juravlee6f87cc2017-05-24 17:41:05 -0700985 ASSERT_TRUE(pmi_no_inline_cache->inline_caches->empty());
Calin Juravlee0ac1152017-02-13 19:03:47 -0800986 }
987}
988
Calin Juravlecea9e9d2017-03-23 19:04:59 -0700989TEST_F(ProfileAssistantTest, MergeProfilesWithDifferentDexOrder) {
990 ScratchFile profile1;
991 ScratchFile reference_profile;
992
993 std::vector<int> profile_fds({GetFd(profile1)});
994 int reference_profile_fd = GetFd(reference_profile);
995
996 // The new profile info will contain the methods with indices 0-100.
997 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
998 ProfileCompilationInfo info1;
999 SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1,
1000 /*start_method_index*/0, /*reverse_dex_write_order*/false);
1001
1002 // The reference profile info will contain the methods with indices 50-150.
1003 // When setting up the profile reverse the order in which the dex files
1004 // are added to the profile. This will verify that profman merges profiles
1005 // with a different dex order correctly.
1006 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
1007 ProfileCompilationInfo reference_info;
1008 SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
1009 &reference_info, kNumberOfMethodsToEnableCompilation / 2, /*reverse_dex_write_order*/true);
1010
1011 // We should advise compilation.
1012 ASSERT_EQ(ProfileAssistant::kCompile,
1013 ProcessProfiles(profile_fds, reference_profile_fd));
1014
1015 // The resulting compilation info must be equal to the merge of the inputs.
1016 ProfileCompilationInfo result;
1017 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
1018 ASSERT_TRUE(result.Load(reference_profile_fd));
1019
1020 ProfileCompilationInfo expected;
1021 ASSERT_TRUE(expected.MergeWith(reference_info));
1022 ASSERT_TRUE(expected.MergeWith(info1));
1023 ASSERT_TRUE(expected.Equals(result));
1024
1025 // The information from profile must remain the same.
1026 CheckProfileInfo(profile1, info1);
1027}
1028
Calin Juravle08556882017-05-26 16:40:45 -07001029TEST_F(ProfileAssistantTest, TestProfileCreateWithInvalidData) {
1030 // Create the profile content.
1031 std::vector<std::string> profile_methods = {
1032 "LTestInline;->inlineMonomorphic(LSuper;)I+invalid_class",
1033 "LTestInline;->invalid_method",
1034 "invalid_class"
1035 };
1036 std::string input_file_contents;
1037 for (std::string& m : profile_methods) {
1038 input_file_contents += m + std::string("\n");
1039 }
1040
1041 // Create the profile and save it to disk.
1042 ScratchFile profile_file;
1043 std::string dex_filename = GetTestDexFileName("ProfileTestMultiDex");
1044 ASSERT_TRUE(CreateProfile(input_file_contents,
1045 profile_file.GetFilename(),
David Brazdilf13ac7c2018-01-30 10:09:08 +00001046 dex_filename,
1047 /* skip_verification */ false));
Calin Juravle08556882017-05-26 16:40:45 -07001048
1049 // Load the profile from disk.
1050 ProfileCompilationInfo info;
1051 profile_file.GetFile()->ResetOffset();
1052 ASSERT_TRUE(info.Load(GetFd(profile_file)));
1053
1054 // Load the dex files and verify that the profile contains the expected methods info.
1055 ScopedObjectAccess soa(Thread::Current());
1056 jobject class_loader = LoadDex("ProfileTestMultiDex");
1057 ASSERT_NE(class_loader, nullptr);
1058
1059 ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader,
1060 "LTestInline;",
1061 "inlineMonomorphic");
1062 const DexFile* dex_file = inline_monomorphic->GetDexFile();
1063
1064 // Verify that the inline cache contains the invalid type.
1065 std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi =
1066 info.GetMethod(dex_file->GetLocation(),
1067 dex_file->GetLocationChecksum(),
1068 inline_monomorphic->GetDexMethodIndex());
1069 ASSERT_TRUE(pmi != nullptr);
1070 ASSERT_EQ(pmi->inline_caches->size(), 1u);
1071 const ProfileCompilationInfo::DexPcData& dex_pc_data = pmi->inline_caches->begin()->second;
1072 dex::TypeIndex invalid_class_index(std::numeric_limits<uint16_t>::max() - 1);
1073 ASSERT_EQ(1u, dex_pc_data.classes.size());
1074 ASSERT_EQ(invalid_class_index, dex_pc_data.classes.begin()->type_index);
1075
1076 // Verify that the start-up classes contain the invalid class.
1077 std::set<dex::TypeIndex> classes;
Mathieu Chartierea650f32017-05-24 12:04:13 -07001078 std::set<uint16_t> hot_methods;
1079 std::set<uint16_t> startup_methods;
1080 std::set<uint16_t> post_start_methods;
1081 ASSERT_TRUE(info.GetClassesAndMethods(*dex_file,
1082 &classes,
1083 &hot_methods,
1084 &startup_methods,
1085 &post_start_methods));
Calin Juravle08556882017-05-26 16:40:45 -07001086 ASSERT_EQ(1u, classes.size());
1087 ASSERT_TRUE(classes.find(invalid_class_index) != classes.end());
1088
1089 // Verify that the invalid method is in the profile.
Mathieu Chartierea650f32017-05-24 12:04:13 -07001090 ASSERT_EQ(2u, hot_methods.size());
Calin Juravle08556882017-05-26 16:40:45 -07001091 uint16_t invalid_method_index = std::numeric_limits<uint16_t>::max() - 1;
Mathieu Chartierea650f32017-05-24 12:04:13 -07001092 ASSERT_TRUE(hot_methods.find(invalid_method_index) != hot_methods.end());
Calin Juravle08556882017-05-26 16:40:45 -07001093}
1094
Mathieu Chartier28b5c582017-06-06 14:12:50 -07001095TEST_F(ProfileAssistantTest, DumpOnly) {
1096 ScratchFile profile;
1097
1098 const uint32_t kNumberOfMethods = 64;
1099 std::vector<uint32_t> hot_methods;
1100 std::vector<uint32_t> startup_methods;
1101 std::vector<uint32_t> post_startup_methods;
1102 for (size_t i = 0; i < kNumberOfMethods; ++i) {
1103 if (i % 2 == 0) {
1104 hot_methods.push_back(i);
1105 }
1106 if (i % 3 == 1) {
1107 startup_methods.push_back(i);
1108 }
1109 if (i % 4 == 2) {
1110 post_startup_methods.push_back(i);
1111 }
1112 }
1113 EXPECT_GT(hot_methods.size(), 0u);
1114 EXPECT_GT(startup_methods.size(), 0u);
1115 EXPECT_GT(post_startup_methods.size(), 0u);
1116 ProfileCompilationInfo info1;
1117 SetupBasicProfile("p1",
1118 1,
1119 kNumberOfMethods,
1120 hot_methods,
1121 startup_methods,
1122 post_startup_methods,
1123 profile,
1124 &info1);
1125 std::string output;
1126 DumpOnly(profile.GetFilename(), &output);
1127 const size_t hot_offset = output.find("hot methods:");
1128 const size_t startup_offset = output.find("startup methods:");
1129 const size_t post_startup_offset = output.find("post startup methods:");
1130 const size_t classes_offset = output.find("classes:");
1131 ASSERT_NE(hot_offset, std::string::npos);
1132 ASSERT_NE(startup_offset, std::string::npos);
1133 ASSERT_NE(post_startup_offset, std::string::npos);
1134 ASSERT_LT(hot_offset, startup_offset);
1135 ASSERT_LT(startup_offset, post_startup_offset);
1136 // Check the actual contents of the dump by looking at the offsets of the methods.
1137 for (uint32_t m : hot_methods) {
1138 const size_t pos = output.find(std::to_string(m) + "[],", hot_offset);
1139 ASSERT_NE(pos, std::string::npos);
1140 EXPECT_LT(pos, startup_offset);
1141 }
1142 for (uint32_t m : startup_methods) {
1143 const size_t pos = output.find(std::to_string(m) + ",", startup_offset);
1144 ASSERT_NE(pos, std::string::npos);
1145 EXPECT_LT(pos, post_startup_offset);
1146 }
1147 for (uint32_t m : post_startup_methods) {
1148 const size_t pos = output.find(std::to_string(m) + ",", post_startup_offset);
1149 ASSERT_NE(pos, std::string::npos);
1150 EXPECT_LT(pos, classes_offset);
1151 }
1152}
1153
Calin Juravlee10c1e22018-01-26 20:10:15 -08001154TEST_F(ProfileAssistantTest, MergeProfilesWithFilter) {
1155 ScratchFile profile1;
1156 ScratchFile profile2;
1157 ScratchFile reference_profile;
1158
1159 std::vector<int> profile_fds({
1160 GetFd(profile1),
1161 GetFd(profile2)});
1162 int reference_profile_fd = GetFd(reference_profile);
1163
1164 // Use a real dex file to generate profile test data.
1165 // The file will be used during merging to filter unwanted data.
1166 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("ProfileTestMultiDex");
1167 const DexFile& d1 = *dex_files[0];
1168 const DexFile& d2 = *dex_files[1];
1169 // The new profile info will contain the methods with indices 0-100.
1170 const uint16_t kNumberOfMethodsToEnableCompilation = 100;
1171 ProfileCompilationInfo info1;
1172 SetupProfile(d1.GetLocation(), d1.GetLocationChecksum(), "p1", 1,
1173 kNumberOfMethodsToEnableCompilation, 0, profile1, &info1);
1174 ProfileCompilationInfo info2;
1175 SetupProfile(d2.GetLocation(), d2.GetLocationChecksum(), "p2", 2,
1176 kNumberOfMethodsToEnableCompilation, 0, profile2, &info2);
1177
1178
1179 // The reference profile info will contain the methods with indices 50-150.
1180 const uint16_t kNumberOfMethodsAlreadyCompiled = 100;
1181 ProfileCompilationInfo reference_info;
1182 SetupProfile(d1.GetLocation(), d1.GetLocationChecksum(), "p1", 1,
1183 kNumberOfMethodsAlreadyCompiled, 0, reference_profile,
1184 &reference_info, kNumberOfMethodsToEnableCompilation / 2);
1185
1186 // Run profman and pass the dex file with --apk-fd.
1187 android::base::unique_fd apk_fd(
1188 open(GetTestDexFileName("ProfileTestMultiDex").c_str(), O_RDONLY));
1189 ASSERT_GE(apk_fd.get(), 0);
1190
1191 std::string profman_cmd = GetProfmanCmd();
1192 std::vector<std::string> argv_str;
1193 argv_str.push_back(profman_cmd);
1194 argv_str.push_back("--profile-file-fd=" + std::to_string(profile1.GetFd()));
1195 argv_str.push_back("--profile-file-fd=" + std::to_string(profile2.GetFd()));
1196 argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile.GetFd()));
1197 argv_str.push_back("--apk-fd=" + std::to_string(apk_fd.get()));
1198 std::string error;
1199
1200 EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0) << error;
1201
1202 // Verify that we can load the result.
1203
1204 ProfileCompilationInfo result;
1205 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
1206 ASSERT_TRUE(result.Load(reference_profile_fd));
1207
1208
1209 ASSERT_TRUE(profile1.GetFile()->ResetOffset());
1210 ASSERT_TRUE(profile2.GetFile()->ResetOffset());
1211 ASSERT_TRUE(reference_profile.GetFile()->ResetOffset());
1212
1213 // Verify that the result filtered out data not belonging to the dex file.
1214 // This is equivalent to checking that the result is equal to the merging of
1215 // all profiles while filtering out data not belonging to the dex file.
1216
1217 ProfileCompilationInfo::ProfileLoadFilterFn filter_fn =
1218 [&d1, &d2](const std::string& dex_location, uint32_t checksum) -> bool {
1219 return (dex_location == ProfileCompilationInfo::GetProfileDexFileKey(d1.GetLocation())
1220 && checksum == d1.GetLocationChecksum())
1221 || (dex_location == ProfileCompilationInfo::GetProfileDexFileKey(d2.GetLocation())
1222 && checksum == d2.GetLocationChecksum());
1223 };
1224
1225 ProfileCompilationInfo info1_filter;
1226 ProfileCompilationInfo info2_filter;
1227 ProfileCompilationInfo expected;
1228
1229 info2_filter.Load(profile1.GetFd(), /*merge_classes*/ true, filter_fn);
1230 info2_filter.Load(profile2.GetFd(), /*merge_classes*/ true, filter_fn);
1231 expected.Load(reference_profile.GetFd(), /*merge_classes*/ true, filter_fn);
1232
1233 ASSERT_TRUE(expected.MergeWith(info1_filter));
1234 ASSERT_TRUE(expected.MergeWith(info2_filter));
1235
1236 ASSERT_TRUE(expected.Equals(result));
1237}
1238
Calin Juravle2e2db782016-02-23 12:00:03 +00001239} // namespace art