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