blob: abbde2d5277aab30c67a2ba81ba8fa269f53ceec [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"
Jiakai Zhange3d589c2022-06-25 00:50:00 +010021#include "profman/profman_result.h"
Calin Juravle024160852016-02-23 12:00:03 +000022
23namespace art {
24
Calin Juravlee02348c2016-03-29 20:33:33 +010025// Minimum number of new methods/classes that profiles
26// must contain to enable recompilation.
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -070027static constexpr const uint32_t kMinNewMethodsForCompilation = 100;
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -070028static constexpr const uint32_t kMinNewClassesForCompilation = 50;
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -070029
Jiakai Zhange3d589c2022-06-25 00:50:00 +010030ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfilesInternal(
31 const std::vector<ScopedFlock>& profile_files,
32 const ScopedFlock& reference_profile_file,
33 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
34 const Options& options) {
Calin Juravle0e82e0f2019-11-11 18:34:10 -080035 ProfileCompilationInfo info(options.IsBootImageMerge());
36
Calin Juravlee02348c2016-03-29 20:33:33 +010037 // Load the reference profile.
Andreas Gampe9b031f72018-10-04 11:03:34 -070038 if (!info.Load(reference_profile_file->Fd(), /*merge_classes=*/ true, filter_fn)) {
Calin Juravle024160852016-02-23 12:00:03 +000039 LOG(WARNING) << "Could not load reference profile file";
Jiakai Zhange3d589c2022-06-25 00:50:00 +010040 return ProfmanResult::kErrorBadProfiles;
Calin Juravle024160852016-02-23 12:00:03 +000041 }
42
Calin Juravle0e82e0f2019-11-11 18:34:10 -080043 if (options.IsBootImageMerge() && !info.IsForBootImage()) {
44 LOG(WARNING) << "Requested merge for boot image profile but the reference profile is regular.";
Jiakai Zhange3d589c2022-06-25 00:50:00 +010045 return ProfmanResult::kErrorBadProfiles;
Calin Juravle0e82e0f2019-11-11 18:34:10 -080046 }
47
Calin Juravlee02348c2016-03-29 20:33:33 +010048 // Store the current state of the reference profile before merging with the current profiles.
49 uint32_t number_of_methods = info.GetNumberOfMethods();
50 uint32_t number_of_classes = info.GetNumberOfResolvedClasses();
51
52 // Merge all current profiles.
53 for (size_t i = 0; i < profile_files.size(); i++) {
Vladimir Markoc63d9672021-03-31 15:50:39 +010054 ProfileCompilationInfo cur_info(options.IsBootImageMerge());
Andreas Gampe9b031f72018-10-04 11:03:34 -070055 if (!cur_info.Load(profile_files[i]->Fd(), /*merge_classes=*/ true, filter_fn)) {
Calin Juravlee02348c2016-03-29 20:33:33 +010056 LOG(WARNING) << "Could not load profile file at index " << i;
Calin Juravlee571a282019-12-03 18:36:01 -080057 if (options.IsForceMerge()) {
58 // If we have to merge forcefully, ignore load failures.
59 // This is useful for boot image profiles to ignore stale profiles which are
60 // cleared lazily.
61 continue;
62 }
Vladimir Markoc63d9672021-03-31 15:50:39 +010063 // TODO: Do we really need to use a different error code for version mismatch?
64 ProfileCompilationInfo wrong_info(!options.IsBootImageMerge());
65 if (wrong_info.Load(profile_files[i]->Fd(), /*merge_classes=*/ true, filter_fn)) {
Jiakai Zhange3d589c2022-06-25 00:50:00 +010066 return ProfmanResult::kErrorDifferentVersions;
Calin Juravlec0200a92019-11-14 09:01:11 -080067 }
Jiakai Zhange3d589c2022-06-25 00:50:00 +010068 return ProfmanResult::kErrorBadProfiles;
Calin Juravle0e82e0f2019-11-11 18:34:10 -080069 }
70
Calin Juravlecea9e9d2017-03-23 19:04:59 -070071 if (!info.MergeWith(cur_info)) {
72 LOG(WARNING) << "Could not merge profile file at index " << i;
Jiakai Zhange3d589c2022-06-25 00:50:00 +010073 return ProfmanResult::kErrorBadProfiles;
Calin Juravlecea9e9d2017-03-23 19:04:59 -070074 }
Calin Juravle024160852016-02-23 12:00:03 +000075 }
Calin Juravlee02348c2016-03-29 20:33:33 +010076
Calin Juravle0e82e0f2019-11-11 18:34:10 -080077 // If we perform a forced merge do not analyze the difference between profiles.
78 if (!options.IsForceMerge()) {
Calin Juravle17374092021-06-08 13:45:09 -070079 if (info.IsEmpty()) {
Jiakai Zhange3d589c2022-06-25 00:50:00 +010080 return ProfmanResult::kSkipCompilationEmptyProfiles;
Calin Juravle17374092021-06-08 13:45:09 -070081 }
Calin Juravle0e82e0f2019-11-11 18:34:10 -080082 uint32_t min_change_in_methods_for_compilation = std::max(
yawanng7c618802020-11-05 20:02:27 +000083 (options.GetMinNewMethodsPercentChangeForCompilation() * number_of_methods) / 100,
Calin Juravle0e82e0f2019-11-11 18:34:10 -080084 kMinNewMethodsForCompilation);
85 uint32_t min_change_in_classes_for_compilation = std::max(
yawanng7c618802020-11-05 20:02:27 +000086 (options.GetMinNewClassesPercentChangeForCompilation() * number_of_classes) / 100,
Calin Juravle0e82e0f2019-11-11 18:34:10 -080087 kMinNewClassesForCompilation);
88 // Check if there is enough new information added by the current profiles.
89 if (((info.GetNumberOfMethods() - number_of_methods) < min_change_in_methods_for_compilation) &&
90 ((info.GetNumberOfResolvedClasses() - number_of_classes)
91 < min_change_in_classes_for_compilation)) {
Jiakai Zhange3d589c2022-06-25 00:50:00 +010092 return ProfmanResult::kSkipCompilationSmallDelta;
Calin Juravle0e82e0f2019-11-11 18:34:10 -080093 }
Calin Juravlee02348c2016-03-29 20:33:33 +010094 }
95
Calin Juravle024160852016-02-23 12:00:03 +000096 // We were successful in merging all profile information. Update the reference profile.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010097 if (!reference_profile_file->ClearContent()) {
Calin Juravle024160852016-02-23 12:00:03 +000098 PLOG(WARNING) << "Could not clear reference profile file";
Jiakai Zhange3d589c2022-06-25 00:50:00 +010099 return ProfmanResult::kErrorIO;
Calin Juravle024160852016-02-23 12:00:03 +0000100 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100101 if (!info.Save(reference_profile_file->Fd())) {
Calin Juravle024160852016-02-23 12:00:03 +0000102 LOG(WARNING) << "Could not save reference profile file";
Jiakai Zhange3d589c2022-06-25 00:50:00 +0100103 return ProfmanResult::kErrorIO;
Calin Juravle024160852016-02-23 12:00:03 +0000104 }
105
Jiakai Zhange3d589c2022-06-25 00:50:00 +0100106 return options.IsForceMerge() ? ProfmanResult::kSuccess : ProfmanResult::kCompile;
Calin Juravle024160852016-02-23 12:00:03 +0000107}
108
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100109class ScopedFlockList {
Calin Juravle024160852016-02-23 12:00:03 +0000110 public:
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100111 explicit ScopedFlockList(size_t size) : flocks_(size) {}
Calin Juravle024160852016-02-23 12:00:03 +0000112
113 // Will block until all the locks are acquired.
114 bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) {
115 for (size_t i = 0; i < filenames.size(); i++) {
Andreas Gampe9b031f72018-10-04 11:03:34 -0700116 flocks_[i] = LockedFile::Open(filenames[i].c_str(), O_RDWR, /* block= */ true, error);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100117 if (flocks_[i].get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000118 *error += " (index=" + std::to_string(i) + ")";
119 return false;
120 }
121 }
122 return true;
123 }
124
125 // Will block until all the locks are acquired.
126 bool Init(const std::vector<int>& fds, /* out */ std::string* error) {
127 for (size_t i = 0; i < fds.size(); i++) {
128 DCHECK_GE(fds[i], 0);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100129 flocks_[i] = LockedFile::DupOf(fds[i], "profile-file",
Andreas Gampe9b031f72018-10-04 11:03:34 -0700130 /* read_only_mode= */ true, error);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100131 if (flocks_[i].get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000132 *error += " (index=" + std::to_string(i) + ")";
133 return false;
134 }
135 }
136 return true;
137 }
138
139 const std::vector<ScopedFlock>& Get() const { return flocks_; }
140
141 private:
142 std::vector<ScopedFlock> flocks_;
143};
144
Jiakai Zhange3d589c2022-06-25 00:50:00 +0100145ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfiles(
Calin Juravle024160852016-02-23 12:00:03 +0000146 const std::vector<int>& profile_files_fd,
Calin Juravlee10c1e22018-01-26 20:10:15 -0800147 int reference_profile_file_fd,
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800148 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
149 const Options& options) {
Calin Juravle024160852016-02-23 12:00:03 +0000150 DCHECK_GE(reference_profile_file_fd, 0);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100151
Calin Juravle024160852016-02-23 12:00:03 +0000152 std::string error;
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100153 ScopedFlockList profile_files(profile_files_fd.size());
154 if (!profile_files.Init(profile_files_fd, &error)) {
Calin Juravle024160852016-02-23 12:00:03 +0000155 LOG(WARNING) << "Could not lock profile files: " << error;
Jiakai Zhange3d589c2022-06-25 00:50:00 +0100156 return ProfmanResult::kErrorCannotLock;
Calin Juravle024160852016-02-23 12:00:03 +0000157 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100158
159 // The reference_profile_file is opened in read/write mode because it's
160 // cleared after processing.
161 ScopedFlock reference_profile_file = LockedFile::DupOf(reference_profile_file_fd,
162 "reference-profile",
Andreas Gampe9b031f72018-10-04 11:03:34 -0700163 /* read_only_mode= */ false,
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100164 &error);
165 if (reference_profile_file.get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000166 LOG(WARNING) << "Could not lock reference profiled files: " << error;
Jiakai Zhange3d589c2022-06-25 00:50:00 +0100167 return ProfmanResult::kErrorCannotLock;
Calin Juravle024160852016-02-23 12:00:03 +0000168 }
169
Calin Juravlee10c1e22018-01-26 20:10:15 -0800170 return ProcessProfilesInternal(profile_files.Get(),
171 reference_profile_file,
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800172 filter_fn,
173 options);
Calin Juravle024160852016-02-23 12:00:03 +0000174}
175
Jiakai Zhange3d589c2022-06-25 00:50:00 +0100176ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfiles(
Calin Juravle024160852016-02-23 12:00:03 +0000177 const std::vector<std::string>& profile_files,
Calin Juravlee10c1e22018-01-26 20:10:15 -0800178 const std::string& reference_profile_file,
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800179 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
180 const Options& options) {
Calin Juravle024160852016-02-23 12:00:03 +0000181 std::string error;
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100182
183 ScopedFlockList profile_files_list(profile_files.size());
184 if (!profile_files_list.Init(profile_files, &error)) {
Calin Juravle024160852016-02-23 12:00:03 +0000185 LOG(WARNING) << "Could not lock profile files: " << error;
Jiakai Zhange3d589c2022-06-25 00:50:00 +0100186 return ProfmanResult::kErrorCannotLock;
Calin Juravle024160852016-02-23 12:00:03 +0000187 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100188
189 ScopedFlock locked_reference_profile_file = LockedFile::Open(
Andreas Gampe9b031f72018-10-04 11:03:34 -0700190 reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100191 if (locked_reference_profile_file.get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000192 LOG(WARNING) << "Could not lock reference profile files: " << error;
Jiakai Zhange3d589c2022-06-25 00:50:00 +0100193 return ProfmanResult::kErrorCannotLock;
Calin Juravle024160852016-02-23 12:00:03 +0000194 }
195
Calin Juravlee10c1e22018-01-26 20:10:15 -0800196 return ProcessProfilesInternal(profile_files_list.Get(),
197 locked_reference_profile_file,
Calin Juravle0e82e0f2019-11-11 18:34:10 -0800198 filter_fn,
199 options);
Calin Juravle024160852016-02-23 12:00:03 +0000200}
201
202} // namespace art