blob: ba5be4dff4ce820bdaa4d53c3d9622416e98ba90 [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++) {
Calin Juravlecea9e9d2017-03-23 19:04:59 -070056 ProfileCompilationInfo cur_info;
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 }
Calin Juravle024160852016-02-23 12:00:03 +000065 return kErrorBadProfiles;
66 }
Calin Juravlec0200a92019-11-14 09:01:11 -080067
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 Juravle0e82e0f2019-11-11 18:34:10 -080081 }
82
Calin Juravlecea9e9d2017-03-23 19:04:59 -070083 if (!info.MergeWith(cur_info)) {
84 LOG(WARNING) << "Could not merge profile file at index " << i;
85 return kErrorBadProfiles;
86 }
Calin Juravle024160852016-02-23 12:00:03 +000087 }
Calin Juravlee02348c2016-03-29 20:33:33 +010088
Calin Juravle0e82e0f2019-11-11 18:34:10 -080089 // 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(
yawanng7c618802020-11-05 20:02:27 +000092 (options.GetMinNewMethodsPercentChangeForCompilation() * number_of_methods) / 100,
Calin Juravle0e82e0f2019-11-11 18:34:10 -080093 kMinNewMethodsForCompilation);
94 uint32_t min_change_in_classes_for_compilation = std::max(
yawanng7c618802020-11-05 20:02:27 +000095 (options.GetMinNewClassesPercentChangeForCompilation() * number_of_classes) / 100,
Calin Juravle0e82e0f2019-11-11 18:34:10 -080096 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 Juravlee02348c2016-03-29 20:33:33 +0100103 }
104
Calin Juravle024160852016-02-23 12:00:03 +0000105 // We were successful in merging all profile information. Update the reference profile.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100106 if (!reference_profile_file->ClearContent()) {
Calin Juravle024160852016-02-23 12:00:03 +0000107 PLOG(WARNING) << "Could not clear reference profile file";
108 return kErrorIO;
109 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100110 if (!info.Save(reference_profile_file->Fd())) {
Calin Juravle024160852016-02-23 12:00:03 +0000111 LOG(WARNING) << "Could not save reference profile file";
112 return kErrorIO;
113 }
114
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800115 return options.IsForceMerge() ? kSuccess : kCompile;
Calin Juravle024160852016-02-23 12:00:03 +0000116}
117
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100118class ScopedFlockList {
Calin Juravle024160852016-02-23 12:00:03 +0000119 public:
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100120 explicit ScopedFlockList(size_t size) : flocks_(size) {}
Calin Juravle024160852016-02-23 12:00:03 +0000121
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 Gampe9b031f72018-10-04 11:03:34 -0700125 flocks_[i] = LockedFile::Open(filenames[i].c_str(), O_RDWR, /* block= */ true, error);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100126 if (flocks_[i].get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000127 *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 Kamatha3d27eb2017-05-11 13:50:59 +0100138 flocks_[i] = LockedFile::DupOf(fds[i], "profile-file",
Andreas Gampe9b031f72018-10-04 11:03:34 -0700139 /* read_only_mode= */ true, error);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100140 if (flocks_[i].get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000141 *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
154ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
155 const std::vector<int>& profile_files_fd,
Calin Juravlee10c1e22018-01-26 20:10:15 -0800156 int reference_profile_file_fd,
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800157 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
158 const Options& options) {
Calin Juravle024160852016-02-23 12:00:03 +0000159 DCHECK_GE(reference_profile_file_fd, 0);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100160
Calin Juravle024160852016-02-23 12:00:03 +0000161 std::string error;
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100162 ScopedFlockList profile_files(profile_files_fd.size());
163 if (!profile_files.Init(profile_files_fd, &error)) {
Calin Juravle024160852016-02-23 12:00:03 +0000164 LOG(WARNING) << "Could not lock profile files: " << error;
165 return kErrorCannotLock;
166 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100167
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 Gampe9b031f72018-10-04 11:03:34 -0700172 /* read_only_mode= */ false,
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100173 &error);
174 if (reference_profile_file.get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000175 LOG(WARNING) << "Could not lock reference profiled files: " << error;
176 return kErrorCannotLock;
177 }
178
Calin Juravlee10c1e22018-01-26 20:10:15 -0800179 return ProcessProfilesInternal(profile_files.Get(),
180 reference_profile_file,
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800181 filter_fn,
182 options);
Calin Juravle024160852016-02-23 12:00:03 +0000183}
184
185ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
186 const std::vector<std::string>& profile_files,
Calin Juravlee10c1e22018-01-26 20:10:15 -0800187 const std::string& reference_profile_file,
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800188 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
189 const Options& options) {
Calin Juravle024160852016-02-23 12:00:03 +0000190 std::string error;
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100191
192 ScopedFlockList profile_files_list(profile_files.size());
193 if (!profile_files_list.Init(profile_files, &error)) {
Calin Juravle024160852016-02-23 12:00:03 +0000194 LOG(WARNING) << "Could not lock profile files: " << error;
195 return kErrorCannotLock;
196 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100197
198 ScopedFlock locked_reference_profile_file = LockedFile::Open(
Andreas Gampe9b031f72018-10-04 11:03:34 -0700199 reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100200 if (locked_reference_profile_file.get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000201 LOG(WARNING) << "Could not lock reference profile files: " << error;
202 return kErrorCannotLock;
203 }
204
Calin Juravlee10c1e22018-01-26 20:10:15 -0800205 return ProcessProfilesInternal(profile_files_list.Get(),
206 locked_reference_profile_file,
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800207 filter_fn,
208 options);
Calin Juravle024160852016-02-23 12:00:03 +0000209}
210
211} // namespace art