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