Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1 | /* |
| 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 Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 19 | #include "art_method-inl.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 20 | #include "base/unix_file/fd_file.h" |
| 21 | #include "common_runtime_test.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 22 | #include "exec_utils.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 23 | #include "jit/profile_compilation_info.h" |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 24 | #include "mirror/class-inl.h" |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame^] | 25 | #include "obj_ptr-inl.h" |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 26 | #include "profile_assistant.h" |
| 27 | #include "scoped_thread_state_change-inl.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 28 | #include "utils.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | class ProfileAssistantTest : public CommonRuntimeTest { |
| 33 | protected: |
| 34 | void SetupProfile(const std::string& id, |
| 35 | uint32_t checksum, |
| 36 | uint16_t number_of_methods, |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 37 | uint16_t number_of_classes, |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 38 | const ScratchFile& profile, |
| 39 | ProfileCompilationInfo* info, |
| 40 | uint16_t start_method_index = 0) { |
| 41 | std::string dex_location1 = "location1" + id; |
| 42 | uint32_t dex_location_checksum1 = checksum; |
| 43 | std::string dex_location2 = "location2" + id; |
| 44 | uint32_t dex_location_checksum2 = 10 * checksum; |
| 45 | for (uint16_t i = start_method_index; i < start_method_index + number_of_methods; i++) { |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 46 | ASSERT_TRUE(info->AddMethodIndex(dex_location1, dex_location_checksum1, i)); |
| 47 | ASSERT_TRUE(info->AddMethodIndex(dex_location2, dex_location_checksum2, i)); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 48 | } |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 49 | for (uint16_t i = 0; i < number_of_classes; i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 50 | ASSERT_TRUE(info->AddClassIndex(dex_location1, dex_location_checksum1, dex::TypeIndex(i))); |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 53 | ASSERT_TRUE(info->Save(GetFd(profile))); |
| 54 | ASSERT_EQ(0, profile.GetFile()->Flush()); |
| 55 | ASSERT_TRUE(profile.GetFile()->ResetOffset()); |
| 56 | } |
| 57 | |
| 58 | int GetFd(const ScratchFile& file) const { |
| 59 | return static_cast<int>(file.GetFd()); |
| 60 | } |
| 61 | |
| 62 | void CheckProfileInfo(ScratchFile& file, const ProfileCompilationInfo& info) { |
| 63 | ProfileCompilationInfo file_info; |
| 64 | ASSERT_TRUE(file.GetFile()->ResetOffset()); |
| 65 | ASSERT_TRUE(file_info.Load(GetFd(file))); |
| 66 | ASSERT_TRUE(file_info.Equals(info)); |
| 67 | } |
| 68 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 69 | std::string GetProfmanCmd() { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 70 | std::string file_path = GetTestAndroidRoot(); |
Calin Juravle | de4fb63 | 2016-02-23 16:53:30 +0000 | [diff] [blame] | 71 | file_path += "/bin/profman"; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 72 | if (kIsDebugBuild) { |
| 73 | file_path += "d"; |
| 74 | } |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 75 | EXPECT_TRUE(OS::FileExists(file_path.c_str())) |
| 76 | << file_path << " should be a valid file path"; |
| 77 | return file_path; |
| 78 | } |
| 79 | // Runs test with given arguments. |
| 80 | int ProcessProfiles(const std::vector<int>& profiles_fd, int reference_profile_fd) { |
| 81 | std::string profman_cmd = GetProfmanCmd(); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 82 | std::vector<std::string> argv_str; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 83 | argv_str.push_back(profman_cmd); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 84 | for (size_t k = 0; k < profiles_fd.size(); k++) { |
| 85 | argv_str.push_back("--profile-file-fd=" + std::to_string(profiles_fd[k])); |
| 86 | } |
| 87 | argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile_fd)); |
| 88 | |
| 89 | std::string error; |
| 90 | return ExecAndReturnCode(argv_str, &error); |
| 91 | } |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 92 | |
| 93 | bool GenerateTestProfile(const std::string& filename) { |
| 94 | std::string profman_cmd = GetProfmanCmd(); |
| 95 | std::vector<std::string> argv_str; |
| 96 | argv_str.push_back(profman_cmd); |
| 97 | argv_str.push_back("--generate-test-profile=" + filename); |
| 98 | std::string error; |
| 99 | return ExecAndReturnCode(argv_str, &error); |
| 100 | } |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 101 | |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 102 | bool CreateProfile(std::string profile_file_contents, |
| 103 | const std::string& filename, |
| 104 | const std::string& dex_location) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 105 | ScratchFile class_names_file; |
| 106 | File* file = class_names_file.GetFile(); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 107 | EXPECT_TRUE(file->WriteFully(profile_file_contents.c_str(), profile_file_contents.length())); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 108 | EXPECT_EQ(0, file->Flush()); |
| 109 | EXPECT_TRUE(file->ResetOffset()); |
| 110 | std::string profman_cmd = GetProfmanCmd(); |
| 111 | std::vector<std::string> argv_str; |
| 112 | argv_str.push_back(profman_cmd); |
| 113 | argv_str.push_back("--create-profile-from=" + class_names_file.GetFilename()); |
| 114 | argv_str.push_back("--reference-profile-file=" + filename); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 115 | argv_str.push_back("--apk=" + dex_location); |
| 116 | argv_str.push_back("--dex-location=" + dex_location); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 117 | std::string error; |
| 118 | EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0); |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | bool DumpClasses(const std::string& filename, std::string* file_contents) { |
| 123 | ScratchFile class_names_file; |
| 124 | std::string profman_cmd = GetProfmanCmd(); |
| 125 | std::vector<std::string> argv_str; |
| 126 | argv_str.push_back(profman_cmd); |
| 127 | argv_str.push_back("--dump-classes"); |
| 128 | argv_str.push_back("--profile-file=" + filename); |
| 129 | argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 130 | argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 131 | argv_str.push_back("--dump-output-to-fd=" + std::to_string(GetFd(class_names_file))); |
| 132 | std::string error; |
| 133 | EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0); |
| 134 | File* file = class_names_file.GetFile(); |
| 135 | EXPECT_EQ(0, file->Flush()); |
| 136 | EXPECT_TRUE(file->ResetOffset()); |
| 137 | int64_t length = file->GetLength(); |
| 138 | std::unique_ptr<char[]> buf(new char[length]); |
| 139 | EXPECT_EQ(file->Read(buf.get(), length, 0), length); |
| 140 | *file_contents = std::string(buf.get(), length); |
| 141 | return true; |
| 142 | } |
| 143 | |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame^] | 144 | bool CreateAndDump(const std::string& input_file_contents, |
| 145 | std::string* output_file_contents) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 146 | ScratchFile profile_file; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 147 | EXPECT_TRUE(CreateProfile(input_file_contents, |
| 148 | profile_file.GetFilename(), |
| 149 | GetLibCoreDexFileNames()[0])); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 150 | profile_file.GetFile()->ResetOffset(); |
| 151 | EXPECT_TRUE(DumpClasses(profile_file.GetFilename(), output_file_contents)); |
| 152 | return true; |
| 153 | } |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 154 | |
| 155 | mirror::Class* GetClass(jobject class_loader, const std::string& clazz) { |
| 156 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 157 | Thread* self = Thread::Current(); |
| 158 | ScopedObjectAccess soa(self); |
| 159 | StackHandleScope<1> hs(self); |
| 160 | Handle<mirror::ClassLoader> h_loader( |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame^] | 161 | hs.NewHandle(ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(class_loader)))); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 162 | return class_linker->FindClass(self, clazz.c_str(), h_loader); |
| 163 | } |
| 164 | |
| 165 | ArtMethod* GetVirtualMethod(jobject class_loader, |
| 166 | const std::string& clazz, |
| 167 | const std::string& name) { |
| 168 | mirror::Class* klass = GetClass(class_loader, clazz); |
| 169 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 170 | const auto pointer_size = class_linker->GetImagePointerSize(); |
| 171 | ArtMethod* method = nullptr; |
| 172 | Thread* self = Thread::Current(); |
| 173 | ScopedObjectAccess soa(self); |
| 174 | for (auto& m : klass->GetVirtualMethods(pointer_size)) { |
| 175 | if (name == m.GetName()) { |
| 176 | EXPECT_TRUE(method == nullptr); |
| 177 | method = &m; |
| 178 | } |
| 179 | } |
| 180 | return method; |
| 181 | } |
| 182 | |
| 183 | // Verify that given method has the expected inline caches and nothing else. |
| 184 | void AssertInlineCaches(ArtMethod* method, |
| 185 | const std::set<mirror::Class*>& expected_clases, |
| 186 | const ProfileCompilationInfo& info, |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 187 | bool is_megamorphic, |
| 188 | bool is_missing_types) |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 189 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 190 | ProfileCompilationInfo::OfflineProfileMethodInfo pmi; |
| 191 | ASSERT_TRUE(info.GetMethod(method->GetDexFile()->GetLocation(), |
| 192 | method->GetDexFile()->GetLocationChecksum(), |
| 193 | method->GetDexMethodIndex(), |
| 194 | &pmi)); |
| 195 | ASSERT_EQ(pmi.inline_caches.size(), 1u); |
| 196 | ProfileCompilationInfo::DexPcData dex_pc_data = pmi.inline_caches.begin()->second; |
| 197 | |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 198 | ASSERT_EQ(dex_pc_data.is_megamorphic, is_megamorphic); |
| 199 | ASSERT_EQ(dex_pc_data.is_missing_types, is_missing_types); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 200 | ASSERT_EQ(expected_clases.size(), dex_pc_data.classes.size()); |
| 201 | size_t found = 0; |
| 202 | for (mirror::Class* it : expected_clases) { |
| 203 | for (const auto& class_ref : dex_pc_data.classes) { |
| 204 | ProfileCompilationInfo::DexReference dex_ref = |
| 205 | pmi.dex_references[class_ref.dex_profile_index]; |
| 206 | if (dex_ref.MatchesDex(&(it->GetDexFile())) && |
| 207 | class_ref.type_index == it->GetDexTypeIndex()) { |
| 208 | found++; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | ASSERT_EQ(expected_clases.size(), found); |
| 214 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferences) { |
| 218 | ScratchFile profile1; |
| 219 | ScratchFile profile2; |
| 220 | ScratchFile reference_profile; |
| 221 | |
| 222 | std::vector<int> profile_fds({ |
| 223 | GetFd(profile1), |
| 224 | GetFd(profile2)}); |
| 225 | int reference_profile_fd = GetFd(reference_profile); |
| 226 | |
| 227 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 228 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 229 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 230 | ProfileCompilationInfo info2; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 231 | SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 232 | |
| 233 | // We should advise compilation. |
| 234 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 235 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 236 | // The resulting compilation info must be equal to the merge of the inputs. |
| 237 | ProfileCompilationInfo result; |
| 238 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 239 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 240 | |
| 241 | ProfileCompilationInfo expected; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 242 | ASSERT_TRUE(expected.MergeWith(info1)); |
| 243 | ASSERT_TRUE(expected.MergeWith(info2)); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 244 | ASSERT_TRUE(expected.Equals(result)); |
| 245 | |
| 246 | // The information from profiles must remain the same. |
| 247 | CheckProfileInfo(profile1, info1); |
| 248 | CheckProfileInfo(profile2, info2); |
| 249 | } |
| 250 | |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 251 | // TODO(calin): Add more tests for classes. |
| 252 | TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferencesBecauseOfClasses) { |
| 253 | ScratchFile profile1; |
| 254 | ScratchFile reference_profile; |
| 255 | |
| 256 | std::vector<int> profile_fds({ |
| 257 | GetFd(profile1)}); |
| 258 | int reference_profile_fd = GetFd(reference_profile); |
| 259 | |
| 260 | const uint16_t kNumberOfClassesToEnableCompilation = 100; |
| 261 | ProfileCompilationInfo info1; |
| 262 | SetupProfile("p1", 1, 0, kNumberOfClassesToEnableCompilation, profile1, &info1); |
| 263 | |
| 264 | // We should advise compilation. |
| 265 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 266 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 267 | // The resulting compilation info must be equal to the merge of the inputs. |
| 268 | ProfileCompilationInfo result; |
| 269 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 270 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 271 | |
| 272 | ProfileCompilationInfo expected; |
| 273 | ASSERT_TRUE(expected.MergeWith(info1)); |
| 274 | ASSERT_TRUE(expected.Equals(result)); |
| 275 | |
| 276 | // The information from profiles must remain the same. |
| 277 | CheckProfileInfo(profile1, info1); |
| 278 | } |
| 279 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 280 | TEST_F(ProfileAssistantTest, AdviseCompilationNonEmptyReferences) { |
| 281 | ScratchFile profile1; |
| 282 | ScratchFile profile2; |
| 283 | ScratchFile reference_profile; |
| 284 | |
| 285 | std::vector<int> profile_fds({ |
| 286 | GetFd(profile1), |
| 287 | GetFd(profile2)}); |
| 288 | int reference_profile_fd = GetFd(reference_profile); |
| 289 | |
| 290 | // The new profile info will contain the methods with indices 0-100. |
| 291 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 292 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 293 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 294 | ProfileCompilationInfo info2; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 295 | SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 296 | |
| 297 | |
| 298 | // The reference profile info will contain the methods with indices 50-150. |
| 299 | const uint16_t kNumberOfMethodsAlreadyCompiled = 100; |
| 300 | ProfileCompilationInfo reference_info; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 301 | SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile, |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 302 | &reference_info, kNumberOfMethodsToEnableCompilation / 2); |
| 303 | |
| 304 | // We should advise compilation. |
| 305 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 306 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 307 | |
| 308 | // The resulting compilation info must be equal to the merge of the inputs |
| 309 | ProfileCompilationInfo result; |
| 310 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 311 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 312 | |
| 313 | ProfileCompilationInfo expected; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 314 | ASSERT_TRUE(expected.MergeWith(info1)); |
| 315 | ASSERT_TRUE(expected.MergeWith(info2)); |
| 316 | ASSERT_TRUE(expected.MergeWith(reference_info)); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 317 | ASSERT_TRUE(expected.Equals(result)); |
| 318 | |
| 319 | // The information from profiles must remain the same. |
| 320 | CheckProfileInfo(profile1, info1); |
| 321 | CheckProfileInfo(profile2, info2); |
| 322 | } |
| 323 | |
| 324 | TEST_F(ProfileAssistantTest, DoNotAdviseCompilation) { |
| 325 | ScratchFile profile1; |
| 326 | ScratchFile profile2; |
| 327 | ScratchFile reference_profile; |
| 328 | |
| 329 | std::vector<int> profile_fds({ |
| 330 | GetFd(profile1), |
| 331 | GetFd(profile2)}); |
| 332 | int reference_profile_fd = GetFd(reference_profile); |
| 333 | |
| 334 | const uint16_t kNumberOfMethodsToSkipCompilation = 1; |
| 335 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 336 | SetupProfile("p1", 1, kNumberOfMethodsToSkipCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 337 | ProfileCompilationInfo info2; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 338 | SetupProfile("p2", 2, kNumberOfMethodsToSkipCompilation, 0, profile2, &info2); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 339 | |
| 340 | // We should not advise compilation. |
| 341 | ASSERT_EQ(ProfileAssistant::kSkipCompilation, |
| 342 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 343 | |
| 344 | // The information from profiles must remain the same. |
| 345 | ProfileCompilationInfo file_info1; |
| 346 | ASSERT_TRUE(profile1.GetFile()->ResetOffset()); |
| 347 | ASSERT_TRUE(file_info1.Load(GetFd(profile1))); |
| 348 | ASSERT_TRUE(file_info1.Equals(info1)); |
| 349 | |
| 350 | ProfileCompilationInfo file_info2; |
| 351 | ASSERT_TRUE(profile2.GetFile()->ResetOffset()); |
| 352 | ASSERT_TRUE(file_info2.Load(GetFd(profile2))); |
| 353 | ASSERT_TRUE(file_info2.Equals(info2)); |
| 354 | |
| 355 | // Reference profile files must remain empty. |
| 356 | ASSERT_EQ(0, reference_profile.GetFile()->GetLength()); |
| 357 | |
| 358 | // The information from profiles must remain the same. |
| 359 | CheckProfileInfo(profile1, info1); |
| 360 | CheckProfileInfo(profile2, info2); |
| 361 | } |
| 362 | |
| 363 | TEST_F(ProfileAssistantTest, FailProcessingBecauseOfProfiles) { |
| 364 | ScratchFile profile1; |
| 365 | ScratchFile profile2; |
| 366 | ScratchFile reference_profile; |
| 367 | |
| 368 | std::vector<int> profile_fds({ |
| 369 | GetFd(profile1), |
| 370 | GetFd(profile2)}); |
| 371 | int reference_profile_fd = GetFd(reference_profile); |
| 372 | |
| 373 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 374 | // Assign different hashes for the same dex file. This will make merging of information to fail. |
| 375 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 376 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 377 | ProfileCompilationInfo info2; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 378 | SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 379 | |
| 380 | // We should fail processing. |
| 381 | ASSERT_EQ(ProfileAssistant::kErrorBadProfiles, |
| 382 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 383 | |
| 384 | // The information from profiles must remain the same. |
| 385 | CheckProfileInfo(profile1, info1); |
| 386 | CheckProfileInfo(profile2, info2); |
| 387 | |
| 388 | // Reference profile files must still remain empty. |
| 389 | ASSERT_EQ(0, reference_profile.GetFile()->GetLength()); |
| 390 | } |
| 391 | |
| 392 | TEST_F(ProfileAssistantTest, FailProcessingBecauseOfReferenceProfiles) { |
| 393 | ScratchFile profile1; |
| 394 | ScratchFile reference_profile; |
| 395 | |
| 396 | std::vector<int> profile_fds({ |
| 397 | GetFd(profile1)}); |
| 398 | int reference_profile_fd = GetFd(reference_profile); |
| 399 | |
| 400 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 401 | // Assign different hashes for the same dex file. This will make merging of information to fail. |
| 402 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 403 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 404 | ProfileCompilationInfo reference_info; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 405 | SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, reference_profile, &reference_info); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 406 | |
| 407 | // We should not advise compilation. |
| 408 | ASSERT_TRUE(profile1.GetFile()->ResetOffset()); |
| 409 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 410 | ASSERT_EQ(ProfileAssistant::kErrorBadProfiles, |
| 411 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 412 | |
| 413 | // The information from profiles must remain the same. |
| 414 | CheckProfileInfo(profile1, info1); |
| 415 | } |
| 416 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 417 | TEST_F(ProfileAssistantTest, TestProfileGeneration) { |
| 418 | ScratchFile profile; |
| 419 | // Generate a test profile. |
| 420 | GenerateTestProfile(profile.GetFilename()); |
| 421 | |
| 422 | // Verify that the generated profile is valid and can be loaded. |
| 423 | ASSERT_TRUE(profile.GetFile()->ResetOffset()); |
| 424 | ProfileCompilationInfo info; |
| 425 | ASSERT_TRUE(info.Load(GetFd(profile))); |
| 426 | } |
| 427 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 428 | TEST_F(ProfileAssistantTest, TestProfileCreationAllMatch) { |
| 429 | // Class names put here need to be in sorted order. |
| 430 | std::vector<std::string> class_names = { |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 431 | "Ljava/lang/Comparable;", |
| 432 | "Ljava/lang/Math;", |
| 433 | "Ljava/lang/Object;" |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 434 | }; |
| 435 | std::string input_file_contents; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 436 | std::string expected_contents; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 437 | for (std::string& class_name : class_names) { |
| 438 | input_file_contents += class_name + std::string("\n"); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 439 | expected_contents += DescriptorToDot(class_name.c_str()) + |
| 440 | std::string("\n"); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 441 | } |
| 442 | std::string output_file_contents; |
| 443 | ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents)); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 444 | ASSERT_EQ(output_file_contents, expected_contents); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame^] | 447 | TEST_F(ProfileAssistantTest, TestProfileCreationGenerateMethods) { |
| 448 | // Class names put here need to be in sorted order. |
| 449 | std::vector<std::string> class_names = { |
| 450 | "Ljava/lang/Math;->*", |
| 451 | }; |
| 452 | std::string input_file_contents; |
| 453 | std::string expected_contents; |
| 454 | for (std::string& class_name : class_names) { |
| 455 | input_file_contents += class_name + std::string("\n"); |
| 456 | expected_contents += DescriptorToDot(class_name.c_str()) + |
| 457 | std::string("\n"); |
| 458 | } |
| 459 | std::string output_file_contents; |
| 460 | ScratchFile profile_file; |
| 461 | EXPECT_TRUE(CreateProfile(input_file_contents, |
| 462 | profile_file.GetFilename(), |
| 463 | GetLibCoreDexFileNames()[0])); |
| 464 | ProfileCompilationInfo info; |
| 465 | profile_file.GetFile()->ResetOffset(); |
| 466 | ASSERT_TRUE(info.Load(GetFd(profile_file))); |
| 467 | // Verify that the profile has matching methods. |
| 468 | ScopedObjectAccess soa(Thread::Current()); |
| 469 | ObjPtr<mirror::Class> klass = GetClass(nullptr, "Ljava/lang/Math;"); |
| 470 | ASSERT_TRUE(klass != nullptr); |
| 471 | size_t method_count = 0; |
| 472 | for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) { |
| 473 | if (!method.IsCopied() && method.GetCodeItem() != nullptr) { |
| 474 | ++method_count; |
| 475 | ProfileCompilationInfo::OfflineProfileMethodInfo pmi; |
| 476 | ASSERT_TRUE(info.GetMethod(method.GetDexFile()->GetLocation(), |
| 477 | method.GetDexFile()->GetLocationChecksum(), |
| 478 | method.GetDexMethodIndex(), |
| 479 | &pmi)); |
| 480 | } |
| 481 | } |
| 482 | EXPECT_GT(method_count, 0u); |
| 483 | } |
| 484 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 485 | TEST_F(ProfileAssistantTest, TestProfileCreationOneNotMatched) { |
| 486 | // Class names put here need to be in sorted order. |
| 487 | std::vector<std::string> class_names = { |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 488 | "Ldoesnt/match/this/one;", |
| 489 | "Ljava/lang/Comparable;", |
| 490 | "Ljava/lang/Object;" |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 491 | }; |
| 492 | std::string input_file_contents; |
| 493 | for (std::string& class_name : class_names) { |
| 494 | input_file_contents += class_name + std::string("\n"); |
| 495 | } |
| 496 | std::string output_file_contents; |
| 497 | ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents)); |
| 498 | std::string expected_contents = |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 499 | DescriptorToDot(class_names[1].c_str()) + std::string("\n") + |
| 500 | DescriptorToDot(class_names[2].c_str()) + std::string("\n"); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 501 | ASSERT_EQ(output_file_contents, expected_contents); |
| 502 | } |
| 503 | |
| 504 | TEST_F(ProfileAssistantTest, TestProfileCreationNoneMatched) { |
| 505 | // Class names put here need to be in sorted order. |
| 506 | std::vector<std::string> class_names = { |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 507 | "Ldoesnt/match/this/one;", |
| 508 | "Ldoesnt/match/this/one/either;", |
| 509 | "Lnor/this/one;" |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 510 | }; |
| 511 | std::string input_file_contents; |
| 512 | for (std::string& class_name : class_names) { |
| 513 | input_file_contents += class_name + std::string("\n"); |
| 514 | } |
| 515 | std::string output_file_contents; |
| 516 | ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents)); |
| 517 | std::string expected_contents(""); |
| 518 | ASSERT_EQ(output_file_contents, expected_contents); |
| 519 | } |
| 520 | |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 521 | TEST_F(ProfileAssistantTest, TestProfileCreateInlineCache) { |
| 522 | // Create the profile content. |
| 523 | std::vector<std::string> methods = { |
| 524 | "LTestInline;->inlineMonomorphic(LSuper;)I+LSubA;", |
| 525 | "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;", |
| 526 | "LTestInline;->inlineMegamorphic(LSuper;)I+LSubA;,LSubB;,LSubC;,LSubD;,LSubE;", |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 527 | "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types", |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 528 | "LTestInline;->noInlineCache(LSuper;)I" |
| 529 | }; |
| 530 | std::string input_file_contents; |
| 531 | for (std::string& m : methods) { |
| 532 | input_file_contents += m + std::string("\n"); |
| 533 | } |
| 534 | |
| 535 | // Create the profile and save it to disk. |
| 536 | ScratchFile profile_file; |
| 537 | ASSERT_TRUE(CreateProfile(input_file_contents, |
| 538 | profile_file.GetFilename(), |
| 539 | GetTestDexFileName("ProfileTestMultiDex"))); |
| 540 | |
| 541 | // Load the profile from disk. |
| 542 | ProfileCompilationInfo info; |
| 543 | profile_file.GetFile()->ResetOffset(); |
| 544 | ASSERT_TRUE(info.Load(GetFd(profile_file))); |
| 545 | |
| 546 | // Load the dex files and verify that the profile contains the expected methods info. |
| 547 | ScopedObjectAccess soa(Thread::Current()); |
| 548 | jobject class_loader = LoadDex("ProfileTestMultiDex"); |
| 549 | ASSERT_NE(class_loader, nullptr); |
| 550 | |
| 551 | mirror::Class* sub_a = GetClass(class_loader, "LSubA;"); |
| 552 | mirror::Class* sub_b = GetClass(class_loader, "LSubB;"); |
| 553 | mirror::Class* sub_c = GetClass(class_loader, "LSubC;"); |
| 554 | |
| 555 | ASSERT_TRUE(sub_a != nullptr); |
| 556 | ASSERT_TRUE(sub_b != nullptr); |
| 557 | ASSERT_TRUE(sub_c != nullptr); |
| 558 | |
| 559 | { |
| 560 | // Verify that method inlineMonomorphic has the expected inline caches and nothing else. |
| 561 | ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader, |
| 562 | "LTestInline;", |
| 563 | "inlineMonomorphic"); |
| 564 | ASSERT_TRUE(inline_monomorphic != nullptr); |
| 565 | std::set<mirror::Class*> expected_monomorphic; |
| 566 | expected_monomorphic.insert(sub_a); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 567 | AssertInlineCaches(inline_monomorphic, |
| 568 | expected_monomorphic, |
| 569 | info, |
| 570 | /*megamorphic*/false, |
| 571 | /*missing_types*/false); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | { |
| 575 | // Verify that method inlinePolymorphic has the expected inline caches and nothing else. |
| 576 | ArtMethod* inline_polymorhic = GetVirtualMethod(class_loader, |
| 577 | "LTestInline;", |
| 578 | "inlinePolymorphic"); |
| 579 | ASSERT_TRUE(inline_polymorhic != nullptr); |
| 580 | std::set<mirror::Class*> expected_polymorphic; |
| 581 | expected_polymorphic.insert(sub_a); |
| 582 | expected_polymorphic.insert(sub_b); |
| 583 | expected_polymorphic.insert(sub_c); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 584 | AssertInlineCaches(inline_polymorhic, |
| 585 | expected_polymorphic, |
| 586 | info, |
| 587 | /*megamorphic*/false, |
| 588 | /*missing_types*/false); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | { |
| 592 | // Verify that method inlineMegamorphic has the expected inline caches and nothing else. |
| 593 | ArtMethod* inline_megamorphic = GetVirtualMethod(class_loader, |
| 594 | "LTestInline;", |
| 595 | "inlineMegamorphic"); |
| 596 | ASSERT_TRUE(inline_megamorphic != nullptr); |
| 597 | std::set<mirror::Class*> expected_megamorphic; |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 598 | AssertInlineCaches(inline_megamorphic, |
| 599 | expected_megamorphic, |
| 600 | info, |
| 601 | /*megamorphic*/true, |
| 602 | /*missing_types*/false); |
| 603 | } |
| 604 | |
| 605 | { |
| 606 | // Verify that method inlineMegamorphic has the expected inline caches and nothing else. |
| 607 | ArtMethod* inline_missing_types = GetVirtualMethod(class_loader, |
| 608 | "LTestInline;", |
| 609 | "inlineMissingTypes"); |
| 610 | ASSERT_TRUE(inline_missing_types != nullptr); |
| 611 | std::set<mirror::Class*> expected_missing_Types; |
| 612 | AssertInlineCaches(inline_missing_types, |
| 613 | expected_missing_Types, |
| 614 | info, |
| 615 | /*megamorphic*/false, |
| 616 | /*missing_types*/true); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | { |
| 620 | // Verify that method noInlineCache has no inline caches in the profile. |
| 621 | ArtMethod* no_inline_cache = GetVirtualMethod(class_loader, "LTestInline;", "noInlineCache"); |
| 622 | ASSERT_TRUE(no_inline_cache != nullptr); |
| 623 | ProfileCompilationInfo::OfflineProfileMethodInfo pmi_no_inline_cache; |
| 624 | ASSERT_TRUE(info.GetMethod(no_inline_cache->GetDexFile()->GetLocation(), |
| 625 | no_inline_cache->GetDexFile()->GetLocationChecksum(), |
| 626 | no_inline_cache->GetDexMethodIndex(), |
| 627 | &pmi_no_inline_cache)); |
| 628 | ASSERT_TRUE(pmi_no_inline_cache.inline_caches.empty()); |
| 629 | } |
| 630 | } |
| 631 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 632 | } // namespace art |