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