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; |
| 27 | static constexpr const uint32_t kMinNewMethodsPercentChangeForCompilation = 2; |
| 28 | static constexpr const uint32_t kMinNewClassesForCompilation = 50; |
| 29 | static constexpr const uint32_t kMinNewClassesPercentChangeForCompilation = 2; |
| 30 | |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 31 | |
| 32 | ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfilesInternal( |
| 33 | const std::vector<ScopedFlock>& profile_files, |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 34 | const ScopedFlock& reference_profile_file, |
Calin Juravle | 3ee9cfd | 2018-12-11 13:38:35 -0800 | [diff] [blame] | 35 | const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn, |
| 36 | bool store_aggregation_counters) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 37 | DCHECK(!profile_files.empty()); |
| 38 | |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 39 | ProfileCompilationInfo info; |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 40 | // Load the reference profile. |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 41 | if (!info.Load(reference_profile_file->Fd(), /*merge_classes=*/ true, filter_fn)) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 42 | LOG(WARNING) << "Could not load reference profile file"; |
| 43 | return kErrorBadProfiles; |
| 44 | } |
| 45 | |
Calin Juravle | 3ee9cfd | 2018-12-11 13:38:35 -0800 | [diff] [blame] | 46 | // If we need to store aggregation counters (e.g. for the boot image profile), |
| 47 | // prepare the reference profile now. |
| 48 | if (store_aggregation_counters) { |
| 49 | info.PrepareForAggregationCounters(); |
| 50 | } |
| 51 | |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 52 | // Store the current state of the reference profile before merging with the current profiles. |
| 53 | uint32_t number_of_methods = info.GetNumberOfMethods(); |
| 54 | uint32_t number_of_classes = info.GetNumberOfResolvedClasses(); |
| 55 | |
| 56 | // Merge all current profiles. |
| 57 | for (size_t i = 0; i < profile_files.size(); i++) { |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 58 | ProfileCompilationInfo cur_info; |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 59 | 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] | 60 | LOG(WARNING) << "Could not load profile file at index " << i; |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 61 | return kErrorBadProfiles; |
| 62 | } |
Calin Juravle | cea9e9d | 2017-03-23 19:04:59 -0700 | [diff] [blame] | 63 | if (!info.MergeWith(cur_info)) { |
| 64 | LOG(WARNING) << "Could not merge profile file at index " << i; |
| 65 | return kErrorBadProfiles; |
| 66 | } |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 67 | } |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 68 | |
Shubham Ajmera | 9ab6e1d | 2017-09-25 18:40:54 -0700 | [diff] [blame] | 69 | uint32_t min_change_in_methods_for_compilation = std::max( |
| 70 | (kMinNewMethodsPercentChangeForCompilation * number_of_methods) / 100, |
| 71 | kMinNewMethodsForCompilation); |
| 72 | uint32_t min_change_in_classes_for_compilation = std::max( |
| 73 | (kMinNewClassesPercentChangeForCompilation * number_of_classes) / 100, |
| 74 | kMinNewClassesForCompilation); |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 75 | // Check if there is enough new information added by the current profiles. |
Shubham Ajmera | 9ab6e1d | 2017-09-25 18:40:54 -0700 | [diff] [blame] | 76 | if (((info.GetNumberOfMethods() - number_of_methods) < min_change_in_methods_for_compilation) && |
| 77 | ((info.GetNumberOfResolvedClasses() - number_of_classes) |
| 78 | < min_change_in_classes_for_compilation)) { |
Calin Juravle | e02348c | 2016-03-29 20:33:33 +0100 | [diff] [blame] | 79 | return kSkipCompilation; |
| 80 | } |
| 81 | |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 82 | // We were successful in merging all profile information. Update the reference profile. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 83 | if (!reference_profile_file->ClearContent()) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 84 | PLOG(WARNING) << "Could not clear reference profile file"; |
| 85 | return kErrorIO; |
| 86 | } |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 87 | if (!info.Save(reference_profile_file->Fd())) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 88 | LOG(WARNING) << "Could not save reference profile file"; |
| 89 | return kErrorIO; |
| 90 | } |
| 91 | |
| 92 | return kCompile; |
| 93 | } |
| 94 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 95 | class ScopedFlockList { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 96 | public: |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 97 | explicit ScopedFlockList(size_t size) : flocks_(size) {} |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 98 | |
| 99 | // Will block until all the locks are acquired. |
| 100 | bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) { |
| 101 | for (size_t i = 0; i < filenames.size(); i++) { |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 102 | 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] | 103 | if (flocks_[i].get() == nullptr) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 104 | *error += " (index=" + std::to_string(i) + ")"; |
| 105 | return false; |
| 106 | } |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | // Will block until all the locks are acquired. |
| 112 | bool Init(const std::vector<int>& fds, /* out */ std::string* error) { |
| 113 | for (size_t i = 0; i < fds.size(); i++) { |
| 114 | DCHECK_GE(fds[i], 0); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 115 | flocks_[i] = LockedFile::DupOf(fds[i], "profile-file", |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 116 | /* read_only_mode= */ 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 | const std::vector<ScopedFlock>& Get() const { return flocks_; } |
| 126 | |
| 127 | private: |
| 128 | std::vector<ScopedFlock> flocks_; |
| 129 | }; |
| 130 | |
| 131 | ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles( |
| 132 | const std::vector<int>& profile_files_fd, |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 133 | int reference_profile_file_fd, |
Calin Juravle | 3ee9cfd | 2018-12-11 13:38:35 -0800 | [diff] [blame] | 134 | const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn, |
| 135 | bool store_aggregation_counters) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 136 | DCHECK_GE(reference_profile_file_fd, 0); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 137 | |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 138 | std::string error; |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 139 | ScopedFlockList profile_files(profile_files_fd.size()); |
| 140 | if (!profile_files.Init(profile_files_fd, &error)) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 141 | LOG(WARNING) << "Could not lock profile files: " << error; |
| 142 | return kErrorCannotLock; |
| 143 | } |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 144 | |
| 145 | // The reference_profile_file is opened in read/write mode because it's |
| 146 | // cleared after processing. |
| 147 | ScopedFlock reference_profile_file = LockedFile::DupOf(reference_profile_file_fd, |
| 148 | "reference-profile", |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 149 | /* read_only_mode= */ false, |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 150 | &error); |
| 151 | if (reference_profile_file.get() == nullptr) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 152 | LOG(WARNING) << "Could not lock reference profiled files: " << error; |
| 153 | return kErrorCannotLock; |
| 154 | } |
| 155 | |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 156 | return ProcessProfilesInternal(profile_files.Get(), |
| 157 | reference_profile_file, |
Calin Juravle | 3ee9cfd | 2018-12-11 13:38:35 -0800 | [diff] [blame] | 158 | filter_fn, |
| 159 | store_aggregation_counters); |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles( |
| 163 | const std::vector<std::string>& profile_files, |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 164 | const std::string& reference_profile_file, |
Calin Juravle | 3ee9cfd | 2018-12-11 13:38:35 -0800 | [diff] [blame] | 165 | const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn, |
| 166 | bool store_aggregation_counters) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 167 | std::string error; |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 168 | |
| 169 | ScopedFlockList profile_files_list(profile_files.size()); |
| 170 | if (!profile_files_list.Init(profile_files, &error)) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 171 | LOG(WARNING) << "Could not lock profile files: " << error; |
| 172 | return kErrorCannotLock; |
| 173 | } |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 174 | |
| 175 | ScopedFlock locked_reference_profile_file = LockedFile::Open( |
Andreas Gampe | 9b031f7 | 2018-10-04 11:03:34 -0700 | [diff] [blame] | 176 | reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 177 | if (locked_reference_profile_file.get() == nullptr) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 178 | LOG(WARNING) << "Could not lock reference profile files: " << error; |
| 179 | return kErrorCannotLock; |
| 180 | } |
| 181 | |
Calin Juravle | e10c1e2 | 2018-01-26 20:10:15 -0800 | [diff] [blame] | 182 | return ProcessProfilesInternal(profile_files_list.Get(), |
| 183 | locked_reference_profile_file, |
Calin Juravle | 3ee9cfd | 2018-12-11 13:38:35 -0800 | [diff] [blame] | 184 | filter_fn, |
| 185 | store_aggregation_counters); |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } // namespace art |