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
22 namespace art {
23
24 // Minimum number of new methods/classes that profiles
25 // must contain to enable recompilation.
26 static constexpr const uint32_t kMinNewMethodsForCompilation = 100;
27 static constexpr const uint32_t kMinNewClassesForCompilation = 50;
28
29
ProcessProfilesInternal(const std::vector<ScopedFlock> & profile_files,const ScopedFlock & reference_profile_file,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)30 ProfileAssistant::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 DCHECK(!profile_files.empty());
36
37 ProfileCompilationInfo info(options.IsBootImageMerge());
38
39 // Load the reference profile.
40 if (!info.Load(reference_profile_file->Fd(), /*merge_classes=*/ true, filter_fn)) {
41 LOG(WARNING) << "Could not load reference profile file";
42 return kErrorBadProfiles;
43 }
44
45 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
50 // 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++) {
56 ProfileCompilationInfo cur_info(options.IsBootImageMerge());
57 if (!cur_info.Load(profile_files[i]->Fd(), /*merge_classes=*/ true, filter_fn)) {
58 LOG(WARNING) << "Could not load profile file at index " << i;
59 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 }
65 // 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)) {
68 return kErrorDifferentVersions;
69 }
70 return kErrorBadProfiles;
71 }
72
73 if (!info.MergeWith(cur_info)) {
74 LOG(WARNING) << "Could not merge profile file at index " << i;
75 return kErrorBadProfiles;
76 }
77 }
78
79 // If we perform a forced merge do not analyze the difference between profiles.
80 if (!options.IsForceMerge()) {
81 if (info.IsEmpty()) {
82 return kSkipCompilationEmptyProfiles;
83 }
84 uint32_t min_change_in_methods_for_compilation = std::max(
85 (options.GetMinNewMethodsPercentChangeForCompilation() * number_of_methods) / 100,
86 kMinNewMethodsForCompilation);
87 uint32_t min_change_in_classes_for_compilation = std::max(
88 (options.GetMinNewClassesPercentChangeForCompilation() * number_of_classes) / 100,
89 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)) {
94 return kSkipCompilationSmallDelta;
95 }
96 }
97
98 // We were successful in merging all profile information. Update the reference profile.
99 if (!reference_profile_file->ClearContent()) {
100 PLOG(WARNING) << "Could not clear reference profile file";
101 return kErrorIO;
102 }
103 if (!info.Save(reference_profile_file->Fd())) {
104 LOG(WARNING) << "Could not save reference profile file";
105 return kErrorIO;
106 }
107
108 return options.IsForceMerge() ? kSuccess : kCompile;
109 }
110
111 class ScopedFlockList {
112 public:
ScopedFlockList(size_t size)113 explicit ScopedFlockList(size_t size) : flocks_(size) {}
114
115 // Will block until all the locks are acquired.
Init(const std::vector<std::string> & filenames,std::string * error)116 bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) {
117 for (size_t i = 0; i < filenames.size(); i++) {
118 flocks_[i] = LockedFile::Open(filenames[i].c_str(), O_RDWR, /* block= */ true, error);
119 if (flocks_[i].get() == nullptr) {
120 *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.
Init(const std::vector<int> & fds,std::string * error)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);
131 flocks_[i] = LockedFile::DupOf(fds[i], "profile-file",
132 /* read_only_mode= */ true, error);
133 if (flocks_[i].get() == nullptr) {
134 *error += " (index=" + std::to_string(i) + ")";
135 return false;
136 }
137 }
138 return true;
139 }
140
Get() const141 const std::vector<ScopedFlock>& Get() const { return flocks_; }
142
143 private:
144 std::vector<ScopedFlock> flocks_;
145 };
146
ProcessProfiles(const std::vector<int> & profile_files_fd,int reference_profile_file_fd,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)147 ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
148 const std::vector<int>& profile_files_fd,
149 int reference_profile_file_fd,
150 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
151 const Options& options) {
152 DCHECK_GE(reference_profile_file_fd, 0);
153
154 std::string error;
155 ScopedFlockList profile_files(profile_files_fd.size());
156 if (!profile_files.Init(profile_files_fd, &error)) {
157 LOG(WARNING) << "Could not lock profile files: " << error;
158 return kErrorCannotLock;
159 }
160
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",
165 /* read_only_mode= */ false,
166 &error);
167 if (reference_profile_file.get() == nullptr) {
168 LOG(WARNING) << "Could not lock reference profiled files: " << error;
169 return kErrorCannotLock;
170 }
171
172 return ProcessProfilesInternal(profile_files.Get(),
173 reference_profile_file,
174 filter_fn,
175 options);
176 }
177
ProcessProfiles(const std::vector<std::string> & profile_files,const std::string & reference_profile_file,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)178 ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
179 const std::vector<std::string>& profile_files,
180 const std::string& reference_profile_file,
181 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
182 const Options& options) {
183 std::string error;
184
185 ScopedFlockList profile_files_list(profile_files.size());
186 if (!profile_files_list.Init(profile_files, &error)) {
187 LOG(WARNING) << "Could not lock profile files: " << error;
188 return kErrorCannotLock;
189 }
190
191 ScopedFlock locked_reference_profile_file = LockedFile::Open(
192 reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error);
193 if (locked_reference_profile_file.get() == nullptr) {
194 LOG(WARNING) << "Could not lock reference profile files: " << error;
195 return kErrorCannotLock;
196 }
197
198 return ProcessProfilesInternal(profile_files_list.Get(),
199 locked_reference_profile_file,
200 filter_fn,
201 options);
202 }
203
204 } // namespace art
205