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 | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 24 | #include "linear_alloc.h" |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 26 | #include "obj_ptr-inl.h" |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 27 | #include "profile_assistant.h" |
| 28 | #include "scoped_thread_state_change-inl.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 29 | #include "utils.h" |
| 30 | |
| 31 | namespace art { |
| 32 | |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 33 | static constexpr size_t kMaxMethodIds = 65535; |
| 34 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 35 | class ProfileAssistantTest : public CommonRuntimeTest { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 36 | public: |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 37 | void PostRuntimeCreate() OVERRIDE { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 38 | arena_.reset(new ArenaAllocator(Runtime::Current()->GetArenaPool())); |
| 39 | } |
| 40 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 41 | protected: |
| 42 | void SetupProfile(const std::string& id, |
| 43 | uint32_t checksum, |
| 44 | uint16_t number_of_methods, |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 45 | uint16_t number_of_classes, |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 46 | const ScratchFile& profile, |
| 47 | ProfileCompilationInfo* info, |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 48 | uint16_t start_method_index = 0, |
| 49 | bool reverse_dex_write_order = false) { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 50 | std::string dex_location1 = "location1" + id; |
| 51 | uint32_t dex_location_checksum1 = checksum; |
| 52 | std::string dex_location2 = "location2" + id; |
| 53 | uint32_t dex_location_checksum2 = 10 * checksum; |
| 54 | for (uint16_t i = start_method_index; i < start_method_index + number_of_methods; i++) { |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 55 | // reverse_dex_write_order controls the order in which the dex files will be added to |
| 56 | // the profile and thus written to disk. |
| 57 | ProfileCompilationInfo::OfflineProfileMethodInfo pmi = |
| 58 | GetOfflineProfileMethodInfo(dex_location1, dex_location_checksum1, |
| 59 | dex_location2, dex_location_checksum2); |
| 60 | if (reverse_dex_write_order) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 61 | ASSERT_TRUE(info->AddMethod(dex_location2, dex_location_checksum2, i, kMaxMethodIds, pmi)); |
| 62 | ASSERT_TRUE(info->AddMethod(dex_location1, dex_location_checksum1, i, kMaxMethodIds, pmi)); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 63 | } else { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 64 | ASSERT_TRUE(info->AddMethod(dex_location1, dex_location_checksum1, i, kMaxMethodIds, pmi)); |
| 65 | ASSERT_TRUE(info->AddMethod(dex_location2, dex_location_checksum2, i, kMaxMethodIds, pmi)); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 66 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 67 | } |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 68 | for (uint16_t i = 0; i < number_of_classes; i++) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 69 | ASSERT_TRUE(info->AddClassIndex(dex_location1, |
| 70 | dex_location_checksum1, |
| 71 | dex::TypeIndex(i), |
| 72 | kMaxMethodIds)); |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 75 | ASSERT_TRUE(info->Save(GetFd(profile))); |
| 76 | ASSERT_EQ(0, profile.GetFile()->Flush()); |
| 77 | ASSERT_TRUE(profile.GetFile()->ResetOffset()); |
| 78 | } |
| 79 | |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame^] | 80 | void SetupBasicProfile(const std::string& id, |
| 81 | uint32_t checksum, |
| 82 | uint16_t number_of_methods, |
| 83 | const std::vector<uint32_t> hot_methods, |
| 84 | const std::vector<uint32_t> startup_methods, |
| 85 | const std::vector<uint32_t> post_startup_methods, |
| 86 | const ScratchFile& profile, |
| 87 | ProfileCompilationInfo* info) { |
| 88 | std::string dex_location = "location1" + id; |
| 89 | for (uint32_t idx : hot_methods) { |
| 90 | info->AddMethodIndex(dex_location, checksum, idx, number_of_methods); |
| 91 | } |
| 92 | for (uint32_t idx : startup_methods) { |
| 93 | info->AddSampledMethod(/*startup*/true, dex_location, checksum, idx, number_of_methods); |
| 94 | } |
| 95 | for (uint32_t idx : post_startup_methods) { |
| 96 | info->AddSampledMethod(/*startup*/false, dex_location, checksum, idx, number_of_methods); |
| 97 | } |
| 98 | ASSERT_TRUE(info->Save(GetFd(profile))); |
| 99 | ASSERT_EQ(0, profile.GetFile()->Flush()); |
| 100 | ASSERT_TRUE(profile.GetFile()->ResetOffset()); |
| 101 | } |
| 102 | |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 103 | // Creates an inline cache which will be destructed at the end of the test. |
| 104 | ProfileCompilationInfo::InlineCacheMap* CreateInlineCacheMap() { |
| 105 | used_inline_caches.emplace_back(new ProfileCompilationInfo::InlineCacheMap( |
| 106 | std::less<uint16_t>(), arena_->Adapter(kArenaAllocProfile))); |
| 107 | return used_inline_caches.back().get(); |
| 108 | } |
| 109 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 110 | ProfileCompilationInfo::OfflineProfileMethodInfo GetOfflineProfileMethodInfo( |
| 111 | const std::string& dex_location1, uint32_t dex_checksum1, |
| 112 | const std::string& dex_location2, uint32_t dex_checksum2) { |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 113 | ProfileCompilationInfo::InlineCacheMap* ic_map = CreateInlineCacheMap(); |
| 114 | ProfileCompilationInfo::OfflineProfileMethodInfo pmi(ic_map); |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 115 | pmi.dex_references.emplace_back(dex_location1, dex_checksum1, kMaxMethodIds); |
| 116 | pmi.dex_references.emplace_back(dex_location2, dex_checksum2, kMaxMethodIds); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 117 | |
| 118 | // Monomorphic |
| 119 | for (uint16_t dex_pc = 0; dex_pc < 11; dex_pc++) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 120 | ProfileCompilationInfo::DexPcData dex_pc_data(arena_.get()); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 121 | dex_pc_data.AddClass(0, dex::TypeIndex(0)); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 122 | ic_map->Put(dex_pc, dex_pc_data); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 123 | } |
| 124 | // Polymorphic |
| 125 | for (uint16_t dex_pc = 11; dex_pc < 22; dex_pc++) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 126 | ProfileCompilationInfo::DexPcData dex_pc_data(arena_.get()); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 127 | dex_pc_data.AddClass(0, dex::TypeIndex(0)); |
| 128 | dex_pc_data.AddClass(1, dex::TypeIndex(1)); |
| 129 | |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 130 | ic_map->Put(dex_pc, dex_pc_data); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 131 | } |
| 132 | // Megamorphic |
| 133 | for (uint16_t dex_pc = 22; dex_pc < 33; dex_pc++) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 134 | ProfileCompilationInfo::DexPcData dex_pc_data(arena_.get()); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 135 | dex_pc_data.SetIsMegamorphic(); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 136 | ic_map->Put(dex_pc, dex_pc_data); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 137 | } |
| 138 | // Missing types |
| 139 | for (uint16_t dex_pc = 33; dex_pc < 44; dex_pc++) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 140 | ProfileCompilationInfo::DexPcData dex_pc_data(arena_.get()); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 141 | dex_pc_data.SetIsMissingTypes(); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 142 | ic_map->Put(dex_pc, dex_pc_data); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return pmi; |
| 146 | } |
| 147 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 148 | int GetFd(const ScratchFile& file) const { |
| 149 | return static_cast<int>(file.GetFd()); |
| 150 | } |
| 151 | |
| 152 | void CheckProfileInfo(ScratchFile& file, const ProfileCompilationInfo& info) { |
| 153 | ProfileCompilationInfo file_info; |
| 154 | ASSERT_TRUE(file.GetFile()->ResetOffset()); |
| 155 | ASSERT_TRUE(file_info.Load(GetFd(file))); |
| 156 | ASSERT_TRUE(file_info.Equals(info)); |
| 157 | } |
| 158 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 159 | std::string GetProfmanCmd() { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 160 | std::string file_path = GetTestAndroidRoot(); |
Calin Juravle | de4fb63 | 2016-02-23 16:53:30 +0000 | [diff] [blame] | 161 | file_path += "/bin/profman"; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 162 | if (kIsDebugBuild) { |
| 163 | file_path += "d"; |
| 164 | } |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 165 | EXPECT_TRUE(OS::FileExists(file_path.c_str())) |
| 166 | << file_path << " should be a valid file path"; |
| 167 | return file_path; |
| 168 | } |
| 169 | // Runs test with given arguments. |
| 170 | int ProcessProfiles(const std::vector<int>& profiles_fd, int reference_profile_fd) { |
| 171 | std::string profman_cmd = GetProfmanCmd(); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 172 | std::vector<std::string> argv_str; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 173 | argv_str.push_back(profman_cmd); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 174 | for (size_t k = 0; k < profiles_fd.size(); k++) { |
| 175 | argv_str.push_back("--profile-file-fd=" + std::to_string(profiles_fd[k])); |
| 176 | } |
| 177 | argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile_fd)); |
| 178 | |
| 179 | std::string error; |
| 180 | return ExecAndReturnCode(argv_str, &error); |
| 181 | } |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 182 | |
| 183 | bool GenerateTestProfile(const std::string& filename) { |
| 184 | std::string profman_cmd = GetProfmanCmd(); |
| 185 | std::vector<std::string> argv_str; |
| 186 | argv_str.push_back(profman_cmd); |
| 187 | argv_str.push_back("--generate-test-profile=" + filename); |
| 188 | std::string error; |
| 189 | return ExecAndReturnCode(argv_str, &error); |
| 190 | } |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 191 | |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 192 | bool GenerateTestProfileWithInputDex(const std::string& filename) { |
| 193 | std::string profman_cmd = GetProfmanCmd(); |
| 194 | std::vector<std::string> argv_str; |
| 195 | argv_str.push_back(profman_cmd); |
| 196 | argv_str.push_back("--generate-test-profile=" + filename); |
| 197 | argv_str.push_back("--generate-test-profile-seed=0"); |
| 198 | argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]); |
| 199 | argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]); |
| 200 | std::string error; |
| 201 | return ExecAndReturnCode(argv_str, &error); |
| 202 | } |
| 203 | |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 204 | bool CreateProfile(std::string profile_file_contents, |
| 205 | const std::string& filename, |
| 206 | const std::string& dex_location) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 207 | ScratchFile class_names_file; |
| 208 | File* file = class_names_file.GetFile(); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 209 | 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] | 210 | EXPECT_EQ(0, file->Flush()); |
| 211 | EXPECT_TRUE(file->ResetOffset()); |
| 212 | std::string profman_cmd = GetProfmanCmd(); |
| 213 | std::vector<std::string> argv_str; |
| 214 | argv_str.push_back(profman_cmd); |
| 215 | argv_str.push_back("--create-profile-from=" + class_names_file.GetFilename()); |
| 216 | argv_str.push_back("--reference-profile-file=" + filename); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 217 | argv_str.push_back("--apk=" + dex_location); |
| 218 | argv_str.push_back("--dex-location=" + dex_location); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 219 | std::string error; |
| 220 | EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0); |
| 221 | return true; |
| 222 | } |
| 223 | |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame^] | 224 | bool RunProfman(const std::string& filename, |
| 225 | std::vector<std::string>& extra_args, |
| 226 | std::string* output) { |
| 227 | ScratchFile output_file; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 228 | std::string profman_cmd = GetProfmanCmd(); |
| 229 | std::vector<std::string> argv_str; |
| 230 | argv_str.push_back(profman_cmd); |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame^] | 231 | argv_str.insert(argv_str.end(), extra_args.begin(), extra_args.end()); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 232 | argv_str.push_back("--profile-file=" + filename); |
| 233 | argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 234 | argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]); |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame^] | 235 | argv_str.push_back("--dump-output-to-fd=" + std::to_string(GetFd(output_file))); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 236 | std::string error; |
| 237 | EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0); |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame^] | 238 | File* file = output_file.GetFile(); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 239 | EXPECT_EQ(0, file->Flush()); |
| 240 | EXPECT_TRUE(file->ResetOffset()); |
| 241 | int64_t length = file->GetLength(); |
| 242 | std::unique_ptr<char[]> buf(new char[length]); |
| 243 | EXPECT_EQ(file->Read(buf.get(), length, 0), length); |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame^] | 244 | *output = std::string(buf.get(), length); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame^] | 248 | bool DumpClassesAndMethods(const std::string& filename, std::string* file_contents) { |
| 249 | std::vector<std::string> extra_args; |
| 250 | extra_args.push_back("--dump-classes-and-methods"); |
| 251 | return RunProfman(filename, extra_args, file_contents); |
| 252 | } |
| 253 | |
| 254 | bool DumpOnly(const std::string& filename, std::string* file_contents) { |
| 255 | std::vector<std::string> extra_args; |
| 256 | extra_args.push_back("--dump-only"); |
| 257 | return RunProfman(filename, extra_args, file_contents); |
| 258 | } |
| 259 | |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 260 | bool CreateAndDump(const std::string& input_file_contents, |
| 261 | std::string* output_file_contents) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 262 | ScratchFile profile_file; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 263 | EXPECT_TRUE(CreateProfile(input_file_contents, |
| 264 | profile_file.GetFilename(), |
| 265 | GetLibCoreDexFileNames()[0])); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 266 | profile_file.GetFile()->ResetOffset(); |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 267 | EXPECT_TRUE(DumpClassesAndMethods(profile_file.GetFilename(), output_file_contents)); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 268 | return true; |
| 269 | } |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 270 | |
| 271 | mirror::Class* GetClass(jobject class_loader, const std::string& clazz) { |
| 272 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 273 | Thread* self = Thread::Current(); |
| 274 | ScopedObjectAccess soa(self); |
| 275 | StackHandleScope<1> hs(self); |
| 276 | Handle<mirror::ClassLoader> h_loader( |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 277 | hs.NewHandle(ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(class_loader)))); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 278 | return class_linker->FindClass(self, clazz.c_str(), h_loader); |
| 279 | } |
| 280 | |
| 281 | ArtMethod* GetVirtualMethod(jobject class_loader, |
| 282 | const std::string& clazz, |
| 283 | const std::string& name) { |
| 284 | mirror::Class* klass = GetClass(class_loader, clazz); |
| 285 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 286 | const auto pointer_size = class_linker->GetImagePointerSize(); |
| 287 | ArtMethod* method = nullptr; |
| 288 | Thread* self = Thread::Current(); |
| 289 | ScopedObjectAccess soa(self); |
| 290 | for (auto& m : klass->GetVirtualMethods(pointer_size)) { |
| 291 | if (name == m.GetName()) { |
| 292 | EXPECT_TRUE(method == nullptr); |
| 293 | method = &m; |
| 294 | } |
| 295 | } |
| 296 | return method; |
| 297 | } |
| 298 | |
| 299 | // Verify that given method has the expected inline caches and nothing else. |
| 300 | void AssertInlineCaches(ArtMethod* method, |
| 301 | const std::set<mirror::Class*>& expected_clases, |
| 302 | const ProfileCompilationInfo& info, |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 303 | bool is_megamorphic, |
| 304 | bool is_missing_types) |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 305 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 306 | std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi = |
| 307 | info.GetMethod(method->GetDexFile()->GetLocation(), |
| 308 | method->GetDexFile()->GetLocationChecksum(), |
| 309 | method->GetDexMethodIndex()); |
| 310 | ASSERT_TRUE(pmi != nullptr); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 311 | ASSERT_EQ(pmi->inline_caches->size(), 1u); |
| 312 | const ProfileCompilationInfo::DexPcData& dex_pc_data = pmi->inline_caches->begin()->second; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 313 | |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 314 | ASSERT_EQ(dex_pc_data.is_megamorphic, is_megamorphic); |
| 315 | ASSERT_EQ(dex_pc_data.is_missing_types, is_missing_types); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 316 | ASSERT_EQ(expected_clases.size(), dex_pc_data.classes.size()); |
| 317 | size_t found = 0; |
| 318 | for (mirror::Class* it : expected_clases) { |
| 319 | for (const auto& class_ref : dex_pc_data.classes) { |
| 320 | ProfileCompilationInfo::DexReference dex_ref = |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 321 | pmi->dex_references[class_ref.dex_profile_index]; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 322 | if (dex_ref.MatchesDex(&(it->GetDexFile())) && |
| 323 | class_ref.type_index == it->GetDexTypeIndex()) { |
| 324 | found++; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | ASSERT_EQ(expected_clases.size(), found); |
| 330 | } |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 331 | |
| 332 | std::unique_ptr<ArenaAllocator> arena_; |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 333 | |
| 334 | // Cache of inline caches generated during tests. |
| 335 | // This makes it easier to pass data between different utilities and ensure that |
| 336 | // caches are destructed at the end of the test. |
| 337 | std::vector<std::unique_ptr<ProfileCompilationInfo::InlineCacheMap>> used_inline_caches; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferences) { |
| 341 | ScratchFile profile1; |
| 342 | ScratchFile profile2; |
| 343 | ScratchFile reference_profile; |
| 344 | |
| 345 | std::vector<int> profile_fds({ |
| 346 | GetFd(profile1), |
| 347 | GetFd(profile2)}); |
| 348 | int reference_profile_fd = GetFd(reference_profile); |
| 349 | |
| 350 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 351 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 352 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 353 | ProfileCompilationInfo info2; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 354 | SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 355 | |
| 356 | // We should advise compilation. |
| 357 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 358 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 359 | // The resulting compilation info must be equal to the merge of the inputs. |
| 360 | ProfileCompilationInfo result; |
| 361 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 362 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 363 | |
| 364 | ProfileCompilationInfo expected; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 365 | ASSERT_TRUE(expected.MergeWith(info1)); |
| 366 | ASSERT_TRUE(expected.MergeWith(info2)); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 367 | ASSERT_TRUE(expected.Equals(result)); |
| 368 | |
| 369 | // The information from profiles must remain the same. |
| 370 | CheckProfileInfo(profile1, info1); |
| 371 | CheckProfileInfo(profile2, info2); |
| 372 | } |
| 373 | |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 374 | // TODO(calin): Add more tests for classes. |
| 375 | TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferencesBecauseOfClasses) { |
| 376 | ScratchFile profile1; |
| 377 | ScratchFile reference_profile; |
| 378 | |
| 379 | std::vector<int> profile_fds({ |
| 380 | GetFd(profile1)}); |
| 381 | int reference_profile_fd = GetFd(reference_profile); |
| 382 | |
| 383 | const uint16_t kNumberOfClassesToEnableCompilation = 100; |
| 384 | ProfileCompilationInfo info1; |
| 385 | SetupProfile("p1", 1, 0, kNumberOfClassesToEnableCompilation, profile1, &info1); |
| 386 | |
| 387 | // We should advise compilation. |
| 388 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 389 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 390 | // The resulting compilation info must be equal to the merge of the inputs. |
| 391 | ProfileCompilationInfo result; |
| 392 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 393 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 394 | |
| 395 | ProfileCompilationInfo expected; |
| 396 | ASSERT_TRUE(expected.MergeWith(info1)); |
| 397 | ASSERT_TRUE(expected.Equals(result)); |
| 398 | |
| 399 | // The information from profiles must remain the same. |
| 400 | CheckProfileInfo(profile1, info1); |
| 401 | } |
| 402 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 403 | TEST_F(ProfileAssistantTest, AdviseCompilationNonEmptyReferences) { |
| 404 | ScratchFile profile1; |
| 405 | ScratchFile profile2; |
| 406 | ScratchFile reference_profile; |
| 407 | |
| 408 | std::vector<int> profile_fds({ |
| 409 | GetFd(profile1), |
| 410 | GetFd(profile2)}); |
| 411 | int reference_profile_fd = GetFd(reference_profile); |
| 412 | |
| 413 | // The new profile info will contain the methods with indices 0-100. |
| 414 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 415 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 416 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 417 | ProfileCompilationInfo info2; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 418 | SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 419 | |
| 420 | |
| 421 | // The reference profile info will contain the methods with indices 50-150. |
| 422 | const uint16_t kNumberOfMethodsAlreadyCompiled = 100; |
| 423 | ProfileCompilationInfo reference_info; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 424 | SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile, |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 425 | &reference_info, kNumberOfMethodsToEnableCompilation / 2); |
| 426 | |
| 427 | // We should advise compilation. |
| 428 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 429 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 430 | |
| 431 | // The resulting compilation info must be equal to the merge of the inputs |
| 432 | ProfileCompilationInfo result; |
| 433 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 434 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 435 | |
| 436 | ProfileCompilationInfo expected; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 437 | ASSERT_TRUE(expected.MergeWith(info1)); |
| 438 | ASSERT_TRUE(expected.MergeWith(info2)); |
| 439 | ASSERT_TRUE(expected.MergeWith(reference_info)); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 440 | ASSERT_TRUE(expected.Equals(result)); |
| 441 | |
| 442 | // The information from profiles must remain the same. |
| 443 | CheckProfileInfo(profile1, info1); |
| 444 | CheckProfileInfo(profile2, info2); |
| 445 | } |
| 446 | |
| 447 | TEST_F(ProfileAssistantTest, DoNotAdviseCompilation) { |
| 448 | ScratchFile profile1; |
| 449 | ScratchFile profile2; |
| 450 | ScratchFile reference_profile; |
| 451 | |
| 452 | std::vector<int> profile_fds({ |
| 453 | GetFd(profile1), |
| 454 | GetFd(profile2)}); |
| 455 | int reference_profile_fd = GetFd(reference_profile); |
| 456 | |
| 457 | const uint16_t kNumberOfMethodsToSkipCompilation = 1; |
| 458 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 459 | SetupProfile("p1", 1, kNumberOfMethodsToSkipCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 460 | ProfileCompilationInfo info2; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 461 | SetupProfile("p2", 2, kNumberOfMethodsToSkipCompilation, 0, profile2, &info2); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 462 | |
| 463 | // We should not advise compilation. |
| 464 | ASSERT_EQ(ProfileAssistant::kSkipCompilation, |
| 465 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 466 | |
| 467 | // The information from profiles must remain the same. |
| 468 | ProfileCompilationInfo file_info1; |
| 469 | ASSERT_TRUE(profile1.GetFile()->ResetOffset()); |
| 470 | ASSERT_TRUE(file_info1.Load(GetFd(profile1))); |
| 471 | ASSERT_TRUE(file_info1.Equals(info1)); |
| 472 | |
| 473 | ProfileCompilationInfo file_info2; |
| 474 | ASSERT_TRUE(profile2.GetFile()->ResetOffset()); |
| 475 | ASSERT_TRUE(file_info2.Load(GetFd(profile2))); |
| 476 | ASSERT_TRUE(file_info2.Equals(info2)); |
| 477 | |
| 478 | // Reference profile files must remain empty. |
| 479 | ASSERT_EQ(0, reference_profile.GetFile()->GetLength()); |
| 480 | |
| 481 | // The information from profiles must remain the same. |
| 482 | CheckProfileInfo(profile1, info1); |
| 483 | CheckProfileInfo(profile2, info2); |
| 484 | } |
| 485 | |
| 486 | TEST_F(ProfileAssistantTest, FailProcessingBecauseOfProfiles) { |
| 487 | ScratchFile profile1; |
| 488 | ScratchFile profile2; |
| 489 | ScratchFile reference_profile; |
| 490 | |
| 491 | std::vector<int> profile_fds({ |
| 492 | GetFd(profile1), |
| 493 | GetFd(profile2)}); |
| 494 | int reference_profile_fd = GetFd(reference_profile); |
| 495 | |
| 496 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 497 | // Assign different hashes for the same dex file. This will make merging of information to fail. |
| 498 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 499 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 500 | ProfileCompilationInfo info2; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 501 | SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 502 | |
| 503 | // We should fail processing. |
| 504 | ASSERT_EQ(ProfileAssistant::kErrorBadProfiles, |
| 505 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 506 | |
| 507 | // The information from profiles must remain the same. |
| 508 | CheckProfileInfo(profile1, info1); |
| 509 | CheckProfileInfo(profile2, info2); |
| 510 | |
| 511 | // Reference profile files must still remain empty. |
| 512 | ASSERT_EQ(0, reference_profile.GetFile()->GetLength()); |
| 513 | } |
| 514 | |
| 515 | TEST_F(ProfileAssistantTest, FailProcessingBecauseOfReferenceProfiles) { |
| 516 | ScratchFile profile1; |
| 517 | ScratchFile reference_profile; |
| 518 | |
| 519 | std::vector<int> profile_fds({ |
| 520 | GetFd(profile1)}); |
| 521 | int reference_profile_fd = GetFd(reference_profile); |
| 522 | |
| 523 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 524 | // Assign different hashes for the same dex file. This will make merging of information to fail. |
| 525 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 526 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 527 | ProfileCompilationInfo reference_info; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 528 | SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, reference_profile, &reference_info); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 529 | |
| 530 | // We should not advise compilation. |
| 531 | ASSERT_TRUE(profile1.GetFile()->ResetOffset()); |
| 532 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 533 | ASSERT_EQ(ProfileAssistant::kErrorBadProfiles, |
| 534 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 535 | |
| 536 | // The information from profiles must remain the same. |
| 537 | CheckProfileInfo(profile1, info1); |
| 538 | } |
| 539 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 540 | TEST_F(ProfileAssistantTest, TestProfileGeneration) { |
| 541 | ScratchFile profile; |
| 542 | // Generate a test profile. |
| 543 | GenerateTestProfile(profile.GetFilename()); |
| 544 | |
| 545 | // Verify that the generated profile is valid and can be loaded. |
| 546 | ASSERT_TRUE(profile.GetFile()->ResetOffset()); |
| 547 | ProfileCompilationInfo info; |
| 548 | ASSERT_TRUE(info.Load(GetFd(profile))); |
| 549 | } |
| 550 | |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 551 | TEST_F(ProfileAssistantTest, TestProfileGenerationWithIndexDex) { |
| 552 | ScratchFile profile; |
| 553 | // Generate a test profile passing in a dex file as reference. |
| 554 | GenerateTestProfileWithInputDex(profile.GetFilename()); |
| 555 | |
| 556 | // Verify that the generated profile is valid and can be loaded. |
| 557 | ASSERT_TRUE(profile.GetFile()->ResetOffset()); |
| 558 | ProfileCompilationInfo info; |
| 559 | ASSERT_TRUE(info.Load(GetFd(profile))); |
| 560 | } |
| 561 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 562 | TEST_F(ProfileAssistantTest, TestProfileCreationAllMatch) { |
| 563 | // Class names put here need to be in sorted order. |
| 564 | std::vector<std::string> class_names = { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 565 | "HLjava/lang/Object;-><init>()V", |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 566 | "Ljava/lang/Comparable;", |
| 567 | "Ljava/lang/Math;", |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 568 | "Ljava/lang/Object;", |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 569 | "SPLjava/lang/Comparable;->compareTo(Ljava/lang/Object;)I", |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 570 | }; |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 571 | std::string file_contents; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 572 | for (std::string& class_name : class_names) { |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 573 | file_contents += class_name + std::string("\n"); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 574 | } |
| 575 | std::string output_file_contents; |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 576 | ASSERT_TRUE(CreateAndDump(file_contents, &output_file_contents)); |
| 577 | ASSERT_EQ(output_file_contents, file_contents); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 580 | TEST_F(ProfileAssistantTest, TestProfileCreationGenerateMethods) { |
| 581 | // Class names put here need to be in sorted order. |
| 582 | std::vector<std::string> class_names = { |
| 583 | "Ljava/lang/Math;->*", |
| 584 | }; |
| 585 | std::string input_file_contents; |
| 586 | std::string expected_contents; |
| 587 | for (std::string& class_name : class_names) { |
| 588 | input_file_contents += class_name + std::string("\n"); |
| 589 | expected_contents += DescriptorToDot(class_name.c_str()) + |
| 590 | std::string("\n"); |
| 591 | } |
| 592 | std::string output_file_contents; |
| 593 | ScratchFile profile_file; |
| 594 | EXPECT_TRUE(CreateProfile(input_file_contents, |
| 595 | profile_file.GetFilename(), |
| 596 | GetLibCoreDexFileNames()[0])); |
| 597 | ProfileCompilationInfo info; |
| 598 | profile_file.GetFile()->ResetOffset(); |
| 599 | ASSERT_TRUE(info.Load(GetFd(profile_file))); |
| 600 | // Verify that the profile has matching methods. |
| 601 | ScopedObjectAccess soa(Thread::Current()); |
| 602 | ObjPtr<mirror::Class> klass = GetClass(nullptr, "Ljava/lang/Math;"); |
| 603 | ASSERT_TRUE(klass != nullptr); |
| 604 | size_t method_count = 0; |
| 605 | for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) { |
| 606 | if (!method.IsCopied() && method.GetCodeItem() != nullptr) { |
| 607 | ++method_count; |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 608 | std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi = |
| 609 | info.GetMethod(method.GetDexFile()->GetLocation(), |
| 610 | method.GetDexFile()->GetLocationChecksum(), |
| 611 | method.GetDexMethodIndex()); |
| 612 | ASSERT_TRUE(pmi != nullptr); |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | EXPECT_GT(method_count, 0u); |
| 616 | } |
| 617 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 618 | TEST_F(ProfileAssistantTest, TestProfileCreationOneNotMatched) { |
| 619 | // Class names put here need to be in sorted order. |
| 620 | std::vector<std::string> class_names = { |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 621 | "Ldoesnt/match/this/one;", |
| 622 | "Ljava/lang/Comparable;", |
| 623 | "Ljava/lang/Object;" |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 624 | }; |
| 625 | std::string input_file_contents; |
| 626 | for (std::string& class_name : class_names) { |
| 627 | input_file_contents += class_name + std::string("\n"); |
| 628 | } |
| 629 | std::string output_file_contents; |
| 630 | ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents)); |
| 631 | std::string expected_contents = |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 632 | class_names[1] + std::string("\n") + |
| 633 | class_names[2] + std::string("\n"); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 634 | ASSERT_EQ(output_file_contents, expected_contents); |
| 635 | } |
| 636 | |
| 637 | TEST_F(ProfileAssistantTest, TestProfileCreationNoneMatched) { |
| 638 | // Class names put here need to be in sorted order. |
| 639 | std::vector<std::string> class_names = { |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 640 | "Ldoesnt/match/this/one;", |
| 641 | "Ldoesnt/match/this/one/either;", |
| 642 | "Lnor/this/one;" |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 643 | }; |
| 644 | std::string input_file_contents; |
| 645 | for (std::string& class_name : class_names) { |
| 646 | input_file_contents += class_name + std::string("\n"); |
| 647 | } |
| 648 | std::string output_file_contents; |
| 649 | ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents)); |
| 650 | std::string expected_contents(""); |
| 651 | ASSERT_EQ(output_file_contents, expected_contents); |
| 652 | } |
| 653 | |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 654 | TEST_F(ProfileAssistantTest, TestProfileCreateInlineCache) { |
| 655 | // Create the profile content. |
| 656 | std::vector<std::string> methods = { |
| 657 | "LTestInline;->inlineMonomorphic(LSuper;)I+LSubA;", |
| 658 | "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;", |
| 659 | "LTestInline;->inlineMegamorphic(LSuper;)I+LSubA;,LSubB;,LSubC;,LSubD;,LSubE;", |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 660 | "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types", |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 661 | "LTestInline;->noInlineCache(LSuper;)I" |
| 662 | }; |
| 663 | std::string input_file_contents; |
| 664 | for (std::string& m : methods) { |
| 665 | input_file_contents += m + std::string("\n"); |
| 666 | } |
| 667 | |
| 668 | // Create the profile and save it to disk. |
| 669 | ScratchFile profile_file; |
| 670 | ASSERT_TRUE(CreateProfile(input_file_contents, |
| 671 | profile_file.GetFilename(), |
| 672 | GetTestDexFileName("ProfileTestMultiDex"))); |
| 673 | |
| 674 | // Load the profile from disk. |
| 675 | ProfileCompilationInfo info; |
| 676 | profile_file.GetFile()->ResetOffset(); |
| 677 | ASSERT_TRUE(info.Load(GetFd(profile_file))); |
| 678 | |
| 679 | // Load the dex files and verify that the profile contains the expected methods info. |
| 680 | ScopedObjectAccess soa(Thread::Current()); |
| 681 | jobject class_loader = LoadDex("ProfileTestMultiDex"); |
| 682 | ASSERT_NE(class_loader, nullptr); |
| 683 | |
| 684 | mirror::Class* sub_a = GetClass(class_loader, "LSubA;"); |
| 685 | mirror::Class* sub_b = GetClass(class_loader, "LSubB;"); |
| 686 | mirror::Class* sub_c = GetClass(class_loader, "LSubC;"); |
| 687 | |
| 688 | ASSERT_TRUE(sub_a != nullptr); |
| 689 | ASSERT_TRUE(sub_b != nullptr); |
| 690 | ASSERT_TRUE(sub_c != nullptr); |
| 691 | |
| 692 | { |
| 693 | // Verify that method inlineMonomorphic has the expected inline caches and nothing else. |
| 694 | ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader, |
| 695 | "LTestInline;", |
| 696 | "inlineMonomorphic"); |
| 697 | ASSERT_TRUE(inline_monomorphic != nullptr); |
| 698 | std::set<mirror::Class*> expected_monomorphic; |
| 699 | expected_monomorphic.insert(sub_a); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 700 | AssertInlineCaches(inline_monomorphic, |
| 701 | expected_monomorphic, |
| 702 | info, |
| 703 | /*megamorphic*/false, |
| 704 | /*missing_types*/false); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | { |
| 708 | // Verify that method inlinePolymorphic has the expected inline caches and nothing else. |
| 709 | ArtMethod* inline_polymorhic = GetVirtualMethod(class_loader, |
| 710 | "LTestInline;", |
| 711 | "inlinePolymorphic"); |
| 712 | ASSERT_TRUE(inline_polymorhic != nullptr); |
| 713 | std::set<mirror::Class*> expected_polymorphic; |
| 714 | expected_polymorphic.insert(sub_a); |
| 715 | expected_polymorphic.insert(sub_b); |
| 716 | expected_polymorphic.insert(sub_c); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 717 | AssertInlineCaches(inline_polymorhic, |
| 718 | expected_polymorphic, |
| 719 | info, |
| 720 | /*megamorphic*/false, |
| 721 | /*missing_types*/false); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | { |
| 725 | // Verify that method inlineMegamorphic has the expected inline caches and nothing else. |
| 726 | ArtMethod* inline_megamorphic = GetVirtualMethod(class_loader, |
| 727 | "LTestInline;", |
| 728 | "inlineMegamorphic"); |
| 729 | ASSERT_TRUE(inline_megamorphic != nullptr); |
| 730 | std::set<mirror::Class*> expected_megamorphic; |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 731 | AssertInlineCaches(inline_megamorphic, |
| 732 | expected_megamorphic, |
| 733 | info, |
| 734 | /*megamorphic*/true, |
| 735 | /*missing_types*/false); |
| 736 | } |
| 737 | |
| 738 | { |
| 739 | // Verify that method inlineMegamorphic has the expected inline caches and nothing else. |
| 740 | ArtMethod* inline_missing_types = GetVirtualMethod(class_loader, |
| 741 | "LTestInline;", |
| 742 | "inlineMissingTypes"); |
| 743 | ASSERT_TRUE(inline_missing_types != nullptr); |
| 744 | std::set<mirror::Class*> expected_missing_Types; |
| 745 | AssertInlineCaches(inline_missing_types, |
| 746 | expected_missing_Types, |
| 747 | info, |
| 748 | /*megamorphic*/false, |
| 749 | /*missing_types*/true); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | { |
| 753 | // Verify that method noInlineCache has no inline caches in the profile. |
| 754 | ArtMethod* no_inline_cache = GetVirtualMethod(class_loader, "LTestInline;", "noInlineCache"); |
| 755 | ASSERT_TRUE(no_inline_cache != nullptr); |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 756 | std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi_no_inline_cache = |
| 757 | info.GetMethod(no_inline_cache->GetDexFile()->GetLocation(), |
| 758 | no_inline_cache->GetDexFile()->GetLocationChecksum(), |
| 759 | no_inline_cache->GetDexMethodIndex()); |
| 760 | ASSERT_TRUE(pmi_no_inline_cache != nullptr); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 761 | ASSERT_TRUE(pmi_no_inline_cache->inline_caches->empty()); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 765 | TEST_F(ProfileAssistantTest, MergeProfilesWithDifferentDexOrder) { |
| 766 | ScratchFile profile1; |
| 767 | ScratchFile reference_profile; |
| 768 | |
| 769 | std::vector<int> profile_fds({GetFd(profile1)}); |
| 770 | int reference_profile_fd = GetFd(reference_profile); |
| 771 | |
| 772 | // The new profile info will contain the methods with indices 0-100. |
| 773 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 774 | ProfileCompilationInfo info1; |
| 775 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1, |
| 776 | /*start_method_index*/0, /*reverse_dex_write_order*/false); |
| 777 | |
| 778 | // The reference profile info will contain the methods with indices 50-150. |
| 779 | // When setting up the profile reverse the order in which the dex files |
| 780 | // are added to the profile. This will verify that profman merges profiles |
| 781 | // with a different dex order correctly. |
| 782 | const uint16_t kNumberOfMethodsAlreadyCompiled = 100; |
| 783 | ProfileCompilationInfo reference_info; |
| 784 | SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile, |
| 785 | &reference_info, kNumberOfMethodsToEnableCompilation / 2, /*reverse_dex_write_order*/true); |
| 786 | |
| 787 | // We should advise compilation. |
| 788 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 789 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 790 | |
| 791 | // The resulting compilation info must be equal to the merge of the inputs. |
| 792 | ProfileCompilationInfo result; |
| 793 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 794 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 795 | |
| 796 | ProfileCompilationInfo expected; |
| 797 | ASSERT_TRUE(expected.MergeWith(reference_info)); |
| 798 | ASSERT_TRUE(expected.MergeWith(info1)); |
| 799 | ASSERT_TRUE(expected.Equals(result)); |
| 800 | |
| 801 | // The information from profile must remain the same. |
| 802 | CheckProfileInfo(profile1, info1); |
| 803 | } |
| 804 | |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 805 | TEST_F(ProfileAssistantTest, TestProfileCreateWithInvalidData) { |
| 806 | // Create the profile content. |
| 807 | std::vector<std::string> profile_methods = { |
| 808 | "LTestInline;->inlineMonomorphic(LSuper;)I+invalid_class", |
| 809 | "LTestInline;->invalid_method", |
| 810 | "invalid_class" |
| 811 | }; |
| 812 | std::string input_file_contents; |
| 813 | for (std::string& m : profile_methods) { |
| 814 | input_file_contents += m + std::string("\n"); |
| 815 | } |
| 816 | |
| 817 | // Create the profile and save it to disk. |
| 818 | ScratchFile profile_file; |
| 819 | std::string dex_filename = GetTestDexFileName("ProfileTestMultiDex"); |
| 820 | ASSERT_TRUE(CreateProfile(input_file_contents, |
| 821 | profile_file.GetFilename(), |
| 822 | dex_filename)); |
| 823 | |
| 824 | // Load the profile from disk. |
| 825 | ProfileCompilationInfo info; |
| 826 | profile_file.GetFile()->ResetOffset(); |
| 827 | ASSERT_TRUE(info.Load(GetFd(profile_file))); |
| 828 | |
| 829 | // Load the dex files and verify that the profile contains the expected methods info. |
| 830 | ScopedObjectAccess soa(Thread::Current()); |
| 831 | jobject class_loader = LoadDex("ProfileTestMultiDex"); |
| 832 | ASSERT_NE(class_loader, nullptr); |
| 833 | |
| 834 | ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader, |
| 835 | "LTestInline;", |
| 836 | "inlineMonomorphic"); |
| 837 | const DexFile* dex_file = inline_monomorphic->GetDexFile(); |
| 838 | |
| 839 | // Verify that the inline cache contains the invalid type. |
| 840 | std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi = |
| 841 | info.GetMethod(dex_file->GetLocation(), |
| 842 | dex_file->GetLocationChecksum(), |
| 843 | inline_monomorphic->GetDexMethodIndex()); |
| 844 | ASSERT_TRUE(pmi != nullptr); |
| 845 | ASSERT_EQ(pmi->inline_caches->size(), 1u); |
| 846 | const ProfileCompilationInfo::DexPcData& dex_pc_data = pmi->inline_caches->begin()->second; |
| 847 | dex::TypeIndex invalid_class_index(std::numeric_limits<uint16_t>::max() - 1); |
| 848 | ASSERT_EQ(1u, dex_pc_data.classes.size()); |
| 849 | ASSERT_EQ(invalid_class_index, dex_pc_data.classes.begin()->type_index); |
| 850 | |
| 851 | // Verify that the start-up classes contain the invalid class. |
| 852 | std::set<dex::TypeIndex> classes; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 853 | std::set<uint16_t> hot_methods; |
| 854 | std::set<uint16_t> startup_methods; |
| 855 | std::set<uint16_t> post_start_methods; |
| 856 | ASSERT_TRUE(info.GetClassesAndMethods(*dex_file, |
| 857 | &classes, |
| 858 | &hot_methods, |
| 859 | &startup_methods, |
| 860 | &post_start_methods)); |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 861 | ASSERT_EQ(1u, classes.size()); |
| 862 | ASSERT_TRUE(classes.find(invalid_class_index) != classes.end()); |
| 863 | |
| 864 | // Verify that the invalid method is in the profile. |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 865 | ASSERT_EQ(2u, hot_methods.size()); |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 866 | uint16_t invalid_method_index = std::numeric_limits<uint16_t>::max() - 1; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 867 | ASSERT_TRUE(hot_methods.find(invalid_method_index) != hot_methods.end()); |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame^] | 870 | TEST_F(ProfileAssistantTest, DumpOnly) { |
| 871 | ScratchFile profile; |
| 872 | |
| 873 | const uint32_t kNumberOfMethods = 64; |
| 874 | std::vector<uint32_t> hot_methods; |
| 875 | std::vector<uint32_t> startup_methods; |
| 876 | std::vector<uint32_t> post_startup_methods; |
| 877 | for (size_t i = 0; i < kNumberOfMethods; ++i) { |
| 878 | if (i % 2 == 0) { |
| 879 | hot_methods.push_back(i); |
| 880 | } |
| 881 | if (i % 3 == 1) { |
| 882 | startup_methods.push_back(i); |
| 883 | } |
| 884 | if (i % 4 == 2) { |
| 885 | post_startup_methods.push_back(i); |
| 886 | } |
| 887 | } |
| 888 | EXPECT_GT(hot_methods.size(), 0u); |
| 889 | EXPECT_GT(startup_methods.size(), 0u); |
| 890 | EXPECT_GT(post_startup_methods.size(), 0u); |
| 891 | ProfileCompilationInfo info1; |
| 892 | SetupBasicProfile("p1", |
| 893 | 1, |
| 894 | kNumberOfMethods, |
| 895 | hot_methods, |
| 896 | startup_methods, |
| 897 | post_startup_methods, |
| 898 | profile, |
| 899 | &info1); |
| 900 | std::string output; |
| 901 | DumpOnly(profile.GetFilename(), &output); |
| 902 | const size_t hot_offset = output.find("hot methods:"); |
| 903 | const size_t startup_offset = output.find("startup methods:"); |
| 904 | const size_t post_startup_offset = output.find("post startup methods:"); |
| 905 | const size_t classes_offset = output.find("classes:"); |
| 906 | ASSERT_NE(hot_offset, std::string::npos); |
| 907 | ASSERT_NE(startup_offset, std::string::npos); |
| 908 | ASSERT_NE(post_startup_offset, std::string::npos); |
| 909 | ASSERT_LT(hot_offset, startup_offset); |
| 910 | ASSERT_LT(startup_offset, post_startup_offset); |
| 911 | // Check the actual contents of the dump by looking at the offsets of the methods. |
| 912 | for (uint32_t m : hot_methods) { |
| 913 | const size_t pos = output.find(std::to_string(m) + "[],", hot_offset); |
| 914 | ASSERT_NE(pos, std::string::npos); |
| 915 | EXPECT_LT(pos, startup_offset); |
| 916 | } |
| 917 | for (uint32_t m : startup_methods) { |
| 918 | const size_t pos = output.find(std::to_string(m) + ",", startup_offset); |
| 919 | ASSERT_NE(pos, std::string::npos); |
| 920 | EXPECT_LT(pos, post_startup_offset); |
| 921 | } |
| 922 | for (uint32_t m : post_startup_methods) { |
| 923 | const size_t pos = output.find(std::to_string(m) + ",", post_startup_offset); |
| 924 | ASSERT_NE(pos, std::string::npos); |
| 925 | EXPECT_LT(pos, classes_offset); |
| 926 | } |
| 927 | } |
| 928 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 929 | } // namespace art |