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,bool store_aggregation_counters)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 bool store_aggregation_counters) {
37 DCHECK(!profile_files.empty());
38
39 ProfileCompilationInfo info;
40 // Load the reference profile.
41 if (!info.Load(reference_profile_file->Fd(), /*merge_classes=*/ true, filter_fn)) {
42 LOG(WARNING) << "Could not load reference profile file";
43 return kErrorBadProfiles;
44 }
45
46 // If we need to store aggregation counters (e.g. for the boot image profile),
47 // prepare the reference profile now.
48 if (store_aggregation_counters) {
49 info.PrepareForAggregationCounters();
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 return kErrorBadProfiles;
62 }
63 if (!info.MergeWith(cur_info)) {
64 LOG(WARNING) << "Could not merge profile file at index " << i;
65 return kErrorBadProfiles;
66 }
67 }
68
69 uint32_t min_change_in_methods_for_compilation = std::max(
70 (kMinNewMethodsPercentChangeForCompilation * number_of_methods) / 100,
71 kMinNewMethodsForCompilation);
72 uint32_t min_change_in_classes_for_compilation = std::max(
73 (kMinNewClassesPercentChangeForCompilation * number_of_classes) / 100,
74 kMinNewClassesForCompilation);
75 // Check if there is enough new information added by the current profiles.
76 if (((info.GetNumberOfMethods() - number_of_methods) < min_change_in_methods_for_compilation) &&
77 ((info.GetNumberOfResolvedClasses() - number_of_classes)
78 < min_change_in_classes_for_compilation)) {
79 return kSkipCompilation;
80 }
81
82 // We were successful in merging all profile information. Update the reference profile.
83 if (!reference_profile_file->ClearContent()) {
84 PLOG(WARNING) << "Could not clear reference profile file";
85 return kErrorIO;
86 }
87 if (!info.Save(reference_profile_file->Fd())) {
88 LOG(WARNING) << "Could not save reference profile file";
89 return kErrorIO;
90 }
91
92 return kCompile;
93 }
94
95 class ScopedFlockList {
96 public:
ScopedFlockList(size_t size)97 explicit ScopedFlockList(size_t size) : flocks_(size) {}
98
99 // Will block until all the locks are acquired.
Init(const std::vector<std::string> & filenames,std::string * error)100 bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) {
101 for (size_t i = 0; i < filenames.size(); i++) {
102 flocks_[i] = LockedFile::Open(filenames[i].c_str(), O_RDWR, /* block= */ true, error);
103 if (flocks_[i].get() == nullptr) {
104 *error += " (index=" + std::to_string(i) + ")";
105 return false;
106 }
107 }
108 return true;
109 }
110
111 // Will block until all the locks are acquired.
Init(const std::vector<int> & fds,std::string * error)112 bool Init(const std::vector<int>& fds, /* out */ std::string* error) {
113 for (size_t i = 0; i < fds.size(); i++) {
114 DCHECK_GE(fds[i], 0);
115 flocks_[i] = LockedFile::DupOf(fds[i], "profile-file",
116 /* read_only_mode= */ 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
Get() const125 const std::vector<ScopedFlock>& Get() const { return flocks_; }
126
127 private:
128 std::vector<ScopedFlock> flocks_;
129 };
130
ProcessProfiles(const std::vector<int> & profile_files_fd,int reference_profile_file_fd,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,bool store_aggregation_counters)131 ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
132 const std::vector<int>& profile_files_fd,
133 int reference_profile_file_fd,
134 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
135 bool store_aggregation_counters) {
136 DCHECK_GE(reference_profile_file_fd, 0);
137
138 std::string error;
139 ScopedFlockList profile_files(profile_files_fd.size());
140 if (!profile_files.Init(profile_files_fd, &error)) {
141 LOG(WARNING) << "Could not lock profile files: " << error;
142 return kErrorCannotLock;
143 }
144
145 // The reference_profile_file is opened in read/write mode because it's
146 // cleared after processing.
147 ScopedFlock reference_profile_file = LockedFile::DupOf(reference_profile_file_fd,
148 "reference-profile",
149 /* read_only_mode= */ false,
150 &error);
151 if (reference_profile_file.get() == nullptr) {
152 LOG(WARNING) << "Could not lock reference profiled files: " << error;
153 return kErrorCannotLock;
154 }
155
156 return ProcessProfilesInternal(profile_files.Get(),
157 reference_profile_file,
158 filter_fn,
159 store_aggregation_counters);
160 }
161
ProcessProfiles(const std::vector<std::string> & profile_files,const std::string & reference_profile_file,const ProfileCompilationInfo::ProfileLoadFilterFn & filter_fn,bool store_aggregation_counters)162 ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
163 const std::vector<std::string>& profile_files,
164 const std::string& reference_profile_file,
165 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
166 bool store_aggregation_counters) {
167 std::string error;
168
169 ScopedFlockList profile_files_list(profile_files.size());
170 if (!profile_files_list.Init(profile_files, &error)) {
171 LOG(WARNING) << "Could not lock profile files: " << error;
172 return kErrorCannotLock;
173 }
174
175 ScopedFlock locked_reference_profile_file = LockedFile::Open(
176 reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error);
177 if (locked_reference_profile_file.get() == nullptr) {
178 LOG(WARNING) << "Could not lock reference profile files: " << error;
179 return kErrorCannotLock;
180 }
181
182 return ProcessProfilesInternal(profile_files_list.Get(),
183 locked_reference_profile_file,
184 filter_fn,
185 store_aggregation_counters);
186 }
187
188 } // namespace art
189