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 | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame^] | 19 | #include "android-base/strings.h" |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 20 | #include "art_method-inl.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 21 | #include "base/unix_file/fd_file.h" |
| 22 | #include "common_runtime_test.h" |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 23 | #include "exec_utils.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 24 | #include "jit/profile_compilation_info.h" |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 25 | #include "linear_alloc.h" |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 26 | #include "mirror/class-inl.h" |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 27 | #include "obj_ptr-inl.h" |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 28 | #include "profile_assistant.h" |
| 29 | #include "scoped_thread_state_change-inl.h" |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 30 | #include "utils.h" |
| 31 | |
| 32 | namespace art { |
| 33 | |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 34 | static constexpr size_t kMaxMethodIds = 65535; |
| 35 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 36 | class ProfileAssistantTest : public CommonRuntimeTest { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 37 | public: |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 38 | void PostRuntimeCreate() OVERRIDE { |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 39 | allocator_.reset(new ArenaAllocator(Runtime::Current()->GetArenaPool())); |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 42 | protected: |
| 43 | void SetupProfile(const std::string& id, |
| 44 | uint32_t checksum, |
| 45 | uint16_t number_of_methods, |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 46 | uint16_t number_of_classes, |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 47 | const ScratchFile& profile, |
| 48 | ProfileCompilationInfo* info, |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 49 | uint16_t start_method_index = 0, |
| 50 | bool reverse_dex_write_order = false) { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 51 | std::string dex_location1 = "location1" + id; |
| 52 | uint32_t dex_location_checksum1 = checksum; |
| 53 | std::string dex_location2 = "location2" + id; |
| 54 | uint32_t dex_location_checksum2 = 10 * checksum; |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame^] | 55 | SetupProfile(dex_location1, |
| 56 | dex_location_checksum1, |
| 57 | dex_location2, |
| 58 | dex_location_checksum2, |
| 59 | number_of_methods, |
| 60 | number_of_classes, |
| 61 | profile, |
| 62 | info, |
| 63 | start_method_index, |
| 64 | reverse_dex_write_order); |
| 65 | } |
| 66 | |
| 67 | void SetupProfile(const std::string& dex_location1, |
| 68 | uint32_t dex_location_checksum1, |
| 69 | const std::string& dex_location2, |
| 70 | uint32_t dex_location_checksum2, |
| 71 | uint16_t number_of_methods, |
| 72 | uint16_t number_of_classes, |
| 73 | const ScratchFile& profile, |
| 74 | ProfileCompilationInfo* info, |
| 75 | uint16_t start_method_index = 0, |
| 76 | bool reverse_dex_write_order = false) { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 77 | 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] | 78 | // reverse_dex_write_order controls the order in which the dex files will be added to |
| 79 | // the profile and thus written to disk. |
| 80 | ProfileCompilationInfo::OfflineProfileMethodInfo pmi = |
| 81 | GetOfflineProfileMethodInfo(dex_location1, dex_location_checksum1, |
| 82 | dex_location2, dex_location_checksum2); |
| 83 | if (reverse_dex_write_order) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 84 | ASSERT_TRUE(info->AddMethod(dex_location2, dex_location_checksum2, i, kMaxMethodIds, pmi)); |
| 85 | ASSERT_TRUE(info->AddMethod(dex_location1, dex_location_checksum1, i, kMaxMethodIds, pmi)); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 86 | } else { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 87 | ASSERT_TRUE(info->AddMethod(dex_location1, dex_location_checksum1, i, kMaxMethodIds, pmi)); |
| 88 | ASSERT_TRUE(info->AddMethod(dex_location2, dex_location_checksum2, i, kMaxMethodIds, pmi)); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 89 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 90 | } |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 91 | for (uint16_t i = 0; i < number_of_classes; i++) { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 92 | ASSERT_TRUE(info->AddClassIndex(dex_location1, |
| 93 | dex_location_checksum1, |
| 94 | dex::TypeIndex(i), |
| 95 | kMaxMethodIds)); |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 98 | ASSERT_TRUE(info->Save(GetFd(profile))); |
| 99 | ASSERT_EQ(0, profile.GetFile()->Flush()); |
| 100 | ASSERT_TRUE(profile.GetFile()->ResetOffset()); |
| 101 | } |
| 102 | |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 103 | void SetupBasicProfile(const std::string& id, |
| 104 | uint32_t checksum, |
| 105 | uint16_t number_of_methods, |
| 106 | const std::vector<uint32_t> hot_methods, |
| 107 | const std::vector<uint32_t> startup_methods, |
| 108 | const std::vector<uint32_t> post_startup_methods, |
| 109 | const ScratchFile& profile, |
| 110 | ProfileCompilationInfo* info) { |
| 111 | std::string dex_location = "location1" + id; |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 112 | using Hotness = ProfileCompilationInfo::MethodHotness; |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 113 | for (uint32_t idx : hot_methods) { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 114 | info->AddMethodIndex(Hotness::kFlagHot, dex_location, checksum, idx, number_of_methods); |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 115 | } |
| 116 | for (uint32_t idx : startup_methods) { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 117 | info->AddMethodIndex(Hotness::kFlagStartup, dex_location, checksum, idx, number_of_methods); |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 118 | } |
| 119 | for (uint32_t idx : post_startup_methods) { |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 120 | info->AddMethodIndex(Hotness::kFlagPostStartup, |
| 121 | dex_location, |
| 122 | checksum, |
| 123 | idx, |
| 124 | number_of_methods); |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 125 | } |
| 126 | ASSERT_TRUE(info->Save(GetFd(profile))); |
| 127 | ASSERT_EQ(0, profile.GetFile()->Flush()); |
| 128 | ASSERT_TRUE(profile.GetFile()->ResetOffset()); |
| 129 | } |
| 130 | |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 131 | // Creates an inline cache which will be destructed at the end of the test. |
| 132 | ProfileCompilationInfo::InlineCacheMap* CreateInlineCacheMap() { |
| 133 | used_inline_caches.emplace_back(new ProfileCompilationInfo::InlineCacheMap( |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 134 | std::less<uint16_t>(), allocator_->Adapter(kArenaAllocProfile))); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 135 | return used_inline_caches.back().get(); |
| 136 | } |
| 137 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 138 | ProfileCompilationInfo::OfflineProfileMethodInfo GetOfflineProfileMethodInfo( |
| 139 | const std::string& dex_location1, uint32_t dex_checksum1, |
| 140 | const std::string& dex_location2, uint32_t dex_checksum2) { |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 141 | ProfileCompilationInfo::InlineCacheMap* ic_map = CreateInlineCacheMap(); |
| 142 | ProfileCompilationInfo::OfflineProfileMethodInfo pmi(ic_map); |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 143 | pmi.dex_references.emplace_back(dex_location1, dex_checksum1, kMaxMethodIds); |
| 144 | pmi.dex_references.emplace_back(dex_location2, dex_checksum2, kMaxMethodIds); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 145 | |
| 146 | // Monomorphic |
| 147 | for (uint16_t dex_pc = 0; dex_pc < 11; dex_pc++) { |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 148 | ProfileCompilationInfo::DexPcData dex_pc_data(allocator_.get()); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 149 | dex_pc_data.AddClass(0, dex::TypeIndex(0)); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 150 | ic_map->Put(dex_pc, dex_pc_data); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 151 | } |
| 152 | // Polymorphic |
| 153 | for (uint16_t dex_pc = 11; dex_pc < 22; dex_pc++) { |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 154 | ProfileCompilationInfo::DexPcData dex_pc_data(allocator_.get()); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 155 | dex_pc_data.AddClass(0, dex::TypeIndex(0)); |
| 156 | dex_pc_data.AddClass(1, dex::TypeIndex(1)); |
| 157 | |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 158 | ic_map->Put(dex_pc, dex_pc_data); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 159 | } |
| 160 | // Megamorphic |
| 161 | for (uint16_t dex_pc = 22; dex_pc < 33; dex_pc++) { |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 162 | ProfileCompilationInfo::DexPcData dex_pc_data(allocator_.get()); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 163 | dex_pc_data.SetIsMegamorphic(); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 164 | ic_map->Put(dex_pc, dex_pc_data); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 165 | } |
| 166 | // Missing types |
| 167 | for (uint16_t dex_pc = 33; dex_pc < 44; dex_pc++) { |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 168 | ProfileCompilationInfo::DexPcData dex_pc_data(allocator_.get()); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 169 | dex_pc_data.SetIsMissingTypes(); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 170 | ic_map->Put(dex_pc, dex_pc_data); |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | return pmi; |
| 174 | } |
| 175 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 176 | int GetFd(const ScratchFile& file) const { |
| 177 | return static_cast<int>(file.GetFd()); |
| 178 | } |
| 179 | |
| 180 | void CheckProfileInfo(ScratchFile& file, const ProfileCompilationInfo& info) { |
| 181 | ProfileCompilationInfo file_info; |
| 182 | ASSERT_TRUE(file.GetFile()->ResetOffset()); |
| 183 | ASSERT_TRUE(file_info.Load(GetFd(file))); |
| 184 | ASSERT_TRUE(file_info.Equals(info)); |
| 185 | } |
| 186 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 187 | std::string GetProfmanCmd() { |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 188 | std::string file_path = GetTestAndroidRoot(); |
Calin Juravle | de4fb63 | 2016-02-23 16:53:30 +0000 | [diff] [blame] | 189 | file_path += "/bin/profman"; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 190 | if (kIsDebugBuild) { |
| 191 | file_path += "d"; |
| 192 | } |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 193 | EXPECT_TRUE(OS::FileExists(file_path.c_str())) |
| 194 | << file_path << " should be a valid file path"; |
| 195 | return file_path; |
| 196 | } |
Mathieu Chartier | f70fe3d | 2017-06-21 15:24:02 -0700 | [diff] [blame] | 197 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 198 | // Runs test with given arguments. |
| 199 | int ProcessProfiles(const std::vector<int>& profiles_fd, int reference_profile_fd) { |
| 200 | std::string profman_cmd = GetProfmanCmd(); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 201 | std::vector<std::string> argv_str; |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 202 | argv_str.push_back(profman_cmd); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 203 | for (size_t k = 0; k < profiles_fd.size(); k++) { |
| 204 | argv_str.push_back("--profile-file-fd=" + std::to_string(profiles_fd[k])); |
| 205 | } |
| 206 | argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile_fd)); |
| 207 | |
| 208 | std::string error; |
| 209 | return ExecAndReturnCode(argv_str, &error); |
| 210 | } |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 211 | |
| 212 | bool GenerateTestProfile(const std::string& filename) { |
| 213 | std::string profman_cmd = GetProfmanCmd(); |
| 214 | std::vector<std::string> argv_str; |
| 215 | argv_str.push_back(profman_cmd); |
| 216 | argv_str.push_back("--generate-test-profile=" + filename); |
| 217 | std::string error; |
| 218 | return ExecAndReturnCode(argv_str, &error); |
| 219 | } |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 220 | |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 221 | bool GenerateTestProfileWithInputDex(const std::string& filename) { |
| 222 | std::string profman_cmd = GetProfmanCmd(); |
| 223 | std::vector<std::string> argv_str; |
| 224 | argv_str.push_back(profman_cmd); |
| 225 | argv_str.push_back("--generate-test-profile=" + filename); |
| 226 | argv_str.push_back("--generate-test-profile-seed=0"); |
| 227 | argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]); |
| 228 | argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]); |
| 229 | std::string error; |
| 230 | return ExecAndReturnCode(argv_str, &error); |
| 231 | } |
| 232 | |
Andreas Gampe | 641a473 | 2017-08-24 13:21:35 -0700 | [diff] [blame] | 233 | bool CreateProfile(const std::string& profile_file_contents, |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 234 | const std::string& filename, |
David Brazdil | f13ac7c | 2018-01-30 10:09:08 +0000 | [diff] [blame] | 235 | const std::string& dex_location, |
| 236 | bool skip_verification) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 237 | ScratchFile class_names_file; |
| 238 | File* file = class_names_file.GetFile(); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 239 | 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] | 240 | EXPECT_EQ(0, file->Flush()); |
| 241 | EXPECT_TRUE(file->ResetOffset()); |
| 242 | std::string profman_cmd = GetProfmanCmd(); |
| 243 | std::vector<std::string> argv_str; |
| 244 | argv_str.push_back(profman_cmd); |
| 245 | argv_str.push_back("--create-profile-from=" + class_names_file.GetFilename()); |
| 246 | argv_str.push_back("--reference-profile-file=" + filename); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 247 | argv_str.push_back("--apk=" + dex_location); |
| 248 | argv_str.push_back("--dex-location=" + dex_location); |
David Brazdil | f13ac7c | 2018-01-30 10:09:08 +0000 | [diff] [blame] | 249 | if (skip_verification) { |
| 250 | argv_str.push_back("--skip-apk-verification"); |
| 251 | } |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 252 | std::string error; |
| 253 | EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0); |
| 254 | return true; |
| 255 | } |
| 256 | |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 257 | bool RunProfman(const std::string& filename, |
| 258 | std::vector<std::string>& extra_args, |
| 259 | std::string* output) { |
| 260 | ScratchFile output_file; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 261 | std::string profman_cmd = GetProfmanCmd(); |
| 262 | std::vector<std::string> argv_str; |
| 263 | argv_str.push_back(profman_cmd); |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 264 | argv_str.insert(argv_str.end(), extra_args.begin(), extra_args.end()); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 265 | argv_str.push_back("--profile-file=" + filename); |
| 266 | argv_str.push_back("--apk=" + GetLibCoreDexFileNames()[0]); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 267 | argv_str.push_back("--dex-location=" + GetLibCoreDexFileNames()[0]); |
David Brazdil | f13ac7c | 2018-01-30 10:09:08 +0000 | [diff] [blame] | 268 | argv_str.push_back("--skip-apk-verification"); |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 269 | 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] | 270 | std::string error; |
| 271 | EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0); |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 272 | File* file = output_file.GetFile(); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 273 | EXPECT_EQ(0, file->Flush()); |
| 274 | EXPECT_TRUE(file->ResetOffset()); |
| 275 | int64_t length = file->GetLength(); |
| 276 | std::unique_ptr<char[]> buf(new char[length]); |
| 277 | EXPECT_EQ(file->Read(buf.get(), length, 0), length); |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 278 | *output = std::string(buf.get(), length); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 279 | return true; |
| 280 | } |
| 281 | |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 282 | bool DumpClassesAndMethods(const std::string& filename, std::string* file_contents) { |
| 283 | std::vector<std::string> extra_args; |
| 284 | extra_args.push_back("--dump-classes-and-methods"); |
| 285 | return RunProfman(filename, extra_args, file_contents); |
| 286 | } |
| 287 | |
| 288 | bool DumpOnly(const std::string& filename, std::string* file_contents) { |
| 289 | std::vector<std::string> extra_args; |
| 290 | extra_args.push_back("--dump-only"); |
| 291 | return RunProfman(filename, extra_args, file_contents); |
| 292 | } |
| 293 | |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 294 | bool CreateAndDump(const std::string& input_file_contents, |
| 295 | std::string* output_file_contents) { |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 296 | ScratchFile profile_file; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 297 | EXPECT_TRUE(CreateProfile(input_file_contents, |
| 298 | profile_file.GetFilename(), |
David Brazdil | f13ac7c | 2018-01-30 10:09:08 +0000 | [diff] [blame] | 299 | GetLibCoreDexFileNames()[0], |
| 300 | /* skip_verification */ true)); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 301 | profile_file.GetFile()->ResetOffset(); |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 302 | EXPECT_TRUE(DumpClassesAndMethods(profile_file.GetFilename(), output_file_contents)); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 303 | return true; |
| 304 | } |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 305 | |
| 306 | mirror::Class* GetClass(jobject class_loader, const std::string& clazz) { |
| 307 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 308 | Thread* self = Thread::Current(); |
| 309 | ScopedObjectAccess soa(self); |
| 310 | StackHandleScope<1> hs(self); |
| 311 | Handle<mirror::ClassLoader> h_loader( |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 312 | hs.NewHandle(ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(class_loader)))); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 313 | return class_linker->FindClass(self, clazz.c_str(), h_loader); |
| 314 | } |
| 315 | |
| 316 | ArtMethod* GetVirtualMethod(jobject class_loader, |
| 317 | const std::string& clazz, |
| 318 | const std::string& name) { |
| 319 | mirror::Class* klass = GetClass(class_loader, clazz); |
| 320 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 321 | const auto pointer_size = class_linker->GetImagePointerSize(); |
| 322 | ArtMethod* method = nullptr; |
| 323 | Thread* self = Thread::Current(); |
| 324 | ScopedObjectAccess soa(self); |
| 325 | for (auto& m : klass->GetVirtualMethods(pointer_size)) { |
| 326 | if (name == m.GetName()) { |
| 327 | EXPECT_TRUE(method == nullptr); |
| 328 | method = &m; |
| 329 | } |
| 330 | } |
| 331 | return method; |
| 332 | } |
| 333 | |
| 334 | // Verify that given method has the expected inline caches and nothing else. |
| 335 | void AssertInlineCaches(ArtMethod* method, |
| 336 | const std::set<mirror::Class*>& expected_clases, |
| 337 | const ProfileCompilationInfo& info, |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 338 | bool is_megamorphic, |
| 339 | bool is_missing_types) |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 340 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 341 | std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi = |
| 342 | info.GetMethod(method->GetDexFile()->GetLocation(), |
| 343 | method->GetDexFile()->GetLocationChecksum(), |
| 344 | method->GetDexMethodIndex()); |
| 345 | ASSERT_TRUE(pmi != nullptr); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 346 | ASSERT_EQ(pmi->inline_caches->size(), 1u); |
| 347 | const ProfileCompilationInfo::DexPcData& dex_pc_data = pmi->inline_caches->begin()->second; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 348 | |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 349 | ASSERT_EQ(dex_pc_data.is_megamorphic, is_megamorphic); |
| 350 | ASSERT_EQ(dex_pc_data.is_missing_types, is_missing_types); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 351 | ASSERT_EQ(expected_clases.size(), dex_pc_data.classes.size()); |
| 352 | size_t found = 0; |
| 353 | for (mirror::Class* it : expected_clases) { |
| 354 | for (const auto& class_ref : dex_pc_data.classes) { |
| 355 | ProfileCompilationInfo::DexReference dex_ref = |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 356 | pmi->dex_references[class_ref.dex_profile_index]; |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 357 | if (dex_ref.MatchesDex(&(it->GetDexFile())) && |
| 358 | class_ref.type_index == it->GetDexTypeIndex()) { |
| 359 | found++; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | ASSERT_EQ(expected_clases.size(), found); |
| 365 | } |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 366 | |
Shubham Ajmera | 9ab6e1d | 2017-09-25 18:40:54 -0700 | [diff] [blame] | 367 | int CheckCompilationMethodPercentChange(uint16_t methods_in_cur_profile, |
| 368 | uint16_t methods_in_ref_profile) { |
| 369 | ScratchFile profile; |
| 370 | ScratchFile reference_profile; |
| 371 | std::vector<int> profile_fds({ GetFd(profile)}); |
| 372 | int reference_profile_fd = GetFd(reference_profile); |
| 373 | std::vector<uint32_t> hot_methods_cur; |
| 374 | std::vector<uint32_t> hot_methods_ref; |
| 375 | std::vector<uint32_t> empty_vector; |
| 376 | for (size_t i = 0; i < methods_in_cur_profile; ++i) { |
| 377 | hot_methods_cur.push_back(i); |
| 378 | } |
| 379 | for (size_t i = 0; i < methods_in_ref_profile; ++i) { |
| 380 | hot_methods_ref.push_back(i); |
| 381 | } |
| 382 | ProfileCompilationInfo info1; |
| 383 | uint16_t methods_in_profile = std::max(methods_in_cur_profile, methods_in_ref_profile); |
| 384 | SetupBasicProfile("p1", 1, methods_in_profile, hot_methods_cur, empty_vector, empty_vector, |
| 385 | profile, &info1); |
| 386 | ProfileCompilationInfo info2; |
| 387 | SetupBasicProfile("p1", 1, methods_in_profile, hot_methods_ref, empty_vector, empty_vector, |
| 388 | reference_profile, &info2); |
| 389 | return ProcessProfiles(profile_fds, reference_profile_fd); |
| 390 | } |
| 391 | |
| 392 | int CheckCompilationClassPercentChange(uint16_t classes_in_cur_profile, |
| 393 | uint16_t classes_in_ref_profile) { |
| 394 | ScratchFile profile; |
| 395 | ScratchFile reference_profile; |
| 396 | |
| 397 | std::vector<int> profile_fds({ GetFd(profile)}); |
| 398 | int reference_profile_fd = GetFd(reference_profile); |
| 399 | |
| 400 | ProfileCompilationInfo info1; |
| 401 | SetupProfile("p1", 1, 0, classes_in_cur_profile, profile, &info1); |
| 402 | ProfileCompilationInfo info2; |
| 403 | SetupProfile("p1", 1, 0, classes_in_ref_profile, reference_profile, &info2); |
| 404 | return ProcessProfiles(profile_fds, reference_profile_fd); |
| 405 | } |
| 406 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 407 | std::unique_ptr<ArenaAllocator> allocator_; |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 408 | |
| 409 | // Cache of inline caches generated during tests. |
| 410 | // This makes it easier to pass data between different utilities and ensure that |
| 411 | // caches are destructed at the end of the test. |
| 412 | std::vector<std::unique_ptr<ProfileCompilationInfo::InlineCacheMap>> used_inline_caches; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 413 | }; |
| 414 | |
| 415 | TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferences) { |
| 416 | ScratchFile profile1; |
| 417 | ScratchFile profile2; |
| 418 | ScratchFile reference_profile; |
| 419 | |
| 420 | std::vector<int> profile_fds({ |
| 421 | GetFd(profile1), |
| 422 | GetFd(profile2)}); |
| 423 | int reference_profile_fd = GetFd(reference_profile); |
| 424 | |
| 425 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 426 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 427 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 428 | ProfileCompilationInfo info2; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 429 | SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 430 | |
| 431 | // We should advise compilation. |
| 432 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 433 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 434 | // The resulting compilation info must be equal to the merge of the inputs. |
| 435 | ProfileCompilationInfo result; |
| 436 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 437 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 438 | |
| 439 | ProfileCompilationInfo expected; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 440 | ASSERT_TRUE(expected.MergeWith(info1)); |
| 441 | ASSERT_TRUE(expected.MergeWith(info2)); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 442 | ASSERT_TRUE(expected.Equals(result)); |
| 443 | |
| 444 | // The information from profiles must remain the same. |
| 445 | CheckProfileInfo(profile1, info1); |
| 446 | CheckProfileInfo(profile2, info2); |
| 447 | } |
| 448 | |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 449 | // TODO(calin): Add more tests for classes. |
| 450 | TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferencesBecauseOfClasses) { |
| 451 | ScratchFile profile1; |
| 452 | ScratchFile reference_profile; |
| 453 | |
| 454 | std::vector<int> profile_fds({ |
| 455 | GetFd(profile1)}); |
| 456 | int reference_profile_fd = GetFd(reference_profile); |
| 457 | |
| 458 | const uint16_t kNumberOfClassesToEnableCompilation = 100; |
| 459 | ProfileCompilationInfo info1; |
| 460 | SetupProfile("p1", 1, 0, kNumberOfClassesToEnableCompilation, profile1, &info1); |
| 461 | |
| 462 | // We should advise compilation. |
| 463 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 464 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 465 | // The resulting compilation info must be equal to the merge of the inputs. |
| 466 | ProfileCompilationInfo result; |
| 467 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 468 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 469 | |
| 470 | ProfileCompilationInfo expected; |
| 471 | ASSERT_TRUE(expected.MergeWith(info1)); |
| 472 | ASSERT_TRUE(expected.Equals(result)); |
| 473 | |
| 474 | // The information from profiles must remain the same. |
| 475 | CheckProfileInfo(profile1, info1); |
| 476 | } |
| 477 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 478 | TEST_F(ProfileAssistantTest, AdviseCompilationNonEmptyReferences) { |
| 479 | ScratchFile profile1; |
| 480 | ScratchFile profile2; |
| 481 | ScratchFile reference_profile; |
| 482 | |
| 483 | std::vector<int> profile_fds({ |
| 484 | GetFd(profile1), |
| 485 | GetFd(profile2)}); |
| 486 | int reference_profile_fd = GetFd(reference_profile); |
| 487 | |
| 488 | // The new profile info will contain the methods with indices 0-100. |
| 489 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 490 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 491 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 492 | ProfileCompilationInfo info2; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 493 | SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 494 | |
| 495 | |
| 496 | // The reference profile info will contain the methods with indices 50-150. |
| 497 | const uint16_t kNumberOfMethodsAlreadyCompiled = 100; |
| 498 | ProfileCompilationInfo reference_info; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 499 | SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile, |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 500 | &reference_info, kNumberOfMethodsToEnableCompilation / 2); |
| 501 | |
| 502 | // We should advise compilation. |
| 503 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 504 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 505 | |
| 506 | // The resulting compilation info must be equal to the merge of the inputs |
| 507 | ProfileCompilationInfo result; |
| 508 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 509 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 510 | |
| 511 | ProfileCompilationInfo expected; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 512 | ASSERT_TRUE(expected.MergeWith(info1)); |
| 513 | ASSERT_TRUE(expected.MergeWith(info2)); |
| 514 | ASSERT_TRUE(expected.MergeWith(reference_info)); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 515 | ASSERT_TRUE(expected.Equals(result)); |
| 516 | |
| 517 | // The information from profiles must remain the same. |
| 518 | CheckProfileInfo(profile1, info1); |
| 519 | CheckProfileInfo(profile2, info2); |
| 520 | } |
| 521 | |
| 522 | TEST_F(ProfileAssistantTest, DoNotAdviseCompilation) { |
| 523 | ScratchFile profile1; |
| 524 | ScratchFile profile2; |
| 525 | ScratchFile reference_profile; |
| 526 | |
| 527 | std::vector<int> profile_fds({ |
| 528 | GetFd(profile1), |
| 529 | GetFd(profile2)}); |
| 530 | int reference_profile_fd = GetFd(reference_profile); |
| 531 | |
Shubham Ajmera | 9ab6e1d | 2017-09-25 18:40:54 -0700 | [diff] [blame] | 532 | const uint16_t kNumberOfMethodsToSkipCompilation = 24; // Threshold is 100. |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 533 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 534 | SetupProfile("p1", 1, kNumberOfMethodsToSkipCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 535 | ProfileCompilationInfo info2; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 536 | SetupProfile("p2", 2, kNumberOfMethodsToSkipCompilation, 0, profile2, &info2); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 537 | |
| 538 | // We should not advise compilation. |
| 539 | ASSERT_EQ(ProfileAssistant::kSkipCompilation, |
| 540 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 541 | |
| 542 | // The information from profiles must remain the same. |
| 543 | ProfileCompilationInfo file_info1; |
| 544 | ASSERT_TRUE(profile1.GetFile()->ResetOffset()); |
| 545 | ASSERT_TRUE(file_info1.Load(GetFd(profile1))); |
| 546 | ASSERT_TRUE(file_info1.Equals(info1)); |
| 547 | |
| 548 | ProfileCompilationInfo file_info2; |
| 549 | ASSERT_TRUE(profile2.GetFile()->ResetOffset()); |
| 550 | ASSERT_TRUE(file_info2.Load(GetFd(profile2))); |
| 551 | ASSERT_TRUE(file_info2.Equals(info2)); |
| 552 | |
| 553 | // Reference profile files must remain empty. |
| 554 | ASSERT_EQ(0, reference_profile.GetFile()->GetLength()); |
| 555 | |
| 556 | // The information from profiles must remain the same. |
| 557 | CheckProfileInfo(profile1, info1); |
| 558 | CheckProfileInfo(profile2, info2); |
| 559 | } |
| 560 | |
Shubham Ajmera | 9ab6e1d | 2017-09-25 18:40:54 -0700 | [diff] [blame] | 561 | TEST_F(ProfileAssistantTest, DoNotAdviseCompilationMethodPercentage) { |
| 562 | const uint16_t kNumberOfMethodsInRefProfile = 6000; |
| 563 | const uint16_t kNumberOfMethodsInCurProfile = 6100; // Threshold is 2%. |
| 564 | // We should not advise compilation. |
| 565 | ASSERT_EQ(ProfileAssistant::kSkipCompilation, |
| 566 | CheckCompilationMethodPercentChange(kNumberOfMethodsInCurProfile, |
| 567 | kNumberOfMethodsInRefProfile)); |
| 568 | } |
| 569 | |
| 570 | TEST_F(ProfileAssistantTest, ShouldAdviseCompilationMethodPercentage) { |
| 571 | const uint16_t kNumberOfMethodsInRefProfile = 6000; |
| 572 | const uint16_t kNumberOfMethodsInCurProfile = 6200; // Threshold is 2%. |
| 573 | // We should advise compilation. |
| 574 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 575 | CheckCompilationMethodPercentChange(kNumberOfMethodsInCurProfile, |
| 576 | kNumberOfMethodsInRefProfile)); |
| 577 | } |
| 578 | |
| 579 | TEST_F(ProfileAssistantTest, DoNotdviseCompilationClassPercentage) { |
| 580 | const uint16_t kNumberOfClassesInRefProfile = 6000; |
| 581 | const uint16_t kNumberOfClassesInCurProfile = 6110; // Threshold is 2%. |
| 582 | // We should not advise compilation. |
| 583 | ASSERT_EQ(ProfileAssistant::kSkipCompilation, |
| 584 | CheckCompilationClassPercentChange(kNumberOfClassesInCurProfile, |
| 585 | kNumberOfClassesInRefProfile)); |
| 586 | } |
| 587 | |
| 588 | TEST_F(ProfileAssistantTest, ShouldAdviseCompilationClassPercentage) { |
| 589 | const uint16_t kNumberOfClassesInRefProfile = 6000; |
| 590 | const uint16_t kNumberOfClassesInCurProfile = 6120; // Threshold is 2%. |
| 591 | // We should advise compilation. |
| 592 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 593 | CheckCompilationClassPercentChange(kNumberOfClassesInCurProfile, |
| 594 | kNumberOfClassesInRefProfile)); |
| 595 | } |
| 596 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 597 | TEST_F(ProfileAssistantTest, FailProcessingBecauseOfProfiles) { |
| 598 | ScratchFile profile1; |
| 599 | ScratchFile profile2; |
| 600 | ScratchFile reference_profile; |
| 601 | |
| 602 | std::vector<int> profile_fds({ |
| 603 | GetFd(profile1), |
| 604 | GetFd(profile2)}); |
| 605 | int reference_profile_fd = GetFd(reference_profile); |
| 606 | |
| 607 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 608 | // Assign different hashes for the same dex file. This will make merging of information to fail. |
| 609 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 610 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 611 | ProfileCompilationInfo info2; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 612 | SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, profile2, &info2); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 613 | |
| 614 | // We should fail processing. |
| 615 | ASSERT_EQ(ProfileAssistant::kErrorBadProfiles, |
| 616 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 617 | |
| 618 | // The information from profiles must remain the same. |
| 619 | CheckProfileInfo(profile1, info1); |
| 620 | CheckProfileInfo(profile2, info2); |
| 621 | |
| 622 | // Reference profile files must still remain empty. |
| 623 | ASSERT_EQ(0, reference_profile.GetFile()->GetLength()); |
| 624 | } |
| 625 | |
| 626 | TEST_F(ProfileAssistantTest, FailProcessingBecauseOfReferenceProfiles) { |
| 627 | ScratchFile profile1; |
| 628 | ScratchFile reference_profile; |
| 629 | |
| 630 | std::vector<int> profile_fds({ |
| 631 | GetFd(profile1)}); |
| 632 | int reference_profile_fd = GetFd(reference_profile); |
| 633 | |
| 634 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 635 | // Assign different hashes for the same dex file. This will make merging of information to fail. |
| 636 | ProfileCompilationInfo info1; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 637 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 638 | ProfileCompilationInfo reference_info; |
Calin Juravle | c824b51 | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 639 | SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, 0, reference_profile, &reference_info); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 640 | |
| 641 | // We should not advise compilation. |
| 642 | ASSERT_TRUE(profile1.GetFile()->ResetOffset()); |
| 643 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 644 | ASSERT_EQ(ProfileAssistant::kErrorBadProfiles, |
| 645 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 646 | |
| 647 | // The information from profiles must remain the same. |
| 648 | CheckProfileInfo(profile1, info1); |
| 649 | } |
| 650 | |
Calin Juravle | 7bcdb53 | 2016-06-07 16:14:47 +0100 | [diff] [blame] | 651 | TEST_F(ProfileAssistantTest, TestProfileGeneration) { |
| 652 | ScratchFile profile; |
| 653 | // Generate a test profile. |
| 654 | GenerateTestProfile(profile.GetFilename()); |
| 655 | |
| 656 | // Verify that the generated profile is valid and can be loaded. |
| 657 | ASSERT_TRUE(profile.GetFile()->ResetOffset()); |
| 658 | ProfileCompilationInfo info; |
| 659 | ASSERT_TRUE(info.Load(GetFd(profile))); |
| 660 | } |
| 661 | |
Jeff Hao | f0a31f8 | 2017-03-27 15:50:37 -0700 | [diff] [blame] | 662 | TEST_F(ProfileAssistantTest, TestProfileGenerationWithIndexDex) { |
| 663 | ScratchFile profile; |
| 664 | // Generate a test profile passing in a dex file as reference. |
| 665 | GenerateTestProfileWithInputDex(profile.GetFilename()); |
| 666 | |
| 667 | // Verify that the generated profile is valid and can be loaded. |
| 668 | ASSERT_TRUE(profile.GetFile()->ResetOffset()); |
| 669 | ProfileCompilationInfo info; |
| 670 | ASSERT_TRUE(info.Load(GetFd(profile))); |
| 671 | } |
| 672 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 673 | TEST_F(ProfileAssistantTest, TestProfileCreationAllMatch) { |
| 674 | // Class names put here need to be in sorted order. |
| 675 | std::vector<std::string> class_names = { |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 676 | "HLjava/lang/Object;-><init>()V", |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 677 | "Ljava/lang/Comparable;", |
| 678 | "Ljava/lang/Math;", |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 679 | "Ljava/lang/Object;", |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 680 | "SPLjava/lang/Comparable;->compareTo(Ljava/lang/Object;)I", |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 681 | }; |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 682 | std::string file_contents; |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 683 | for (std::string& class_name : class_names) { |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 684 | file_contents += class_name + std::string("\n"); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 685 | } |
| 686 | std::string output_file_contents; |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 687 | ASSERT_TRUE(CreateAndDump(file_contents, &output_file_contents)); |
| 688 | ASSERT_EQ(output_file_contents, file_contents); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 691 | TEST_F(ProfileAssistantTest, TestProfileCreationGenerateMethods) { |
| 692 | // Class names put here need to be in sorted order. |
| 693 | std::vector<std::string> class_names = { |
| 694 | "Ljava/lang/Math;->*", |
| 695 | }; |
| 696 | std::string input_file_contents; |
| 697 | std::string expected_contents; |
| 698 | for (std::string& class_name : class_names) { |
| 699 | input_file_contents += class_name + std::string("\n"); |
| 700 | expected_contents += DescriptorToDot(class_name.c_str()) + |
| 701 | std::string("\n"); |
| 702 | } |
| 703 | std::string output_file_contents; |
| 704 | ScratchFile profile_file; |
| 705 | EXPECT_TRUE(CreateProfile(input_file_contents, |
| 706 | profile_file.GetFilename(), |
David Brazdil | f13ac7c | 2018-01-30 10:09:08 +0000 | [diff] [blame] | 707 | GetLibCoreDexFileNames()[0], |
| 708 | /* skip_verification */ true)); |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 709 | ProfileCompilationInfo info; |
| 710 | profile_file.GetFile()->ResetOffset(); |
| 711 | ASSERT_TRUE(info.Load(GetFd(profile_file))); |
| 712 | // Verify that the profile has matching methods. |
| 713 | ScopedObjectAccess soa(Thread::Current()); |
| 714 | ObjPtr<mirror::Class> klass = GetClass(nullptr, "Ljava/lang/Math;"); |
| 715 | ASSERT_TRUE(klass != nullptr); |
| 716 | size_t method_count = 0; |
| 717 | for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) { |
| 718 | if (!method.IsCopied() && method.GetCodeItem() != nullptr) { |
| 719 | ++method_count; |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 720 | std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi = |
| 721 | info.GetMethod(method.GetDexFile()->GetLocation(), |
| 722 | method.GetDexFile()->GetLocationChecksum(), |
| 723 | method.GetDexMethodIndex()); |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 724 | ASSERT_TRUE(pmi != nullptr) << method.PrettyMethod(); |
Mathieu Chartier | d808e8b | 2017-03-21 13:37:41 -0700 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | EXPECT_GT(method_count, 0u); |
| 728 | } |
| 729 | |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 730 | TEST_F(ProfileAssistantTest, TestBootImageProfile) { |
| 731 | const std::string core_dex = GetLibCoreDexFileNames()[0]; |
| 732 | |
| 733 | std::vector<ScratchFile> profiles; |
| 734 | |
| 735 | // In image with enough clean occurrences. |
| 736 | const std::string kCleanClass = "Ljava/lang/CharSequence;"; |
| 737 | // In image with enough dirty occurrences. |
| 738 | const std::string kDirtyClass = "Ljava/lang/Object;"; |
| 739 | // Not in image becauseof not enough occurrences. |
| 740 | const std::string kUncommonCleanClass = "Ljava/lang/Process;"; |
| 741 | const std::string kUncommonDirtyClass = "Ljava/lang/Package;"; |
| 742 | // Method that is hot. |
| 743 | // Also adds the class through inference since it is in each dex. |
| 744 | const std::string kHotMethod = "Ljava/lang/Comparable;->compareTo(Ljava/lang/Object;)I"; |
| 745 | // Method that doesn't add the class since its only in one profile. Should still show up in the |
| 746 | // boot profile. |
| 747 | const std::string kOtherMethod = "Ljava/util/HashMap;-><init>()V"; |
Mathieu Chartier | 8eecddf | 2017-07-12 16:05:54 -0700 | [diff] [blame] | 748 | // Method that gets marked as hot since it's in multiple profiles. |
| 749 | const std::string kMultiMethod = "Ljava/util/ArrayList;->clear()V"; |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 750 | |
| 751 | // Thresholds for this test. |
| 752 | static const size_t kDirtyThreshold = 3; |
| 753 | static const size_t kCleanThreshold = 2; |
Mathieu Chartier | 8eecddf | 2017-07-12 16:05:54 -0700 | [diff] [blame] | 754 | static const size_t kMethodThreshold = 2; |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 755 | |
| 756 | // Create a bunch of boot profiles. |
| 757 | std::string dex1 = |
| 758 | kCleanClass + "\n" + |
| 759 | kDirtyClass + "\n" + |
| 760 | kUncommonCleanClass + "\n" + |
| 761 | "H" + kHotMethod + "\n" + |
| 762 | kUncommonDirtyClass; |
| 763 | profiles.emplace_back(ScratchFile()); |
David Brazdil | f13ac7c | 2018-01-30 10:09:08 +0000 | [diff] [blame] | 764 | EXPECT_TRUE(CreateProfile( |
| 765 | dex1, profiles.back().GetFilename(), core_dex, /* skip_verification */ true)); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 766 | |
| 767 | // Create a bunch of boot profiles. |
| 768 | std::string dex2 = |
| 769 | kCleanClass + "\n" + |
| 770 | kDirtyClass + "\n" + |
| 771 | "P" + kHotMethod + "\n" + |
Mathieu Chartier | 8eecddf | 2017-07-12 16:05:54 -0700 | [diff] [blame] | 772 | "P" + kMultiMethod + "\n" + |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 773 | kUncommonDirtyClass; |
| 774 | profiles.emplace_back(ScratchFile()); |
David Brazdil | f13ac7c | 2018-01-30 10:09:08 +0000 | [diff] [blame] | 775 | EXPECT_TRUE(CreateProfile( |
| 776 | dex2, profiles.back().GetFilename(), core_dex, /* skip_verification */ true)); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 777 | |
| 778 | // Create a bunch of boot profiles. |
| 779 | std::string dex3 = |
| 780 | "S" + kHotMethod + "\n" + |
| 781 | "P" + kOtherMethod + "\n" + |
Mathieu Chartier | 8eecddf | 2017-07-12 16:05:54 -0700 | [diff] [blame] | 782 | "P" + kMultiMethod + "\n" + |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 783 | kDirtyClass + "\n"; |
| 784 | profiles.emplace_back(ScratchFile()); |
David Brazdil | f13ac7c | 2018-01-30 10:09:08 +0000 | [diff] [blame] | 785 | EXPECT_TRUE(CreateProfile( |
| 786 | dex3, profiles.back().GetFilename(), core_dex, /* skip_verification */ true)); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 787 | |
| 788 | // Generate the boot profile. |
| 789 | ScratchFile out_profile; |
| 790 | std::vector<std::string> args; |
| 791 | args.push_back(GetProfmanCmd()); |
| 792 | args.push_back("--generate-boot-image-profile"); |
| 793 | args.push_back("--boot-image-class-threshold=" + std::to_string(kDirtyThreshold)); |
| 794 | args.push_back("--boot-image-clean-class-threshold=" + std::to_string(kCleanThreshold)); |
Mathieu Chartier | 8eecddf | 2017-07-12 16:05:54 -0700 | [diff] [blame] | 795 | args.push_back("--boot-image-sampled-method-threshold=" + std::to_string(kMethodThreshold)); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 796 | args.push_back("--reference-profile-file=" + out_profile.GetFilename()); |
| 797 | args.push_back("--apk=" + core_dex); |
| 798 | args.push_back("--dex-location=" + core_dex); |
David Brazdil | f13ac7c | 2018-01-30 10:09:08 +0000 | [diff] [blame] | 799 | args.push_back("--skip-apk-verification"); |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 800 | for (const ScratchFile& profile : profiles) { |
| 801 | args.push_back("--profile-file=" + profile.GetFilename()); |
| 802 | } |
| 803 | std::string error; |
| 804 | EXPECT_EQ(ExecAndReturnCode(args, &error), 0) << error; |
| 805 | ASSERT_EQ(0, out_profile.GetFile()->Flush()); |
| 806 | ASSERT_TRUE(out_profile.GetFile()->ResetOffset()); |
| 807 | |
| 808 | // Verify the boot profile contents. |
| 809 | std::string output_file_contents; |
| 810 | EXPECT_TRUE(DumpClassesAndMethods(out_profile.GetFilename(), &output_file_contents)); |
| 811 | // Common classes, should be in the classes of the profile. |
| 812 | EXPECT_NE(output_file_contents.find(kCleanClass + "\n"), std::string::npos) |
| 813 | << output_file_contents; |
| 814 | EXPECT_NE(output_file_contents.find(kDirtyClass + "\n"), std::string::npos) |
| 815 | << output_file_contents; |
| 816 | // Uncommon classes, should not fit preloaded class criteria and should not be in the profile. |
| 817 | EXPECT_EQ(output_file_contents.find(kUncommonCleanClass + "\n"), std::string::npos) |
| 818 | << output_file_contents; |
| 819 | EXPECT_EQ(output_file_contents.find(kUncommonDirtyClass + "\n"), std::string::npos) |
| 820 | << output_file_contents; |
| 821 | // Inferred class from a method common to all three profiles. |
| 822 | EXPECT_NE(output_file_contents.find("Ljava/lang/Comparable;\n"), std::string::npos) |
| 823 | << output_file_contents; |
| 824 | // Aggregated methods hotness information. |
| 825 | EXPECT_NE(output_file_contents.find("HSP" + kHotMethod), std::string::npos) |
| 826 | << output_file_contents; |
Mathieu Chartier | 8eecddf | 2017-07-12 16:05:54 -0700 | [diff] [blame] | 827 | EXPECT_NE(output_file_contents.find("P" + kOtherMethod), std::string::npos) |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 828 | << output_file_contents; |
| 829 | // Not inferred class, method is only in one profile. |
| 830 | EXPECT_EQ(output_file_contents.find("Ljava/util/HashMap;\n"), std::string::npos) |
| 831 | << output_file_contents; |
Mathieu Chartier | 8eecddf | 2017-07-12 16:05:54 -0700 | [diff] [blame] | 832 | // Test the sampled methods that became hot. |
| 833 | // Other method is in only one profile, it should not become hot. |
| 834 | EXPECT_EQ(output_file_contents.find("HP" + kOtherMethod), std::string::npos) |
| 835 | << output_file_contents; |
| 836 | // Multi method is in at least two profiles, it should become hot. |
| 837 | EXPECT_NE(output_file_contents.find("HP" + kMultiMethod), std::string::npos) |
| 838 | << output_file_contents; |
Mathieu Chartier | 2f79455 | 2017-06-19 10:58:08 -0700 | [diff] [blame] | 839 | } |
| 840 | |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 841 | TEST_F(ProfileAssistantTest, TestProfileCreationOneNotMatched) { |
| 842 | // Class names put here need to be in sorted order. |
| 843 | std::vector<std::string> class_names = { |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 844 | "Ldoesnt/match/this/one;", |
| 845 | "Ljava/lang/Comparable;", |
| 846 | "Ljava/lang/Object;" |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 847 | }; |
| 848 | std::string input_file_contents; |
| 849 | for (std::string& class_name : class_names) { |
| 850 | input_file_contents += class_name + std::string("\n"); |
| 851 | } |
| 852 | std::string output_file_contents; |
| 853 | ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents)); |
| 854 | std::string expected_contents = |
Mathieu Chartier | 3406726 | 2017-04-06 13:55:46 -0700 | [diff] [blame] | 855 | class_names[1] + std::string("\n") + |
| 856 | class_names[2] + std::string("\n"); |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 857 | ASSERT_EQ(output_file_contents, expected_contents); |
| 858 | } |
| 859 | |
| 860 | TEST_F(ProfileAssistantTest, TestProfileCreationNoneMatched) { |
| 861 | // Class names put here need to be in sorted order. |
| 862 | std::vector<std::string> class_names = { |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 863 | "Ldoesnt/match/this/one;", |
| 864 | "Ldoesnt/match/this/one/either;", |
| 865 | "Lnor/this/one;" |
David Sehr | 7c80f2d | 2017-02-07 16:47:58 -0800 | [diff] [blame] | 866 | }; |
| 867 | std::string input_file_contents; |
| 868 | for (std::string& class_name : class_names) { |
| 869 | input_file_contents += class_name + std::string("\n"); |
| 870 | } |
| 871 | std::string output_file_contents; |
| 872 | ASSERT_TRUE(CreateAndDump(input_file_contents, &output_file_contents)); |
| 873 | std::string expected_contents(""); |
| 874 | ASSERT_EQ(output_file_contents, expected_contents); |
| 875 | } |
| 876 | |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 877 | TEST_F(ProfileAssistantTest, TestProfileCreateInlineCache) { |
| 878 | // Create the profile content. |
| 879 | std::vector<std::string> methods = { |
| 880 | "LTestInline;->inlineMonomorphic(LSuper;)I+LSubA;", |
| 881 | "LTestInline;->inlinePolymorphic(LSuper;)I+LSubA;,LSubB;,LSubC;", |
| 882 | "LTestInline;->inlineMegamorphic(LSuper;)I+LSubA;,LSubB;,LSubC;,LSubD;,LSubE;", |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 883 | "LTestInline;->inlineMissingTypes(LSuper;)I+missing_types", |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 884 | "LTestInline;->noInlineCache(LSuper;)I" |
| 885 | }; |
| 886 | std::string input_file_contents; |
| 887 | for (std::string& m : methods) { |
| 888 | input_file_contents += m + std::string("\n"); |
| 889 | } |
| 890 | |
| 891 | // Create the profile and save it to disk. |
| 892 | ScratchFile profile_file; |
| 893 | ASSERT_TRUE(CreateProfile(input_file_contents, |
| 894 | profile_file.GetFilename(), |
David Brazdil | f13ac7c | 2018-01-30 10:09:08 +0000 | [diff] [blame] | 895 | GetTestDexFileName("ProfileTestMultiDex"), |
| 896 | /* skip_verification */ false)); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 897 | |
| 898 | // Load the profile from disk. |
| 899 | ProfileCompilationInfo info; |
| 900 | profile_file.GetFile()->ResetOffset(); |
| 901 | ASSERT_TRUE(info.Load(GetFd(profile_file))); |
| 902 | |
| 903 | // Load the dex files and verify that the profile contains the expected methods info. |
| 904 | ScopedObjectAccess soa(Thread::Current()); |
| 905 | jobject class_loader = LoadDex("ProfileTestMultiDex"); |
| 906 | ASSERT_NE(class_loader, nullptr); |
| 907 | |
| 908 | mirror::Class* sub_a = GetClass(class_loader, "LSubA;"); |
| 909 | mirror::Class* sub_b = GetClass(class_loader, "LSubB;"); |
| 910 | mirror::Class* sub_c = GetClass(class_loader, "LSubC;"); |
| 911 | |
| 912 | ASSERT_TRUE(sub_a != nullptr); |
| 913 | ASSERT_TRUE(sub_b != nullptr); |
| 914 | ASSERT_TRUE(sub_c != nullptr); |
| 915 | |
| 916 | { |
| 917 | // Verify that method inlineMonomorphic has the expected inline caches and nothing else. |
| 918 | ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader, |
| 919 | "LTestInline;", |
| 920 | "inlineMonomorphic"); |
| 921 | ASSERT_TRUE(inline_monomorphic != nullptr); |
| 922 | std::set<mirror::Class*> expected_monomorphic; |
| 923 | expected_monomorphic.insert(sub_a); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 924 | AssertInlineCaches(inline_monomorphic, |
| 925 | expected_monomorphic, |
| 926 | info, |
| 927 | /*megamorphic*/false, |
| 928 | /*missing_types*/false); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | { |
| 932 | // Verify that method inlinePolymorphic has the expected inline caches and nothing else. |
| 933 | ArtMethod* inline_polymorhic = GetVirtualMethod(class_loader, |
| 934 | "LTestInline;", |
| 935 | "inlinePolymorphic"); |
| 936 | ASSERT_TRUE(inline_polymorhic != nullptr); |
| 937 | std::set<mirror::Class*> expected_polymorphic; |
| 938 | expected_polymorphic.insert(sub_a); |
| 939 | expected_polymorphic.insert(sub_b); |
| 940 | expected_polymorphic.insert(sub_c); |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 941 | AssertInlineCaches(inline_polymorhic, |
| 942 | expected_polymorphic, |
| 943 | info, |
| 944 | /*megamorphic*/false, |
| 945 | /*missing_types*/false); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | { |
| 949 | // Verify that method inlineMegamorphic has the expected inline caches and nothing else. |
| 950 | ArtMethod* inline_megamorphic = GetVirtualMethod(class_loader, |
| 951 | "LTestInline;", |
| 952 | "inlineMegamorphic"); |
| 953 | ASSERT_TRUE(inline_megamorphic != nullptr); |
| 954 | std::set<mirror::Class*> expected_megamorphic; |
Calin Juravle | 589e71e | 2017-03-03 16:05:05 -0800 | [diff] [blame] | 955 | AssertInlineCaches(inline_megamorphic, |
| 956 | expected_megamorphic, |
| 957 | info, |
| 958 | /*megamorphic*/true, |
| 959 | /*missing_types*/false); |
| 960 | } |
| 961 | |
| 962 | { |
| 963 | // Verify that method inlineMegamorphic has the expected inline caches and nothing else. |
| 964 | ArtMethod* inline_missing_types = GetVirtualMethod(class_loader, |
| 965 | "LTestInline;", |
| 966 | "inlineMissingTypes"); |
| 967 | ASSERT_TRUE(inline_missing_types != nullptr); |
| 968 | std::set<mirror::Class*> expected_missing_Types; |
| 969 | AssertInlineCaches(inline_missing_types, |
| 970 | expected_missing_Types, |
| 971 | info, |
| 972 | /*megamorphic*/false, |
| 973 | /*missing_types*/true); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | { |
| 977 | // Verify that method noInlineCache has no inline caches in the profile. |
| 978 | ArtMethod* no_inline_cache = GetVirtualMethod(class_loader, "LTestInline;", "noInlineCache"); |
| 979 | ASSERT_TRUE(no_inline_cache != nullptr); |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 980 | std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi_no_inline_cache = |
| 981 | info.GetMethod(no_inline_cache->GetDexFile()->GetLocation(), |
| 982 | no_inline_cache->GetDexFile()->GetLocationChecksum(), |
| 983 | no_inline_cache->GetDexMethodIndex()); |
| 984 | ASSERT_TRUE(pmi_no_inline_cache != nullptr); |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 985 | ASSERT_TRUE(pmi_no_inline_cache->inline_caches->empty()); |
Calin Juravle | e0ac115 | 2017-02-13 19:03:47 -0800 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 989 | TEST_F(ProfileAssistantTest, MergeProfilesWithDifferentDexOrder) { |
| 990 | ScratchFile profile1; |
| 991 | ScratchFile reference_profile; |
| 992 | |
| 993 | std::vector<int> profile_fds({GetFd(profile1)}); |
| 994 | int reference_profile_fd = GetFd(reference_profile); |
| 995 | |
| 996 | // The new profile info will contain the methods with indices 0-100. |
| 997 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 998 | ProfileCompilationInfo info1; |
| 999 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, 0, profile1, &info1, |
| 1000 | /*start_method_index*/0, /*reverse_dex_write_order*/false); |
| 1001 | |
| 1002 | // The reference profile info will contain the methods with indices 50-150. |
| 1003 | // When setting up the profile reverse the order in which the dex files |
| 1004 | // are added to the profile. This will verify that profman merges profiles |
| 1005 | // with a different dex order correctly. |
| 1006 | const uint16_t kNumberOfMethodsAlreadyCompiled = 100; |
| 1007 | ProfileCompilationInfo reference_info; |
| 1008 | SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, 0, reference_profile, |
| 1009 | &reference_info, kNumberOfMethodsToEnableCompilation / 2, /*reverse_dex_write_order*/true); |
| 1010 | |
| 1011 | // We should advise compilation. |
| 1012 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 1013 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 1014 | |
| 1015 | // The resulting compilation info must be equal to the merge of the inputs. |
| 1016 | ProfileCompilationInfo result; |
| 1017 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 1018 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 1019 | |
| 1020 | ProfileCompilationInfo expected; |
| 1021 | ASSERT_TRUE(expected.MergeWith(reference_info)); |
| 1022 | ASSERT_TRUE(expected.MergeWith(info1)); |
| 1023 | ASSERT_TRUE(expected.Equals(result)); |
| 1024 | |
| 1025 | // The information from profile must remain the same. |
| 1026 | CheckProfileInfo(profile1, info1); |
| 1027 | } |
| 1028 | |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 1029 | TEST_F(ProfileAssistantTest, TestProfileCreateWithInvalidData) { |
| 1030 | // Create the profile content. |
| 1031 | std::vector<std::string> profile_methods = { |
| 1032 | "LTestInline;->inlineMonomorphic(LSuper;)I+invalid_class", |
| 1033 | "LTestInline;->invalid_method", |
| 1034 | "invalid_class" |
| 1035 | }; |
| 1036 | std::string input_file_contents; |
| 1037 | for (std::string& m : profile_methods) { |
| 1038 | input_file_contents += m + std::string("\n"); |
| 1039 | } |
| 1040 | |
| 1041 | // Create the profile and save it to disk. |
| 1042 | ScratchFile profile_file; |
| 1043 | std::string dex_filename = GetTestDexFileName("ProfileTestMultiDex"); |
| 1044 | ASSERT_TRUE(CreateProfile(input_file_contents, |
| 1045 | profile_file.GetFilename(), |
David Brazdil | f13ac7c | 2018-01-30 10:09:08 +0000 | [diff] [blame] | 1046 | dex_filename, |
| 1047 | /* skip_verification */ false)); |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 1048 | |
| 1049 | // Load the profile from disk. |
| 1050 | ProfileCompilationInfo info; |
| 1051 | profile_file.GetFile()->ResetOffset(); |
| 1052 | ASSERT_TRUE(info.Load(GetFd(profile_file))); |
| 1053 | |
| 1054 | // Load the dex files and verify that the profile contains the expected methods info. |
| 1055 | ScopedObjectAccess soa(Thread::Current()); |
| 1056 | jobject class_loader = LoadDex("ProfileTestMultiDex"); |
| 1057 | ASSERT_NE(class_loader, nullptr); |
| 1058 | |
| 1059 | ArtMethod* inline_monomorphic = GetVirtualMethod(class_loader, |
| 1060 | "LTestInline;", |
| 1061 | "inlineMonomorphic"); |
| 1062 | const DexFile* dex_file = inline_monomorphic->GetDexFile(); |
| 1063 | |
| 1064 | // Verify that the inline cache contains the invalid type. |
| 1065 | std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> pmi = |
| 1066 | info.GetMethod(dex_file->GetLocation(), |
| 1067 | dex_file->GetLocationChecksum(), |
| 1068 | inline_monomorphic->GetDexMethodIndex()); |
| 1069 | ASSERT_TRUE(pmi != nullptr); |
| 1070 | ASSERT_EQ(pmi->inline_caches->size(), 1u); |
| 1071 | const ProfileCompilationInfo::DexPcData& dex_pc_data = pmi->inline_caches->begin()->second; |
| 1072 | dex::TypeIndex invalid_class_index(std::numeric_limits<uint16_t>::max() - 1); |
| 1073 | ASSERT_EQ(1u, dex_pc_data.classes.size()); |
| 1074 | ASSERT_EQ(invalid_class_index, dex_pc_data.classes.begin()->type_index); |
| 1075 | |
| 1076 | // Verify that the start-up classes contain the invalid class. |
| 1077 | std::set<dex::TypeIndex> classes; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1078 | std::set<uint16_t> hot_methods; |
| 1079 | std::set<uint16_t> startup_methods; |
| 1080 | std::set<uint16_t> post_start_methods; |
| 1081 | ASSERT_TRUE(info.GetClassesAndMethods(*dex_file, |
| 1082 | &classes, |
| 1083 | &hot_methods, |
| 1084 | &startup_methods, |
| 1085 | &post_start_methods)); |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 1086 | ASSERT_EQ(1u, classes.size()); |
| 1087 | ASSERT_TRUE(classes.find(invalid_class_index) != classes.end()); |
| 1088 | |
| 1089 | // Verify that the invalid method is in the profile. |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1090 | ASSERT_EQ(2u, hot_methods.size()); |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 1091 | uint16_t invalid_method_index = std::numeric_limits<uint16_t>::max() - 1; |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 1092 | ASSERT_TRUE(hot_methods.find(invalid_method_index) != hot_methods.end()); |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Mathieu Chartier | 28b5c58 | 2017-06-06 14:12:50 -0700 | [diff] [blame] | 1095 | TEST_F(ProfileAssistantTest, DumpOnly) { |
| 1096 | ScratchFile profile; |
| 1097 | |
| 1098 | const uint32_t kNumberOfMethods = 64; |
| 1099 | std::vector<uint32_t> hot_methods; |
| 1100 | std::vector<uint32_t> startup_methods; |
| 1101 | std::vector<uint32_t> post_startup_methods; |
| 1102 | for (size_t i = 0; i < kNumberOfMethods; ++i) { |
| 1103 | if (i % 2 == 0) { |
| 1104 | hot_methods.push_back(i); |
| 1105 | } |
| 1106 | if (i % 3 == 1) { |
| 1107 | startup_methods.push_back(i); |
| 1108 | } |
| 1109 | if (i % 4 == 2) { |
| 1110 | post_startup_methods.push_back(i); |
| 1111 | } |
| 1112 | } |
| 1113 | EXPECT_GT(hot_methods.size(), 0u); |
| 1114 | EXPECT_GT(startup_methods.size(), 0u); |
| 1115 | EXPECT_GT(post_startup_methods.size(), 0u); |
| 1116 | ProfileCompilationInfo info1; |
| 1117 | SetupBasicProfile("p1", |
| 1118 | 1, |
| 1119 | kNumberOfMethods, |
| 1120 | hot_methods, |
| 1121 | startup_methods, |
| 1122 | post_startup_methods, |
| 1123 | profile, |
| 1124 | &info1); |
| 1125 | std::string output; |
| 1126 | DumpOnly(profile.GetFilename(), &output); |
| 1127 | const size_t hot_offset = output.find("hot methods:"); |
| 1128 | const size_t startup_offset = output.find("startup methods:"); |
| 1129 | const size_t post_startup_offset = output.find("post startup methods:"); |
| 1130 | const size_t classes_offset = output.find("classes:"); |
| 1131 | ASSERT_NE(hot_offset, std::string::npos); |
| 1132 | ASSERT_NE(startup_offset, std::string::npos); |
| 1133 | ASSERT_NE(post_startup_offset, std::string::npos); |
| 1134 | ASSERT_LT(hot_offset, startup_offset); |
| 1135 | ASSERT_LT(startup_offset, post_startup_offset); |
| 1136 | // Check the actual contents of the dump by looking at the offsets of the methods. |
| 1137 | for (uint32_t m : hot_methods) { |
| 1138 | const size_t pos = output.find(std::to_string(m) + "[],", hot_offset); |
| 1139 | ASSERT_NE(pos, std::string::npos); |
| 1140 | EXPECT_LT(pos, startup_offset); |
| 1141 | } |
| 1142 | for (uint32_t m : startup_methods) { |
| 1143 | const size_t pos = output.find(std::to_string(m) + ",", startup_offset); |
| 1144 | ASSERT_NE(pos, std::string::npos); |
| 1145 | EXPECT_LT(pos, post_startup_offset); |
| 1146 | } |
| 1147 | for (uint32_t m : post_startup_methods) { |
| 1148 | const size_t pos = output.find(std::to_string(m) + ",", post_startup_offset); |
| 1149 | ASSERT_NE(pos, std::string::npos); |
| 1150 | EXPECT_LT(pos, classes_offset); |
| 1151 | } |
| 1152 | } |
| 1153 | |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame^] | 1154 | TEST_F(ProfileAssistantTest, MergeProfilesWithFilter) { |
| 1155 | ScratchFile profile1; |
| 1156 | ScratchFile profile2; |
| 1157 | ScratchFile reference_profile; |
| 1158 | |
| 1159 | std::vector<int> profile_fds({ |
| 1160 | GetFd(profile1), |
| 1161 | GetFd(profile2)}); |
| 1162 | int reference_profile_fd = GetFd(reference_profile); |
| 1163 | |
| 1164 | // Use a real dex file to generate profile test data. |
| 1165 | // The file will be used during merging to filter unwanted data. |
| 1166 | std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("ProfileTestMultiDex"); |
| 1167 | const DexFile& d1 = *dex_files[0]; |
| 1168 | const DexFile& d2 = *dex_files[1]; |
| 1169 | // The new profile info will contain the methods with indices 0-100. |
| 1170 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 1171 | ProfileCompilationInfo info1; |
| 1172 | SetupProfile(d1.GetLocation(), d1.GetLocationChecksum(), "p1", 1, |
| 1173 | kNumberOfMethodsToEnableCompilation, 0, profile1, &info1); |
| 1174 | ProfileCompilationInfo info2; |
| 1175 | SetupProfile(d2.GetLocation(), d2.GetLocationChecksum(), "p2", 2, |
| 1176 | kNumberOfMethodsToEnableCompilation, 0, profile2, &info2); |
| 1177 | |
| 1178 | |
| 1179 | // The reference profile info will contain the methods with indices 50-150. |
| 1180 | const uint16_t kNumberOfMethodsAlreadyCompiled = 100; |
| 1181 | ProfileCompilationInfo reference_info; |
| 1182 | SetupProfile(d1.GetLocation(), d1.GetLocationChecksum(), "p1", 1, |
| 1183 | kNumberOfMethodsAlreadyCompiled, 0, reference_profile, |
| 1184 | &reference_info, kNumberOfMethodsToEnableCompilation / 2); |
| 1185 | |
| 1186 | // Run profman and pass the dex file with --apk-fd. |
| 1187 | android::base::unique_fd apk_fd( |
| 1188 | open(GetTestDexFileName("ProfileTestMultiDex").c_str(), O_RDONLY)); |
| 1189 | ASSERT_GE(apk_fd.get(), 0); |
| 1190 | |
| 1191 | std::string profman_cmd = GetProfmanCmd(); |
| 1192 | std::vector<std::string> argv_str; |
| 1193 | argv_str.push_back(profman_cmd); |
| 1194 | argv_str.push_back("--profile-file-fd=" + std::to_string(profile1.GetFd())); |
| 1195 | argv_str.push_back("--profile-file-fd=" + std::to_string(profile2.GetFd())); |
| 1196 | argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile.GetFd())); |
| 1197 | argv_str.push_back("--apk-fd=" + std::to_string(apk_fd.get())); |
| 1198 | std::string error; |
| 1199 | |
| 1200 | EXPECT_EQ(ExecAndReturnCode(argv_str, &error), 0) << error; |
| 1201 | |
| 1202 | // Verify that we can load the result. |
| 1203 | |
| 1204 | ProfileCompilationInfo result; |
| 1205 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 1206 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 1207 | |
| 1208 | |
| 1209 | ASSERT_TRUE(profile1.GetFile()->ResetOffset()); |
| 1210 | ASSERT_TRUE(profile2.GetFile()->ResetOffset()); |
| 1211 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 1212 | |
| 1213 | // Verify that the result filtered out data not belonging to the dex file. |
| 1214 | // This is equivalent to checking that the result is equal to the merging of |
| 1215 | // all profiles while filtering out data not belonging to the dex file. |
| 1216 | |
| 1217 | ProfileCompilationInfo::ProfileLoadFilterFn filter_fn = |
| 1218 | [&d1, &d2](const std::string& dex_location, uint32_t checksum) -> bool { |
| 1219 | return (dex_location == ProfileCompilationInfo::GetProfileDexFileKey(d1.GetLocation()) |
| 1220 | && checksum == d1.GetLocationChecksum()) |
| 1221 | || (dex_location == ProfileCompilationInfo::GetProfileDexFileKey(d2.GetLocation()) |
| 1222 | && checksum == d2.GetLocationChecksum()); |
| 1223 | }; |
| 1224 | |
| 1225 | ProfileCompilationInfo info1_filter; |
| 1226 | ProfileCompilationInfo info2_filter; |
| 1227 | ProfileCompilationInfo expected; |
| 1228 | |
| 1229 | info2_filter.Load(profile1.GetFd(), /*merge_classes*/ true, filter_fn); |
| 1230 | info2_filter.Load(profile2.GetFd(), /*merge_classes*/ true, filter_fn); |
| 1231 | expected.Load(reference_profile.GetFd(), /*merge_classes*/ true, filter_fn); |
| 1232 | |
| 1233 | ASSERT_TRUE(expected.MergeWith(info1_filter)); |
| 1234 | ASSERT_TRUE(expected.MergeWith(info2_filter)); |
| 1235 | |
| 1236 | ASSERT_TRUE(expected.Equals(result)); |
| 1237 | } |
| 1238 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1239 | } // namespace art |