Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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_saver.h" |
| 18 | |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <fcntl.h> |
| 22 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 24 | #include "base/systrace.h" |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 25 | #include "base/time_utils.h" |
| 26 | #include "compiler_filter.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 27 | #include "oat_file_manager.h" |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 28 | #include "scoped_thread_state_change.h" |
| 29 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 33 | // TODO: read the constants from ProfileOptions, |
| 34 | // Add a random delay each time we go to sleep so that we don't hammer the CPU |
| 35 | // with all profile savers running at the same time. |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 36 | static constexpr const uint64_t kRandomDelayMaxMs = 30 * 1000; // 30 seconds |
Calin Juravle | 0cdaa6c | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 37 | static constexpr const uint64_t kMaxBackoffMs = 10 * 60 * 1000; // 10 minutes |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 38 | static constexpr const uint64_t kSavePeriodMs = 20 * 1000; // 20 seconds |
Calin Juravle | c15e566 | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 39 | static constexpr const uint64_t kSaveResolvedClassesDelayMs = 2 * 1000; // 2 seconds |
Calin Juravle | 0cdaa6c | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 40 | static constexpr const double kBackoffCoef = 2.0; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 41 | |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 42 | static constexpr const uint32_t kMinimumNumberOfMethodsToSave = 10; |
| 43 | static constexpr const uint32_t kMinimumNumberOfClassesToSave = 10; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 44 | |
| 45 | ProfileSaver* ProfileSaver::instance_ = nullptr; |
| 46 | pthread_t ProfileSaver::profiler_pthread_ = 0U; |
| 47 | |
| 48 | ProfileSaver::ProfileSaver(const std::string& output_filename, |
| 49 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 50 | const std::vector<std::string>& code_paths, |
| 51 | const std::string& foreign_dex_profile_path, |
| 52 | const std::string& app_data_dir) |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 53 | : jit_code_cache_(jit_code_cache), |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 54 | foreign_dex_profile_path_(foreign_dex_profile_path), |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 55 | shutting_down_(false), |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 56 | last_save_number_of_methods_(0), |
Calin Juravle | 0cdaa6c | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 57 | last_save_number_of_classes_(0), |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 58 | wait_lock_("ProfileSaver wait lock"), |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 59 | period_condition_("ProfileSaver period condition", wait_lock_), |
| 60 | total_bytes_written_(0), |
| 61 | total_number_of_writes_(0), |
| 62 | total_number_of_code_cache_queries_(0), |
| 63 | total_number_of_skipped_writes_(0), |
| 64 | total_number_of_failed_writes_(0), |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 65 | total_ms_of_sleep_(0), |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 66 | total_ns_of_work_(0), |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 67 | total_number_of_foreign_dex_marks_(0), |
| 68 | max_number_of_profile_entries_cached_(0) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 69 | AddTrackedLocations(output_filename, code_paths); |
Calin Juravle | c15e566 | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 70 | // We only need to save the resolved classes if the profile file is empty. |
| 71 | // Otherwise we must have already save them (we always do it during the first |
| 72 | // ever profile save). |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 73 | app_data_dir_ = ""; |
| 74 | if (!app_data_dir.empty()) { |
| 75 | // The application directory is used to determine which dex files are owned by app. |
| 76 | // Since it could be a symlink (e.g. /data/data instead of /data/user/0), and we |
| 77 | // don't have control over how the dex files are actually loaded (symlink or canonical path), |
| 78 | // store it's canonical form to be sure we use the same base when comparing. |
| 79 | UniqueCPtr<const char[]> app_data_dir_real_path(realpath(app_data_dir.c_str(), nullptr)); |
| 80 | if (app_data_dir_real_path != nullptr) { |
| 81 | app_data_dir_.assign(app_data_dir_real_path.get()); |
| 82 | } else { |
| 83 | LOG(WARNING) << "Failed to get the real path for app dir: " << app_data_dir_ |
| 84 | << ". The app dir will not be used to determine which dex files belong to the app"; |
| 85 | } |
| 86 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void ProfileSaver::Run() { |
| 90 | srand(MicroTime() * getpid()); |
| 91 | Thread* self = Thread::Current(); |
| 92 | |
| 93 | uint64_t save_period_ms = kSavePeriodMs; |
| 94 | VLOG(profiler) << "Save profiling information every " << save_period_ms << " ms"; |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 95 | bool cache_resolved_classes = true; |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 96 | while (!ShuttingDown(self)) { |
| 97 | uint64_t sleep_time_ms; |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 98 | if (cache_resolved_classes) { |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 99 | // Sleep less long for the first iteration since we want to record loaded classes shortly |
| 100 | // after app launch. |
Calin Juravle | c15e566 | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 101 | sleep_time_ms = kSaveResolvedClassesDelayMs; |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 102 | } else { |
| 103 | const uint64_t random_sleep_delay_ms = rand() % kRandomDelayMaxMs; |
| 104 | sleep_time_ms = save_period_ms + random_sleep_delay_ms; |
| 105 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 106 | { |
| 107 | MutexLock mu(self, wait_lock_); |
| 108 | period_condition_.TimedWait(self, sleep_time_ms, 0); |
| 109 | } |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 110 | total_ms_of_sleep_ += sleep_time_ms; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 111 | if (ShuttingDown(self)) { |
| 112 | break; |
| 113 | } |
| 114 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 115 | uint64_t start = NanoTime(); |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 116 | if (cache_resolved_classes) { |
| 117 | // TODO(calin) This only considers the case of the primary profile file. |
| 118 | // Anything that gets loaded in the same VM will not have their resolved |
| 119 | // classes save (unless they started before the initial saving was done). |
| 120 | FetchAndCacheResolvedClasses(); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 121 | } else { |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 122 | bool profile_saved_to_disk = ProcessProfilingInfo(); |
Calin Juravle | 28530da | 2016-03-31 15:29:54 +0100 | [diff] [blame^] | 123 | if (profile_saved_to_disk) { |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 124 | // Reset the period to the initial value as it's highly likely to JIT again. |
| 125 | save_period_ms = kSavePeriodMs; |
Calin Juravle | 0cdaa6c | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 126 | VLOG(profiler) << "Profile saver: saved something, period reset to: " << save_period_ms; |
Calin Juravle | 28530da | 2016-03-31 15:29:54 +0100 | [diff] [blame^] | 127 | } else { |
| 128 | // If we don't need to save now it is less likely that we will need to do |
| 129 | // so in the future. Increase the time between saves according to the |
| 130 | // kBackoffCoef, but make it no larger than kMaxBackoffMs. |
| 131 | save_period_ms = std::min(kMaxBackoffMs, |
| 132 | static_cast<uint64_t>(kBackoffCoef * save_period_ms)); |
| 133 | VLOG(profiler) << "Profile saver: nothing to save, delaying period to: " << save_period_ms; |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 134 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 135 | } |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 136 | cache_resolved_classes = false; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 137 | |
| 138 | total_ns_of_work_ += (NanoTime() - start); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 142 | ProfileCompilationInfo* ProfileSaver::GetCachedProfiledInfo(const std::string& filename) { |
| 143 | auto info_it = profile_cache_.find(filename); |
| 144 | if (info_it == profile_cache_.end()) { |
| 145 | info_it = profile_cache_.Put(filename, ProfileCompilationInfo()); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 146 | } |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 147 | return &info_it->second; |
| 148 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 149 | |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 150 | void ProfileSaver::FetchAndCacheResolvedClasses() { |
| 151 | ScopedTrace trace(__PRETTY_FUNCTION__); |
| 152 | |
| 153 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 154 | std::set<DexCacheResolvedClasses> resolved_classes = |
| 155 | class_linker->GetResolvedClasses(/*ignore boot classes*/ true); |
| 156 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 157 | uint64_t total_number_of_profile_entries_cached = 0; |
| 158 | for (const auto& it : tracked_dex_base_locations_) { |
| 159 | std::set<DexCacheResolvedClasses> resolved_classes_for_location; |
| 160 | const std::string& filename = it.first; |
| 161 | const std::set<std::string>& locations = it.second; |
| 162 | |
| 163 | for (const DexCacheResolvedClasses& classes : resolved_classes) { |
| 164 | if (locations.find(classes.GetDexLocation()) != locations.end()) { |
| 165 | resolved_classes_for_location.insert(classes); |
| 166 | } |
| 167 | } |
| 168 | ProfileCompilationInfo* info = GetCachedProfiledInfo(filename); |
| 169 | info->AddMethodsAndClasses(std::vector<ArtMethod*>(), resolved_classes_for_location); |
| 170 | total_number_of_profile_entries_cached += resolved_classes_for_location.size(); |
| 171 | } |
| 172 | max_number_of_profile_entries_cached_ = std::max( |
| 173 | max_number_of_profile_entries_cached_, |
| 174 | total_number_of_profile_entries_cached); |
| 175 | } |
| 176 | |
| 177 | bool ProfileSaver::ProcessProfilingInfo() { |
| 178 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 179 | SafeMap<std::string, std::set<std::string>> tracked_locations; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 180 | { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 181 | // Make a copy so that we don't hold the lock while doing I/O. |
| 182 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 183 | tracked_locations = tracked_dex_base_locations_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 184 | } |
Calin Juravle | c15e566 | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 185 | |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 186 | bool profile_file_saved = false; |
| 187 | uint64_t total_number_of_profile_entries_cached = 0; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 188 | for (const auto& it : tracked_locations) { |
| 189 | if (ShuttingDown(Thread::Current())) { |
| 190 | return true; |
| 191 | } |
| 192 | const std::string& filename = it.first; |
| 193 | const std::set<std::string>& locations = it.second; |
| 194 | std::vector<ArtMethod*> methods; |
| 195 | { |
| 196 | ScopedObjectAccess soa(Thread::Current()); |
| 197 | jit_code_cache_->GetCompiledArtMethods(locations, methods); |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 198 | total_number_of_code_cache_queries_++; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 199 | } |
Calin Juravle | c15e566 | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 200 | |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 201 | ProfileCompilationInfo* cached_info = GetCachedProfiledInfo(filename); |
| 202 | cached_info->AddMethodsAndClasses(methods, std::set<DexCacheResolvedClasses>()); |
Calin Juravle | 0cdaa6c | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 203 | int64_t delta_number_of_methods = |
| 204 | cached_info->GetNumberOfMethods() - |
| 205 | static_cast<int64_t>(last_save_number_of_methods_); |
| 206 | int64_t delta_number_of_classes = |
| 207 | cached_info->GetNumberOfResolvedClasses() - |
| 208 | static_cast<int64_t>(last_save_number_of_classes_); |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 209 | |
| 210 | if (delta_number_of_methods < kMinimumNumberOfMethodsToSave && |
| 211 | delta_number_of_classes < kMinimumNumberOfClassesToSave) { |
Calin Juravle | 0cdaa6c | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 212 | VLOG(profiler) << "Not enough information to save to: " << filename |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 213 | << " Nr of methods: " << delta_number_of_methods |
| 214 | << " Nr of classes: " << delta_number_of_classes; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 215 | total_number_of_skipped_writes_++; |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 216 | continue; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 217 | } |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 218 | uint64_t bytes_written; |
Calin Juravle | fe297a9 | 2016-03-24 20:33:22 +0000 | [diff] [blame] | 219 | // Force the save. In case the profile data is corrupted or the the profile |
| 220 | // has the wrong version this will "fix" the file to the correct format. |
| 221 | if (cached_info->MergeAndSave(filename, &bytes_written, /*force*/ true)) { |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 222 | last_save_number_of_methods_ = cached_info->GetNumberOfMethods(); |
Calin Juravle | 0cdaa6c | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 223 | last_save_number_of_classes_ = cached_info->GetNumberOfResolvedClasses(); |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 224 | // Clear resolved classes. No need to store them around as |
| 225 | // they don't change after the first write. |
| 226 | cached_info->ClearResolvedClasses(); |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 227 | if (bytes_written > 0) { |
| 228 | total_number_of_writes_++; |
| 229 | total_bytes_written_ += bytes_written; |
Calin Juravle | 0cdaa6c | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 230 | profile_file_saved = true; |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 231 | } else { |
| 232 | // At this point we could still have avoided the write. |
| 233 | // We load and merge the data from the file lazily at its first ever |
| 234 | // save attempt. So, whatever we are trying to save could already be |
| 235 | // in the file. |
| 236 | total_number_of_skipped_writes_++; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 237 | } |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 238 | } else { |
| 239 | LOG(WARNING) << "Could not save profiling info to " << filename; |
| 240 | total_number_of_failed_writes_++; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 241 | } |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 242 | total_number_of_profile_entries_cached += |
| 243 | cached_info->GetNumberOfMethods() + |
| 244 | cached_info->GetNumberOfResolvedClasses(); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 245 | } |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 246 | max_number_of_profile_entries_cached_ = std::max( |
| 247 | max_number_of_profile_entries_cached_, |
| 248 | total_number_of_profile_entries_cached); |
| 249 | return profile_file_saved; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void* ProfileSaver::RunProfileSaverThread(void* arg) { |
| 253 | Runtime* runtime = Runtime::Current(); |
| 254 | ProfileSaver* profile_saver = reinterpret_cast<ProfileSaver*>(arg); |
| 255 | |
| 256 | CHECK(runtime->AttachCurrentThread("Profile Saver", |
| 257 | /*as_daemon*/true, |
| 258 | runtime->GetSystemThreadGroup(), |
| 259 | /*create_peer*/true)); |
| 260 | profile_saver->Run(); |
| 261 | |
| 262 | runtime->DetachCurrentThread(); |
| 263 | VLOG(profiler) << "Profile saver shutdown"; |
| 264 | return nullptr; |
| 265 | } |
| 266 | |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 267 | static bool ShouldProfileLocation(const std::string& location) { |
| 268 | const OatFileManager& oat_manager = Runtime::Current()->GetOatFileManager(); |
| 269 | const OatFile* oat_file = oat_manager.FindOpenedOatFileFromOatLocation(location); |
| 270 | if (oat_file == nullptr) { |
| 271 | // This can happen if we fallback to run code directly from the APK. |
| 272 | // Profile it with the hope that the background dexopt will get us back into |
| 273 | // a good state. |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 274 | return true; |
| 275 | } |
| 276 | CompilerFilter::Filter filter = oat_file->GetCompilerFilter(); |
| 277 | if (filter == CompilerFilter::kSpeed || CompilerFilter::kEverything) { |
| 278 | return false; |
| 279 | } |
| 280 | return true; |
| 281 | } |
| 282 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 283 | void ProfileSaver::Start(const std::string& output_filename, |
| 284 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 285 | const std::vector<std::string>& code_paths, |
| 286 | const std::string& foreign_dex_profile_path, |
| 287 | const std::string& app_data_dir) { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 288 | DCHECK(Runtime::Current()->UseJit()); |
| 289 | DCHECK(!output_filename.empty()); |
| 290 | DCHECK(jit_code_cache != nullptr); |
| 291 | |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 292 | std::vector<std::string> code_paths_to_profile; |
| 293 | |
| 294 | for (const std::string& location : code_paths) { |
| 295 | if (ShouldProfileLocation(location)) { |
| 296 | code_paths_to_profile.push_back(location); |
| 297 | } else { |
| 298 | VLOG(profiler) |
| 299 | << "Skip profiling oat file because it's already speed|everything compiled:" |
| 300 | << location; |
| 301 | } |
| 302 | } |
| 303 | if (code_paths_to_profile.empty()) { |
| 304 | return; |
| 305 | } |
| 306 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 307 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 308 | if (instance_ != nullptr) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 309 | // If we already have an instance, make sure it uses the same jit_code_cache. |
| 310 | // This may be called multiple times via Runtime::registerAppInfo (e.g. for |
| 311 | // apps which share the same runtime). |
| 312 | DCHECK_EQ(instance_->jit_code_cache_, jit_code_cache); |
| 313 | // Add the code_paths to the tracked locations. |
| 314 | instance_->AddTrackedLocations(output_filename, code_paths); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | |
| 318 | VLOG(profiler) << "Starting profile saver using output file: " << output_filename |
| 319 | << ". Tracking: " << Join(code_paths, ':'); |
| 320 | |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 321 | instance_ = new ProfileSaver(output_filename, |
| 322 | jit_code_cache, |
| 323 | code_paths, |
| 324 | foreign_dex_profile_path, |
| 325 | app_data_dir); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 326 | |
| 327 | // Create a new thread which does the saving. |
| 328 | CHECK_PTHREAD_CALL( |
| 329 | pthread_create, |
| 330 | (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)), |
| 331 | "Profile saver thread"); |
| 332 | } |
| 333 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 334 | void ProfileSaver::Stop(bool dump_info) { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 335 | ProfileSaver* profile_saver = nullptr; |
| 336 | pthread_t profiler_pthread = 0U; |
| 337 | |
| 338 | { |
| 339 | MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 340 | VLOG(profiler) << "Stopping profile saver thread"; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 341 | profile_saver = instance_; |
| 342 | profiler_pthread = profiler_pthread_; |
| 343 | if (instance_ == nullptr) { |
| 344 | DCHECK(false) << "Tried to stop a profile saver which was not started"; |
| 345 | return; |
| 346 | } |
| 347 | if (instance_->shutting_down_) { |
| 348 | DCHECK(false) << "Tried to stop the profile saver twice"; |
| 349 | return; |
| 350 | } |
| 351 | instance_->shutting_down_ = true; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 352 | if (dump_info) { |
| 353 | instance_->DumpInfo(LOG(INFO)); |
| 354 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | { |
| 358 | // Wake up the saver thread if it is sleeping to allow for a clean exit. |
| 359 | MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_); |
| 360 | profile_saver->period_condition_.Signal(Thread::Current()); |
| 361 | } |
| 362 | |
| 363 | // Wait for the saver thread to stop. |
| 364 | CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown"); |
| 365 | |
| 366 | { |
| 367 | MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_); |
| 368 | instance_ = nullptr; |
| 369 | profiler_pthread_ = 0U; |
| 370 | } |
| 371 | delete profile_saver; |
| 372 | } |
| 373 | |
| 374 | bool ProfileSaver::ShuttingDown(Thread* self) { |
| 375 | MutexLock mu(self, *Locks::profiler_lock_); |
| 376 | return shutting_down_; |
| 377 | } |
| 378 | |
| 379 | bool ProfileSaver::IsStarted() { |
| 380 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 381 | return instance_ != nullptr; |
| 382 | } |
| 383 | |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 384 | void ProfileSaver::AddTrackedLocations(const std::string& output_filename, |
| 385 | const std::vector<std::string>& code_paths) { |
| 386 | auto it = tracked_dex_base_locations_.find(output_filename); |
| 387 | if (it == tracked_dex_base_locations_.end()) { |
| 388 | tracked_dex_base_locations_.Put(output_filename, |
| 389 | std::set<std::string>(code_paths.begin(), code_paths.end())); |
| 390 | } else { |
| 391 | it->second.insert(code_paths.begin(), code_paths.end()); |
| 392 | } |
| 393 | } |
| 394 | |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 395 | void ProfileSaver::NotifyDexUse(const std::string& dex_location) { |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 396 | if (!ShouldProfileLocation(dex_location)) { |
| 397 | return; |
| 398 | } |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 399 | std::set<std::string> app_code_paths; |
| 400 | std::string foreign_dex_profile_path; |
| 401 | std::string app_data_dir; |
| 402 | { |
| 403 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 404 | if (instance_ == nullptr) { |
| 405 | return; |
| 406 | } |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 407 | // Make a copy so that we don't hold the lock while doing I/O. |
| 408 | for (const auto& it : instance_->tracked_dex_base_locations_) { |
| 409 | app_code_paths.insert(it.second.begin(), it.second.end()); |
| 410 | } |
| 411 | foreign_dex_profile_path = instance_->foreign_dex_profile_path_; |
| 412 | app_data_dir = instance_->app_data_dir_; |
| 413 | } |
| 414 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 415 | bool mark_created = MaybeRecordDexUseInternal(dex_location, |
| 416 | app_code_paths, |
| 417 | foreign_dex_profile_path, |
| 418 | app_data_dir); |
| 419 | if (mark_created) { |
| 420 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 421 | if (instance_ != nullptr) { |
| 422 | instance_->total_number_of_foreign_dex_marks_++; |
| 423 | } |
| 424 | } |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 427 | bool ProfileSaver::MaybeRecordDexUseInternal( |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 428 | const std::string& dex_location, |
| 429 | const std::set<std::string>& app_code_paths, |
| 430 | const std::string& foreign_dex_profile_path, |
| 431 | const std::string& app_data_dir) { |
Calin Juravle | 1fae45f | 2016-03-08 12:52:52 +0000 | [diff] [blame] | 432 | if (dex_location.empty()) { |
| 433 | LOG(WARNING) << "Asked to record foreign dex use with an empty dex location."; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 434 | return false; |
Calin Juravle | 1fae45f | 2016-03-08 12:52:52 +0000 | [diff] [blame] | 435 | } |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 436 | if (foreign_dex_profile_path.empty()) { |
| 437 | LOG(WARNING) << "Asked to record foreign dex use without a valid profile path "; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 438 | return false; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | UniqueCPtr<const char[]> dex_location_real_path(realpath(dex_location.c_str(), nullptr)); |
Calin Juravle | 1fae45f | 2016-03-08 12:52:52 +0000 | [diff] [blame] | 442 | if (dex_location_real_path == nullptr) { |
| 443 | PLOG(WARNING) << "Could not get realpath for " << dex_location; |
| 444 | } |
| 445 | std::string dex_location_real_path_str((dex_location_real_path == nullptr) |
| 446 | ? dex_location.c_str() |
| 447 | : dex_location_real_path.get()); |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 448 | |
| 449 | if (dex_location_real_path_str.compare(0, app_data_dir.length(), app_data_dir) == 0) { |
| 450 | // The dex location is under the application folder. Nothing to record. |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 451 | return false; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | if (app_code_paths.find(dex_location) != app_code_paths.end()) { |
| 455 | // The dex location belongs to the application code paths. Nothing to record. |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 456 | return false; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 457 | } |
| 458 | // Do another round of checks with the real paths. |
| 459 | // Note that we could cache all the real locations in the saver (since it's an expensive |
| 460 | // operation). However we expect that app_code_paths is small (usually 1 element), and |
| 461 | // NotifyDexUse is called just a few times in the app lifetime. So we make the compromise |
| 462 | // to save some bytes of memory usage. |
| 463 | for (const auto& app_code_location : app_code_paths) { |
| 464 | UniqueCPtr<const char[]> real_app_code_location(realpath(app_code_location.c_str(), nullptr)); |
Calin Juravle | 1fae45f | 2016-03-08 12:52:52 +0000 | [diff] [blame] | 465 | if (real_app_code_location == nullptr) { |
| 466 | PLOG(WARNING) << "Could not get realpath for " << app_code_location; |
| 467 | } |
| 468 | std::string real_app_code_location_str((real_app_code_location == nullptr) |
| 469 | ? app_code_location.c_str() |
| 470 | : real_app_code_location.get()); |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 471 | if (real_app_code_location_str == dex_location_real_path_str) { |
| 472 | // The dex location belongs to the application code paths. Nothing to record. |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 473 | return false; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | // For foreign dex files we record a flag on disk. PackageManager will (potentially) take this |
| 478 | // into account when deciding how to optimize the loaded dex file. |
| 479 | // The expected flag name is the canonical path of the apk where '/' is substituted to '@'. |
| 480 | // (it needs to be kept in sync with |
| 481 | // frameworks/base/services/core/java/com/android/server/pm/PackageDexOptimizer.java) |
| 482 | std::replace(dex_location_real_path_str.begin(), dex_location_real_path_str.end(), '/', '@'); |
| 483 | std::string flag_path = foreign_dex_profile_path + "/" + dex_location_real_path_str; |
| 484 | // No need to give any sort of access to flag_path. The system has enough permissions |
| 485 | // to test for its existence. |
| 486 | int fd = TEMP_FAILURE_RETRY(open(flag_path.c_str(), O_CREAT | O_EXCL, 0)); |
| 487 | if (fd != -1) { |
| 488 | if (close(fd) != 0) { |
| 489 | PLOG(WARNING) << "Could not close file after flagging foreign dex use " << flag_path; |
| 490 | } |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 491 | return true; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 492 | } else { |
| 493 | if (errno != EEXIST) { |
| 494 | // Another app could have already created the file. |
| 495 | PLOG(WARNING) << "Could not create foreign dex use mark " << flag_path; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 496 | return false; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 497 | } |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 498 | return true; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 502 | void ProfileSaver::DumpInstanceInfo(std::ostream& os) { |
| 503 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 504 | if (instance_ != nullptr) { |
| 505 | instance_->DumpInfo(os); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | void ProfileSaver::DumpInfo(std::ostream& os) { |
| 510 | os << "ProfileSaver total_bytes_written=" << total_bytes_written_ << '\n' |
| 511 | << "ProfileSaver total_number_of_writes=" << total_number_of_writes_ << '\n' |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 512 | << "ProfileSaver total_number_of_code_cache_queries=" |
| 513 | << total_number_of_code_cache_queries_ << '\n' |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 514 | << "ProfileSaver total_number_of_skipped_writes=" << total_number_of_skipped_writes_ << '\n' |
| 515 | << "ProfileSaver total_number_of_failed_writes=" << total_number_of_failed_writes_ << '\n' |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 516 | << "ProfileSaver total_ms_of_sleep=" << total_ms_of_sleep_ << '\n' |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 517 | << "ProfileSaver total_ms_of_work=" << NsToMs(total_ns_of_work_) << '\n' |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 518 | << "ProfileSaver total_number_of_foreign_dex_marks=" |
| 519 | << total_number_of_foreign_dex_marks_ << '\n' |
| 520 | << "ProfileSaver max_number_profile_entries_cached=" |
| 521 | << max_number_of_profile_entries_cached_ << '\n'; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 524 | } // namespace art |