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++) { |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 56 | ProfileCompilationInfo cur_info; |
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 | } |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 65 | return kErrorBadProfiles; |
| 66 | } |
Calin Juravle | c0200a9 | 2019-11-14 09:01:11 -0800 | [diff] [blame] | 67 | |
| 68 | // Check version mismatch. |
| 69 | // This may happen during profile analysis if one profile is regular and |
| 70 | // the other one is for the boot image. For example when switching on-off |
| 71 | // the boot image profiles. |
| 72 | if (!info.SameVersion(cur_info)) { |
| 73 | if (options.IsForceMerge()) { |
| 74 | // If we have to merge forcefully, ignore the current profile and |
| 75 | // continue to the next one. |
| 76 | continue; |
| 77 | } else { |
| 78 | // Otherwise, return an error. |
| 79 | return kErrorDifferentVersions; |
| 80 | } |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 83 | if (!info.MergeWith(cur_info)) { |
| 84 | LOG(WARNING) << "Could not merge profile file at index " << i; |
| 85 | return kErrorBadProfiles; |
| 86 | } |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 87 | } |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 88 | |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 89 | // If we perform a forced merge do not analyze the difference between profiles. |
| 90 | if (!options.IsForceMerge()) { |
| 91 | uint32_t min_change_in_methods_for_compilation = std::max( |
yawanng | 7c61880 | 2020-11-05 20:02:27 +0000 | [diff] [blame] | 92 | (options.GetMinNewMethodsPercentChangeForCompilation() * number_of_methods) / 100, |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 93 | kMinNewMethodsForCompilation); |
| 94 | uint32_t min_change_in_classes_for_compilation = std::max( |
yawanng | 7c61880 | 2020-11-05 20:02:27 +0000 | [diff] [blame] | 95 | (options.GetMinNewClassesPercentChangeForCompilation() * number_of_classes) / 100, |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 96 | kMinNewClassesForCompilation); |
| 97 | // Check if there is enough new information added by the current profiles. |
| 98 | if (((info.GetNumberOfMethods() - number_of_methods) < min_change_in_methods_for_compilation) && |
| 99 | ((info.GetNumberOfResolvedClasses() - number_of_classes) |
| 100 | < min_change_in_classes_for_compilation)) { |
| 101 | return kSkipCompilation; |
| 102 | } |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 105 | // We were successful in merging all profile information. Update the reference profile. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 106 | if (!reference_profile_file->ClearContent()) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 107 | PLOG(WARNING) << "Could not clear reference profile file"; |
| 108 | return kErrorIO; |
| 109 | } |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 110 | if (!info.Save(reference_profile_file->Fd())) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 111 | LOG(WARNING) << "Could not save reference profile file"; |
| 112 | return kErrorIO; |
| 113 | } |
| 114 | |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 115 | return options.IsForceMerge() ? kSuccess : kCompile; |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 118 | class ScopedFlockList { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 119 | public: |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 120 | explicit ScopedFlockList(size_t size) : flocks_(size) {} |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 121 | |
| 122 | // Will block until all the locks are acquired. |
| 123 | bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) { |
| 124 | for (size_t i = 0; i < filenames.size(); i++) { |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 125 | 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] | 126 | if (flocks_[i].get() == nullptr) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 127 | *error += " (index=" + std::to_string(i) + ")"; |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | // Will block until all the locks are acquired. |
| 135 | bool Init(const std::vector<int>& fds, /* out */ std::string* error) { |
| 136 | for (size_t i = 0; i < fds.size(); i++) { |
| 137 | DCHECK_GE(fds[i], 0); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 138 | flocks_[i] = LockedFile::DupOf(fds[i], "profile-file", |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 139 | /* read_only_mode= */ true, error); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 140 | if (flocks_[i].get() == nullptr) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 141 | *error += " (index=" + std::to_string(i) + ")"; |
| 142 | return false; |
| 143 | } |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | const std::vector<ScopedFlock>& Get() const { return flocks_; } |
| 149 | |
| 150 | private: |
| 151 | std::vector<ScopedFlock> flocks_; |
| 152 | }; |
| 153 | |
| 154 | ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles( |
| 155 | const std::vector<int>& profile_files_fd, |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 156 | int reference_profile_file_fd, |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 157 | const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn, |
| 158 | const Options& options) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 159 | DCHECK_GE(reference_profile_file_fd, 0); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 160 | |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 161 | std::string error; |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 162 | ScopedFlockList profile_files(profile_files_fd.size()); |
| 163 | if (!profile_files.Init(profile_files_fd, &error)) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 164 | LOG(WARNING) << "Could not lock profile files: " << error; |
| 165 | return kErrorCannotLock; |
| 166 | } |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 167 | |
| 168 | // The reference_profile_file is opened in read/write mode because it's |
| 169 | // cleared after processing. |
| 170 | ScopedFlock reference_profile_file = LockedFile::DupOf(reference_profile_file_fd, |
| 171 | "reference-profile", |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 172 | /* read_only_mode= */ false, |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 173 | &error); |
| 174 | if (reference_profile_file.get() == nullptr) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 175 | LOG(WARNING) << "Could not lock reference profiled files: " << error; |
| 176 | return kErrorCannotLock; |
| 177 | } |
| 178 | |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 179 | return ProcessProfilesInternal(profile_files.Get(), |
| 180 | reference_profile_file, |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 181 | filter_fn, |
| 182 | options); |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles( |
| 186 | const std::vector<std::string>& profile_files, |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 187 | const std::string& reference_profile_file, |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 188 | const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn, |
| 189 | const Options& options) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 190 | std::string error; |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 191 | |
| 192 | ScopedFlockList profile_files_list(profile_files.size()); |
| 193 | if (!profile_files_list.Init(profile_files, &error)) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 194 | LOG(WARNING) << "Could not lock profile files: " << error; |
| 195 | return kErrorCannotLock; |
| 196 | } |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 197 | |
| 198 | ScopedFlock locked_reference_profile_file = LockedFile::Open( |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 199 | reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 200 | if (locked_reference_profile_file.get() == nullptr) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 201 | LOG(WARNING) << "Could not lock reference profile files: " << error; |
| 202 | return kErrorCannotLock; |
| 203 | } |
| 204 | |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 205 | return ProcessProfilesInternal(profile_files_list.Get(), |
| 206 | locked_reference_profile_file, |
Calin Juravle | 0e82e0f | 2019-11-11 18:34:10 -0800 | [diff] [blame] | 207 | filter_fn, |
| 208 | options); |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | } // namespace art |