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 <fcntl.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 20 | #include <sys/resource.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/types.h> |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 23 | |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 24 | #include "android-base/strings.h" |
| 25 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 26 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 27 | #include "base/enums.h" |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 28 | #include "base/logging.h" // For VLOG. |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 29 | #include "base/scoped_arena_containers.h" |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 30 | #include "base/stl_util.h" |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 31 | #include "base/systrace.h" |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 32 | #include "base/time_utils.h" |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 33 | #include "class_table-inl.h" |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 34 | #include "compiler_filter.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 35 | #include "dex/dex_file_loader.h" |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 36 | #include "dex_reference_collection.h" |
Mathieu Chartier | 3910037 | 2017-05-17 13:14:10 -0700 | [diff] [blame] | 37 | #include "gc/collector_type.h" |
| 38 | #include "gc/gc_cause.h" |
| 39 | #include "gc/scoped_gc_critical_section.h" |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 40 | #include "jit/profile_compilation_info.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 41 | #include "oat_file_manager.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 42 | #include "scoped_thread_state_change-inl.h" |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 43 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 44 | namespace art { |
| 45 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 46 | ProfileSaver* ProfileSaver::instance_ = nullptr; |
| 47 | pthread_t ProfileSaver::profiler_pthread_ = 0U; |
| 48 | |
Mathieu Chartier | 5494e5b | 2017-06-26 14:13:27 -0700 | [diff] [blame] | 49 | // At what priority to schedule the saver threads. 9 is the lowest foreground priority on device. |
| 50 | static constexpr int kProfileSaverPthreadPriority = 9; |
| 51 | |
| 52 | static void SetProfileSaverThreadPriority(pthread_t thread, int priority) { |
| 53 | #if defined(ART_TARGET_ANDROID) |
| 54 | int result = setpriority(PRIO_PROCESS, pthread_gettid_np(thread), priority); |
| 55 | if (result != 0) { |
| 56 | LOG(ERROR) << "Failed to setpriority to :" << priority; |
| 57 | } |
| 58 | #else |
| 59 | UNUSED(thread); |
| 60 | UNUSED(priority); |
| 61 | #endif |
| 62 | } |
| 63 | |
| 64 | static int GetDefaultThreadPriority() { |
| 65 | #if defined(ART_TARGET_ANDROID) |
| 66 | pthread_attr_t attr; |
| 67 | sched_param param; |
| 68 | pthread_attr_init(&attr); |
| 69 | pthread_attr_getschedparam(&attr, ¶m); |
| 70 | return param.sched_priority; |
| 71 | #else |
| 72 | return 0; |
| 73 | #endif |
| 74 | } |
| 75 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 76 | ProfileSaver::ProfileSaver(const ProfileSaverOptions& options, |
| 77 | const std::string& output_filename, |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 78 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 79 | const std::vector<std::string>& code_paths) |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 80 | : jit_code_cache_(jit_code_cache), |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 81 | shutting_down_(false), |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 82 | last_time_ns_saver_woke_up_(0), |
| 83 | jit_activity_notifications_(0), |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 84 | wait_lock_("ProfileSaver wait lock"), |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 85 | period_condition_("ProfileSaver period condition", wait_lock_), |
| 86 | total_bytes_written_(0), |
| 87 | total_number_of_writes_(0), |
| 88 | total_number_of_code_cache_queries_(0), |
| 89 | total_number_of_skipped_writes_(0), |
| 90 | total_number_of_failed_writes_(0), |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 91 | total_ms_of_sleep_(0), |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 92 | total_ns_of_work_(0), |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 93 | max_number_of_profile_entries_cached_(0), |
| 94 | total_number_of_hot_spikes_(0), |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 95 | total_number_of_wake_ups_(0), |
| 96 | options_(options) { |
| 97 | DCHECK(options_.IsEnabled()); |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 98 | AddTrackedLocations(output_filename, code_paths); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 101 | ProfileSaver::~ProfileSaver() { |
| 102 | for (auto& it : profile_cache_) { |
| 103 | delete it.second; |
| 104 | } |
| 105 | } |
| 106 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 107 | void ProfileSaver::Run() { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 108 | Thread* self = Thread::Current(); |
| 109 | |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 110 | // Fetch the resolved classes for the app images after sleeping for |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 111 | // options_.GetSaveResolvedClassesDelayMs(). |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 112 | // TODO(calin) This only considers the case of the primary profile file. |
| 113 | // Anything that gets loaded in the same VM will not have their resolved |
| 114 | // classes save (unless they started before the initial saving was done). |
| 115 | { |
| 116 | MutexLock mu(self, wait_lock_); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 117 | const uint64_t end_time = NanoTime() + MsToNs(options_.GetSaveResolvedClassesDelayMs()); |
Mathieu Chartier | 0ec065d | 2016-05-18 19:51:23 -0700 | [diff] [blame] | 118 | while (true) { |
| 119 | const uint64_t current_time = NanoTime(); |
| 120 | if (current_time >= end_time) { |
| 121 | break; |
| 122 | } |
| 123 | period_condition_.TimedWait(self, NsToMs(end_time - current_time), 0); |
| 124 | } |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 125 | total_ms_of_sleep_ += options_.GetSaveResolvedClassesDelayMs(); |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 126 | } |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 127 | FetchAndCacheResolvedClassesAndMethods(/*startup*/ true); |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 128 | |
| 129 | // Loop for the profiled methods. |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 130 | while (!ShuttingDown(self)) { |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 131 | uint64_t sleep_start = NanoTime(); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 132 | { |
Calin Juravle | 0233a41 | 2016-05-18 15:49:36 -0700 | [diff] [blame] | 133 | uint64_t sleep_time = 0; |
| 134 | { |
| 135 | MutexLock mu(self, wait_lock_); |
| 136 | period_condition_.Wait(self); |
Calin Juravle | dc85bd7 | 2016-05-25 18:09:53 +0100 | [diff] [blame] | 137 | sleep_time = NanoTime() - sleep_start; |
Calin Juravle | 0233a41 | 2016-05-18 15:49:36 -0700 | [diff] [blame] | 138 | } |
| 139 | // Check if the thread was woken up for shutdown. |
| 140 | if (ShuttingDown(self)) { |
| 141 | break; |
| 142 | } |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 143 | total_number_of_wake_ups_++; |
| 144 | // We might have been woken up by a huge number of notifications to guarantee saving. |
| 145 | // If we didn't meet the minimum saving period go back to sleep (only if missed by |
| 146 | // a reasonable margin). |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 147 | uint64_t min_save_period_ns = MsToNs(options_.GetMinSavePeriodMs()); |
| 148 | while (min_save_period_ns * 0.9 > sleep_time) { |
Calin Juravle | 0233a41 | 2016-05-18 15:49:36 -0700 | [diff] [blame] | 149 | { |
| 150 | MutexLock mu(self, wait_lock_); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 151 | period_condition_.TimedWait(self, NsToMs(min_save_period_ns - sleep_time), 0); |
Calin Juravle | dc85bd7 | 2016-05-25 18:09:53 +0100 | [diff] [blame] | 152 | sleep_time = NanoTime() - sleep_start; |
Calin Juravle | 0233a41 | 2016-05-18 15:49:36 -0700 | [diff] [blame] | 153 | } |
| 154 | // Check if the thread was woken up for shutdown. |
| 155 | if (ShuttingDown(self)) { |
| 156 | break; |
| 157 | } |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 158 | total_number_of_wake_ups_++; |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 159 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 160 | } |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 161 | total_ms_of_sleep_ += NsToMs(NanoTime() - sleep_start); |
| 162 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 163 | if (ShuttingDown(self)) { |
| 164 | break; |
| 165 | } |
| 166 | |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 167 | uint16_t number_of_new_methods = 0; |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 168 | uint64_t start_work = NanoTime(); |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 169 | bool profile_saved_to_disk = ProcessProfilingInfo(/*force_save*/false, &number_of_new_methods); |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 170 | // Update the notification counter based on result. Note that there might be contention on this |
| 171 | // but we don't care about to be 100% precise. |
| 172 | if (!profile_saved_to_disk) { |
| 173 | // If we didn't save to disk it may be because we didn't have enough new methods. |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 174 | // Set the jit activity notifications to number_of_new_methods so we can wake up earlier |
| 175 | // if needed. |
| 176 | jit_activity_notifications_ = number_of_new_methods; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 177 | } |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 178 | total_ns_of_work_ += NanoTime() - start_work; |
| 179 | } |
| 180 | } |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 181 | |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 182 | void ProfileSaver::NotifyJitActivity() { |
| 183 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 184 | if (instance_ == nullptr || instance_->shutting_down_) { |
| 185 | return; |
| 186 | } |
| 187 | instance_->NotifyJitActivityInternal(); |
| 188 | } |
| 189 | |
| 190 | void ProfileSaver::WakeUpSaver() { |
| 191 | jit_activity_notifications_ = 0; |
| 192 | last_time_ns_saver_woke_up_ = NanoTime(); |
| 193 | period_condition_.Signal(Thread::Current()); |
| 194 | } |
| 195 | |
| 196 | void ProfileSaver::NotifyJitActivityInternal() { |
| 197 | // Unlikely to overflow but if it happens, |
| 198 | // we would have waken up the saver long before that. |
| 199 | jit_activity_notifications_++; |
| 200 | // Note that we are not as precise as we could be here but we don't want to wake the saver |
| 201 | // every time we see a hot method. |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 202 | if (jit_activity_notifications_ > options_.GetMinNotificationBeforeWake()) { |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 203 | MutexLock wait_mutex(Thread::Current(), wait_lock_); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 204 | if ((NanoTime() - last_time_ns_saver_woke_up_) > MsToNs(options_.GetMinSavePeriodMs())) { |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 205 | WakeUpSaver(); |
Serguei Katkov | 87de9cf | 2016-08-01 17:47:04 +0700 | [diff] [blame] | 206 | } else if (jit_activity_notifications_ > options_.GetMaxNotificationBeforeWake()) { |
| 207 | // Make sure to wake up the saver if we see a spike in the number of notifications. |
| 208 | // This is a precaution to avoid losing a big number of methods in case |
| 209 | // this is a spike with no jit after. |
| 210 | total_number_of_hot_spikes_++; |
| 211 | WakeUpSaver(); |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 212 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Mathieu Chartier | 5494e5b | 2017-06-26 14:13:27 -0700 | [diff] [blame] | 216 | class ScopedDefaultPriority { |
| 217 | public: |
| 218 | explicit ScopedDefaultPriority(pthread_t thread) : thread_(thread) { |
| 219 | SetProfileSaverThreadPriority(thread_, GetDefaultThreadPriority()); |
| 220 | } |
| 221 | |
| 222 | ~ScopedDefaultPriority() { |
| 223 | SetProfileSaverThreadPriority(thread_, kProfileSaverPthreadPriority); |
| 224 | } |
| 225 | |
| 226 | private: |
| 227 | const pthread_t thread_; |
| 228 | }; |
| 229 | |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 230 | // GetClassLoadersVisitor takes a snapshot of the class loaders and stores them in the out |
| 231 | // class_loaders argument. Not affected by class unloading since there are no suspend points in |
| 232 | // the caller. |
| 233 | class GetClassLoadersVisitor : public ClassLoaderVisitor { |
| 234 | public: |
| 235 | explicit GetClassLoadersVisitor(VariableSizedHandleScope* hs, |
| 236 | std::vector<Handle<mirror::ClassLoader>>* class_loaders) |
| 237 | : hs_(hs), |
| 238 | class_loaders_(class_loaders) {} |
| 239 | |
| 240 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
| 241 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE { |
| 242 | class_loaders_->push_back(hs_->NewHandle(class_loader)); |
| 243 | } |
| 244 | |
| 245 | private: |
| 246 | VariableSizedHandleScope* const hs_; |
| 247 | std::vector<Handle<mirror::ClassLoader>>* const class_loaders_; |
| 248 | }; |
| 249 | |
| 250 | // GetClassesVisitor takes a snapshot of the loaded classes that we may want to visit and stores |
| 251 | // them in the out argument. Not affected by class unloading since there are no suspend points in |
| 252 | // the caller. |
| 253 | class GetClassesVisitor : public ClassVisitor { |
| 254 | public: |
| 255 | explicit GetClassesVisitor(bool profile_boot_class_path, |
| 256 | ScopedArenaVector<ObjPtr<mirror::Class>>* out) |
| 257 | : profile_boot_class_path_(profile_boot_class_path), |
| 258 | out_(out) {} |
| 259 | |
| 260 | virtual bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 261 | if (klass->IsProxyClass() || |
| 262 | klass->IsArrayClass() || |
| 263 | klass->IsPrimitive() || |
| 264 | !klass->IsResolved() || |
| 265 | klass->IsErroneousResolved() || |
| 266 | (!profile_boot_class_path_ && klass->GetClassLoader() == nullptr)) { |
| 267 | return true; |
| 268 | } |
| 269 | out_->push_back(klass); |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | private: |
| 274 | const bool profile_boot_class_path_; |
| 275 | ScopedArenaVector<ObjPtr<mirror::Class>>* const out_; |
| 276 | }; |
| 277 | |
| 278 | using MethodReferenceCollection = DexReferenceCollection<uint16_t, ScopedArenaAllocatorAdapter>; |
| 279 | using TypeReferenceCollection = DexReferenceCollection<dex::TypeIndex, |
| 280 | ScopedArenaAllocatorAdapter>; |
| 281 | |
| 282 | // Iterate over all of the loaded classes and visit each one. For each class, add it to the |
| 283 | // resolved_classes out argument if startup is true. |
| 284 | // Add methods to the hot_methods out argument if the number of samples is greater or equal to |
| 285 | // hot_method_sample_threshold, add it to sampled_methods if it has at least one sample. |
| 286 | static void SampleClassesAndExecutedMethods(pthread_t profiler_pthread, |
| 287 | bool profile_boot_class_path, |
| 288 | ScopedArenaAllocator* allocator, |
| 289 | uint32_t hot_method_sample_threshold, |
| 290 | bool startup, |
| 291 | TypeReferenceCollection* resolved_classes, |
| 292 | MethodReferenceCollection* hot_methods, |
| 293 | MethodReferenceCollection* sampled_methods) { |
| 294 | Thread* const self = Thread::Current(); |
| 295 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 296 | // Restore profile saver thread priority during the GC critical section. This helps prevent |
| 297 | // priority inversions blocking the GC for long periods of time. |
| 298 | std::unique_ptr<ScopedDefaultPriority> sdp; |
| 299 | // Only restore default priority if we are the profile saver thread. Other threads that call this |
| 300 | // are threads calling Stop and the signal catcher (for SIGUSR1). |
| 301 | if (pthread_self() == profiler_pthread) { |
| 302 | sdp.reset(new ScopedDefaultPriority(profiler_pthread)); |
| 303 | } |
| 304 | |
| 305 | // Do ScopedGCCriticalSection before acquiring mutator lock to prevent the GC running and |
| 306 | // blocking threads during thread root flipping. Since the GC is a background thread, blocking it |
| 307 | // is not a problem. |
| 308 | ScopedObjectAccess soa(self); |
| 309 | gc::ScopedGCCriticalSection sgcs(self, |
| 310 | gc::kGcCauseProfileSaver, |
| 311 | gc::kCollectorTypeCriticalSection); |
| 312 | VariableSizedHandleScope hs(soa.Self()); |
| 313 | std::vector<Handle<mirror::ClassLoader>> class_loaders; |
| 314 | if (profile_boot_class_path) { |
| 315 | // First add the boot class loader since visit classloaders doesn't visit it. |
| 316 | class_loaders.push_back(hs.NewHandle<mirror::ClassLoader>(nullptr)); |
| 317 | } |
| 318 | GetClassLoadersVisitor class_loader_visitor(&hs, &class_loaders); |
| 319 | { |
| 320 | // Read the class loaders into a temporary array to prevent contention problems on the |
| 321 | // class_linker_classes_lock. |
| 322 | ScopedTrace trace2("Get class loaders"); |
| 323 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
| 324 | class_linker->VisitClassLoaders(&class_loader_visitor); |
| 325 | } |
| 326 | ScopedArenaVector<ObjPtr<mirror::Class>> classes(allocator->Adapter()); |
| 327 | for (Handle<mirror::ClassLoader> class_loader : class_loaders) { |
| 328 | ClassTable* table = class_linker->ClassTableForClassLoader(class_loader.Get()); |
| 329 | if (table == nullptr) { |
| 330 | // If the class loader has not loaded any classes, it may have a null table. |
| 331 | continue; |
| 332 | } |
| 333 | GetClassesVisitor get_classes_visitor(profile_boot_class_path, &classes); |
| 334 | { |
| 335 | // Collect the classes into a temporary array to prevent lock contention on the class |
| 336 | // table lock. We want to avoid blocking class loading in other threads as much as |
| 337 | // possible. |
| 338 | ScopedTrace trace3("Visiting class table"); |
| 339 | table->Visit(get_classes_visitor); |
| 340 | } |
| 341 | for (ObjPtr<mirror::Class> klass : classes) { |
| 342 | if (startup) { |
| 343 | // We only record classes for the startup case. This may change in the future. |
| 344 | resolved_classes->AddReference(&klass->GetDexFile(), klass->GetDexTypeIndex()); |
| 345 | } |
| 346 | // Visit all of the methods in the class to see which ones were executed. |
| 347 | for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) { |
| 348 | if (!method.IsNative()) { |
| 349 | DCHECK(!method.IsProxyMethod()); |
| 350 | const uint16_t counter = method.GetCounter(); |
| 351 | // Mark startup methods as hot if they have more than hot_method_sample_threshold |
| 352 | // samples. This means they will get compiled by the compiler driver. |
| 353 | if (method.GetProfilingInfo(kRuntimePointerSize) != nullptr || |
Orion Hodson | cfcc9cf | 2017-09-29 15:07:27 +0100 | [diff] [blame] | 354 | method.PreviouslyWarm() || |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 355 | counter >= hot_method_sample_threshold) { |
| 356 | hot_methods->AddReference(method.GetDexFile(), method.GetDexMethodIndex()); |
| 357 | } else if (counter != 0) { |
| 358 | sampled_methods->AddReference(method.GetDexFile(), method.GetDexMethodIndex()); |
| 359 | } |
| 360 | } else { |
Vladimir Marko | 2196c65 | 2017-11-30 16:16:07 +0000 | [diff] [blame] | 361 | // We do not record native methods. Once we AOT-compile the app, all native |
| 362 | // methods shall have their thunks compiled. |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | } |
| 366 | classes.clear(); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void ProfileSaver::FetchAndCacheResolvedClassesAndMethods(bool startup) { |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 371 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 372 | const uint64_t start_time = NanoTime(); |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 373 | |
| 374 | // Resolve any new registered locations. |
| 375 | ResolveTrackedLocations(); |
| 376 | |
Mathieu Chartier | 3910037 | 2017-05-17 13:14:10 -0700 | [diff] [blame] | 377 | Thread* const self = Thread::Current(); |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 378 | Runtime* const runtime = Runtime::Current(); |
| 379 | ArenaStack stack(runtime->GetArenaPool()); |
| 380 | ScopedArenaAllocator allocator(&stack); |
| 381 | MethodReferenceCollection hot_methods(allocator.Adapter(), allocator.Adapter()); |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 382 | MethodReferenceCollection sampled_methods(allocator.Adapter(), allocator.Adapter()); |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 383 | TypeReferenceCollection resolved_classes(allocator.Adapter(), allocator.Adapter()); |
Mathieu Chartier | 273d110 | 2017-06-06 17:07:13 -0700 | [diff] [blame] | 384 | const bool is_low_ram = Runtime::Current()->GetHeap()->IsLowMemoryMode(); |
Mathieu Chartier | 5494e5b | 2017-06-26 14:13:27 -0700 | [diff] [blame] | 385 | pthread_t profiler_pthread; |
Mathieu Chartier | c600eaa | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 386 | { |
Mathieu Chartier | 5494e5b | 2017-06-26 14:13:27 -0700 | [diff] [blame] | 387 | MutexLock mu(self, *Locks::profiler_lock_); |
| 388 | profiler_pthread = profiler_pthread_; |
| 389 | } |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 390 | const uint32_t hot_method_sample_threshold = startup ? |
| 391 | options_.GetHotStartupMethodSamples(is_low_ram) : |
| 392 | std::numeric_limits<uint32_t>::max(); |
| 393 | SampleClassesAndExecutedMethods(profiler_pthread, |
| 394 | options_.GetProfileBootClassPath(), |
| 395 | &allocator, |
| 396 | hot_method_sample_threshold, |
| 397 | startup, |
| 398 | &resolved_classes, |
| 399 | &hot_methods, |
| 400 | &sampled_methods); |
Mathieu Chartier | 3910037 | 2017-05-17 13:14:10 -0700 | [diff] [blame] | 401 | MutexLock mu(self, *Locks::profiler_lock_); |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 402 | uint64_t total_number_of_profile_entries_cached = 0; |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 403 | using Hotness = ProfileCompilationInfo::MethodHotness; |
Mathieu Chartier | 9275af6 | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 404 | |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 405 | for (const auto& it : tracked_dex_base_locations_) { |
Mathieu Chartier | 9275af6 | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 406 | std::set<DexCacheResolvedClasses> resolved_classes_for_location; |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 407 | const std::string& filename = it.first; |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 408 | auto info_it = profile_cache_.find(filename); |
| 409 | if (info_it == profile_cache_.end()) { |
| 410 | info_it = profile_cache_.Put( |
| 411 | filename, |
| 412 | new ProfileCompilationInfo(Runtime::Current()->GetArenaPool())); |
| 413 | } |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 414 | ProfileCompilationInfo* cached_info = info_it->second; |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 415 | |
| 416 | const std::set<std::string>& locations = it.second; |
| 417 | for (const auto& pair : hot_methods.GetMap()) { |
| 418 | const DexFile* const dex_file = pair.first; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 419 | const std::string base_location = DexFileLoader::GetBaseLocation(dex_file->GetLocation()); |
| 420 | if (locations.find(base_location) != locations.end()) { |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 421 | const MethodReferenceCollection::IndexVector& indices = pair.second; |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 422 | uint8_t flags = Hotness::kFlagHot; |
| 423 | flags |= startup ? Hotness::kFlagStartup : Hotness::kFlagPostStartup; |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 424 | cached_info->AddMethodsForDex( |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 425 | static_cast<Hotness::Flag>(flags), |
Mathieu Chartier | bbe3a5e | 2017-06-13 16:36:17 -0700 | [diff] [blame] | 426 | dex_file, |
| 427 | indices.begin(), |
| 428 | indices.end()); |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 431 | for (const auto& pair : sampled_methods.GetMap()) { |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 432 | const DexFile* const dex_file = pair.first; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 433 | const std::string base_location = DexFileLoader::GetBaseLocation(dex_file->GetLocation()); |
| 434 | if (locations.find(base_location) != locations.end()) { |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 435 | const MethodReferenceCollection::IndexVector& indices = pair.second; |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 436 | cached_info->AddMethodsForDex(startup ? Hotness::kFlagStartup : Hotness::kFlagPostStartup, |
Mathieu Chartier | db40eac | 2017-06-09 18:34:11 -0700 | [diff] [blame] | 437 | dex_file, |
| 438 | indices.begin(), |
| 439 | indices.end()); |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | for (const auto& pair : resolved_classes.GetMap()) { |
| 443 | const DexFile* const dex_file = pair.first; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 444 | const std::string base_location = DexFileLoader::GetBaseLocation(dex_file->GetLocation()); |
| 445 | if (locations.find(base_location) != locations.end()) { |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 446 | const TypeReferenceCollection::IndexVector& classes = pair.second; |
| 447 | VLOG(profiler) << "Added " << classes.size() << " classes for location " |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 448 | << base_location |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 449 | << " (" << dex_file->GetLocation() << ")"; |
| 450 | cached_info->AddClassesForDex(dex_file, classes.begin(), classes.end()); |
| 451 | } else { |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 452 | VLOG(profiler) << "Location not found " << base_location |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 453 | << " (" << dex_file->GetLocation() << ")"; |
| 454 | } |
| 455 | } |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 456 | total_number_of_profile_entries_cached += resolved_classes_for_location.size(); |
| 457 | } |
| 458 | max_number_of_profile_entries_cached_ = std::max( |
| 459 | max_number_of_profile_entries_cached_, |
| 460 | total_number_of_profile_entries_cached); |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 461 | VLOG(profiler) << "Profile saver recorded " << hot_methods.NumReferences() << " hot methods and " |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 462 | << sampled_methods.NumReferences() << " sampled methods with threshold " |
| 463 | << hot_method_sample_threshold << " in " |
| 464 | << PrettyDuration(NanoTime() - start_time); |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 467 | bool ProfileSaver::ProcessProfilingInfo(bool force_save, /*out*/uint16_t* number_of_new_methods) { |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 468 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 469 | |
| 470 | // Resolve any new registered locations. |
| 471 | ResolveTrackedLocations(); |
| 472 | |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 473 | SafeMap<std::string, std::set<std::string>> tracked_locations; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 474 | { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 475 | // Make a copy so that we don't hold the lock while doing I/O. |
| 476 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 477 | tracked_locations = tracked_dex_base_locations_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 478 | } |
Calin Juravle | c15e566 | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 479 | |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 480 | bool profile_file_saved = false; |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 481 | if (number_of_new_methods != nullptr) { |
| 482 | *number_of_new_methods = 0; |
| 483 | } |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 484 | |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 485 | // We only need to do this once, not once per dex location. |
| 486 | // TODO: Figure out a way to only do it when stuff has changed? It takes 30-50ms. |
| 487 | FetchAndCacheResolvedClassesAndMethods(/*startup*/ false); |
| 488 | |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 489 | for (const auto& it : tracked_locations) { |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 490 | if (!force_save && ShuttingDown(Thread::Current())) { |
| 491 | // The ProfileSaver is in shutdown mode, meaning a stop request was made and |
| 492 | // we need to exit cleanly (by waiting for the saver thread to finish). Unless |
| 493 | // we have a request for a forced save, do not do any processing so that we |
| 494 | // speed up the exit. |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 495 | return true; |
| 496 | } |
| 497 | const std::string& filename = it.first; |
| 498 | const std::set<std::string>& locations = it.second; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 499 | std::vector<ProfileMethodInfo> profile_methods; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 500 | { |
| 501 | ScopedObjectAccess soa(Thread::Current()); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 502 | jit_code_cache_->GetProfiledMethods(locations, profile_methods); |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 503 | total_number_of_code_cache_queries_++; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 504 | } |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 505 | { |
| 506 | ProfileCompilationInfo info(Runtime::Current()->GetArenaPool()); |
| 507 | if (!info.Load(filename, /*clear_if_invalid*/ true)) { |
| 508 | LOG(WARNING) << "Could not forcefully load profile " << filename; |
| 509 | continue; |
| 510 | } |
| 511 | uint64_t last_save_number_of_methods = info.GetNumberOfMethods(); |
| 512 | uint64_t last_save_number_of_classes = info.GetNumberOfResolvedClasses(); |
Calin Juravle | c15e566 | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 513 | |
Calin Juravle | ee9cb41 | 2018-02-13 20:32:35 -0800 | [diff] [blame] | 514 | info.AddMethods(profile_methods, ProfileCompilationInfo::MethodHotness::kFlagPostStartup); |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 515 | auto profile_cache_it = profile_cache_.find(filename); |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 516 | if (profile_cache_it != profile_cache_.end()) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 517 | info.MergeWith(*(profile_cache_it->second)); |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 518 | } |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 519 | |
| 520 | int64_t delta_number_of_methods = |
| 521 | info.GetNumberOfMethods() - last_save_number_of_methods; |
| 522 | int64_t delta_number_of_classes = |
| 523 | info.GetNumberOfResolvedClasses() - last_save_number_of_classes; |
| 524 | |
| 525 | if (!force_save && |
| 526 | delta_number_of_methods < options_.GetMinMethodsToSave() && |
| 527 | delta_number_of_classes < options_.GetMinClassesToSave()) { |
| 528 | VLOG(profiler) << "Not enough information to save to: " << filename |
| 529 | << " Number of methods: " << delta_number_of_methods |
| 530 | << " Number of classes: " << delta_number_of_classes; |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 531 | total_number_of_skipped_writes_++; |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 532 | continue; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 533 | } |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 534 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 535 | if (number_of_new_methods != nullptr) { |
| 536 | *number_of_new_methods = |
| 537 | std::max(static_cast<uint16_t>(delta_number_of_methods), |
| 538 | *number_of_new_methods); |
| 539 | } |
| 540 | uint64_t bytes_written; |
| 541 | // Force the save. In case the profile data is corrupted or the the profile |
| 542 | // has the wrong version this will "fix" the file to the correct format. |
| 543 | if (info.Save(filename, &bytes_written)) { |
| 544 | // We managed to save the profile. Clear the cache stored during startup. |
| 545 | if (profile_cache_it != profile_cache_.end()) { |
| 546 | ProfileCompilationInfo *cached_info = profile_cache_it->second; |
| 547 | profile_cache_.erase(profile_cache_it); |
| 548 | delete cached_info; |
| 549 | } |
| 550 | if (bytes_written > 0) { |
| 551 | total_number_of_writes_++; |
| 552 | total_bytes_written_ += bytes_written; |
| 553 | profile_file_saved = true; |
| 554 | } else { |
| 555 | // At this point we could still have avoided the write. |
| 556 | // We load and merge the data from the file lazily at its first ever |
| 557 | // save attempt. So, whatever we are trying to save could already be |
| 558 | // in the file. |
| 559 | total_number_of_skipped_writes_++; |
| 560 | } |
| 561 | } else { |
| 562 | LOG(WARNING) << "Could not save profiling info to " << filename; |
| 563 | total_number_of_failed_writes_++; |
| 564 | } |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 565 | } |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 566 | } |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 567 | |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 568 | // Trim the maps to madvise the pages used for profile info. |
| 569 | // It is unlikely we will need them again in the near feature. |
| 570 | Runtime::Current()->GetArenaPool()->TrimMaps(); |
| 571 | |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 572 | return profile_file_saved; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void* ProfileSaver::RunProfileSaverThread(void* arg) { |
| 576 | Runtime* runtime = Runtime::Current(); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 577 | |
Calin Juravle | e55fda1 | 2016-04-28 12:59:33 +0100 | [diff] [blame] | 578 | bool attached = runtime->AttachCurrentThread("Profile Saver", |
| 579 | /*as_daemon*/true, |
| 580 | runtime->GetSystemThreadGroup(), |
| 581 | /*create_peer*/true); |
| 582 | if (!attached) { |
| 583 | CHECK(runtime->IsShuttingDown(Thread::Current())); |
| 584 | return nullptr; |
| 585 | } |
| 586 | |
| 587 | ProfileSaver* profile_saver = reinterpret_cast<ProfileSaver*>(arg); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 588 | profile_saver->Run(); |
| 589 | |
| 590 | runtime->DetachCurrentThread(); |
| 591 | VLOG(profiler) << "Profile saver shutdown"; |
| 592 | return nullptr; |
| 593 | } |
| 594 | |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 595 | static bool ShouldProfileLocation(const std::string& location) { |
Calin Juravle | 7506423 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 596 | OatFileManager& oat_manager = Runtime::Current()->GetOatFileManager(); |
| 597 | const OatFile* oat_file = oat_manager.FindOpenedOatFileFromDexLocation(location); |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 598 | if (oat_file == nullptr) { |
| 599 | // This can happen if we fallback to run code directly from the APK. |
| 600 | // Profile it with the hope that the background dexopt will get us back into |
| 601 | // a good state. |
Calin Juravle | 7506423 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 602 | VLOG(profiler) << "Asked to profile a location without an oat file:" << location; |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 603 | return true; |
| 604 | } |
| 605 | CompilerFilter::Filter filter = oat_file->GetCompilerFilter(); |
Calin Juravle | d19dc46 | 2016-04-19 18:17:41 +0100 | [diff] [blame] | 606 | if ((filter == CompilerFilter::kSpeed) || (filter == CompilerFilter::kEverything)) { |
Calin Juravle | 7506423 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 607 | VLOG(profiler) |
Calin Juravle | d19dc46 | 2016-04-19 18:17:41 +0100 | [diff] [blame] | 608 | << "Skip profiling oat file because it's already speed|everything compiled: " |
| 609 | << location << " oat location: " << oat_file->GetLocation(); |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 610 | return false; |
| 611 | } |
| 612 | return true; |
| 613 | } |
| 614 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 615 | void ProfileSaver::Start(const ProfileSaverOptions& options, |
| 616 | const std::string& output_filename, |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 617 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 618 | const std::vector<std::string>& code_paths) { |
Mathieu Chartier | 885a713 | 2017-06-10 14:35:11 -0700 | [diff] [blame] | 619 | Runtime* const runtime = Runtime::Current(); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 620 | DCHECK(options.IsEnabled()); |
Mathieu Chartier | 885a713 | 2017-06-10 14:35:11 -0700 | [diff] [blame] | 621 | DCHECK(runtime->GetJit() != nullptr); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 622 | DCHECK(!output_filename.empty()); |
| 623 | DCHECK(jit_code_cache != nullptr); |
| 624 | |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 625 | std::vector<std::string> code_paths_to_profile; |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 626 | for (const std::string& location : code_paths) { |
| 627 | if (ShouldProfileLocation(location)) { |
| 628 | code_paths_to_profile.push_back(location); |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 629 | } |
| 630 | } |
Mathieu Chartier | 885a713 | 2017-06-10 14:35:11 -0700 | [diff] [blame] | 631 | |
| 632 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 633 | // Support getting profile samples for the boot class path. This will be used to generate the boot |
| 634 | // image profile. The intention is to use this code to generate to boot image but not use it in |
| 635 | // production. b/37966211 |
| 636 | if (options.GetProfileBootClassPath()) { |
| 637 | std::set<std::string> code_paths_keys; |
| 638 | for (const std::string& location : code_paths) { |
| 639 | code_paths_keys.insert(ProfileCompilationInfo::GetProfileDexFileKey(location)); |
| 640 | } |
| 641 | for (const DexFile* dex_file : runtime->GetClassLinker()->GetBootClassPath()) { |
| 642 | // Don't check ShouldProfileLocation since the boot class path may be speed compiled. |
| 643 | const std::string& location = dex_file->GetLocation(); |
| 644 | const std::string key = ProfileCompilationInfo::GetProfileDexFileKey(location); |
| 645 | VLOG(profiler) << "Registering boot dex file " << location; |
| 646 | if (code_paths_keys.find(key) != code_paths_keys.end()) { |
| 647 | LOG(WARNING) << "Boot class path location key conflicts with code path " << location; |
| 648 | } else if (instance_ == nullptr) { |
| 649 | // Only add the boot class path once since Start may be called multiple times for secondary |
| 650 | // dexes. |
| 651 | // We still do the collision check above. This handles any secondary dexes that conflict |
| 652 | // with the boot class path dex files. |
| 653 | code_paths_to_profile.push_back(location); |
| 654 | } |
| 655 | } |
| 656 | } |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 657 | if (code_paths_to_profile.empty()) { |
Calin Juravle | 7506423 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 658 | VLOG(profiler) << "No code paths should be profiled."; |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 659 | return; |
| 660 | } |
| 661 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 662 | if (instance_ != nullptr) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 663 | // If we already have an instance, make sure it uses the same jit_code_cache. |
| 664 | // This may be called multiple times via Runtime::registerAppInfo (e.g. for |
| 665 | // apps which share the same runtime). |
| 666 | DCHECK_EQ(instance_->jit_code_cache_, jit_code_cache); |
| 667 | // Add the code_paths to the tracked locations. |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 668 | instance_->AddTrackedLocations(output_filename, code_paths_to_profile); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 669 | return; |
| 670 | } |
| 671 | |
| 672 | VLOG(profiler) << "Starting profile saver using output file: " << output_filename |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 673 | << ". Tracking: " << android::base::Join(code_paths_to_profile, ':'); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 674 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 675 | instance_ = new ProfileSaver(options, |
| 676 | output_filename, |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 677 | jit_code_cache, |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 678 | code_paths_to_profile); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 679 | |
| 680 | // Create a new thread which does the saving. |
| 681 | CHECK_PTHREAD_CALL( |
| 682 | pthread_create, |
| 683 | (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)), |
| 684 | "Profile saver thread"); |
Nicolas Geoffray | 23caed8 | 2017-04-21 14:30:18 +0100 | [diff] [blame] | 685 | |
Mathieu Chartier | 5494e5b | 2017-06-26 14:13:27 -0700 | [diff] [blame] | 686 | SetProfileSaverThreadPriority(profiler_pthread_, kProfileSaverPthreadPriority); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 689 | void ProfileSaver::Stop(bool dump_info) { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 690 | ProfileSaver* profile_saver = nullptr; |
| 691 | pthread_t profiler_pthread = 0U; |
| 692 | |
| 693 | { |
| 694 | MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 695 | VLOG(profiler) << "Stopping profile saver thread"; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 696 | profile_saver = instance_; |
| 697 | profiler_pthread = profiler_pthread_; |
| 698 | if (instance_ == nullptr) { |
| 699 | DCHECK(false) << "Tried to stop a profile saver which was not started"; |
| 700 | return; |
| 701 | } |
| 702 | if (instance_->shutting_down_) { |
| 703 | DCHECK(false) << "Tried to stop the profile saver twice"; |
| 704 | return; |
| 705 | } |
| 706 | instance_->shutting_down_ = true; |
| 707 | } |
| 708 | |
| 709 | { |
| 710 | // Wake up the saver thread if it is sleeping to allow for a clean exit. |
| 711 | MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_); |
| 712 | profile_saver->period_condition_.Signal(Thread::Current()); |
| 713 | } |
| 714 | |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 715 | // Force save everything before destroying the thread since we want profiler_pthread_ to remain |
| 716 | // valid. |
| 717 | instance_->ProcessProfilingInfo(/*force_save*/true, /*number_of_new_methods*/nullptr); |
| 718 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 719 | // Wait for the saver thread to stop. |
| 720 | CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown"); |
| 721 | |
| 722 | { |
| 723 | MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | 9db22e8 | 2017-03-30 16:31:23 -0700 | [diff] [blame] | 724 | if (dump_info) { |
| 725 | instance_->DumpInfo(LOG_STREAM(INFO)); |
| 726 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 727 | instance_ = nullptr; |
| 728 | profiler_pthread_ = 0U; |
| 729 | } |
| 730 | delete profile_saver; |
| 731 | } |
| 732 | |
| 733 | bool ProfileSaver::ShuttingDown(Thread* self) { |
| 734 | MutexLock mu(self, *Locks::profiler_lock_); |
| 735 | return shutting_down_; |
| 736 | } |
| 737 | |
| 738 | bool ProfileSaver::IsStarted() { |
| 739 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 740 | return instance_ != nullptr; |
| 741 | } |
| 742 | |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 743 | static void AddTrackedLocationsToMap(const std::string& output_filename, |
| 744 | const std::vector<std::string>& code_paths, |
| 745 | SafeMap<std::string, std::set<std::string>>* map) { |
| 746 | auto it = map->find(output_filename); |
| 747 | if (it == map->end()) { |
| 748 | map->Put(output_filename, std::set<std::string>(code_paths.begin(), code_paths.end())); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 749 | } else { |
| 750 | it->second.insert(code_paths.begin(), code_paths.end()); |
| 751 | } |
| 752 | } |
| 753 | |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 754 | void ProfileSaver::AddTrackedLocations(const std::string& output_filename, |
| 755 | const std::vector<std::string>& code_paths) { |
| 756 | // Add the code paths to the list of tracked location. |
| 757 | AddTrackedLocationsToMap(output_filename, code_paths, &tracked_dex_base_locations_); |
| 758 | // The code paths may contain symlinks which could fool the profiler. |
| 759 | // If the dex file is compiled with an absolute location but loaded with symlink |
| 760 | // the profiler could skip the dex due to location mismatch. |
| 761 | // To avoid this, we add the code paths to the temporary cache of 'to_be_resolved' |
| 762 | // locations. When the profiler thread executes we will resolve the paths to their |
| 763 | // real paths. |
| 764 | // Note that we delay taking the realpath to avoid spending more time than needed |
| 765 | // when registering location (as it is done during app launch). |
| 766 | AddTrackedLocationsToMap(output_filename, |
| 767 | code_paths, |
| 768 | &tracked_dex_base_locations_to_be_resolved_); |
| 769 | } |
| 770 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 771 | void ProfileSaver::DumpInstanceInfo(std::ostream& os) { |
| 772 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 773 | if (instance_ != nullptr) { |
| 774 | instance_->DumpInfo(os); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | void ProfileSaver::DumpInfo(std::ostream& os) { |
| 779 | os << "ProfileSaver total_bytes_written=" << total_bytes_written_ << '\n' |
| 780 | << "ProfileSaver total_number_of_writes=" << total_number_of_writes_ << '\n' |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 781 | << "ProfileSaver total_number_of_code_cache_queries=" |
| 782 | << total_number_of_code_cache_queries_ << '\n' |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 783 | << "ProfileSaver total_number_of_skipped_writes=" << total_number_of_skipped_writes_ << '\n' |
| 784 | << "ProfileSaver total_number_of_failed_writes=" << total_number_of_failed_writes_ << '\n' |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 785 | << "ProfileSaver total_ms_of_sleep=" << total_ms_of_sleep_ << '\n' |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 786 | << "ProfileSaver total_ms_of_work=" << NsToMs(total_ns_of_work_) << '\n' |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 787 | << "ProfileSaver max_number_profile_entries_cached=" |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 788 | << max_number_of_profile_entries_cached_ << '\n' |
| 789 | << "ProfileSaver total_number_of_hot_spikes=" << total_number_of_hot_spikes_ << '\n' |
| 790 | << "ProfileSaver total_number_of_wake_ups=" << total_number_of_wake_ups_ << '\n'; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 793 | |
| 794 | void ProfileSaver::ForceProcessProfiles() { |
| 795 | ProfileSaver* saver = nullptr; |
| 796 | { |
| 797 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 798 | saver = instance_; |
| 799 | } |
| 800 | // TODO(calin): this is not actually thread safe as the instance_ may have been deleted, |
| 801 | // but we only use this in testing when we now this won't happen. |
| 802 | // Refactor the way we handle the instance so that we don't end up in this situation. |
| 803 | if (saver != nullptr) { |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 804 | saver->ProcessProfilingInfo(/*force_save*/true, /*number_of_new_methods*/nullptr); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 808 | bool ProfileSaver::HasSeenMethod(const std::string& profile, bool hot, MethodReference ref) { |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 809 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 810 | if (instance_ != nullptr) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 811 | ProfileCompilationInfo info(Runtime::Current()->GetArenaPool()); |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 812 | if (!info.Load(profile, /*clear_if_invalid*/false)) { |
| 813 | return false; |
| 814 | } |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 815 | ProfileCompilationInfo::MethodHotness hotness = info.GetMethodHotness(ref); |
Mathieu Chartier | 48a951b | 2017-07-13 17:04:02 -0700 | [diff] [blame] | 816 | // Ignore hot parameter for now since it was causing test 595 to be flaky. TODO: Investigate. |
| 817 | // b/63635729 |
| 818 | UNUSED(hot); |
| 819 | return hotness.IsInProfile(); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 820 | } |
| 821 | return false; |
| 822 | } |
| 823 | |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 824 | void ProfileSaver::ResolveTrackedLocations() { |
| 825 | SafeMap<std::string, std::set<std::string>> locations_to_be_resolved; |
| 826 | { |
| 827 | // Make a copy so that we don't hold the lock while doing I/O. |
| 828 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 829 | locations_to_be_resolved = tracked_dex_base_locations_to_be_resolved_; |
| 830 | tracked_dex_base_locations_to_be_resolved_.clear(); |
| 831 | } |
| 832 | |
| 833 | // Resolve the locations. |
| 834 | SafeMap<std::string, std::vector<std::string>> resolved_locations_map; |
| 835 | for (const auto& it : locations_to_be_resolved) { |
| 836 | const std::string& filename = it.first; |
| 837 | const std::set<std::string>& locations = it.second; |
| 838 | auto resolved_locations_it = resolved_locations_map.Put( |
| 839 | filename, |
| 840 | std::vector<std::string>(locations.size())); |
| 841 | |
| 842 | for (const auto& location : locations) { |
| 843 | UniqueCPtr<const char[]> location_real(realpath(location.c_str(), nullptr)); |
| 844 | // Note that it's ok if we cannot get the real path. |
| 845 | if (location_real != nullptr) { |
| 846 | resolved_locations_it->second.emplace_back(location_real.get()); |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | // Add the resolved locations to the tracked collection. |
| 852 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 853 | for (const auto& it : resolved_locations_map) { |
| 854 | AddTrackedLocationsToMap(it.first, it.second, &tracked_dex_base_locations_); |
| 855 | } |
| 856 | } |
| 857 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 858 | } // namespace art |