• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()) {
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     uint32_t min_change_in_methods_for_compilation = std::max(
83         (options.GetMinNewMethodsPercentChangeForCompilation() * number_of_methods) / 100,
84         kMinNewMethodsForCompilation);
85     uint32_t min_change_in_classes_for_compilation = std::max(
86         (options.GetMinNewClassesPercentChangeForCompilation() * number_of_classes) / 100,
87         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)) {
92       return ProfmanResult::kSkipCompilationSmallDelta;
93     }
94   }
95 
96   // We were successful in merging all profile information. Update the reference profile.
97   if (!reference_profile_file->ClearContent()) {
98     PLOG(WARNING) << "Could not clear reference profile file";
99     return ProfmanResult::kErrorIO;
100   }
101   if (!info.Save(reference_profile_file->Fd())) {
102     LOG(WARNING) << "Could not save reference profile file";
103     return ProfmanResult::kErrorIO;
104   }
105 
106   return options.IsForceMerge() ? ProfmanResult::kSuccess : ProfmanResult::kCompile;
107 }
108 
109 class ScopedFlockList {
110  public:
ScopedFlockList(size_t size)111   explicit ScopedFlockList(size_t size) : flocks_(size) {}
112 
113   // Will block until all the locks are acquired.
Init(const std::vector<std::string> & filenames,std::string * error)114   bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) {
115     for (size_t i = 0; i < filenames.size(); i++) {
116       flocks_[i] = LockedFile::Open(filenames[i].c_str(), O_RDWR, /* block= */ true, error);
117       if (flocks_[i].get() == nullptr) {
118         *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.
Init(const std::vector<int> & fds,std::string * error)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);
129       flocks_[i] = LockedFile::DupOf(fds[i], "profile-file",
130                                      /* read_only_mode= */ true, error);
131       if (flocks_[i].get() == nullptr) {
132         *error += " (index=" + std::to_string(i) + ")";
133         return false;
134       }
135     }
136     return true;
137   }
138 
Get() const139   const std::vector<ScopedFlock>& Get() const { return flocks_; }
140 
141  private:
142   std::vector<ScopedFlock> flocks_;
143 };
144 
ProcessProfiles(const std::vector<int> & profile_files_fd,int reference_profile_file_fd,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)145 ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfiles(
146         const std::vector<int>& profile_files_fd,
147         int reference_profile_file_fd,
148         const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
149         const Options& options) {
150   DCHECK_GE(reference_profile_file_fd, 0);
151 
152   std::string error;
153   ScopedFlockList profile_files(profile_files_fd.size());
154   if (!profile_files.Init(profile_files_fd, &error)) {
155     LOG(WARNING) << "Could not lock profile files: " << error;
156     return ProfmanResult::kErrorCannotLock;
157   }
158 
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",
163                                                          /* read_only_mode= */ false,
164                                                          &error);
165   if (reference_profile_file.get() == nullptr) {
166     LOG(WARNING) << "Could not lock reference profiled files: " << error;
167     return ProfmanResult::kErrorCannotLock;
168   }
169 
170   return ProcessProfilesInternal(profile_files.Get(),
171                                  reference_profile_file,
172                                  filter_fn,
173                                  options);
174 }
175 
ProcessProfiles(const std::vector<std::string> & profile_files,const std::string & reference_profile_file,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)176 ProfmanResult::ProcessingResult ProfileAssistant::ProcessProfiles(
177         const std::vector<std::string>& profile_files,
178         const std::string& reference_profile_file,
179         const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
180         const Options& options) {
181   std::string error;
182 
183   ScopedFlockList profile_files_list(profile_files.size());
184   if (!profile_files_list.Init(profile_files, &error)) {
185     LOG(WARNING) << "Could not lock profile files: " << error;
186     return ProfmanResult::kErrorCannotLock;
187   }
188 
189   ScopedFlock locked_reference_profile_file = LockedFile::Open(
190       reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error);
191   if (locked_reference_profile_file.get() == nullptr) {
192     LOG(WARNING) << "Could not lock reference profile files: " << error;
193     return ProfmanResult::kErrorCannotLock;
194   }
195 
196   return ProcessProfilesInternal(profile_files_list.Get(),
197                                  locked_reference_profile_file,
198                                  filter_fn,
199                                  options);
200 }
201 
202 }  // namespace art
203