Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 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/unix_file/fd_file.h" |
| 20 | #include "os.h" |
| 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | // Minimum number of new methods that profiles must contain to enable recompilation. |
| 25 | static constexpr const uint32_t kMinNewMethodsForCompilation = 10; |
| 26 | |
| 27 | ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfilesInternal( |
| 28 | const std::vector<ScopedFlock>& profile_files, |
| 29 | const ScopedFlock& reference_profile_file) { |
| 30 | DCHECK(!profile_files.empty()); |
| 31 | |
| 32 | std::vector<ProfileCompilationInfo> new_info(profile_files.size()); |
| 33 | bool should_compile = false; |
| 34 | // Read the main profile files. |
| 35 | for (size_t i = 0; i < new_info.size(); i++) { |
| 36 | if (!new_info[i].Load(profile_files[i].GetFile()->Fd())) { |
| 37 | LOG(WARNING) << "Could not load profile file at index " << i; |
| 38 | return kErrorBadProfiles; |
| 39 | } |
| 40 | // Do we have enough new profiled methods that will make the compilation worthwhile? |
| 41 | should_compile |= (new_info[i].GetNumberOfMethods() > kMinNewMethodsForCompilation); |
| 42 | } |
| 43 | |
| 44 | if (!should_compile) { |
| 45 | return kSkipCompilation; |
| 46 | } |
| 47 | |
| 48 | // Merge information. |
| 49 | ProfileCompilationInfo info; |
| 50 | if (!info.Load(reference_profile_file.GetFile()->Fd())) { |
| 51 | LOG(WARNING) << "Could not load reference profile file"; |
| 52 | return kErrorBadProfiles; |
| 53 | } |
| 54 | |
| 55 | for (size_t i = 0; i < new_info.size(); i++) { |
| 56 | // Merge all data into a single object. |
| 57 | if (!info.Load(new_info[i])) { |
| 58 | LOG(WARNING) << "Could not merge profile data at index " << i; |
| 59 | return kErrorBadProfiles; |
| 60 | } |
| 61 | } |
| 62 | // We were successful in merging all profile information. Update the reference profile. |
| 63 | if (!reference_profile_file.GetFile()->ClearContent()) { |
| 64 | PLOG(WARNING) << "Could not clear reference profile file"; |
| 65 | return kErrorIO; |
| 66 | } |
| 67 | if (!info.Save(reference_profile_file.GetFile()->Fd())) { |
| 68 | LOG(WARNING) << "Could not save reference profile file"; |
| 69 | return kErrorIO; |
| 70 | } |
| 71 | |
| 72 | return kCompile; |
| 73 | } |
| 74 | |
| 75 | static bool InitFlock(const std::string& filename, ScopedFlock& flock, std::string* error) { |
| 76 | return flock.Init(filename.c_str(), O_RDWR, /* block */ true, error); |
| 77 | } |
| 78 | |
| 79 | static bool InitFlock(int fd, ScopedFlock& flock, std::string* error) { |
| 80 | DCHECK_GE(fd, 0); |
| 81 | // We do not own the descriptor, so disable auto-close and don't check usage. |
| 82 | File file(fd, false); |
| 83 | file.DisableAutoClose(); |
| 84 | return flock.Init(&file, error); |
| 85 | } |
| 86 | |
| 87 | class ScopedCollectionFlock { |
| 88 | public: |
| 89 | explicit ScopedCollectionFlock(size_t size) : flocks_(size) {} |
| 90 | |
| 91 | // Will block until all the locks are acquired. |
| 92 | bool Init(const std::vector<std::string>& filenames, /* out */ std::string* error) { |
| 93 | for (size_t i = 0; i < filenames.size(); i++) { |
| 94 | if (!InitFlock(filenames[i], flocks_[i], error)) { |
| 95 | *error += " (index=" + std::to_string(i) + ")"; |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | // Will block until all the locks are acquired. |
| 103 | bool Init(const std::vector<int>& fds, /* out */ std::string* error) { |
| 104 | for (size_t i = 0; i < fds.size(); i++) { |
| 105 | DCHECK_GE(fds[i], 0); |
| 106 | if (!InitFlock(fds[i], flocks_[i], error)) { |
| 107 | *error += " (index=" + std::to_string(i) + ")"; |
| 108 | return false; |
| 109 | } |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | const std::vector<ScopedFlock>& Get() const { return flocks_; } |
| 115 | |
| 116 | private: |
| 117 | std::vector<ScopedFlock> flocks_; |
| 118 | }; |
| 119 | |
| 120 | ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles( |
| 121 | const std::vector<int>& profile_files_fd, |
| 122 | int reference_profile_file_fd) { |
| 123 | DCHECK_GE(reference_profile_file_fd, 0); |
| 124 | std::string error; |
| 125 | ScopedCollectionFlock profile_files_flocks(profile_files_fd.size()); |
| 126 | if (!profile_files_flocks.Init(profile_files_fd, &error)) { |
| 127 | LOG(WARNING) << "Could not lock profile files: " << error; |
| 128 | return kErrorCannotLock; |
| 129 | } |
| 130 | ScopedFlock reference_profile_file_flock; |
| 131 | if (!InitFlock(reference_profile_file_fd, reference_profile_file_flock, &error)) { |
| 132 | LOG(WARNING) << "Could not lock reference profiled files: " << error; |
| 133 | return kErrorCannotLock; |
| 134 | } |
| 135 | |
| 136 | return ProcessProfilesInternal(profile_files_flocks.Get(), |
| 137 | reference_profile_file_flock); |
| 138 | } |
| 139 | |
| 140 | ProfileAssistant::ProcessingResult ProfileAssistant::ProcessProfiles( |
| 141 | const std::vector<std::string>& profile_files, |
| 142 | const std::string& reference_profile_file) { |
| 143 | std::string error; |
| 144 | ScopedCollectionFlock profile_files_flocks(profile_files.size()); |
| 145 | if (!profile_files_flocks.Init(profile_files, &error)) { |
| 146 | LOG(WARNING) << "Could not lock profile files: " << error; |
| 147 | return kErrorCannotLock; |
| 148 | } |
| 149 | ScopedFlock reference_profile_file_flock; |
| 150 | if (!InitFlock(reference_profile_file, reference_profile_file_flock, &error)) { |
| 151 | LOG(WARNING) << "Could not lock reference profile files: " << error; |
| 152 | return kErrorCannotLock; |
| 153 | } |
| 154 | |
| 155 | return ProcessProfilesInternal(profile_files_flocks.Get(), |
| 156 | reference_profile_file_flock); |
| 157 | } |
| 158 | |
| 159 | } // namespace art |