blob: b65bb43cc30a19a8b3528b40140750cb7a511c93 [file] [log] [blame]
Calin Juravle024160852016-02-23 12:00:03 +00001/*
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
David Sehrc431b9d2018-03-02 12:01:51 -080019#include "base/os.h"
Calin Juravle024160852016-02-23 12:00:03 +000020#include "base/unix_file/fd_file.h"
Calin Juravle024160852016-02-23 12:00:03 +000021
22namespace art {
23
Calin Juravlee02348c2016-03-29 20:33:33 +010024// Minimum number of new methods/classes that profiles
25// must contain to enable recompilation.
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -070026static constexpr const uint32_t kMinNewMethodsForCompilation = 100;
27static constexpr const uint32_t kMinNewMethodsPercentChangeForCompilation = 2;
28static constexpr const uint32_t kMinNewClassesForCompilation = 50;
29static constexpr const uint32_t kMinNewClassesPercentChangeForCompilation = 2;
30
Calin Juravle024160852016-02-23 12:00:03 +000031
32ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfilesInternal(
33 const std::vector<ScopedFlock>& profile_files,
Calin Juravlee10c1e22018-01-26 20:10:15 -080034 const ScopedFlock& reference_profile_file,
Calin Juravle3ee9cfd2018-12-11 13:38:35 -080035 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
36 bool store_aggregation_counters) {
Calin Juravle024160852016-02-23 12:00:03 +000037 DCHECK(!profile_files.empty());
38
Calin Juravle024160852016-02-23 12:00:03 +000039 ProfileCompilationInfo info;
Calin Juravlee02348c2016-03-29 20:33:33 +010040 // Load the reference profile.
Andreas Gampe9b031f72018-10-04 11:03:34 -070041 if (!info.Load(reference_profile_file->Fd(), /*merge_classes=*/ true, filter_fn)) {
Calin Juravle024160852016-02-23 12:00:03 +000042 LOG(WARNING) << "Could not load reference profile file";
43 return kErrorBadProfiles;
44 }
45
Calin Juravle3ee9cfd2018-12-11 13:38:35 -080046 // 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
Calin Juravlee02348c2016-03-29 20:33:33 +010052 // 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++) {
Calin Juravlecea9e9d2017-03-23 19:04:59 -070058 ProfileCompilationInfo cur_info;
Andreas Gampe9b031f72018-10-04 11:03:34 -070059 if (!cur_info.Load(profile_files[i]->Fd(), /*merge_classes=*/ true, filter_fn)) {
Calin Juravlee02348c2016-03-29 20:33:33 +010060 LOG(WARNING) << "Could not load profile file at index " << i;
Calin Juravle024160852016-02-23 12:00:03 +000061 return kErrorBadProfiles;
62 }
Calin Juravlecea9e9d2017-03-23 19:04:59 -070063 if (!info.MergeWith(cur_info)) {
64 LOG(WARNING) << "Could not merge profile file at index " << i;
65 return kErrorBadProfiles;
66 }
Calin Juravle024160852016-02-23 12:00:03 +000067 }
Calin Juravlee02348c2016-03-29 20:33:33 +010068
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -070069 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);
Calin Juravlee02348c2016-03-29 20:33:33 +010075 // Check if there is enough new information added by the current profiles.
Shubham Ajmera9ab6e1d2017-09-25 18:40:54 -070076 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)) {
Calin Juravlee02348c2016-03-29 20:33:33 +010079 return kSkipCompilation;
80 }
81
Calin Juravle024160852016-02-23 12:00:03 +000082 // We were successful in merging all profile information. Update the reference profile.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010083 if (!reference_profile_file->ClearContent()) {
Calin Juravle024160852016-02-23 12:00:03 +000084 PLOG(WARNING) << "Could not clear reference profile file";
85 return kErrorIO;
86 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010087 if (!info.Save(reference_profile_file->Fd())) {
Calin Juravle024160852016-02-23 12:00:03 +000088 LOG(WARNING) << "Could not save reference profile file";
89 return kErrorIO;
90 }
91
92 return kCompile;
93}
94
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010095class ScopedFlockList {
Calin Juravle024160852016-02-23 12:00:03 +000096 public:
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010097 explicit ScopedFlockList(size_t size) : flocks_(size) {}
Calin Juravle024160852016-02-23 12:00:03 +000098
99 // Will block until all the locks are acquired.
100 bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) {
101 for (size_t i = 0; i < filenames.size(); i++) {
Andreas Gampe9b031f72018-10-04 11:03:34 -0700102 flocks_[i] = LockedFile::Open(filenames[i].c_str(), O_RDWR, /* block= */ true, error);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100103 if (flocks_[i].get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000104 *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.
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);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100115 flocks_[i] = LockedFile::DupOf(fds[i], "profile-file",
Andreas Gampe9b031f72018-10-04 11:03:34 -0700116 /* read_only_mode= */ true, error);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100117 if (flocks_[i].get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000118 *error += " (index=" + std::to_string(i) + ")";
119 return false;
120 }
121 }
122 return true;
123 }
124
125 const std::vector<ScopedFlock>& Get() const { return flocks_; }
126
127 private:
128 std::vector<ScopedFlock> flocks_;
129};
130
131ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
132 const std::vector<int>& profile_files_fd,
Calin Juravlee10c1e22018-01-26 20:10:15 -0800133 int reference_profile_file_fd,
Calin Juravle3ee9cfd2018-12-11 13:38:35 -0800134 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
135 bool store_aggregation_counters) {
Calin Juravle024160852016-02-23 12:00:03 +0000136 DCHECK_GE(reference_profile_file_fd, 0);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100137
Calin Juravle024160852016-02-23 12:00:03 +0000138 std::string error;
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100139 ScopedFlockList profile_files(profile_files_fd.size());
140 if (!profile_files.Init(profile_files_fd, &error)) {
Calin Juravle024160852016-02-23 12:00:03 +0000141 LOG(WARNING) << "Could not lock profile files: " << error;
142 return kErrorCannotLock;
143 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100144
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",
Andreas Gampe9b031f72018-10-04 11:03:34 -0700149 /* read_only_mode= */ false,
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100150 &error);
151 if (reference_profile_file.get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000152 LOG(WARNING) << "Could not lock reference profiled files: " << error;
153 return kErrorCannotLock;
154 }
155
Calin Juravlee10c1e22018-01-26 20:10:15 -0800156 return ProcessProfilesInternal(profile_files.Get(),
157 reference_profile_file,
Calin Juravle3ee9cfd2018-12-11 13:38:35 -0800158 filter_fn,
159 store_aggregation_counters);
Calin Juravle024160852016-02-23 12:00:03 +0000160}
161
162ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
163 const std::vector<std::string>& profile_files,
Calin Juravlee10c1e22018-01-26 20:10:15 -0800164 const std::string& reference_profile_file,
Calin Juravle3ee9cfd2018-12-11 13:38:35 -0800165 const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
166 bool store_aggregation_counters) {
Calin Juravle024160852016-02-23 12:00:03 +0000167 std::string error;
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100168
169 ScopedFlockList profile_files_list(profile_files.size());
170 if (!profile_files_list.Init(profile_files, &error)) {
Calin Juravle024160852016-02-23 12:00:03 +0000171 LOG(WARNING) << "Could not lock profile files: " << error;
172 return kErrorCannotLock;
173 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100174
175 ScopedFlock locked_reference_profile_file = LockedFile::Open(
Andreas Gampe9b031f72018-10-04 11:03:34 -0700176 reference_profile_file.c_str(), O_RDWR, /* block= */ true, &error);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100177 if (locked_reference_profile_file.get() == nullptr) {
Calin Juravle024160852016-02-23 12:00:03 +0000178 LOG(WARNING) << "Could not lock reference profile files: " << error;
179 return kErrorCannotLock;
180 }
181
Calin Juravlee10c1e22018-01-26 20:10:15 -0800182 return ProcessProfilesInternal(profile_files_list.Get(),
183 locked_reference_profile_file,
Calin Juravle3ee9cfd2018-12-11 13:38:35 -0800184 filter_fn,
185 store_aggregation_counters);
Calin Juravle024160852016-02-23 12:00:03 +0000186}
187
188} // namespace art