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 kMinNewMethodsPercentChangeForCompilation = 2;
28 static constexpr const uint32_t kMinNewClassesForCompilation = 50;
29 static constexpr const uint32_t kMinNewClassesPercentChangeForCompilation = 2;
30 
31 
ProcessProfilesInternal(const std::vector<ScopedFlock> & profile_files,const ScopedFlock & reference_profile_file,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)32 ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfilesInternal(
33         const std::vector<ScopedFlock>& profile_files,
34         const ScopedFlock& reference_profile_file,
35         const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
36         const Options& options) {
37   DCHECK(!profile_files.empty());
38 
39   ProfileCompilationInfo info(options.IsBootImageMerge());
40 
41   // Load the reference profile.
42   if (!info.Load(reference_profile_file->Fd(), /*merge_classes=*/ true, filter_fn)) {
43     LOG(WARNING) << "Could not load reference profile file";
44     return kErrorBadProfiles;
45   }
46 
47   if (options.IsBootImageMerge() && !info.IsForBootImage()) {
48     LOG(WARNING) << "Requested merge for boot image profile but the reference profile is regular.";
49     return kErrorBadProfiles;
50   }
51 
52   // Store the current state of the reference profile before merging with the current profiles.
53   uint32_t number_of_methods = info.GetNumberOfMethods();
54   uint32_t number_of_classes = info.GetNumberOfResolvedClasses();
55 
56   // Merge all current profiles.
57   for (size_t i = 0; i < profile_files.size(); i++) {
58     ProfileCompilationInfo cur_info;
59     if (!cur_info.Load(profile_files[i]->Fd(), /*merge_classes=*/ true, filter_fn)) {
60       LOG(WARNING) << "Could not load profile file at index " << i;
61       if (options.IsForceMerge()) {
62         // If we have to merge forcefully, ignore load failures.
63         // This is useful for boot image profiles to ignore stale profiles which are
64         // cleared lazily.
65         continue;
66       }
67       return kErrorBadProfiles;
68     }
69 
70     // Check version mismatch.
71     // This may happen during profile analysis if one profile is regular and
72     // the other one is for the boot image. For example when switching on-off
73     // the boot image profiles.
74     if (!info.SameVersion(cur_info)) {
75       if (options.IsForceMerge()) {
76         // If we have to merge forcefully, ignore the current profile and
77         // continue to the next one.
78         continue;
79       } else {
80         // Otherwise, return an error.
81         return kErrorDifferentVersions;
82       }
83     }
84 
85     if (!info.MergeWith(cur_info)) {
86       LOG(WARNING) << "Could not merge profile file at index " << i;
87       return kErrorBadProfiles;
88     }
89   }
90 
91   // If we perform a forced merge do not analyze the difference between profiles.
92   if (!options.IsForceMerge()) {
93     uint32_t min_change_in_methods_for_compilation = std::max(
94         (kMinNewMethodsPercentChangeForCompilation * number_of_methods) / 100,
95         kMinNewMethodsForCompilation);
96     uint32_t min_change_in_classes_for_compilation = std::max(
97         (kMinNewClassesPercentChangeForCompilation * 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) < min_change_in_methods_for_compilation) &&
101         ((info.GetNumberOfResolvedClasses() - number_of_classes)
102             < min_change_in_classes_for_compilation)) {
103       return kSkipCompilation;
104     }
105   }
106 
107   // We were successful in merging all profile information. Update the reference profile.
108   if (!reference_profile_file->ClearContent()) {
109     PLOG(WARNING) << "Could not clear reference profile file";
110     return kErrorIO;
111   }
112   if (!info.Save(reference_profile_file->Fd())) {
113     LOG(WARNING) << "Could not save reference profile file";
114     return kErrorIO;
115   }
116 
117   return options.IsForceMerge() ? kSuccess : kCompile;
118 }
119 
120 class ScopedFlockList {
121  public:
ScopedFlockList(size_t size)122   explicit ScopedFlockList(size_t size) : flocks_(size) {}
123 
124   // Will block until all the locks are acquired.
Init(const std::vector<std::string> & filenames,std::string * error)125   bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) {
126     for (size_t i = 0; i < filenames.size(); i++) {
127       flocks_[i] = LockedFile::Open(filenames[i].c_str(), O_RDWR, /* block= */ true, error);
128       if (flocks_[i].get() == nullptr) {
129         *error += " (index=" + std::to_string(i) + ")";
130         return false;
131       }
132     }
133     return true;
134   }
135 
136   // Will block until all the locks are acquired.
Init(const std::vector<int> & fds,std::string * error)137   bool Init(const std::vector<int>& fds, /* out */ std::string* error) {
138     for (size_t i = 0; i < fds.size(); i++) {
139       DCHECK_GE(fds[i], 0);
140       flocks_[i] = LockedFile::DupOf(fds[i], "profile-file",
141                                      /* read_only_mode= */ true, error);
142       if (flocks_[i].get() == nullptr) {
143         *error += " (index=" + std::to_string(i) + ")";
144         return false;
145       }
146     }
147     return true;
148   }
149 
Get() const150   const std::vector<ScopedFlock>& Get() const { return flocks_; }
151 
152  private:
153   std::vector<ScopedFlock> flocks_;
154 };
155 
ProcessProfiles(const std::vector<int> & profile_files_fd,int reference_profile_file_fd,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)156 ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
157         const std::vector<int>& profile_files_fd,
158         int reference_profile_file_fd,
159         const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
160         const Options& options) {
161   DCHECK_GE(reference_profile_file_fd, 0);
162 
163   std::string error;
164   ScopedFlockList profile_files(profile_files_fd.size());
165   if (!profile_files.Init(profile_files_fd, &error)) {
166     LOG(WARNING) << "Could not lock profile files: " << error;
167     return kErrorCannotLock;
168   }
169 
170   // The reference_profile_file is opened in read/write mode because it's
171   // cleared after processing.
172   ScopedFlock reference_profile_file = LockedFile::DupOf(reference_profile_file_fd,
173                                                          "reference-profile",
174                                                          /* read_only_mode= */ false,
175                                                          &error);
176   if (reference_profile_file.get() == nullptr) {
177     LOG(WARNING) << "Could not lock reference profiled files: " << error;
178     return kErrorCannotLock;
179   }
180 
181   return ProcessProfilesInternal(profile_files.Get(),
182                                  reference_profile_file,
183                                  filter_fn,
184                                  options);
185 }
186 
ProcessProfiles(const std::vector<std::string> & profile_files,const std::string & reference_profile_file,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,const Options & options)187 ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
188         const std::vector<std::string>& profile_files,
189         const std::string& reference_profile_file,
190         const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
191         const Options& options) {
192   std::string error;
193 
194   ScopedFlockList profile_files_list(profile_files.size());
195   if (!profile_files_list.Init(profile_files, &error)) {
196     LOG(WARNING) << "Could not lock profile files: " << error;
197     return kErrorCannotLock;
198   }
199 
200   ScopedFlock locked_reference_profile_file = LockedFile::Open(
201       reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error);
202   if (locked_reference_profile_file.get() == nullptr) {
203     LOG(WARNING) << "Could not lock reference profile files: " << error;
204     return kErrorCannotLock;
205   }
206 
207   return ProcessProfilesInternal(profile_files_list.Get(),
208                                  locked_reference_profile_file,
209                                  filter_fn,
210                                  options);
211 }
212 
213 }  // namespace art
214