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