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
19 #include "base/os.h"
20 #include "base/unix_file/fd_file.h"
21 #include "profman/profman_result.h"
22
23 namespace art {
24
25 // Minimum number of new methods/classes that profiles
26 // must contain to enable recompilation.
27 static constexpr const uint32_t kMinNewMethodsForCompilation = 100;
28 static constexpr const uint32_t kMinNewClassesForCompilation = 50;
29
ProcessProfilesInternal(const std::vector<ScopedFlock> & profile_files,const ScopedFlock & reference_profile_file,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)30 ProfmanResult::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) {
35 ProfileCompilationInfo info(options.IsBootImageMerge());
36
37 // Load the reference profile.
38 if (!info.Load(reference_profile_file->Fd(), /*merge_classes=*/ true, filter_fn)) {
39 LOG(WARNING) << "Could not load reference profile file";
40 return ProfmanResult::kErrorBadProfiles;
41 }
42
43 if (options.IsBootImageMerge() && !info.IsForBootImage()) {
44 LOG(WARNING) << "Requested merge for boot image profile but the reference profile is regular.";
45 return ProfmanResult::kErrorBadProfiles;
46 }
47
48 // 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++) {
54 ProfileCompilationInfo cur_info(options.IsBootImageMerge());
55 if (!cur_info.Load(profile_files[i]->Fd(), /*merge_classes=*/ true, filter_fn)) {
56 LOG(WARNING) << "Could not load profile file at index " << i;
57 if (options.IsForceMerge() || options.IsForceMergeAndAnalyze()) {
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 }
63 // 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)) {
66 return ProfmanResult::kErrorDifferentVersions;
67 }
68 return ProfmanResult::kErrorBadProfiles;
69 }
70
71 if (!info.MergeWith(cur_info)) {
72 LOG(WARNING) << "Could not merge profile file at index " << i;
73 return ProfmanResult::kErrorBadProfiles;
74 }
75 }
76
77 // If we perform a forced merge do not analyze the difference between profiles.
78 if (!options.IsForceMerge()) {
79 if (info.IsEmpty()) {
80 return ProfmanResult::kSkipCompilationEmptyProfiles;
81 }
82
83 if (options.IsForceMergeAndAnalyze()) {
84 // When we force merge and analyze, we want to always recompile unless there is absolutely no
85 // difference between before and after the merge (i.e., the classes and methods in the
86 // reference profile were already a superset of those in all current profiles before the
87 // merge.)
88 if (info.GetNumberOfMethods() == number_of_methods &&
89 info.GetNumberOfResolvedClasses() == number_of_classes) {
90 return ProfmanResult::kSkipCompilationSmallDelta;
91 }
92 } else {
93 uint32_t min_change_in_methods_for_compilation = std::max(
94 (options.GetMinNewMethodsPercentChangeForCompilation() * number_of_methods) / 100,
95 kMinNewMethodsForCompilation);
96 uint32_t min_change_in_classes_for_compilation = std::max(
97 (options.GetMinNewClassesPercentChangeForCompilation() * number_of_classes) / 100,
98 kMinNewClassesForCompilation);
99 // Check if there is enough new information added by the current profiles.
100 if (((info.GetNumberOfMethods() - number_of_methods) <
101 min_change_in_methods_for_compilation) &&
102 ((info.GetNumberOfResolvedClasses() - number_of_classes) <
103 min_change_in_classes_for_compilation)) {
104 return ProfmanResult::kSkipCompilationSmallDelta;
105 }
106 }
107 }
108
109 // We were successful in merging all profile information. Update the reference profile.
110 if (!reference_profile_file->ClearContent()) {
111 PLOG(WARNING) << "Could not clear reference profile file";
112 return ProfmanResult::kErrorIO;
113 }
114 if (!info.Save(reference_profile_file->Fd())) {
115 LOG(WARNING) << "Could not save reference profile file";
116 return ProfmanResult::kErrorIO;
117 }
118
119 return options.IsForceMerge() ? ProfmanResult::kSuccess : ProfmanResult::kCompile;
120 }
121
122 class ScopedFlockList {
123 public:
ScopedFlockList(size_t size)124 explicit ScopedFlockList(size_t size) : flocks_(size) {}
125
126 // Will block until all the locks are acquired.
Init(const std::vector<std::string> & filenames,std::string * error)127 bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) {
128 for (size_t i = 0; i < filenames.size(); i++) {
129 flocks_[i] = LockedFile::Open(filenames[i].c_str(), O_RDWR, /* block= */ true, error);
130 if (flocks_[i].get() == nullptr) {
131 *error += " (index=" + std::to_string(i) + ")";
132 return false;
133 }
134 }
135 return true;
136 }
137
138 // Will block until all the locks are acquired.
Init(const std::vector<int> & fds,std::string * error)139 bool Init(const std::vector<int>& fds, /* out */ std::string* error) {
140 for (size_t i = 0; i < fds.size(); i++) {
141 DCHECK_GE(fds[i], 0);
142 flocks_[i] = LockedFile::DupOf(fds[i], "profile-file",
143 /* read_only_mode= */ true, error);
144 if (flocks_[i].get() == nullptr) {
145 *error += " (index=" + std::to_string(i) + ")";
146 return false;
147 }
148 }
149 return true;
150 }
151
Get() const152 const std::vector<ScopedFlock>& Get() const { return flocks_; }
153
154 private:
155 std::vector<ScopedFlock> flocks_;
156 };
157
ProcessProfiles(const std::vector<int> & profile_files_fd,int reference_profile_file_fd,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)158 ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfiles(
159 const std::vector<int>& profile_files_fd,
160 int reference_profile_file_fd,
161 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
162 const Options& options) {
163 DCHECK_GE(reference_profile_file_fd, 0);
164
165 std::string error;
166 ScopedFlockList profile_files(profile_files_fd.size());
167 if (!profile_files.Init(profile_files_fd, &error)) {
168 LOG(WARNING) << "Could not lock profile files: " << error;
169 return ProfmanResult::kErrorCannotLock;
170 }
171
172 // The reference_profile_file is opened in read/write mode because it's
173 // cleared after processing.
174 ScopedFlock reference_profile_file = LockedFile::DupOf(reference_profile_file_fd,
175 "reference-profile",
176 /* read_only_mode= */ false,
177 &error);
178 if (reference_profile_file.get() == nullptr) {
179 LOG(WARNING) << "Could not lock reference profiled files: " << error;
180 return ProfmanResult::kErrorCannotLock;
181 }
182
183 return ProcessProfilesInternal(profile_files.Get(),
184 reference_profile_file,
185 filter_fn,
186 options);
187 }
188
ProcessProfiles(const std::vector<std::string> & profile_files,const std::string & reference_profile_file,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)189 ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfiles(
190 const std::vector<std::string>& profile_files,
191 const std::string& reference_profile_file,
192 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
193 const Options& options) {
194 std::string error;
195
196 ScopedFlockList profile_files_list(profile_files.size());
197 if (!profile_files_list.Init(profile_files, &error)) {
198 LOG(WARNING) << "Could not lock profile files: " << error;
199 return ProfmanResult::kErrorCannotLock;
200 }
201
202 ScopedFlock locked_reference_profile_file = LockedFile::Open(
203 reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error);
204 if (locked_reference_profile_file.get() == nullptr) {
205 LOG(WARNING) << "Could not lock reference profile files: " << error;
206 return ProfmanResult::kErrorCannotLock;
207 }
208
209 return ProcessProfilesInternal(profile_files_list.Get(),
210 locked_reference_profile_file,
211 filter_fn,
212 options);
213 }
214
215 } // namespace art
216