Calin Juravle | 02416085 | 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 "profile_assistant.h" |
| 18 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 19 | #include "base/os.h" |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 20 | #include "base/unix_file/fd_file.h" |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 21 | #include "profman/profman_result.h" |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 25 | // Minimum number of new methods/classes that profiles |
| 26 | // must contain to enable recompilation. |
Shubham Ajmera | 9ab6e1d | 2017-09-25 18:40:54 -0700 | [diff] [blame] | 27 | static constexpr const uint32_t kMinNewMethodsForCompilation = 100; |
Shubham Ajmera | 9ab6e1d | 2017-09-25 18:40:54 -0700 | [diff] [blame] | 28 | static constexpr const uint32_t kMinNewClassesForCompilation = 50; |
Shubham Ajmera | 9ab6e1d | 2017-09-25 18:40:54 -0700 | [diff] [blame] | 29 | |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 30 | ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfilesInternal( |
| 31 | const std::vector<ScopedFlock>& profile_files, |
| 32 | const ScopedFlock& reference_profile_file, |
| 33 | const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn, |
| 34 | const Options& options) { |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 35 | ProfileCompilationInfo info(options.IsBootImageMerge()); |
| 36 | |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 37 | // Load the reference profile. |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 38 | if (!info.Load(reference_profile_file->Fd(), /*merge_classes=*/ true, filter_fn)) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 39 | LOG(WARNING) << "Could not load reference profile file"; |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 40 | return ProfmanResult::kErrorBadProfiles; |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 43 | if (options.IsBootImageMerge() && !info.IsForBootImage()) { |
| 44 | LOG(WARNING) << "Requested merge for boot image profile but the reference profile is regular."; |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 45 | return ProfmanResult::kErrorBadProfiles; |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 48 | // Store the current state of the reference profile before merging with the current profiles. |
| 49 | uint32_t number_of_methods = info.GetNumberOfMethods(); |
| 50 | uint32_t number_of_classes = info.GetNumberOfResolvedClasses(); |
| 51 | |
| 52 | // Merge all current profiles. |
| 53 | for (size_t i = 0; i < profile_files.size(); i++) { |
Vladimir Marko | c63d967 | 2021-03-31 15:50:39 +0100 | [diff] [blame] | 54 | ProfileCompilationInfo cur_info(options.IsBootImageMerge()); |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 55 | if (!cur_info.Load(profile_files[i]->Fd(), /*merge_classes=*/ true, filter_fn)) { |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 56 | LOG(WARNING) << "Could not load profile file at index " << i; |
Calin Juravle | e571a28 | 2019-12-03 18:36:01 -0800 | [diff] [blame] | 57 | if (options.IsForceMerge()) { |
| 58 | // If we have to merge forcefully, ignore load failures. |
| 59 | // This is useful for boot image profiles to ignore stale profiles which are |
| 60 | // cleared lazily. |
| 61 | continue; |
| 62 | } |
Vladimir Marko | c63d967 | 2021-03-31 15:50:39 +0100 | [diff] [blame] | 63 | // TODO: Do we really need to use a different error code for version mismatch? |
| 64 | ProfileCompilationInfo wrong_info(!options.IsBootImageMerge()); |
| 65 | if (wrong_info.Load(profile_files[i]->Fd(), /*merge_classes=*/ true, filter_fn)) { |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 66 | return ProfmanResult::kErrorDifferentVersions; |
Calin Juravle | c0200a9 | 2019-11-14 09:01:11 -0800 | [diff] [blame] | 67 | } |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 68 | return ProfmanResult::kErrorBadProfiles; |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 71 | if (!info.MergeWith(cur_info)) { |
| 72 | LOG(WARNING) << "Could not merge profile file at index " << i; |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 73 | return ProfmanResult::kErrorBadProfiles; |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 74 | } |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 75 | } |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 76 | |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 77 | // If we perform a forced merge do not analyze the difference between profiles. |
| 78 | if (!options.IsForceMerge()) { |
Calin Juravle | 1737409 | 2021-06-08 13:45:09 -0700 | [diff] [blame] | 79 | if (info.IsEmpty()) { |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 80 | return ProfmanResult::kSkipCompilationEmptyProfiles; |
Calin Juravle | 1737409 | 2021-06-08 13:45:09 -0700 | [diff] [blame] | 81 | } |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 82 | uint32_t min_change_in_methods_for_compilation = std::max( |
yawanng | 7c61880 | 2020-11-05 20:02:27 +0000 | [diff] [blame] | 83 | (options.GetMinNewMethodsPercentChangeForCompilation() * number_of_methods) / 100, |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 84 | kMinNewMethodsForCompilation); |
| 85 | uint32_t min_change_in_classes_for_compilation = std::max( |
yawanng | 7c61880 | 2020-11-05 20:02:27 +0000 | [diff] [blame] | 86 | (options.GetMinNewClassesPercentChangeForCompilation() * number_of_classes) / 100, |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 87 | kMinNewClassesForCompilation); |
| 88 | // Check if there is enough new information added by the current profiles. |
| 89 | if (((info.GetNumberOfMethods() - number_of_methods) < min_change_in_methods_for_compilation) && |
| 90 | ((info.GetNumberOfResolvedClasses() - number_of_classes) |
| 91 | < min_change_in_classes_for_compilation)) { |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 92 | return ProfmanResult::kSkipCompilationSmallDelta; |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 93 | } |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 96 | // We were successful in merging all profile information. Update the reference profile. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 97 | if (!reference_profile_file->ClearContent()) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 98 | PLOG(WARNING) << "Could not clear reference profile file"; |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 99 | return ProfmanResult::kErrorIO; |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 100 | } |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 101 | if (!info.Save(reference_profile_file->Fd())) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 102 | LOG(WARNING) << "Could not save reference profile file"; |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 103 | return ProfmanResult::kErrorIO; |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 106 | return options.IsForceMerge() ? ProfmanResult::kSuccess : ProfmanResult::kCompile; |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 109 | class ScopedFlockList { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 110 | public: |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 111 | explicit ScopedFlockList(size_t size) : flocks_(size) {} |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 112 | |
| 113 | // Will block until all the locks are acquired. |
| 114 | bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) { |
| 115 | for (size_t i = 0; i < filenames.size(); i++) { |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 116 | flocks_[i] = LockedFile::Open(filenames[i].c_str(), O_RDWR, /* block= */ true, error); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 117 | if (flocks_[i].get() == nullptr) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 118 | *error += " (index=" + std::to_string(i) + ")"; |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | // Will block until all the locks are acquired. |
| 126 | bool Init(const std::vector<int>& fds, /* out */ std::string* error) { |
| 127 | for (size_t i = 0; i < fds.size(); i++) { |
| 128 | DCHECK_GE(fds[i], 0); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 129 | flocks_[i] = LockedFile::DupOf(fds[i], "profile-file", |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 130 | /* read_only_mode= */ true, error); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 131 | if (flocks_[i].get() == nullptr) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 132 | *error += " (index=" + std::to_string(i) + ")"; |
| 133 | return false; |
| 134 | } |
| 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | const std::vector<ScopedFlock>& Get() const { return flocks_; } |
| 140 | |
| 141 | private: |
| 142 | std::vector<ScopedFlock> flocks_; |
| 143 | }; |
| 144 | |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 145 | ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfiles( |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 146 | const std::vector<int>& profile_files_fd, |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 147 | int reference_profile_file_fd, |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 148 | const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn, |
| 149 | const Options& options) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 150 | DCHECK_GE(reference_profile_file_fd, 0); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 151 | |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 152 | std::string error; |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 153 | ScopedFlockList profile_files(profile_files_fd.size()); |
| 154 | if (!profile_files.Init(profile_files_fd, &error)) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 155 | LOG(WARNING) << "Could not lock profile files: " << error; |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 156 | return ProfmanResult::kErrorCannotLock; |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 157 | } |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 158 | |
| 159 | // The reference_profile_file is opened in read/write mode because it's |
| 160 | // cleared after processing. |
| 161 | ScopedFlock reference_profile_file = LockedFile::DupOf(reference_profile_file_fd, |
| 162 | "reference-profile", |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 163 | /* read_only_mode= */ false, |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 164 | &error); |
| 165 | if (reference_profile_file.get() == nullptr) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 166 | LOG(WARNING) << "Could not lock reference profiled files: " << error; |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 167 | return ProfmanResult::kErrorCannotLock; |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 170 | return ProcessProfilesInternal(profile_files.Get(), |
| 171 | reference_profile_file, |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 172 | filter_fn, |
| 173 | options); |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 176 | ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfiles( |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 177 | const std::vector<std::string>& profile_files, |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 178 | const std::string& reference_profile_file, |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 179 | const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn, |
| 180 | const Options& options) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 181 | std::string error; |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 182 | |
| 183 | ScopedFlockList profile_files_list(profile_files.size()); |
| 184 | if (!profile_files_list.Init(profile_files, &error)) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 185 | LOG(WARNING) << "Could not lock profile files: " << error; |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 186 | return ProfmanResult::kErrorCannotLock; |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 187 | } |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 188 | |
| 189 | ScopedFlock locked_reference_profile_file = LockedFile::Open( |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 190 | reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 191 | if (locked_reference_profile_file.get() == nullptr) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 192 | LOG(WARNING) << "Could not lock reference profile files: " << error; |
Jiakai Zhang | e3d589c | 2022-06-25 00:50:00 +0100 | [diff] [blame] | 193 | return ProfmanResult::kErrorCannotLock; |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 196 | return ProcessProfilesInternal(profile_files_list.Get(), |
| 197 | locked_reference_profile_file, |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 198 | filter_fn, |
| 199 | options); |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | } // namespace art |