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