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 | |
| 19 | #include "base/unix_file/fd_file.h" |
| 20 | #include "common_runtime_test.h" |
| 21 | #include "profile_assistant.h" |
| 22 | #include "jit/offline_profiling_info.h" |
| 23 | #include "utils.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class ProfileAssistantTest : public CommonRuntimeTest { |
| 28 | protected: |
| 29 | void SetupProfile(const std::string& id, |
| 30 | uint32_t checksum, |
| 31 | uint16_t number_of_methods, |
| 32 | const ScratchFile& profile, |
| 33 | ProfileCompilationInfo* info, |
| 34 | uint16_t start_method_index = 0) { |
| 35 | std::string dex_location1 = "location1" + id; |
| 36 | uint32_t dex_location_checksum1 = checksum; |
| 37 | std::string dex_location2 = "location2" + id; |
| 38 | uint32_t dex_location_checksum2 = 10 * checksum; |
| 39 | for (uint16_t i = start_method_index; i < start_method_index + number_of_methods; i++) { |
| 40 | ASSERT_TRUE(info->AddData(dex_location1, dex_location_checksum1, i)); |
| 41 | ASSERT_TRUE(info->AddData(dex_location2, dex_location_checksum2, i)); |
| 42 | } |
| 43 | ASSERT_TRUE(info->Save(GetFd(profile))); |
| 44 | ASSERT_EQ(0, profile.GetFile()->Flush()); |
| 45 | ASSERT_TRUE(profile.GetFile()->ResetOffset()); |
| 46 | } |
| 47 | |
| 48 | int GetFd(const ScratchFile& file) const { |
| 49 | return static_cast<int>(file.GetFd()); |
| 50 | } |
| 51 | |
| 52 | void CheckProfileInfo(ScratchFile& file, const ProfileCompilationInfo& info) { |
| 53 | ProfileCompilationInfo file_info; |
| 54 | ASSERT_TRUE(file.GetFile()->ResetOffset()); |
| 55 | ASSERT_TRUE(file_info.Load(GetFd(file))); |
| 56 | ASSERT_TRUE(file_info.Equals(info)); |
| 57 | } |
| 58 | |
| 59 | // Runs test with given arguments. |
| 60 | int ProcessProfiles(const std::vector<int>& profiles_fd, int reference_profile_fd) { |
| 61 | std::string file_path = GetTestAndroidRoot(); |
Calin Juravle | de4fb63 | 2016-02-23 16:53:30 +0000 | [diff] [blame^] | 62 | file_path += "/bin/profman"; |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 63 | if (kIsDebugBuild) { |
| 64 | file_path += "d"; |
| 65 | } |
| 66 | |
| 67 | EXPECT_TRUE(OS::FileExists(file_path.c_str())) << file_path << " should be a valid file path"; |
| 68 | std::vector<std::string> argv_str; |
| 69 | argv_str.push_back(file_path); |
| 70 | for (size_t k = 0; k < profiles_fd.size(); k++) { |
| 71 | argv_str.push_back("--profile-file-fd=" + std::to_string(profiles_fd[k])); |
| 72 | } |
| 73 | argv_str.push_back("--reference-profile-file-fd=" + std::to_string(reference_profile_fd)); |
| 74 | |
| 75 | std::string error; |
| 76 | return ExecAndReturnCode(argv_str, &error); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | TEST_F(ProfileAssistantTest, AdviseCompilationEmptyReferences) { |
| 81 | ScratchFile profile1; |
| 82 | ScratchFile profile2; |
| 83 | ScratchFile reference_profile; |
| 84 | |
| 85 | std::vector<int> profile_fds({ |
| 86 | GetFd(profile1), |
| 87 | GetFd(profile2)}); |
| 88 | int reference_profile_fd = GetFd(reference_profile); |
| 89 | |
| 90 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 91 | ProfileCompilationInfo info1; |
| 92 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, profile1, &info1); |
| 93 | ProfileCompilationInfo info2; |
| 94 | SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, profile2, &info2); |
| 95 | |
| 96 | // We should advise compilation. |
| 97 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 98 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 99 | // The resulting compilation info must be equal to the merge of the inputs. |
| 100 | ProfileCompilationInfo result; |
| 101 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 102 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 103 | |
| 104 | ProfileCompilationInfo expected; |
| 105 | ASSERT_TRUE(expected.Load(info1)); |
| 106 | ASSERT_TRUE(expected.Load(info2)); |
| 107 | ASSERT_TRUE(expected.Equals(result)); |
| 108 | |
| 109 | // The information from profiles must remain the same. |
| 110 | CheckProfileInfo(profile1, info1); |
| 111 | CheckProfileInfo(profile2, info2); |
| 112 | } |
| 113 | |
| 114 | TEST_F(ProfileAssistantTest, AdviseCompilationNonEmptyReferences) { |
| 115 | ScratchFile profile1; |
| 116 | ScratchFile profile2; |
| 117 | ScratchFile reference_profile; |
| 118 | |
| 119 | std::vector<int> profile_fds({ |
| 120 | GetFd(profile1), |
| 121 | GetFd(profile2)}); |
| 122 | int reference_profile_fd = GetFd(reference_profile); |
| 123 | |
| 124 | // The new profile info will contain the methods with indices 0-100. |
| 125 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 126 | ProfileCompilationInfo info1; |
| 127 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, profile1, &info1); |
| 128 | ProfileCompilationInfo info2; |
| 129 | SetupProfile("p2", 2, kNumberOfMethodsToEnableCompilation, profile2, &info2); |
| 130 | |
| 131 | |
| 132 | // The reference profile info will contain the methods with indices 50-150. |
| 133 | const uint16_t kNumberOfMethodsAlreadyCompiled = 100; |
| 134 | ProfileCompilationInfo reference_info; |
| 135 | SetupProfile("p1", 1, kNumberOfMethodsAlreadyCompiled, reference_profile, |
| 136 | &reference_info, kNumberOfMethodsToEnableCompilation / 2); |
| 137 | |
| 138 | // We should advise compilation. |
| 139 | ASSERT_EQ(ProfileAssistant::kCompile, |
| 140 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 141 | |
| 142 | // The resulting compilation info must be equal to the merge of the inputs |
| 143 | ProfileCompilationInfo result; |
| 144 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 145 | ASSERT_TRUE(result.Load(reference_profile_fd)); |
| 146 | |
| 147 | ProfileCompilationInfo expected; |
| 148 | ASSERT_TRUE(expected.Load(info1)); |
| 149 | ASSERT_TRUE(expected.Load(info2)); |
| 150 | ASSERT_TRUE(expected.Load(reference_info)); |
| 151 | ASSERT_TRUE(expected.Equals(result)); |
| 152 | |
| 153 | // The information from profiles must remain the same. |
| 154 | CheckProfileInfo(profile1, info1); |
| 155 | CheckProfileInfo(profile2, info2); |
| 156 | } |
| 157 | |
| 158 | TEST_F(ProfileAssistantTest, DoNotAdviseCompilation) { |
| 159 | ScratchFile profile1; |
| 160 | ScratchFile profile2; |
| 161 | ScratchFile reference_profile; |
| 162 | |
| 163 | std::vector<int> profile_fds({ |
| 164 | GetFd(profile1), |
| 165 | GetFd(profile2)}); |
| 166 | int reference_profile_fd = GetFd(reference_profile); |
| 167 | |
| 168 | const uint16_t kNumberOfMethodsToSkipCompilation = 1; |
| 169 | ProfileCompilationInfo info1; |
| 170 | SetupProfile("p1", 1, kNumberOfMethodsToSkipCompilation, profile1, &info1); |
| 171 | ProfileCompilationInfo info2; |
| 172 | SetupProfile("p2", 2, kNumberOfMethodsToSkipCompilation, profile2, &info2); |
| 173 | |
| 174 | // We should not advise compilation. |
| 175 | ASSERT_EQ(ProfileAssistant::kSkipCompilation, |
| 176 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 177 | |
| 178 | // The information from profiles must remain the same. |
| 179 | ProfileCompilationInfo file_info1; |
| 180 | ASSERT_TRUE(profile1.GetFile()->ResetOffset()); |
| 181 | ASSERT_TRUE(file_info1.Load(GetFd(profile1))); |
| 182 | ASSERT_TRUE(file_info1.Equals(info1)); |
| 183 | |
| 184 | ProfileCompilationInfo file_info2; |
| 185 | ASSERT_TRUE(profile2.GetFile()->ResetOffset()); |
| 186 | ASSERT_TRUE(file_info2.Load(GetFd(profile2))); |
| 187 | ASSERT_TRUE(file_info2.Equals(info2)); |
| 188 | |
| 189 | // Reference profile files must remain empty. |
| 190 | ASSERT_EQ(0, reference_profile.GetFile()->GetLength()); |
| 191 | |
| 192 | // The information from profiles must remain the same. |
| 193 | CheckProfileInfo(profile1, info1); |
| 194 | CheckProfileInfo(profile2, info2); |
| 195 | } |
| 196 | |
| 197 | TEST_F(ProfileAssistantTest, FailProcessingBecauseOfProfiles) { |
| 198 | ScratchFile profile1; |
| 199 | ScratchFile profile2; |
| 200 | ScratchFile reference_profile; |
| 201 | |
| 202 | std::vector<int> profile_fds({ |
| 203 | GetFd(profile1), |
| 204 | GetFd(profile2)}); |
| 205 | int reference_profile_fd = GetFd(reference_profile); |
| 206 | |
| 207 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 208 | // Assign different hashes for the same dex file. This will make merging of information to fail. |
| 209 | ProfileCompilationInfo info1; |
| 210 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, profile1, &info1); |
| 211 | ProfileCompilationInfo info2; |
| 212 | SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, profile2, &info2); |
| 213 | |
| 214 | // We should fail processing. |
| 215 | ASSERT_EQ(ProfileAssistant::kErrorBadProfiles, |
| 216 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 217 | |
| 218 | // The information from profiles must remain the same. |
| 219 | CheckProfileInfo(profile1, info1); |
| 220 | CheckProfileInfo(profile2, info2); |
| 221 | |
| 222 | // Reference profile files must still remain empty. |
| 223 | ASSERT_EQ(0, reference_profile.GetFile()->GetLength()); |
| 224 | } |
| 225 | |
| 226 | TEST_F(ProfileAssistantTest, FailProcessingBecauseOfReferenceProfiles) { |
| 227 | ScratchFile profile1; |
| 228 | ScratchFile reference_profile; |
| 229 | |
| 230 | std::vector<int> profile_fds({ |
| 231 | GetFd(profile1)}); |
| 232 | int reference_profile_fd = GetFd(reference_profile); |
| 233 | |
| 234 | const uint16_t kNumberOfMethodsToEnableCompilation = 100; |
| 235 | // Assign different hashes for the same dex file. This will make merging of information to fail. |
| 236 | ProfileCompilationInfo info1; |
| 237 | SetupProfile("p1", 1, kNumberOfMethodsToEnableCompilation, profile1, &info1); |
| 238 | ProfileCompilationInfo reference_info; |
| 239 | SetupProfile("p1", 2, kNumberOfMethodsToEnableCompilation, reference_profile, &reference_info); |
| 240 | |
| 241 | // We should not advise compilation. |
| 242 | ASSERT_TRUE(profile1.GetFile()->ResetOffset()); |
| 243 | ASSERT_TRUE(reference_profile.GetFile()->ResetOffset()); |
| 244 | ASSERT_EQ(ProfileAssistant::kErrorBadProfiles, |
| 245 | ProcessProfiles(profile_fds, reference_profile_fd)); |
| 246 | |
| 247 | // The information from profiles must remain the same. |
| 248 | CheckProfileInfo(profile1, info1); |
| 249 | } |
| 250 | |
| 251 | } // namespace art |