blob: d0987388455771d2309b6eaeed410607702bc913 [file] [log] [blame]
Calin Juravle024160852016-02-23 12:00:03 +00001/*
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 Sehrc431b9d2018-03-02 12:01:51 -080019#include "base/os.h"
Calin Juravle024160852016-02-23 12:00:03 +000020#include "base/unix_file/fd_file.h"
Calin Juravle024160852016-02-23 12:00:03 +000021
22namespace art {
23
Calin Juravlee02348c2016-03-29 20:33:33 +010024// Minimum number of new methods/classes that profiles
25// must contain to enable recompilation.
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -070026static constexpr const uint32_t kMinNewMethodsForCompilation = 100;
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -070027static constexpr const uint32_t kMinNewClassesForCompilation = 50;
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -070028
Calin Juravle024160852016-02-23 12:00:03 +000029
30ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfilesInternal(
31 const std::vector<ScopedFlock>& profile_files,
Calin Juravlee10c1e22018-01-26 20:10:15 -080032 const ScopedFlock& reference_profile_file,
Calin Juravle0e82e0f2019-11-11 18:34:10 -080033 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
34 const Options& options) {
Calin Juravle024160852016-02-23 12:00:03 +000035 DCHECK(!profile_files.empty());
36
Calin Juravle0e82e0f2019-11-11 18:34:10 -080037 ProfileCompilationInfo info(options.IsBootImageMerge());
38
Calin Juravlee02348c2016-03-29 20:33:33 +010039 // Load the reference profile.
Andreas Gampe9b031f72018-10-04 11:03:34 -070040 if (!info.Load(reference_profile_file->Fd(), /*merge_classes=*/ true, filter_fn)) {
Calin Juravle024160852016-02-23 12:00:03 +000041 LOG(WARNING) << "Could not load reference profile file";
42 return kErrorBadProfiles;
43 }
44
Calin Juravle0e82e0f2019-11-11 18:34:10 -080045 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 Juravlee02348c2016-03-29 20:33:33 +010050 // 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 Markoc63d9672021-03-31 15:50:39 +010056 ProfileCompilationInfo cur_info(options.IsBootImageMerge());
Andreas Gampe9b031f72018-10-04 11:03:34 -070057 if (!cur_info.Load(profile_files[i]->Fd(), /*merge_classes=*/ true, filter_fn)) {
Calin Juravlee02348c2016-03-29 20:33:33 +010058 LOG(WARNING) << "Could not load profile file at index " << i;
Calin Juravlee571a282019-12-03 18:36:01 -080059 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 Markoc63d9672021-03-31 15:50:39 +010065 // 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 Juravlec0200a92019-11-14 09:01:11 -080068 return kErrorDifferentVersions;
69 }
Vladimir Markoc63d9672021-03-31 15:50:39 +010070 return kErrorBadProfiles;
Calin Juravle0e82e0f2019-11-11 18:34:10 -080071 }
72
Calin Juravlecea9e9d2017-03-23 19:04:59 -070073 if (!info.MergeWith(cur_info)) {
74 LOG(WARNING) << "Could not merge profile file at index " << i;
75 return kErrorBadProfiles;
76 }
Calin Juravle024160852016-02-23 12:00:03 +000077 }
Calin Juravlee02348c2016-03-29 20:33:33 +010078
Calin Juravle0e82e0f2019-11-11 18:34:10 -080079 // If we perform a forced merge do not analyze the difference between profiles.
80 if (!options.IsForceMerge()) {
Calin Juravle17374092021-06-08 13:45:09 -070081 if (info.IsEmpty()) {
82 return kSkipCompilationEmptyProfiles;
83 }
Calin Juravle0e82e0f2019-11-11 18:34:10 -080084 uint32_t min_change_in_methods_for_compilation = std::max(
yawanng7c618802020-11-05 20:02:27 +000085 (options.GetMinNewMethodsPercentChangeForCompilation() * number_of_methods) / 100,
Calin Juravle0e82e0f2019-11-11 18:34:10 -080086 kMinNewMethodsForCompilation);
87 uint32_t min_change_in_classes_for_compilation = std::max(
yawanng7c618802020-11-05 20:02:27 +000088 (options.GetMinNewClassesPercentChangeForCompilation() * number_of_classes) / 100,
Calin Juravle0e82e0f2019-11-11 18:34:10 -080089 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 Juravle17374092021-06-08 13:45:09 -070094 return kSkipCompilationSmallDelta;
Calin Juravle0e82e0f2019-11-11 18:34:10 -080095 }
Calin Juravlee02348c2016-03-29 20:33:33 +010096 }
97
Calin Juravle024160852016-02-23 12:00:03 +000098 // We were successful in merging all profile information. Update the reference profile.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010099 if (!reference_profile_file->ClearContent()) {
Calin Juravle024160852016-02-23 12:00:03 +0000100 PLOG(WARNING) << "Could not clear reference profile file";
101 return kErrorIO;
102 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100103 if (!info.Save(reference_profile_file->Fd())) {
Calin Juravle024160852016-02-23 12:00:03 +0000104 LOG(WARNING) << "Could not save reference profile file";
105 return kErrorIO;
106 }
107
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800108 return options.IsForceMerge() ? kSuccess : kCompile;
Calin Juravle024160852016-02-23 12:00:03 +0000109}
110
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100111class ScopedFlockList {
Calin Juravle024160852016-02-23 12:00:03 +0000112 public:
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100113 explicit ScopedFlockList(size_t size) : flocks_(size) {}
Calin Juravle024160852016-02-23 12:00:03 +0000114
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 Gampe9b031f72018-10-04 11:03:34 -0700118 flocks_[i] = LockedFile::Open(filenames[i].c_str(), O_RDWR, /* block= */ true, error);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100119 if (flocks_[i].get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000120 *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 Kamatha3d27eb2017-05-11 13:50:59 +0100131 flocks_[i] = LockedFile::DupOf(fds[i], "profile-file",
Andreas Gampe9b031f72018-10-04 11:03:34 -0700132 /* read_only_mode= */ true, error);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100133 if (flocks_[i].get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000134 *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
147ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
148 const std::vector<int>& profile_files_fd,
Calin Juravlee10c1e22018-01-26 20:10:15 -0800149 int reference_profile_file_fd,
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800150 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
151 const Options& options) {
Calin Juravle024160852016-02-23 12:00:03 +0000152 DCHECK_GE(reference_profile_file_fd, 0);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100153
Calin Juravle024160852016-02-23 12:00:03 +0000154 std::string error;
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100155 ScopedFlockList profile_files(profile_files_fd.size());
156 if (!profile_files.Init(profile_files_fd, &error)) {
Calin Juravle024160852016-02-23 12:00:03 +0000157 LOG(WARNING) << "Could not lock profile files: " << error;
158 return kErrorCannotLock;
159 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100160
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 Gampe9b031f72018-10-04 11:03:34 -0700165 /* read_only_mode= */ false,
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100166 &error);
167 if (reference_profile_file.get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000168 LOG(WARNING) << "Could not lock reference profiled files: " << error;
169 return kErrorCannotLock;
170 }
171
Calin Juravlee10c1e22018-01-26 20:10:15 -0800172 return ProcessProfilesInternal(profile_files.Get(),
173 reference_profile_file,
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800174 filter_fn,
175 options);
Calin Juravle024160852016-02-23 12:00:03 +0000176}
177
178ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
179 const std::vector<std::string>& profile_files,
Calin Juravlee10c1e22018-01-26 20:10:15 -0800180 const std::string& reference_profile_file,
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800181 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
182 const Options& options) {
Calin Juravle024160852016-02-23 12:00:03 +0000183 std::string error;
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100184
185 ScopedFlockList profile_files_list(profile_files.size());
186 if (!profile_files_list.Init(profile_files, &error)) {
Calin Juravle024160852016-02-23 12:00:03 +0000187 LOG(WARNING) << "Could not lock profile files: " << error;
188 return kErrorCannotLock;
189 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100190
191 ScopedFlock locked_reference_profile_file = LockedFile::Open(
Andreas Gampe9b031f72018-10-04 11:03:34 -0700192 reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100193 if (locked_reference_profile_file.get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000194 LOG(WARNING) << "Could not lock reference profile files: " << error;
195 return kErrorCannotLock;
196 }
197
Calin Juravlee10c1e22018-01-26 20:10:15 -0800198 return ProcessProfilesInternal(profile_files_list.Get(),
199 locked_reference_profile_file,
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800200 filter_fn,
201 options);
Calin Juravle024160852016-02-23 12:00:03 +0000202}
203
204} // namespace art