xref: /aosp_15_r20/art/profman/profile_assistant.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "profile_assistant.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "base/os.h"
20*795d594fSAndroid Build Coastguard Worker #include "base/unix_file/fd_file.h"
21*795d594fSAndroid Build Coastguard Worker #include "profman/profman_result.h"
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker namespace art {
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker // Minimum number of new methods/classes that profiles
26*795d594fSAndroid Build Coastguard Worker // must contain to enable recompilation.
27*795d594fSAndroid Build Coastguard Worker static constexpr const uint32_t kMinNewMethodsForCompilation = 100;
28*795d594fSAndroid Build Coastguard Worker static constexpr const uint32_t kMinNewClassesForCompilation = 50;
29*795d594fSAndroid Build Coastguard Worker 
ProcessProfilesInternal(const std::vector<ScopedFlock> & profile_files,const ScopedFlock & reference_profile_file,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)30*795d594fSAndroid Build Coastguard Worker ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfilesInternal(
31*795d594fSAndroid Build Coastguard Worker     const std::vector<ScopedFlock>& profile_files,
32*795d594fSAndroid Build Coastguard Worker     const ScopedFlock& reference_profile_file,
33*795d594fSAndroid Build Coastguard Worker     const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
34*795d594fSAndroid Build Coastguard Worker     const Options& options) {
35*795d594fSAndroid Build Coastguard Worker   ProfileCompilationInfo info(options.IsBootImageMerge());
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker   // Load the reference profile.
38*795d594fSAndroid Build Coastguard Worker   if (!info.Load(reference_profile_file->Fd(), /*merge_classes=*/ true, filter_fn)) {
39*795d594fSAndroid Build Coastguard Worker     LOG(WARNING) << "Could not load reference profile file";
40*795d594fSAndroid Build Coastguard Worker     return ProfmanResult::kErrorBadProfiles;
41*795d594fSAndroid Build Coastguard Worker   }
42*795d594fSAndroid Build Coastguard Worker 
43*795d594fSAndroid Build Coastguard Worker   if (options.IsBootImageMerge() && !info.IsForBootImage()) {
44*795d594fSAndroid Build Coastguard Worker     LOG(WARNING) << "Requested merge for boot image profile but the reference profile is regular.";
45*795d594fSAndroid Build Coastguard Worker     return ProfmanResult::kErrorBadProfiles;
46*795d594fSAndroid Build Coastguard Worker   }
47*795d594fSAndroid Build Coastguard Worker 
48*795d594fSAndroid Build Coastguard Worker   // Store the current state of the reference profile before merging with the current profiles.
49*795d594fSAndroid Build Coastguard Worker   uint32_t number_of_methods = info.GetNumberOfMethods();
50*795d594fSAndroid Build Coastguard Worker   uint32_t number_of_classes = info.GetNumberOfResolvedClasses();
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker   // Merge all current profiles.
53*795d594fSAndroid Build Coastguard Worker   for (size_t i = 0; i < profile_files.size(); i++) {
54*795d594fSAndroid Build Coastguard Worker     ProfileCompilationInfo cur_info(options.IsBootImageMerge());
55*795d594fSAndroid Build Coastguard Worker     if (!cur_info.Load(profile_files[i]->Fd(), /*merge_classes=*/ true, filter_fn)) {
56*795d594fSAndroid Build Coastguard Worker       LOG(WARNING) << "Could not load profile file at index " << i;
57*795d594fSAndroid Build Coastguard Worker       if (options.IsForceMerge() || options.IsForceMergeAndAnalyze()) {
58*795d594fSAndroid Build Coastguard Worker         // If we have to merge forcefully, ignore load failures.
59*795d594fSAndroid Build Coastguard Worker         // This is useful for boot image profiles to ignore stale profiles which are
60*795d594fSAndroid Build Coastguard Worker         // cleared lazily.
61*795d594fSAndroid Build Coastguard Worker         continue;
62*795d594fSAndroid Build Coastguard Worker       }
63*795d594fSAndroid Build Coastguard Worker       // TODO: Do we really need to use a different error code for version mismatch?
64*795d594fSAndroid Build Coastguard Worker       ProfileCompilationInfo wrong_info(!options.IsBootImageMerge());
65*795d594fSAndroid Build Coastguard Worker       if (wrong_info.Load(profile_files[i]->Fd(), /*merge_classes=*/ true, filter_fn)) {
66*795d594fSAndroid Build Coastguard Worker         return ProfmanResult::kErrorDifferentVersions;
67*795d594fSAndroid Build Coastguard Worker       }
68*795d594fSAndroid Build Coastguard Worker       return ProfmanResult::kErrorBadProfiles;
69*795d594fSAndroid Build Coastguard Worker     }
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker     if (!info.MergeWith(cur_info)) {
72*795d594fSAndroid Build Coastguard Worker       LOG(WARNING) << "Could not merge profile file at index " << i;
73*795d594fSAndroid Build Coastguard Worker       return ProfmanResult::kErrorBadProfiles;
74*795d594fSAndroid Build Coastguard Worker     }
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker 
77*795d594fSAndroid Build Coastguard Worker   // If we perform a forced merge do not analyze the difference between profiles.
78*795d594fSAndroid Build Coastguard Worker   if (!options.IsForceMerge()) {
79*795d594fSAndroid Build Coastguard Worker     if (info.IsEmpty()) {
80*795d594fSAndroid Build Coastguard Worker       return ProfmanResult::kSkipCompilationEmptyProfiles;
81*795d594fSAndroid Build Coastguard Worker     }
82*795d594fSAndroid Build Coastguard Worker 
83*795d594fSAndroid Build Coastguard Worker     if (options.IsForceMergeAndAnalyze()) {
84*795d594fSAndroid Build Coastguard Worker       // When we force merge and analyze, we want to always recompile unless there is absolutely no
85*795d594fSAndroid Build Coastguard Worker       // difference between before and after the merge (i.e., the classes and methods in the
86*795d594fSAndroid Build Coastguard Worker       // reference profile were already a superset of those in all current profiles before the
87*795d594fSAndroid Build Coastguard Worker       // merge.)
88*795d594fSAndroid Build Coastguard Worker       if (info.GetNumberOfMethods() == number_of_methods &&
89*795d594fSAndroid Build Coastguard Worker           info.GetNumberOfResolvedClasses() == number_of_classes) {
90*795d594fSAndroid Build Coastguard Worker         return ProfmanResult::kSkipCompilationSmallDelta;
91*795d594fSAndroid Build Coastguard Worker       }
92*795d594fSAndroid Build Coastguard Worker     } else {
93*795d594fSAndroid Build Coastguard Worker       uint32_t min_change_in_methods_for_compilation = std::max(
94*795d594fSAndroid Build Coastguard Worker           (options.GetMinNewMethodsPercentChangeForCompilation() * number_of_methods) / 100,
95*795d594fSAndroid Build Coastguard Worker           kMinNewMethodsForCompilation);
96*795d594fSAndroid Build Coastguard Worker       uint32_t min_change_in_classes_for_compilation = std::max(
97*795d594fSAndroid Build Coastguard Worker           (options.GetMinNewClassesPercentChangeForCompilation() * number_of_classes) / 100,
98*795d594fSAndroid Build Coastguard Worker           kMinNewClassesForCompilation);
99*795d594fSAndroid Build Coastguard Worker       // Check if there is enough new information added by the current profiles.
100*795d594fSAndroid Build Coastguard Worker       if (((info.GetNumberOfMethods() - number_of_methods) <
101*795d594fSAndroid Build Coastguard Worker            min_change_in_methods_for_compilation) &&
102*795d594fSAndroid Build Coastguard Worker           ((info.GetNumberOfResolvedClasses() - number_of_classes) <
103*795d594fSAndroid Build Coastguard Worker            min_change_in_classes_for_compilation)) {
104*795d594fSAndroid Build Coastguard Worker         return ProfmanResult::kSkipCompilationSmallDelta;
105*795d594fSAndroid Build Coastguard Worker       }
106*795d594fSAndroid Build Coastguard Worker     }
107*795d594fSAndroid Build Coastguard Worker   }
108*795d594fSAndroid Build Coastguard Worker 
109*795d594fSAndroid Build Coastguard Worker   // We were successful in merging all profile information. Update the reference profile.
110*795d594fSAndroid Build Coastguard Worker   if (!reference_profile_file->ClearContent()) {
111*795d594fSAndroid Build Coastguard Worker     PLOG(WARNING) << "Could not clear reference profile file";
112*795d594fSAndroid Build Coastguard Worker     return ProfmanResult::kErrorIO;
113*795d594fSAndroid Build Coastguard Worker   }
114*795d594fSAndroid Build Coastguard Worker   if (!info.Save(reference_profile_file->Fd())) {
115*795d594fSAndroid Build Coastguard Worker     LOG(WARNING) << "Could not save reference profile file";
116*795d594fSAndroid Build Coastguard Worker     return ProfmanResult::kErrorIO;
117*795d594fSAndroid Build Coastguard Worker   }
118*795d594fSAndroid Build Coastguard Worker 
119*795d594fSAndroid Build Coastguard Worker   return options.IsForceMerge() ? ProfmanResult::kSuccess : ProfmanResult::kCompile;
120*795d594fSAndroid Build Coastguard Worker }
121*795d594fSAndroid Build Coastguard Worker 
122*795d594fSAndroid Build Coastguard Worker class ScopedFlockList {
123*795d594fSAndroid Build Coastguard Worker  public:
ScopedFlockList(size_t size)124*795d594fSAndroid Build Coastguard Worker   explicit ScopedFlockList(size_t size) : flocks_(size) {}
125*795d594fSAndroid Build Coastguard Worker 
126*795d594fSAndroid Build Coastguard Worker   // Will block until all the locks are acquired.
Init(const std::vector<std::string> & filenames,std::string * error)127*795d594fSAndroid Build Coastguard Worker   bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) {
128*795d594fSAndroid Build Coastguard Worker     for (size_t i = 0; i < filenames.size(); i++) {
129*795d594fSAndroid Build Coastguard Worker       flocks_[i] = LockedFile::Open(filenames[i].c_str(), O_RDWR, /* block= */ true, error);
130*795d594fSAndroid Build Coastguard Worker       if (flocks_[i].get() == nullptr) {
131*795d594fSAndroid Build Coastguard Worker         *error += " (index=" + std::to_string(i) + ")";
132*795d594fSAndroid Build Coastguard Worker         return false;
133*795d594fSAndroid Build Coastguard Worker       }
134*795d594fSAndroid Build Coastguard Worker     }
135*795d594fSAndroid Build Coastguard Worker     return true;
136*795d594fSAndroid Build Coastguard Worker   }
137*795d594fSAndroid Build Coastguard Worker 
138*795d594fSAndroid Build Coastguard Worker   // Will block until all the locks are acquired.
Init(const std::vector<int> & fds,std::string * error)139*795d594fSAndroid Build Coastguard Worker   bool Init(const std::vector<int>& fds, /* out */ std::string* error) {
140*795d594fSAndroid Build Coastguard Worker     for (size_t i = 0; i < fds.size(); i++) {
141*795d594fSAndroid Build Coastguard Worker       DCHECK_GE(fds[i], 0);
142*795d594fSAndroid Build Coastguard Worker       flocks_[i] = LockedFile::DupOf(fds[i], "profile-file",
143*795d594fSAndroid Build Coastguard Worker                                      /* read_only_mode= */ true, error);
144*795d594fSAndroid Build Coastguard Worker       if (flocks_[i].get() == nullptr) {
145*795d594fSAndroid Build Coastguard Worker         *error += " (index=" + std::to_string(i) + ")";
146*795d594fSAndroid Build Coastguard Worker         return false;
147*795d594fSAndroid Build Coastguard Worker       }
148*795d594fSAndroid Build Coastguard Worker     }
149*795d594fSAndroid Build Coastguard Worker     return true;
150*795d594fSAndroid Build Coastguard Worker   }
151*795d594fSAndroid Build Coastguard Worker 
Get() const152*795d594fSAndroid Build Coastguard Worker   const std::vector<ScopedFlock>& Get() const { return flocks_; }
153*795d594fSAndroid Build Coastguard Worker 
154*795d594fSAndroid Build Coastguard Worker  private:
155*795d594fSAndroid Build Coastguard Worker   std::vector<ScopedFlock> flocks_;
156*795d594fSAndroid Build Coastguard Worker };
157*795d594fSAndroid Build Coastguard Worker 
ProcessProfiles(const std::vector<int> & profile_files_fd,int reference_profile_file_fd,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)158*795d594fSAndroid Build Coastguard Worker ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfiles(
159*795d594fSAndroid Build Coastguard Worker         const std::vector<int>& profile_files_fd,
160*795d594fSAndroid Build Coastguard Worker         int reference_profile_file_fd,
161*795d594fSAndroid Build Coastguard Worker         const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
162*795d594fSAndroid Build Coastguard Worker         const Options& options) {
163*795d594fSAndroid Build Coastguard Worker   DCHECK_GE(reference_profile_file_fd, 0);
164*795d594fSAndroid Build Coastguard Worker 
165*795d594fSAndroid Build Coastguard Worker   std::string error;
166*795d594fSAndroid Build Coastguard Worker   ScopedFlockList profile_files(profile_files_fd.size());
167*795d594fSAndroid Build Coastguard Worker   if (!profile_files.Init(profile_files_fd, &error)) {
168*795d594fSAndroid Build Coastguard Worker     LOG(WARNING) << "Could not lock profile files: " << error;
169*795d594fSAndroid Build Coastguard Worker     return ProfmanResult::kErrorCannotLock;
170*795d594fSAndroid Build Coastguard Worker   }
171*795d594fSAndroid Build Coastguard Worker 
172*795d594fSAndroid Build Coastguard Worker   // The reference_profile_file is opened in read/write mode because it's
173*795d594fSAndroid Build Coastguard Worker   // cleared after processing.
174*795d594fSAndroid Build Coastguard Worker   ScopedFlock reference_profile_file = LockedFile::DupOf(reference_profile_file_fd,
175*795d594fSAndroid Build Coastguard Worker                                                          "reference-profile",
176*795d594fSAndroid Build Coastguard Worker                                                          /* read_only_mode= */ false,
177*795d594fSAndroid Build Coastguard Worker                                                          &error);
178*795d594fSAndroid Build Coastguard Worker   if (reference_profile_file.get() == nullptr) {
179*795d594fSAndroid Build Coastguard Worker     LOG(WARNING) << "Could not lock reference profiled files: " << error;
180*795d594fSAndroid Build Coastguard Worker     return ProfmanResult::kErrorCannotLock;
181*795d594fSAndroid Build Coastguard Worker   }
182*795d594fSAndroid Build Coastguard Worker 
183*795d594fSAndroid Build Coastguard Worker   return ProcessProfilesInternal(profile_files.Get(),
184*795d594fSAndroid Build Coastguard Worker                                  reference_profile_file,
185*795d594fSAndroid Build Coastguard Worker                                  filter_fn,
186*795d594fSAndroid Build Coastguard Worker                                  options);
187*795d594fSAndroid Build Coastguard Worker }
188*795d594fSAndroid Build Coastguard Worker 
ProcessProfiles(const std::vector<std::string> & profile_files,const std::string & reference_profile_file,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)189*795d594fSAndroid Build Coastguard Worker ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfiles(
190*795d594fSAndroid Build Coastguard Worker         const std::vector<std::string>& profile_files,
191*795d594fSAndroid Build Coastguard Worker         const std::string& reference_profile_file,
192*795d594fSAndroid Build Coastguard Worker         const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
193*795d594fSAndroid Build Coastguard Worker         const Options& options) {
194*795d594fSAndroid Build Coastguard Worker   std::string error;
195*795d594fSAndroid Build Coastguard Worker 
196*795d594fSAndroid Build Coastguard Worker   ScopedFlockList profile_files_list(profile_files.size());
197*795d594fSAndroid Build Coastguard Worker   if (!profile_files_list.Init(profile_files, &error)) {
198*795d594fSAndroid Build Coastguard Worker     LOG(WARNING) << "Could not lock profile files: " << error;
199*795d594fSAndroid Build Coastguard Worker     return ProfmanResult::kErrorCannotLock;
200*795d594fSAndroid Build Coastguard Worker   }
201*795d594fSAndroid Build Coastguard Worker 
202*795d594fSAndroid Build Coastguard Worker   ScopedFlock locked_reference_profile_file = LockedFile::Open(
203*795d594fSAndroid Build Coastguard Worker       reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error);
204*795d594fSAndroid Build Coastguard Worker   if (locked_reference_profile_file.get() == nullptr) {
205*795d594fSAndroid Build Coastguard Worker     LOG(WARNING) << "Could not lock reference profile files: " << error;
206*795d594fSAndroid Build Coastguard Worker     return ProfmanResult::kErrorCannotLock;
207*795d594fSAndroid Build Coastguard Worker   }
208*795d594fSAndroid Build Coastguard Worker 
209*795d594fSAndroid Build Coastguard Worker   return ProcessProfilesInternal(profile_files_list.Get(),
210*795d594fSAndroid Build Coastguard Worker                                  locked_reference_profile_file,
211*795d594fSAndroid Build Coastguard Worker                                  filter_fn,
212*795d594fSAndroid Build Coastguard Worker                                  options);
213*795d594fSAndroid Build Coastguard Worker }
214*795d594fSAndroid Build Coastguard Worker 
215*795d594fSAndroid Build Coastguard Worker }  // namespace art
216