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 | c90bc92 | 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" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 24 | #include "base/enums.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 25 | #include "base/systrace.h" |
Calin Juravle | 050fb1b | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 26 | #include "base/time_utils.h" |
| 27 | #include "compiler_filter.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 28 | #include "oat_file_manager.h" |
Calin Juravle | 050fb1b | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 29 | #include "scoped_thread_state_change.h" |
| 30 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 34 | ProfileSaver* ProfileSaver::instance_ = nullptr; |
| 35 | pthread_t ProfileSaver::profiler_pthread_ = 0U; |
| 36 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 37 | ProfileSaver::ProfileSaver(const ProfileSaverOptions& options, |
| 38 | const std::string& output_filename, |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 39 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 40 | const std::vector<std::string>& code_paths, |
| 41 | const std::string& foreign_dex_profile_path, |
| 42 | const std::string& app_data_dir) |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 43 | : jit_code_cache_(jit_code_cache), |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 44 | foreign_dex_profile_path_(foreign_dex_profile_path), |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 45 | shutting_down_(false), |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 46 | last_save_number_of_methods_(0), |
Calin Juravle | 698f4d1 | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 47 | last_save_number_of_classes_(0), |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 48 | last_time_ns_saver_woke_up_(0), |
| 49 | jit_activity_notifications_(0), |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 50 | wait_lock_("ProfileSaver wait lock"), |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 51 | period_condition_("ProfileSaver period condition", wait_lock_), |
| 52 | total_bytes_written_(0), |
| 53 | total_number_of_writes_(0), |
| 54 | total_number_of_code_cache_queries_(0), |
| 55 | total_number_of_skipped_writes_(0), |
| 56 | total_number_of_failed_writes_(0), |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 57 | total_ms_of_sleep_(0), |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 58 | total_ns_of_work_(0), |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 59 | total_number_of_foreign_dex_marks_(0), |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 60 | max_number_of_profile_entries_cached_(0), |
| 61 | total_number_of_hot_spikes_(0), |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 62 | total_number_of_wake_ups_(0), |
| 63 | options_(options) { |
| 64 | DCHECK(options_.IsEnabled()); |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 65 | AddTrackedLocations(output_filename, app_data_dir, code_paths); |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 66 | if (!app_data_dir.empty()) { |
| 67 | // The application directory is used to determine which dex files are owned by app. |
| 68 | // Since it could be a symlink (e.g. /data/data instead of /data/user/0), and we |
| 69 | // don't have control over how the dex files are actually loaded (symlink or canonical path), |
| 70 | // store it's canonical form to be sure we use the same base when comparing. |
| 71 | UniqueCPtr<const char[]> app_data_dir_real_path(realpath(app_data_dir.c_str(), nullptr)); |
| 72 | if (app_data_dir_real_path != nullptr) { |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 73 | app_data_dirs_.emplace(app_data_dir_real_path.get()); |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 74 | } else { |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 75 | LOG(WARNING) << "Failed to get the real path for app dir: " << app_data_dir |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 76 | << ". The app dir will not be used to determine which dex files belong to the app"; |
| 77 | } |
| 78 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void ProfileSaver::Run() { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 82 | Thread* self = Thread::Current(); |
| 83 | |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 84 | // Fetch the resolved classes for the app images after sleeping for |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 85 | // options_.GetSaveResolvedClassesDelayMs(). |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 86 | // TODO(calin) This only considers the case of the primary profile file. |
| 87 | // Anything that gets loaded in the same VM will not have their resolved |
| 88 | // classes save (unless they started before the initial saving was done). |
| 89 | { |
| 90 | MutexLock mu(self, wait_lock_); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 91 | const uint64_t end_time = NanoTime() + MsToNs(options_.GetSaveResolvedClassesDelayMs()); |
Mathieu Chartier | a57305e | 2016-05-18 19:51:23 -0700 | [diff] [blame] | 92 | while (true) { |
| 93 | const uint64_t current_time = NanoTime(); |
| 94 | if (current_time >= end_time) { |
| 95 | break; |
| 96 | } |
| 97 | period_condition_.TimedWait(self, NsToMs(end_time - current_time), 0); |
| 98 | } |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 99 | total_ms_of_sleep_ += options_.GetSaveResolvedClassesDelayMs(); |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 100 | } |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 101 | FetchAndCacheResolvedClassesAndMethods(); |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 102 | |
| 103 | // Loop for the profiled methods. |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 104 | while (!ShuttingDown(self)) { |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 105 | uint64_t sleep_start = NanoTime(); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 106 | { |
Calin Juravle | 8fbea8e | 2016-05-18 15:49:36 -0700 | [diff] [blame] | 107 | uint64_t sleep_time = 0; |
| 108 | { |
| 109 | MutexLock mu(self, wait_lock_); |
| 110 | period_condition_.Wait(self); |
Calin Juravle | 5d04eb6 | 2016-05-25 18:09:53 +0100 | [diff] [blame] | 111 | sleep_time = NanoTime() - sleep_start; |
Calin Juravle | 8fbea8e | 2016-05-18 15:49:36 -0700 | [diff] [blame] | 112 | } |
| 113 | // Check if the thread was woken up for shutdown. |
| 114 | if (ShuttingDown(self)) { |
| 115 | break; |
| 116 | } |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 117 | total_number_of_wake_ups_++; |
| 118 | // We might have been woken up by a huge number of notifications to guarantee saving. |
| 119 | // If we didn't meet the minimum saving period go back to sleep (only if missed by |
| 120 | // a reasonable margin). |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 121 | uint64_t min_save_period_ns = MsToNs(options_.GetMinSavePeriodMs()); |
| 122 | while (min_save_period_ns * 0.9 > sleep_time) { |
Calin Juravle | 8fbea8e | 2016-05-18 15:49:36 -0700 | [diff] [blame] | 123 | { |
| 124 | MutexLock mu(self, wait_lock_); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 125 | period_condition_.TimedWait(self, NsToMs(min_save_period_ns - sleep_time), 0); |
Calin Juravle | 5d04eb6 | 2016-05-25 18:09:53 +0100 | [diff] [blame] | 126 | sleep_time = NanoTime() - sleep_start; |
Calin Juravle | 8fbea8e | 2016-05-18 15:49:36 -0700 | [diff] [blame] | 127 | } |
| 128 | // Check if the thread was woken up for shutdown. |
| 129 | if (ShuttingDown(self)) { |
| 130 | break; |
| 131 | } |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 132 | total_number_of_wake_ups_++; |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 133 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 134 | } |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 135 | total_ms_of_sleep_ += NsToMs(NanoTime() - sleep_start); |
| 136 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 137 | if (ShuttingDown(self)) { |
| 138 | break; |
| 139 | } |
| 140 | |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 141 | uint16_t new_methods = 0; |
| 142 | uint64_t start_work = NanoTime(); |
| 143 | bool profile_saved_to_disk = ProcessProfilingInfo(&new_methods); |
| 144 | // Update the notification counter based on result. Note that there might be contention on this |
| 145 | // but we don't care about to be 100% precise. |
| 146 | if (!profile_saved_to_disk) { |
| 147 | // If we didn't save to disk it may be because we didn't have enough new methods. |
| 148 | // Set the jit activity notifications to new_methods so we can wake up earlier if needed. |
| 149 | jit_activity_notifications_ = new_methods; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 150 | } |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 151 | total_ns_of_work_ += NanoTime() - start_work; |
| 152 | } |
| 153 | } |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 154 | |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 155 | void ProfileSaver::NotifyJitActivity() { |
| 156 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 157 | if (instance_ == nullptr || instance_->shutting_down_) { |
| 158 | return; |
| 159 | } |
| 160 | instance_->NotifyJitActivityInternal(); |
| 161 | } |
| 162 | |
| 163 | void ProfileSaver::WakeUpSaver() { |
| 164 | jit_activity_notifications_ = 0; |
| 165 | last_time_ns_saver_woke_up_ = NanoTime(); |
| 166 | period_condition_.Signal(Thread::Current()); |
| 167 | } |
| 168 | |
| 169 | void ProfileSaver::NotifyJitActivityInternal() { |
| 170 | // Unlikely to overflow but if it happens, |
| 171 | // we would have waken up the saver long before that. |
| 172 | jit_activity_notifications_++; |
| 173 | // Note that we are not as precise as we could be here but we don't want to wake the saver |
| 174 | // every time we see a hot method. |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 175 | if (jit_activity_notifications_ > options_.GetMinNotificationBeforeWake()) { |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 176 | MutexLock wait_mutex(Thread::Current(), wait_lock_); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 177 | if ((NanoTime() - last_time_ns_saver_woke_up_) > MsToNs(options_.GetMinSavePeriodMs())) { |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 178 | WakeUpSaver(); |
| 179 | } |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 180 | } else if (jit_activity_notifications_ > options_.GetMaxNotificationBeforeWake()) { |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 181 | // Make sure to wake up the saver if we see a spike in the number of notifications. |
| 182 | // This is a precaution to avoid "loosing" a big number of methods in case |
| 183 | // this is a spike with no jit after. |
| 184 | total_number_of_hot_spikes_++; |
| 185 | MutexLock wait_mutex(Thread::Current(), wait_lock_); |
| 186 | WakeUpSaver(); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 190 | ProfileCompilationInfo* ProfileSaver::GetCachedProfiledInfo(const std::string& filename) { |
| 191 | auto info_it = profile_cache_.find(filename); |
| 192 | if (info_it == profile_cache_.end()) { |
| 193 | info_it = profile_cache_.Put(filename, ProfileCompilationInfo()); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 194 | } |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 195 | return &info_it->second; |
| 196 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 197 | |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 198 | // Get resolved methods that have a profile info or more than kStartupMethodSamples samples. |
| 199 | // Excludes native methods and classes in the boot image. |
| 200 | class GetMethodsVisitor : public ClassVisitor { |
| 201 | public: |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 202 | GetMethodsVisitor(std::vector<MethodReference>* methods, uint32_t startup_method_samples) |
| 203 | : methods_(methods), |
| 204 | startup_method_samples_(startup_method_samples) {} |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 205 | |
| 206 | virtual bool operator()(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_) { |
| 207 | if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
| 208 | return true; |
| 209 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 210 | for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) { |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 211 | if (!method.IsNative()) { |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 212 | if (method.GetCounter() >= startup_method_samples_ || |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 213 | method.GetProfilingInfo(kRuntimePointerSize) != nullptr) { |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 214 | // Have samples, add to profile. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame^] | 215 | const DexFile* dex_file = |
| 216 | method.GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetDexFile(); |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 217 | methods_->push_back(MethodReference(dex_file, method.GetDexMethodIndex())); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | private: |
| 225 | std::vector<MethodReference>* const methods_; |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 226 | uint32_t startup_method_samples_; |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | void ProfileSaver::FetchAndCacheResolvedClassesAndMethods() { |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 230 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 231 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 232 | std::set<DexCacheResolvedClasses> resolved_classes = |
| 233 | class_linker->GetResolvedClasses(/*ignore boot classes*/ true); |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 234 | |
| 235 | std::vector<MethodReference> methods; |
| 236 | { |
| 237 | ScopedTrace trace2("Get hot methods"); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 238 | GetMethodsVisitor visitor(&methods, options_.GetStartupMethodSamples()); |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 239 | ScopedObjectAccess soa(Thread::Current()); |
| 240 | class_linker->VisitClasses(&visitor); |
| 241 | VLOG(profiler) << "Methods with samples greater than " |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 242 | << options_.GetStartupMethodSamples() << " = " << methods.size(); |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 243 | } |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 244 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 245 | uint64_t total_number_of_profile_entries_cached = 0; |
Mathieu Chartier | b384e5e | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 246 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 247 | for (const auto& it : tracked_dex_base_locations_) { |
Mathieu Chartier | b384e5e | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 248 | std::set<DexCacheResolvedClasses> resolved_classes_for_location; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 249 | const std::string& filename = it.first; |
| 250 | const std::set<std::string>& locations = it.second; |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 251 | std::vector<MethodReference> methods_for_location; |
| 252 | for (const MethodReference& ref : methods) { |
| 253 | if (locations.find(ref.dex_file->GetBaseLocation()) != locations.end()) { |
| 254 | methods_for_location.push_back(ref); |
| 255 | } |
| 256 | } |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 257 | for (const DexCacheResolvedClasses& classes : resolved_classes) { |
Mathieu Chartier | b384e5e | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 258 | if (locations.find(classes.GetBaseLocation()) != locations.end()) { |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 259 | VLOG(profiler) << "Added " << classes.GetClasses().size() << " classes for location " |
| 260 | << classes.GetBaseLocation() << " (" << classes.GetDexLocation() << ")"; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 261 | resolved_classes_for_location.insert(classes); |
Mathieu Chartier | b384e5e | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 262 | } else { |
| 263 | VLOG(profiler) << "Location not found " << classes.GetBaseLocation() |
| 264 | << " (" << classes.GetDexLocation() << ")"; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | ProfileCompilationInfo* info = GetCachedProfiledInfo(filename); |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 268 | info->AddMethodsAndClasses(methods_for_location, resolved_classes_for_location); |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 269 | total_number_of_profile_entries_cached += resolved_classes_for_location.size(); |
| 270 | } |
| 271 | max_number_of_profile_entries_cached_ = std::max( |
| 272 | max_number_of_profile_entries_cached_, |
| 273 | total_number_of_profile_entries_cached); |
| 274 | } |
| 275 | |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 276 | bool ProfileSaver::ProcessProfilingInfo(uint16_t* new_methods) { |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 277 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 278 | SafeMap<std::string, std::set<std::string>> tracked_locations; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 279 | { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 280 | // Make a copy so that we don't hold the lock while doing I/O. |
| 281 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 282 | tracked_locations = tracked_dex_base_locations_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 283 | } |
Calin Juravle | b9c1b9b | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 284 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 285 | bool profile_file_saved = false; |
| 286 | uint64_t total_number_of_profile_entries_cached = 0; |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 287 | *new_methods = 0; |
| 288 | |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 289 | for (const auto& it : tracked_locations) { |
| 290 | if (ShuttingDown(Thread::Current())) { |
| 291 | return true; |
| 292 | } |
| 293 | const std::string& filename = it.first; |
| 294 | const std::set<std::string>& locations = it.second; |
Calin Juravle | 9962962 | 2016-04-19 16:33:46 +0100 | [diff] [blame] | 295 | std::vector<MethodReference> methods; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 296 | { |
| 297 | ScopedObjectAccess soa(Thread::Current()); |
Calin Juravle | 9962962 | 2016-04-19 16:33:46 +0100 | [diff] [blame] | 298 | jit_code_cache_->GetProfiledMethods(locations, methods); |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 299 | total_number_of_code_cache_queries_++; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 300 | } |
Calin Juravle | b9c1b9b | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 301 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 302 | ProfileCompilationInfo* cached_info = GetCachedProfiledInfo(filename); |
| 303 | cached_info->AddMethodsAndClasses(methods, std::set<DexCacheResolvedClasses>()); |
Calin Juravle | 698f4d1 | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 304 | int64_t delta_number_of_methods = |
| 305 | cached_info->GetNumberOfMethods() - |
| 306 | static_cast<int64_t>(last_save_number_of_methods_); |
| 307 | int64_t delta_number_of_classes = |
| 308 | cached_info->GetNumberOfResolvedClasses() - |
| 309 | static_cast<int64_t>(last_save_number_of_classes_); |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 310 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 311 | if (delta_number_of_methods < options_.GetMinMethodsToSave() && |
| 312 | delta_number_of_classes < options_.GetMinClassesToSave()) { |
Calin Juravle | 698f4d1 | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 313 | VLOG(profiler) << "Not enough information to save to: " << filename |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 314 | << " Number of methods: " << delta_number_of_methods |
| 315 | << " Number of classes: " << delta_number_of_classes; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 316 | total_number_of_skipped_writes_++; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 317 | continue; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 318 | } |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 319 | *new_methods = std::max(static_cast<uint16_t>(delta_number_of_methods), *new_methods); |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 320 | uint64_t bytes_written; |
Calin Juravle | 5d1bd0a | 2016-03-24 20:33:22 +0000 | [diff] [blame] | 321 | // Force the save. In case the profile data is corrupted or the the profile |
| 322 | // has the wrong version this will "fix" the file to the correct format. |
| 323 | if (cached_info->MergeAndSave(filename, &bytes_written, /*force*/ true)) { |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 324 | last_save_number_of_methods_ = cached_info->GetNumberOfMethods(); |
Calin Juravle | 698f4d1 | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 325 | last_save_number_of_classes_ = cached_info->GetNumberOfResolvedClasses(); |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 326 | // Clear resolved classes. No need to store them around as |
| 327 | // they don't change after the first write. |
| 328 | cached_info->ClearResolvedClasses(); |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 329 | if (bytes_written > 0) { |
| 330 | total_number_of_writes_++; |
| 331 | total_bytes_written_ += bytes_written; |
Calin Juravle | 698f4d1 | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 332 | profile_file_saved = true; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 333 | } else { |
| 334 | // At this point we could still have avoided the write. |
| 335 | // We load and merge the data from the file lazily at its first ever |
| 336 | // save attempt. So, whatever we are trying to save could already be |
| 337 | // in the file. |
| 338 | total_number_of_skipped_writes_++; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 339 | } |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 340 | } else { |
| 341 | LOG(WARNING) << "Could not save profiling info to " << filename; |
| 342 | total_number_of_failed_writes_++; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 343 | } |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 344 | total_number_of_profile_entries_cached += |
| 345 | cached_info->GetNumberOfMethods() + |
| 346 | cached_info->GetNumberOfResolvedClasses(); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 347 | } |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 348 | max_number_of_profile_entries_cached_ = std::max( |
| 349 | max_number_of_profile_entries_cached_, |
| 350 | total_number_of_profile_entries_cached); |
| 351 | return profile_file_saved; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void* ProfileSaver::RunProfileSaverThread(void* arg) { |
| 355 | Runtime* runtime = Runtime::Current(); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 356 | |
Calin Juravle | f39f009 | 2016-04-28 12:59:33 +0100 | [diff] [blame] | 357 | bool attached = runtime->AttachCurrentThread("Profile Saver", |
| 358 | /*as_daemon*/true, |
| 359 | runtime->GetSystemThreadGroup(), |
| 360 | /*create_peer*/true); |
| 361 | if (!attached) { |
| 362 | CHECK(runtime->IsShuttingDown(Thread::Current())); |
| 363 | return nullptr; |
| 364 | } |
| 365 | |
| 366 | ProfileSaver* profile_saver = reinterpret_cast<ProfileSaver*>(arg); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 367 | profile_saver->Run(); |
| 368 | |
| 369 | runtime->DetachCurrentThread(); |
| 370 | VLOG(profiler) << "Profile saver shutdown"; |
| 371 | return nullptr; |
| 372 | } |
| 373 | |
Calin Juravle | 050fb1b | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 374 | static bool ShouldProfileLocation(const std::string& location) { |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 375 | OatFileManager& oat_manager = Runtime::Current()->GetOatFileManager(); |
| 376 | const OatFile* oat_file = oat_manager.FindOpenedOatFileFromDexLocation(location); |
Calin Juravle | 050fb1b | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 377 | if (oat_file == nullptr) { |
| 378 | // This can happen if we fallback to run code directly from the APK. |
| 379 | // Profile it with the hope that the background dexopt will get us back into |
| 380 | // a good state. |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 381 | VLOG(profiler) << "Asked to profile a location without an oat file:" << location; |
Calin Juravle | 050fb1b | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 382 | return true; |
| 383 | } |
| 384 | CompilerFilter::Filter filter = oat_file->GetCompilerFilter(); |
Calin Juravle | 6543933 | 2016-04-19 18:17:41 +0100 | [diff] [blame] | 385 | if ((filter == CompilerFilter::kSpeed) || (filter == CompilerFilter::kEverything)) { |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 386 | VLOG(profiler) |
Calin Juravle | 6543933 | 2016-04-19 18:17:41 +0100 | [diff] [blame] | 387 | << "Skip profiling oat file because it's already speed|everything compiled: " |
| 388 | << location << " oat location: " << oat_file->GetLocation(); |
Calin Juravle | 050fb1b | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 389 | return false; |
| 390 | } |
| 391 | return true; |
| 392 | } |
| 393 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 394 | void ProfileSaver::Start(const ProfileSaverOptions& options, |
| 395 | const std::string& output_filename, |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 396 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 397 | const std::vector<std::string>& code_paths, |
| 398 | const std::string& foreign_dex_profile_path, |
| 399 | const std::string& app_data_dir) { |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 400 | DCHECK(options.IsEnabled()); |
| 401 | DCHECK(Runtime::Current()->GetJit() != nullptr); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 402 | DCHECK(!output_filename.empty()); |
| 403 | DCHECK(jit_code_cache != nullptr); |
| 404 | |
Calin Juravle | 050fb1b | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 405 | std::vector<std::string> code_paths_to_profile; |
| 406 | |
| 407 | for (const std::string& location : code_paths) { |
| 408 | if (ShouldProfileLocation(location)) { |
| 409 | code_paths_to_profile.push_back(location); |
Calin Juravle | 050fb1b | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | if (code_paths_to_profile.empty()) { |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 413 | VLOG(profiler) << "No code paths should be profiled."; |
Calin Juravle | 050fb1b | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 414 | return; |
| 415 | } |
| 416 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 417 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 418 | if (instance_ != nullptr) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 419 | // If we already have an instance, make sure it uses the same jit_code_cache. |
| 420 | // This may be called multiple times via Runtime::registerAppInfo (e.g. for |
| 421 | // apps which share the same runtime). |
| 422 | DCHECK_EQ(instance_->jit_code_cache_, jit_code_cache); |
| 423 | // Add the code_paths to the tracked locations. |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 424 | instance_->AddTrackedLocations(output_filename, app_data_dir, code_paths_to_profile); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | |
| 428 | VLOG(profiler) << "Starting profile saver using output file: " << output_filename |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 429 | << ". Tracking: " << Join(code_paths_to_profile, ':'); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 430 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 431 | instance_ = new ProfileSaver(options, |
| 432 | output_filename, |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 433 | jit_code_cache, |
Calin Juravle | 0b79127 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 434 | code_paths_to_profile, |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 435 | foreign_dex_profile_path, |
| 436 | app_data_dir); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 437 | |
| 438 | // Create a new thread which does the saving. |
| 439 | CHECK_PTHREAD_CALL( |
| 440 | pthread_create, |
| 441 | (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)), |
| 442 | "Profile saver thread"); |
| 443 | } |
| 444 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 445 | void ProfileSaver::Stop(bool dump_info) { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 446 | ProfileSaver* profile_saver = nullptr; |
| 447 | pthread_t profiler_pthread = 0U; |
| 448 | |
| 449 | { |
| 450 | MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 451 | VLOG(profiler) << "Stopping profile saver thread"; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 452 | profile_saver = instance_; |
| 453 | profiler_pthread = profiler_pthread_; |
| 454 | if (instance_ == nullptr) { |
| 455 | DCHECK(false) << "Tried to stop a profile saver which was not started"; |
| 456 | return; |
| 457 | } |
| 458 | if (instance_->shutting_down_) { |
| 459 | DCHECK(false) << "Tried to stop the profile saver twice"; |
| 460 | return; |
| 461 | } |
| 462 | instance_->shutting_down_ = true; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 463 | if (dump_info) { |
| 464 | instance_->DumpInfo(LOG(INFO)); |
| 465 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | { |
| 469 | // Wake up the saver thread if it is sleeping to allow for a clean exit. |
| 470 | MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_); |
| 471 | profile_saver->period_condition_.Signal(Thread::Current()); |
| 472 | } |
| 473 | |
| 474 | // Wait for the saver thread to stop. |
| 475 | CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown"); |
| 476 | |
| 477 | { |
| 478 | MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_); |
| 479 | instance_ = nullptr; |
| 480 | profiler_pthread_ = 0U; |
| 481 | } |
| 482 | delete profile_saver; |
| 483 | } |
| 484 | |
| 485 | bool ProfileSaver::ShuttingDown(Thread* self) { |
| 486 | MutexLock mu(self, *Locks::profiler_lock_); |
| 487 | return shutting_down_; |
| 488 | } |
| 489 | |
| 490 | bool ProfileSaver::IsStarted() { |
| 491 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 492 | return instance_ != nullptr; |
| 493 | } |
| 494 | |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 495 | void ProfileSaver::AddTrackedLocations(const std::string& output_filename, |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 496 | const std::string& app_data_dir, |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 497 | const std::vector<std::string>& code_paths) { |
| 498 | auto it = tracked_dex_base_locations_.find(output_filename); |
| 499 | if (it == tracked_dex_base_locations_.end()) { |
| 500 | tracked_dex_base_locations_.Put(output_filename, |
| 501 | std::set<std::string>(code_paths.begin(), code_paths.end())); |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 502 | app_data_dirs_.insert(app_data_dir); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 503 | } else { |
| 504 | it->second.insert(code_paths.begin(), code_paths.end()); |
| 505 | } |
| 506 | } |
| 507 | |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 508 | void ProfileSaver::NotifyDexUse(const std::string& dex_location) { |
Calin Juravle | 050fb1b | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 509 | if (!ShouldProfileLocation(dex_location)) { |
| 510 | return; |
| 511 | } |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 512 | std::set<std::string> app_code_paths; |
| 513 | std::string foreign_dex_profile_path; |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 514 | std::set<std::string> app_data_dirs; |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 515 | { |
| 516 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 517 | if (instance_ == nullptr) { |
| 518 | return; |
| 519 | } |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 520 | // Make a copy so that we don't hold the lock while doing I/O. |
| 521 | for (const auto& it : instance_->tracked_dex_base_locations_) { |
| 522 | app_code_paths.insert(it.second.begin(), it.second.end()); |
| 523 | } |
| 524 | foreign_dex_profile_path = instance_->foreign_dex_profile_path_; |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 525 | app_data_dirs.insert(instance_->app_data_dirs_.begin(), instance_->app_data_dirs_.end()); |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 528 | bool mark_created = MaybeRecordDexUseInternal(dex_location, |
| 529 | app_code_paths, |
| 530 | foreign_dex_profile_path, |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 531 | app_data_dirs); |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 532 | if (mark_created) { |
| 533 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 534 | if (instance_ != nullptr) { |
| 535 | instance_->total_number_of_foreign_dex_marks_++; |
| 536 | } |
| 537 | } |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 540 | bool ProfileSaver::MaybeRecordDexUseInternal( |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 541 | const std::string& dex_location, |
| 542 | const std::set<std::string>& app_code_paths, |
| 543 | const std::string& foreign_dex_profile_path, |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 544 | const std::set<std::string>& app_data_dirs) { |
Calin Juravle | f529e9b | 2016-03-08 12:52:52 +0000 | [diff] [blame] | 545 | if (dex_location.empty()) { |
| 546 | LOG(WARNING) << "Asked to record foreign dex use with an empty dex location."; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 547 | return false; |
Calin Juravle | f529e9b | 2016-03-08 12:52:52 +0000 | [diff] [blame] | 548 | } |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 549 | if (foreign_dex_profile_path.empty()) { |
| 550 | LOG(WARNING) << "Asked to record foreign dex use without a valid profile path "; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 551 | return false; |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | UniqueCPtr<const char[]> dex_location_real_path(realpath(dex_location.c_str(), nullptr)); |
Calin Juravle | f529e9b | 2016-03-08 12:52:52 +0000 | [diff] [blame] | 555 | if (dex_location_real_path == nullptr) { |
| 556 | PLOG(WARNING) << "Could not get realpath for " << dex_location; |
| 557 | } |
| 558 | std::string dex_location_real_path_str((dex_location_real_path == nullptr) |
| 559 | ? dex_location.c_str() |
| 560 | : dex_location_real_path.get()); |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 561 | |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 562 | if (app_data_dirs.find(dex_location_real_path_str) != app_data_dirs.end()) { |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 563 | // The dex location is under the application folder. Nothing to record. |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 564 | return false; |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | if (app_code_paths.find(dex_location) != app_code_paths.end()) { |
| 568 | // The dex location belongs to the application code paths. Nothing to record. |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 569 | return false; |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 570 | } |
| 571 | // Do another round of checks with the real paths. |
| 572 | // Note that we could cache all the real locations in the saver (since it's an expensive |
| 573 | // operation). However we expect that app_code_paths is small (usually 1 element), and |
| 574 | // NotifyDexUse is called just a few times in the app lifetime. So we make the compromise |
| 575 | // to save some bytes of memory usage. |
| 576 | for (const auto& app_code_location : app_code_paths) { |
| 577 | UniqueCPtr<const char[]> real_app_code_location(realpath(app_code_location.c_str(), nullptr)); |
Calin Juravle | f529e9b | 2016-03-08 12:52:52 +0000 | [diff] [blame] | 578 | if (real_app_code_location == nullptr) { |
| 579 | PLOG(WARNING) << "Could not get realpath for " << app_code_location; |
| 580 | } |
| 581 | std::string real_app_code_location_str((real_app_code_location == nullptr) |
| 582 | ? app_code_location.c_str() |
| 583 | : real_app_code_location.get()); |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 584 | if (real_app_code_location_str == dex_location_real_path_str) { |
| 585 | // The dex location belongs to the application code paths. Nothing to record. |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 586 | return false; |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | // For foreign dex files we record a flag on disk. PackageManager will (potentially) take this |
| 591 | // into account when deciding how to optimize the loaded dex file. |
| 592 | // The expected flag name is the canonical path of the apk where '/' is substituted to '@'. |
| 593 | // (it needs to be kept in sync with |
| 594 | // frameworks/base/services/core/java/com/android/server/pm/PackageDexOptimizer.java) |
| 595 | std::replace(dex_location_real_path_str.begin(), dex_location_real_path_str.end(), '/', '@'); |
| 596 | std::string flag_path = foreign_dex_profile_path + "/" + dex_location_real_path_str; |
Richard Uhler | e18d619 | 2016-05-10 14:01:18 -0700 | [diff] [blame] | 597 | // We use O_RDONLY as the access mode because we must supply some access |
| 598 | // mode, and there is no access mode that means 'create but do not read' the |
| 599 | // file. We will not not actually read from the file. |
| 600 | int fd = TEMP_FAILURE_RETRY(open(flag_path.c_str(), |
| 601 | O_CREAT | O_RDONLY | O_EXCL | O_CLOEXEC | O_NOFOLLOW, 0)); |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 602 | if (fd != -1) { |
| 603 | if (close(fd) != 0) { |
| 604 | PLOG(WARNING) << "Could not close file after flagging foreign dex use " << flag_path; |
| 605 | } |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 606 | return true; |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 607 | } else { |
Richard Uhler | e18d619 | 2016-05-10 14:01:18 -0700 | [diff] [blame] | 608 | if (errno != EEXIST && errno != EACCES) { |
| 609 | // Another app could have already created the file, and selinux may not |
| 610 | // allow the read access to the file implied by the call to open. |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 611 | PLOG(WARNING) << "Could not create foreign dex use mark " << flag_path; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 612 | return false; |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 613 | } |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 614 | return true; |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 618 | void ProfileSaver::DumpInstanceInfo(std::ostream& os) { |
| 619 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 620 | if (instance_ != nullptr) { |
| 621 | instance_->DumpInfo(os); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | void ProfileSaver::DumpInfo(std::ostream& os) { |
| 626 | os << "ProfileSaver total_bytes_written=" << total_bytes_written_ << '\n' |
| 627 | << "ProfileSaver total_number_of_writes=" << total_number_of_writes_ << '\n' |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 628 | << "ProfileSaver total_number_of_code_cache_queries=" |
| 629 | << total_number_of_code_cache_queries_ << '\n' |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 630 | << "ProfileSaver total_number_of_skipped_writes=" << total_number_of_skipped_writes_ << '\n' |
| 631 | << "ProfileSaver total_number_of_failed_writes=" << total_number_of_failed_writes_ << '\n' |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 632 | << "ProfileSaver total_ms_of_sleep=" << total_ms_of_sleep_ << '\n' |
Calin Juravle | 050fb1b | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 633 | << "ProfileSaver total_ms_of_work=" << NsToMs(total_ns_of_work_) << '\n' |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 634 | << "ProfileSaver total_number_of_foreign_dex_marks=" |
| 635 | << total_number_of_foreign_dex_marks_ << '\n' |
| 636 | << "ProfileSaver max_number_profile_entries_cached=" |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 637 | << max_number_of_profile_entries_cached_ << '\n' |
| 638 | << "ProfileSaver total_number_of_hot_spikes=" << total_number_of_hot_spikes_ << '\n' |
| 639 | << "ProfileSaver total_number_of_wake_ups=" << total_number_of_wake_ups_ << '\n'; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 642 | |
| 643 | void ProfileSaver::ForceProcessProfiles() { |
| 644 | ProfileSaver* saver = nullptr; |
| 645 | { |
| 646 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 647 | saver = instance_; |
| 648 | } |
| 649 | // TODO(calin): this is not actually thread safe as the instance_ may have been deleted, |
| 650 | // but we only use this in testing when we now this won't happen. |
| 651 | // Refactor the way we handle the instance so that we don't end up in this situation. |
| 652 | if (saver != nullptr) { |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 653 | uint16_t new_methods; |
| 654 | saver->ProcessProfilingInfo(&new_methods); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
| 658 | bool ProfileSaver::HasSeenMethod(const std::string& profile, |
| 659 | const DexFile* dex_file, |
| 660 | uint16_t method_idx) { |
| 661 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 662 | if (instance_ != nullptr) { |
| 663 | ProfileCompilationInfo* info = instance_->GetCachedProfiledInfo(profile); |
| 664 | if (info != nullptr) { |
| 665 | return info->ContainsMethod(MethodReference(dex_file, method_idx)); |
| 666 | } |
| 667 | } |
| 668 | return false; |
| 669 | } |
| 670 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 671 | } // namespace art |