blob: a25460e4274a323175d06f39abbf94b6472a26c7 [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
19#include "base/unix_file/fd_file.h"
20#include "os.h"
21
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.
Calin Juravle024160852016-02-23 12:00:03 +000026static constexpr const uint32_t kMinNewMethodsForCompilation = 10;
Calin Juravlee02348c2016-03-29 20:33:33 +010027static constexpr const uint32_t kMinNewClassesForCompilation = 10;
Calin Juravle024160852016-02-23 12:00:03 +000028
29ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfilesInternal(
30 const std::vector<ScopedFlock>& profile_files,
31 const ScopedFlock& reference_profile_file) {
32 DCHECK(!profile_files.empty());
33
Calin Juravle024160852016-02-23 12:00:03 +000034 ProfileCompilationInfo info;
Calin Juravlee02348c2016-03-29 20:33:33 +010035 // Load the reference profile.
Calin Juravle024160852016-02-23 12:00:03 +000036 if (!info.Load(reference_profile_file.GetFile()->Fd())) {
37 LOG(WARNING) << "Could not load reference profile file";
38 return kErrorBadProfiles;
39 }
40
Calin Juravlee02348c2016-03-29 20:33:33 +010041 // Store the current state of the reference profile before merging with the current profiles.
42 uint32_t number_of_methods = info.GetNumberOfMethods();
43 uint32_t number_of_classes = info.GetNumberOfResolvedClasses();
44
45 // Merge all current profiles.
46 for (size_t i = 0; i < profile_files.size(); i++) {
47 if (!info.Load(profile_files[i].GetFile()->Fd())) {
48 LOG(WARNING) << "Could not load profile file at index " << i;
Calin Juravle024160852016-02-23 12:00:03 +000049 return kErrorBadProfiles;
50 }
51 }
Calin Juravlee02348c2016-03-29 20:33:33 +010052
53 // Check if there is enough new information added by the current profiles.
54 if (((info.GetNumberOfMethods() - number_of_methods) < kMinNewMethodsForCompilation) &&
55 ((info.GetNumberOfResolvedClasses() - number_of_classes) < kMinNewClassesForCompilation)) {
56 return kSkipCompilation;
57 }
58
Calin Juravle024160852016-02-23 12:00:03 +000059 // We were successful in merging all profile information. Update the reference profile.
60 if (!reference_profile_file.GetFile()->ClearContent()) {
61 PLOG(WARNING) << "Could not clear reference profile file";
62 return kErrorIO;
63 }
64 if (!info.Save(reference_profile_file.GetFile()->Fd())) {
65 LOG(WARNING) << "Could not save reference profile file";
66 return kErrorIO;
67 }
68
69 return kCompile;
70}
71
72static bool InitFlock(const std::string& filename, ScopedFlock& flock, std::string* error) {
73 return flock.Init(filename.c_str(), O_RDWR, /* block */ true, error);
74}
75
76static bool InitFlock(int fd, ScopedFlock& flock, std::string* error) {
77 DCHECK_GE(fd, 0);
78 // We do not own the descriptor, so disable auto-close and don't check usage.
79 File file(fd, false);
80 file.DisableAutoClose();
81 return flock.Init(&file, error);
82}
83
84class ScopedCollectionFlock {
85 public:
86 explicit ScopedCollectionFlock(size_t size) : flocks_(size) {}
87
88 // Will block until all the locks are acquired.
89 bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) {
90 for (size_t i = 0; i < filenames.size(); i++) {
91 if (!InitFlock(filenames[i], flocks_[i], error)) {
92 *error += " (index=" + std::to_string(i) + ")";
93 return false;
94 }
95 }
96 return true;
97 }
98
99 // Will block until all the locks are acquired.
100 bool Init(const std::vector<int>& fds, /* out */ std::string* error) {
101 for (size_t i = 0; i < fds.size(); i++) {
102 DCHECK_GE(fds[i], 0);
103 if (!InitFlock(fds[i], flocks_[i], error)) {
104 *error += " (index=" + std::to_string(i) + ")";
105 return false;
106 }
107 }
108 return true;
109 }
110
111 const std::vector<ScopedFlock>& Get() const { return flocks_; }
112
113 private:
114 std::vector<ScopedFlock> flocks_;
115};
116
117ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
118 const std::vector<int>& profile_files_fd,
119 int reference_profile_file_fd) {
120 DCHECK_GE(reference_profile_file_fd, 0);
121 std::string error;
122 ScopedCollectionFlock profile_files_flocks(profile_files_fd.size());
123 if (!profile_files_flocks.Init(profile_files_fd, &error)) {
124 LOG(WARNING) << "Could not lock profile files: " << error;
125 return kErrorCannotLock;
126 }
127 ScopedFlock reference_profile_file_flock;
128 if (!InitFlock(reference_profile_file_fd, reference_profile_file_flock, &error)) {
129 LOG(WARNING) << "Could not lock reference profiled files: " << error;
130 return kErrorCannotLock;
131 }
132
133 return ProcessProfilesInternal(profile_files_flocks.Get(),
134 reference_profile_file_flock);
135}
136
137ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles(
138 const std::vector<std::string>& profile_files,
139 const std::string& reference_profile_file) {
140 std::string error;
141 ScopedCollectionFlock profile_files_flocks(profile_files.size());
142 if (!profile_files_flocks.Init(profile_files, &error)) {
143 LOG(WARNING) << "Could not lock profile files: " << error;
144 return kErrorCannotLock;
145 }
146 ScopedFlock reference_profile_file_flock;
147 if (!InitFlock(reference_profile_file, reference_profile_file_flock, &error)) {
148 LOG(WARNING) << "Could not lock reference profile files: " << error;
149 return kErrorCannotLock;
150 }
151
152 return ProcessProfilesInternal(profile_files_flocks.Get(),
153 reference_profile_file_flock);
154}
155
156} // namespace art