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 | |
Nicolas Geoffray | 23caed8 | 2017-04-21 14:30:18 +0100 | [diff] [blame] | 19 | #include <sys/resource.h> |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 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" |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 28 | #include "base/scoped_arena_containers.h" |
Andreas Gampe | 5678db5 | 2017-06-08 14:11:18 -0700 | [diff] [blame] | 29 | #include "base/stl_util.h" |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 30 | #include "base/systrace.h" |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 31 | #include "base/time_utils.h" |
| 32 | #include "compiler_filter.h" |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 33 | #include "dex_reference_collection.h" |
Mathieu Chartier | 3910037 | 2017-05-17 13:14:10 -0700 | [diff] [blame] | 34 | #include "gc/collector_type.h" |
| 35 | #include "gc/gc_cause.h" |
| 36 | #include "gc/scoped_gc_critical_section.h" |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 37 | #include "jit/profile_compilation_info-inl.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 38 | #include "oat_file_manager.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 39 | #include "scoped_thread_state_change-inl.h" |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 40 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 41 | namespace art { |
| 42 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 43 | ProfileSaver* ProfileSaver::instance_ = nullptr; |
| 44 | pthread_t ProfileSaver::profiler_pthread_ = 0U; |
| 45 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 46 | ProfileSaver::ProfileSaver(const ProfileSaverOptions& options, |
| 47 | const std::string& output_filename, |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 48 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 49 | const std::vector<std::string>& code_paths) |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 50 | : jit_code_cache_(jit_code_cache), |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 51 | shutting_down_(false), |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 52 | last_time_ns_saver_woke_up_(0), |
| 53 | jit_activity_notifications_(0), |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 54 | wait_lock_("ProfileSaver wait lock"), |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 55 | period_condition_("ProfileSaver period condition", wait_lock_), |
| 56 | total_bytes_written_(0), |
| 57 | total_number_of_writes_(0), |
| 58 | total_number_of_code_cache_queries_(0), |
| 59 | total_number_of_skipped_writes_(0), |
| 60 | total_number_of_failed_writes_(0), |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 61 | total_ms_of_sleep_(0), |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 62 | total_ns_of_work_(0), |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 63 | max_number_of_profile_entries_cached_(0), |
| 64 | total_number_of_hot_spikes_(0), |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 65 | total_number_of_wake_ups_(0), |
| 66 | options_(options) { |
| 67 | DCHECK(options_.IsEnabled()); |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 68 | AddTrackedLocations(output_filename, code_paths); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 71 | ProfileSaver::~ProfileSaver() { |
| 72 | for (auto& it : profile_cache_) { |
| 73 | delete it.second; |
| 74 | } |
| 75 | } |
| 76 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 77 | void ProfileSaver::Run() { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 78 | Thread* self = Thread::Current(); |
| 79 | |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 80 | // Fetch the resolved classes for the app images after sleeping for |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 81 | // options_.GetSaveResolvedClassesDelayMs(). |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 82 | // TODO(calin) This only considers the case of the primary profile file. |
| 83 | // Anything that gets loaded in the same VM will not have their resolved |
| 84 | // classes save (unless they started before the initial saving was done). |
| 85 | { |
| 86 | MutexLock mu(self, wait_lock_); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 87 | const uint64_t end_time = NanoTime() + MsToNs(options_.GetSaveResolvedClassesDelayMs()); |
Mathieu Chartier | 0ec065d | 2016-05-18 19:51:23 -0700 | [diff] [blame] | 88 | while (true) { |
| 89 | const uint64_t current_time = NanoTime(); |
| 90 | if (current_time >= end_time) { |
| 91 | break; |
| 92 | } |
| 93 | period_condition_.TimedWait(self, NsToMs(end_time - current_time), 0); |
| 94 | } |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 95 | total_ms_of_sleep_ += options_.GetSaveResolvedClassesDelayMs(); |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 96 | } |
Mathieu Chartier | c600eaa | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 97 | FetchAndCacheResolvedClassesAndMethods(); |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 98 | |
| 99 | // Loop for the profiled methods. |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 100 | while (!ShuttingDown(self)) { |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 101 | uint64_t sleep_start = NanoTime(); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 102 | { |
Calin Juravle | 0233a41 | 2016-05-18 15:49:36 -0700 | [diff] [blame] | 103 | uint64_t sleep_time = 0; |
| 104 | { |
| 105 | MutexLock mu(self, wait_lock_); |
| 106 | period_condition_.Wait(self); |
Calin Juravle | dc85bd7 | 2016-05-25 18:09:53 +0100 | [diff] [blame] | 107 | sleep_time = NanoTime() - sleep_start; |
Calin Juravle | 0233a41 | 2016-05-18 15:49:36 -0700 | [diff] [blame] | 108 | } |
| 109 | // Check if the thread was woken up for shutdown. |
| 110 | if (ShuttingDown(self)) { |
| 111 | break; |
| 112 | } |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 113 | total_number_of_wake_ups_++; |
| 114 | // We might have been woken up by a huge number of notifications to guarantee saving. |
| 115 | // If we didn't meet the minimum saving period go back to sleep (only if missed by |
| 116 | // a reasonable margin). |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 117 | uint64_t min_save_period_ns = MsToNs(options_.GetMinSavePeriodMs()); |
| 118 | while (min_save_period_ns * 0.9 > sleep_time) { |
Calin Juravle | 0233a41 | 2016-05-18 15:49:36 -0700 | [diff] [blame] | 119 | { |
| 120 | MutexLock mu(self, wait_lock_); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 121 | period_condition_.TimedWait(self, NsToMs(min_save_period_ns - sleep_time), 0); |
Calin Juravle | dc85bd7 | 2016-05-25 18:09:53 +0100 | [diff] [blame] | 122 | sleep_time = NanoTime() - sleep_start; |
Calin Juravle | 0233a41 | 2016-05-18 15:49:36 -0700 | [diff] [blame] | 123 | } |
| 124 | // Check if the thread was woken up for shutdown. |
| 125 | if (ShuttingDown(self)) { |
| 126 | break; |
| 127 | } |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 128 | total_number_of_wake_ups_++; |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 129 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 130 | } |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 131 | total_ms_of_sleep_ += NsToMs(NanoTime() - sleep_start); |
| 132 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 133 | if (ShuttingDown(self)) { |
| 134 | break; |
| 135 | } |
| 136 | |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 137 | uint16_t number_of_new_methods = 0; |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 138 | uint64_t start_work = NanoTime(); |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 139 | 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] | 140 | // Update the notification counter based on result. Note that there might be contention on this |
| 141 | // but we don't care about to be 100% precise. |
| 142 | if (!profile_saved_to_disk) { |
| 143 | // 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] | 144 | // Set the jit activity notifications to number_of_new_methods so we can wake up earlier |
| 145 | // if needed. |
| 146 | jit_activity_notifications_ = number_of_new_methods; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 147 | } |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 148 | total_ns_of_work_ += NanoTime() - start_work; |
| 149 | } |
| 150 | } |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 151 | |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 152 | void ProfileSaver::NotifyJitActivity() { |
| 153 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 154 | if (instance_ == nullptr || instance_->shutting_down_) { |
| 155 | return; |
| 156 | } |
| 157 | instance_->NotifyJitActivityInternal(); |
| 158 | } |
| 159 | |
| 160 | void ProfileSaver::WakeUpSaver() { |
| 161 | jit_activity_notifications_ = 0; |
| 162 | last_time_ns_saver_woke_up_ = NanoTime(); |
| 163 | period_condition_.Signal(Thread::Current()); |
| 164 | } |
| 165 | |
| 166 | void ProfileSaver::NotifyJitActivityInternal() { |
| 167 | // Unlikely to overflow but if it happens, |
| 168 | // we would have waken up the saver long before that. |
| 169 | jit_activity_notifications_++; |
| 170 | // Note that we are not as precise as we could be here but we don't want to wake the saver |
| 171 | // every time we see a hot method. |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 172 | if (jit_activity_notifications_ > options_.GetMinNotificationBeforeWake()) { |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 173 | MutexLock wait_mutex(Thread::Current(), wait_lock_); |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 174 | if ((NanoTime() - last_time_ns_saver_woke_up_) > MsToNs(options_.GetMinSavePeriodMs())) { |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 175 | WakeUpSaver(); |
Serguei Katkov | 87de9cf | 2016-08-01 17:47:04 +0700 | [diff] [blame] | 176 | } else if (jit_activity_notifications_ > options_.GetMaxNotificationBeforeWake()) { |
| 177 | // Make sure to wake up the saver if we see a spike in the number of notifications. |
| 178 | // This is a precaution to avoid losing a big number of methods in case |
| 179 | // this is a spike with no jit after. |
| 180 | total_number_of_hot_spikes_++; |
| 181 | WakeUpSaver(); |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 182 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 186 | using MethodReferenceCollection = DexReferenceCollection<uint16_t, ScopedArenaAllocatorAdapter>; |
| 187 | using TypeReferenceCollection = DexReferenceCollection<dex::TypeIndex, |
| 188 | ScopedArenaAllocatorAdapter>; |
| 189 | |
Mathieu Chartier | c600eaa | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 190 | // Get resolved methods that have a profile info or more than kStartupMethodSamples samples. |
| 191 | // Excludes native methods and classes in the boot image. |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 192 | class GetClassesAndMethodsVisitor : public ClassVisitor { |
Mathieu Chartier | c600eaa | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 193 | public: |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 194 | GetClassesAndMethodsVisitor(MethodReferenceCollection* hot_methods, |
| 195 | MethodReferenceCollection* sampled_methods, |
| 196 | TypeReferenceCollection* resolved_classes, |
| 197 | uint32_t hot_method_sample_threshold) |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 198 | : hot_methods_(hot_methods), |
Mathieu Chartier | 7b135c8 | 2017-06-05 12:54:01 -0700 | [diff] [blame] | 199 | sampled_methods_(sampled_methods), |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 200 | resolved_classes_(resolved_classes), |
Mathieu Chartier | 7b135c8 | 2017-06-05 12:54:01 -0700 | [diff] [blame] | 201 | hot_method_sample_threshold_(hot_method_sample_threshold) {} |
Mathieu Chartier | c600eaa | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 202 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 203 | virtual bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 204 | if (klass->IsProxyClass() || |
| 205 | klass->IsArrayClass() || |
| 206 | !klass->IsResolved() || |
| 207 | klass->IsErroneousResolved() || |
| 208 | klass->GetClassLoader() == nullptr) { |
Mathieu Chartier | c600eaa | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 209 | return true; |
| 210 | } |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 211 | DCHECK(klass->GetDexCache() != nullptr) << klass->PrettyClass(); |
| 212 | resolved_classes_->AddReference(&klass->GetDexFile(), klass->GetDexTypeIndex()); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 213 | for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) { |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 214 | if (!method.IsNative()) { |
| 215 | DCHECK(!method.IsProxyMethod()); |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 216 | const uint16_t counter = method.GetCounter(); |
Mathieu Chartier | 7b135c8 | 2017-06-05 12:54:01 -0700 | [diff] [blame] | 217 | // Mark startup methods as hot if they have more than hot_method_sample_threshold_ samples. |
| 218 | // This means they will get compiled by the compiler driver. |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 219 | if (method.GetProfilingInfo(kRuntimePointerSize) != nullptr || |
Mathieu Chartier | 7b135c8 | 2017-06-05 12:54:01 -0700 | [diff] [blame] | 220 | (method.GetAccessFlags() & kAccPreviouslyWarm) != 0 || |
| 221 | counter >= hot_method_sample_threshold_) { |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 222 | hot_methods_->AddReference(method.GetDexFile(), method.GetDexMethodIndex()); |
Mathieu Chartier | 7b135c8 | 2017-06-05 12:54:01 -0700 | [diff] [blame] | 223 | } else if (counter != 0) { |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 224 | sampled_methods_->AddReference(method.GetDexFile(), method.GetDexMethodIndex()); |
Mathieu Chartier | c600eaa | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 225 | } |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 226 | } else { |
| 227 | CHECK_EQ(method.GetCounter(), 0u); |
Mathieu Chartier | c600eaa | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | private: |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 234 | MethodReferenceCollection* const hot_methods_; |
| 235 | MethodReferenceCollection* const sampled_methods_; |
| 236 | TypeReferenceCollection* const resolved_classes_; |
Mathieu Chartier | 7b135c8 | 2017-06-05 12:54:01 -0700 | [diff] [blame] | 237 | uint32_t hot_method_sample_threshold_; |
Mathieu Chartier | c600eaa | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | void ProfileSaver::FetchAndCacheResolvedClassesAndMethods() { |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 241 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 242 | const uint64_t start_time = NanoTime(); |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 243 | |
| 244 | // Resolve any new registered locations. |
| 245 | ResolveTrackedLocations(); |
| 246 | |
Mathieu Chartier | 3910037 | 2017-05-17 13:14:10 -0700 | [diff] [blame] | 247 | Thread* const self = Thread::Current(); |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 248 | Runtime* const runtime = Runtime::Current(); |
| 249 | ArenaStack stack(runtime->GetArenaPool()); |
| 250 | ScopedArenaAllocator allocator(&stack); |
| 251 | MethodReferenceCollection hot_methods(allocator.Adapter(), allocator.Adapter()); |
| 252 | MethodReferenceCollection startup_methods(allocator.Adapter(), allocator.Adapter()); |
| 253 | TypeReferenceCollection resolved_classes(allocator.Adapter(), allocator.Adapter()); |
Mathieu Chartier | 273d110 | 2017-06-06 17:07:13 -0700 | [diff] [blame^] | 254 | const bool is_low_ram = Runtime::Current()->GetHeap()->IsLowMemoryMode(); |
| 255 | const size_t hot_threshold = options_.GetHotStartupMethodSamples(is_low_ram); |
Mathieu Chartier | c600eaa | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 256 | { |
Mathieu Chartier | 3910037 | 2017-05-17 13:14:10 -0700 | [diff] [blame] | 257 | ScopedObjectAccess soa(self); |
| 258 | gc::ScopedGCCriticalSection sgcs(self, |
| 259 | gc::kGcCauseProfileSaver, |
| 260 | gc::kCollectorTypeCriticalSection); |
Mathieu Chartier | 3910037 | 2017-05-17 13:14:10 -0700 | [diff] [blame] | 261 | { |
| 262 | ScopedTrace trace2("Get hot methods"); |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 263 | GetClassesAndMethodsVisitor visitor(&hot_methods, |
| 264 | &startup_methods, |
| 265 | &resolved_classes, |
| 266 | hot_threshold); |
| 267 | runtime->GetClassLinker()->VisitClasses(&visitor); |
Mathieu Chartier | 3910037 | 2017-05-17 13:14:10 -0700 | [diff] [blame] | 268 | } |
Mathieu Chartier | c600eaa | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 269 | } |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 270 | |
Mathieu Chartier | 3910037 | 2017-05-17 13:14:10 -0700 | [diff] [blame] | 271 | MutexLock mu(self, *Locks::profiler_lock_); |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 272 | uint64_t total_number_of_profile_entries_cached = 0; |
Mathieu Chartier | 9275af6 | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 273 | |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 274 | for (const auto& it : tracked_dex_base_locations_) { |
Mathieu Chartier | 9275af6 | 2016-04-29 12:03:56 -0700 | [diff] [blame] | 275 | std::set<DexCacheResolvedClasses> resolved_classes_for_location; |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 276 | const std::string& filename = it.first; |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 277 | auto info_it = profile_cache_.Put( |
| 278 | filename, |
| 279 | new ProfileCompilationInfo(Runtime::Current()->GetArenaPool())); |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 280 | ProfileCompilationInfo* cached_info = info_it->second; |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 281 | |
| 282 | const std::set<std::string>& locations = it.second; |
| 283 | for (const auto& pair : hot_methods.GetMap()) { |
| 284 | const DexFile* const dex_file = pair.first; |
| 285 | if (locations.find(dex_file->GetBaseLocation()) != locations.end()) { |
| 286 | cached_info->AddSampledMethodsForDex(/*startup*/ true, |
| 287 | dex_file, |
| 288 | pair.second.begin(), |
| 289 | pair.second.end()); |
| 290 | // Adding hot methods is a bit slow, TODO: optimize. |
| 291 | cached_info->AddHotMethodsForDex(dex_file, pair.second.begin(), pair.second.end()); |
| 292 | } |
| 293 | } |
| 294 | for (const auto& pair : startup_methods.GetMap()) { |
| 295 | const DexFile* const dex_file = pair.first; |
| 296 | if (locations.find(dex_file->GetBaseLocation()) != locations.end()) { |
| 297 | cached_info->AddSampledMethodsForDex(/*startup*/ true, |
| 298 | dex_file, |
| 299 | pair.second.begin(), |
| 300 | pair.second.end()); |
| 301 | } |
| 302 | } |
| 303 | for (const auto& pair : resolved_classes.GetMap()) { |
| 304 | const DexFile* const dex_file = pair.first; |
| 305 | if (locations.find(dex_file->GetBaseLocation()) != locations.end()) { |
| 306 | const TypeReferenceCollection::IndexVector& classes = pair.second; |
| 307 | VLOG(profiler) << "Added " << classes.size() << " classes for location " |
| 308 | << dex_file->GetBaseLocation() |
| 309 | << " (" << dex_file->GetLocation() << ")"; |
| 310 | cached_info->AddClassesForDex(dex_file, classes.begin(), classes.end()); |
| 311 | } else { |
| 312 | VLOG(profiler) << "Location not found " << dex_file->GetBaseLocation() |
| 313 | << " (" << dex_file->GetLocation() << ")"; |
| 314 | } |
| 315 | } |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 316 | total_number_of_profile_entries_cached += resolved_classes_for_location.size(); |
| 317 | } |
| 318 | max_number_of_profile_entries_cached_ = std::max( |
| 319 | max_number_of_profile_entries_cached_, |
| 320 | total_number_of_profile_entries_cached); |
Mathieu Chartier | faf8320 | 2017-06-08 10:35:20 -0700 | [diff] [blame] | 321 | VLOG(profiler) << "Profile saver recorded " << hot_methods.NumReferences() << " hot methods and " |
| 322 | << startup_methods.NumReferences() << " startup methods with threshold " |
| 323 | << hot_threshold << " in " << PrettyDuration(NanoTime() - start_time); |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 326 | 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] | 327 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 328 | |
| 329 | // Resolve any new registered locations. |
| 330 | ResolveTrackedLocations(); |
| 331 | |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 332 | SafeMap<std::string, std::set<std::string>> tracked_locations; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 333 | { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 334 | // Make a copy so that we don't hold the lock while doing I/O. |
| 335 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 336 | tracked_locations = tracked_dex_base_locations_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 337 | } |
Calin Juravle | c15e566 | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 338 | |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 339 | bool profile_file_saved = false; |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 340 | if (number_of_new_methods != nullptr) { |
| 341 | *number_of_new_methods = 0; |
| 342 | } |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 343 | |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 344 | for (const auto& it : tracked_locations) { |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 345 | if (!force_save && ShuttingDown(Thread::Current())) { |
| 346 | // The ProfileSaver is in shutdown mode, meaning a stop request was made and |
| 347 | // we need to exit cleanly (by waiting for the saver thread to finish). Unless |
| 348 | // we have a request for a forced save, do not do any processing so that we |
| 349 | // speed up the exit. |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 350 | return true; |
| 351 | } |
| 352 | const std::string& filename = it.first; |
| 353 | const std::set<std::string>& locations = it.second; |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 354 | std::vector<ProfileMethodInfo> profile_methods; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 355 | { |
| 356 | ScopedObjectAccess soa(Thread::Current()); |
Calin Juravle | 940eb0c | 2017-01-30 19:30:44 -0800 | [diff] [blame] | 357 | jit_code_cache_->GetProfiledMethods(locations, profile_methods); |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 358 | total_number_of_code_cache_queries_++; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 359 | } |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 360 | { |
| 361 | ProfileCompilationInfo info(Runtime::Current()->GetArenaPool()); |
| 362 | if (!info.Load(filename, /*clear_if_invalid*/ true)) { |
| 363 | LOG(WARNING) << "Could not forcefully load profile " << filename; |
| 364 | continue; |
| 365 | } |
| 366 | uint64_t last_save_number_of_methods = info.GetNumberOfMethods(); |
| 367 | uint64_t last_save_number_of_classes = info.GetNumberOfResolvedClasses(); |
Calin Juravle | c15e566 | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 368 | |
Mathieu Chartier | ea650f3 | 2017-05-24 12:04:13 -0700 | [diff] [blame] | 369 | info.AddMethodsAndClasses(profile_methods, std::set<DexCacheResolvedClasses>()); |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 370 | auto profile_cache_it = profile_cache_.find(filename); |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 371 | if (profile_cache_it != profile_cache_.end()) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 372 | info.MergeWith(*(profile_cache_it->second)); |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 373 | } |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 374 | |
| 375 | int64_t delta_number_of_methods = |
| 376 | info.GetNumberOfMethods() - last_save_number_of_methods; |
| 377 | int64_t delta_number_of_classes = |
| 378 | info.GetNumberOfResolvedClasses() - last_save_number_of_classes; |
| 379 | |
| 380 | if (!force_save && |
| 381 | delta_number_of_methods < options_.GetMinMethodsToSave() && |
| 382 | delta_number_of_classes < options_.GetMinClassesToSave()) { |
| 383 | VLOG(profiler) << "Not enough information to save to: " << filename |
| 384 | << " Number of methods: " << delta_number_of_methods |
| 385 | << " Number of classes: " << delta_number_of_classes; |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 386 | total_number_of_skipped_writes_++; |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 387 | continue; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 388 | } |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 389 | if (number_of_new_methods != nullptr) { |
| 390 | *number_of_new_methods = |
| 391 | std::max(static_cast<uint16_t>(delta_number_of_methods), |
| 392 | *number_of_new_methods); |
| 393 | } |
| 394 | uint64_t bytes_written; |
| 395 | // Force the save. In case the profile data is corrupted or the the profile |
| 396 | // has the wrong version this will "fix" the file to the correct format. |
| 397 | if (info.Save(filename, &bytes_written)) { |
| 398 | // We managed to save the profile. Clear the cache stored during startup. |
| 399 | if (profile_cache_it != profile_cache_.end()) { |
| 400 | ProfileCompilationInfo *cached_info = profile_cache_it->second; |
| 401 | profile_cache_.erase(profile_cache_it); |
| 402 | delete cached_info; |
| 403 | } |
| 404 | if (bytes_written > 0) { |
| 405 | total_number_of_writes_++; |
| 406 | total_bytes_written_ += bytes_written; |
| 407 | profile_file_saved = true; |
| 408 | } else { |
| 409 | // At this point we could still have avoided the write. |
| 410 | // We load and merge the data from the file lazily at its first ever |
| 411 | // save attempt. So, whatever we are trying to save could already be |
| 412 | // in the file. |
| 413 | total_number_of_skipped_writes_++; |
| 414 | } |
| 415 | } else { |
| 416 | LOG(WARNING) << "Could not save profiling info to " << filename; |
| 417 | total_number_of_failed_writes_++; |
| 418 | } |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 419 | } |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 420 | // Trim the maps to madvise the pages used for profile info. |
| 421 | // It is unlikely we will need them again in the near feature. |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 422 | Runtime::Current()->GetArenaPool()->TrimMaps(); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 423 | } |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 424 | |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 425 | return profile_file_saved; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void* ProfileSaver::RunProfileSaverThread(void* arg) { |
| 429 | Runtime* runtime = Runtime::Current(); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 430 | |
Calin Juravle | e55fda1 | 2016-04-28 12:59:33 +0100 | [diff] [blame] | 431 | bool attached = runtime->AttachCurrentThread("Profile Saver", |
| 432 | /*as_daemon*/true, |
| 433 | runtime->GetSystemThreadGroup(), |
| 434 | /*create_peer*/true); |
| 435 | if (!attached) { |
| 436 | CHECK(runtime->IsShuttingDown(Thread::Current())); |
| 437 | return nullptr; |
| 438 | } |
| 439 | |
| 440 | ProfileSaver* profile_saver = reinterpret_cast<ProfileSaver*>(arg); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 441 | profile_saver->Run(); |
| 442 | |
| 443 | runtime->DetachCurrentThread(); |
| 444 | VLOG(profiler) << "Profile saver shutdown"; |
| 445 | return nullptr; |
| 446 | } |
| 447 | |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 448 | static bool ShouldProfileLocation(const std::string& location) { |
Calin Juravle | 7506423 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 449 | OatFileManager& oat_manager = Runtime::Current()->GetOatFileManager(); |
| 450 | const OatFile* oat_file = oat_manager.FindOpenedOatFileFromDexLocation(location); |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 451 | if (oat_file == nullptr) { |
| 452 | // This can happen if we fallback to run code directly from the APK. |
| 453 | // Profile it with the hope that the background dexopt will get us back into |
| 454 | // a good state. |
Calin Juravle | 7506423 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 455 | VLOG(profiler) << "Asked to profile a location without an oat file:" << location; |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 456 | return true; |
| 457 | } |
| 458 | CompilerFilter::Filter filter = oat_file->GetCompilerFilter(); |
Calin Juravle | d19dc46 | 2016-04-19 18:17:41 +0100 | [diff] [blame] | 459 | if ((filter == CompilerFilter::kSpeed) || (filter == CompilerFilter::kEverything)) { |
Calin Juravle | 7506423 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 460 | VLOG(profiler) |
Calin Juravle | d19dc46 | 2016-04-19 18:17:41 +0100 | [diff] [blame] | 461 | << "Skip profiling oat file because it's already speed|everything compiled: " |
| 462 | << location << " oat location: " << oat_file->GetLocation(); |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 463 | return false; |
| 464 | } |
| 465 | return true; |
| 466 | } |
| 467 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 468 | void ProfileSaver::Start(const ProfileSaverOptions& options, |
| 469 | const std::string& output_filename, |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 470 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 471 | const std::vector<std::string>& code_paths) { |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 472 | DCHECK(options.IsEnabled()); |
| 473 | DCHECK(Runtime::Current()->GetJit() != nullptr); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 474 | DCHECK(!output_filename.empty()); |
| 475 | DCHECK(jit_code_cache != nullptr); |
| 476 | |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 477 | std::vector<std::string> code_paths_to_profile; |
| 478 | |
| 479 | for (const std::string& location : code_paths) { |
| 480 | if (ShouldProfileLocation(location)) { |
| 481 | code_paths_to_profile.push_back(location); |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | if (code_paths_to_profile.empty()) { |
Calin Juravle | 7506423 | 2016-04-18 16:38:27 +0100 | [diff] [blame] | 485 | VLOG(profiler) << "No code paths should be profiled."; |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 486 | return; |
| 487 | } |
| 488 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 489 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 490 | if (instance_ != nullptr) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 491 | // If we already have an instance, make sure it uses the same jit_code_cache. |
| 492 | // This may be called multiple times via Runtime::registerAppInfo (e.g. for |
| 493 | // apps which share the same runtime). |
| 494 | DCHECK_EQ(instance_->jit_code_cache_, jit_code_cache); |
| 495 | // Add the code_paths to the tracked locations. |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 496 | instance_->AddTrackedLocations(output_filename, code_paths_to_profile); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | |
| 500 | VLOG(profiler) << "Starting profile saver using output file: " << output_filename |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 501 | << ". Tracking: " << android::base::Join(code_paths_to_profile, ':'); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 502 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 503 | instance_ = new ProfileSaver(options, |
| 504 | output_filename, |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 505 | jit_code_cache, |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 506 | code_paths_to_profile); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 507 | |
| 508 | // Create a new thread which does the saving. |
| 509 | CHECK_PTHREAD_CALL( |
| 510 | pthread_create, |
| 511 | (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)), |
| 512 | "Profile saver thread"); |
Nicolas Geoffray | 23caed8 | 2017-04-21 14:30:18 +0100 | [diff] [blame] | 513 | |
| 514 | #if defined(ART_TARGET_ANDROID) |
| 515 | // At what priority to schedule the saver threads. 9 is the lowest foreground priority on device. |
| 516 | static constexpr int kProfileSaverPthreadPriority = 9; |
| 517 | int result = setpriority( |
| 518 | PRIO_PROCESS, pthread_gettid_np(profiler_pthread_), kProfileSaverPthreadPriority); |
| 519 | if (result != 0) { |
| 520 | PLOG(ERROR) << "Failed to setpriority to :" << kProfileSaverPthreadPriority; |
| 521 | } |
| 522 | #endif |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 525 | void ProfileSaver::Stop(bool dump_info) { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 526 | ProfileSaver* profile_saver = nullptr; |
| 527 | pthread_t profiler_pthread = 0U; |
| 528 | |
| 529 | { |
| 530 | MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 531 | VLOG(profiler) << "Stopping profile saver thread"; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 532 | profile_saver = instance_; |
| 533 | profiler_pthread = profiler_pthread_; |
| 534 | if (instance_ == nullptr) { |
| 535 | DCHECK(false) << "Tried to stop a profile saver which was not started"; |
| 536 | return; |
| 537 | } |
| 538 | if (instance_->shutting_down_) { |
| 539 | DCHECK(false) << "Tried to stop the profile saver twice"; |
| 540 | return; |
| 541 | } |
| 542 | instance_->shutting_down_ = true; |
| 543 | } |
| 544 | |
| 545 | { |
| 546 | // Wake up the saver thread if it is sleeping to allow for a clean exit. |
| 547 | MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_); |
| 548 | profile_saver->period_condition_.Signal(Thread::Current()); |
| 549 | } |
| 550 | |
| 551 | // Wait for the saver thread to stop. |
| 552 | CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown"); |
| 553 | |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 554 | // Force save everything before destroying the instance. |
| 555 | instance_->ProcessProfilingInfo(/*force_save*/true, /*number_of_new_methods*/nullptr); |
| 556 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 557 | { |
| 558 | MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | 9db22e8 | 2017-03-30 16:31:23 -0700 | [diff] [blame] | 559 | if (dump_info) { |
| 560 | instance_->DumpInfo(LOG_STREAM(INFO)); |
| 561 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 562 | instance_ = nullptr; |
| 563 | profiler_pthread_ = 0U; |
| 564 | } |
| 565 | delete profile_saver; |
| 566 | } |
| 567 | |
| 568 | bool ProfileSaver::ShuttingDown(Thread* self) { |
| 569 | MutexLock mu(self, *Locks::profiler_lock_); |
| 570 | return shutting_down_; |
| 571 | } |
| 572 | |
| 573 | bool ProfileSaver::IsStarted() { |
| 574 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 575 | return instance_ != nullptr; |
| 576 | } |
| 577 | |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 578 | static void AddTrackedLocationsToMap(const std::string& output_filename, |
| 579 | const std::vector<std::string>& code_paths, |
| 580 | SafeMap<std::string, std::set<std::string>>* map) { |
| 581 | auto it = map->find(output_filename); |
| 582 | if (it == map->end()) { |
| 583 | 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] | 584 | } else { |
| 585 | it->second.insert(code_paths.begin(), code_paths.end()); |
| 586 | } |
| 587 | } |
| 588 | |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 589 | void ProfileSaver::AddTrackedLocations(const std::string& output_filename, |
| 590 | const std::vector<std::string>& code_paths) { |
| 591 | // Add the code paths to the list of tracked location. |
| 592 | AddTrackedLocationsToMap(output_filename, code_paths, &tracked_dex_base_locations_); |
| 593 | // The code paths may contain symlinks which could fool the profiler. |
| 594 | // If the dex file is compiled with an absolute location but loaded with symlink |
| 595 | // the profiler could skip the dex due to location mismatch. |
| 596 | // To avoid this, we add the code paths to the temporary cache of 'to_be_resolved' |
| 597 | // locations. When the profiler thread executes we will resolve the paths to their |
| 598 | // real paths. |
| 599 | // Note that we delay taking the realpath to avoid spending more time than needed |
| 600 | // when registering location (as it is done during app launch). |
| 601 | AddTrackedLocationsToMap(output_filename, |
| 602 | code_paths, |
| 603 | &tracked_dex_base_locations_to_be_resolved_); |
| 604 | } |
| 605 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 606 | void ProfileSaver::DumpInstanceInfo(std::ostream& os) { |
| 607 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 608 | if (instance_ != nullptr) { |
| 609 | instance_->DumpInfo(os); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | void ProfileSaver::DumpInfo(std::ostream& os) { |
| 614 | os << "ProfileSaver total_bytes_written=" << total_bytes_written_ << '\n' |
| 615 | << "ProfileSaver total_number_of_writes=" << total_number_of_writes_ << '\n' |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 616 | << "ProfileSaver total_number_of_code_cache_queries=" |
| 617 | << total_number_of_code_cache_queries_ << '\n' |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 618 | << "ProfileSaver total_number_of_skipped_writes=" << total_number_of_skipped_writes_ << '\n' |
| 619 | << "ProfileSaver total_number_of_failed_writes=" << total_number_of_failed_writes_ << '\n' |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 620 | << "ProfileSaver total_ms_of_sleep=" << total_ms_of_sleep_ << '\n' |
Calin Juravle | 6044fa7 | 2016-03-25 17:17:09 +0000 | [diff] [blame] | 621 | << "ProfileSaver total_ms_of_work=" << NsToMs(total_ns_of_work_) << '\n' |
Calin Juravle | 85f7bf3 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 622 | << "ProfileSaver max_number_profile_entries_cached=" |
Calin Juravle | 5fbb0fe | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 623 | << max_number_of_profile_entries_cached_ << '\n' |
| 624 | << "ProfileSaver total_number_of_hot_spikes=" << total_number_of_hot_spikes_ << '\n' |
| 625 | << "ProfileSaver total_number_of_wake_ups=" << total_number_of_wake_ups_ << '\n'; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 628 | |
| 629 | void ProfileSaver::ForceProcessProfiles() { |
| 630 | ProfileSaver* saver = nullptr; |
| 631 | { |
| 632 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 633 | saver = instance_; |
| 634 | } |
| 635 | // TODO(calin): this is not actually thread safe as the instance_ may have been deleted, |
| 636 | // but we only use this in testing when we now this won't happen. |
| 637 | // Refactor the way we handle the instance so that we don't end up in this situation. |
| 638 | if (saver != nullptr) { |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 639 | saver->ProcessProfilingInfo(/*force_save*/true, /*number_of_new_methods*/nullptr); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
| 643 | bool ProfileSaver::HasSeenMethod(const std::string& profile, |
| 644 | const DexFile* dex_file, |
| 645 | uint16_t method_idx) { |
| 646 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 647 | if (instance_ != nullptr) { |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 648 | ProfileCompilationInfo info(Runtime::Current()->GetArenaPool()); |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 649 | if (!info.Load(profile, /*clear_if_invalid*/false)) { |
| 650 | return false; |
| 651 | } |
Mathieu Chartier | 7b135c8 | 2017-06-05 12:54:01 -0700 | [diff] [blame] | 652 | return info.ContainsHotMethod(MethodReference(dex_file, method_idx)); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 653 | } |
| 654 | return false; |
| 655 | } |
| 656 | |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 657 | void ProfileSaver::ResolveTrackedLocations() { |
| 658 | SafeMap<std::string, std::set<std::string>> locations_to_be_resolved; |
| 659 | { |
| 660 | // Make a copy so that we don't hold the lock while doing I/O. |
| 661 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 662 | locations_to_be_resolved = tracked_dex_base_locations_to_be_resolved_; |
| 663 | tracked_dex_base_locations_to_be_resolved_.clear(); |
| 664 | } |
| 665 | |
| 666 | // Resolve the locations. |
| 667 | SafeMap<std::string, std::vector<std::string>> resolved_locations_map; |
| 668 | for (const auto& it : locations_to_be_resolved) { |
| 669 | const std::string& filename = it.first; |
| 670 | const std::set<std::string>& locations = it.second; |
| 671 | auto resolved_locations_it = resolved_locations_map.Put( |
| 672 | filename, |
| 673 | std::vector<std::string>(locations.size())); |
| 674 | |
| 675 | for (const auto& location : locations) { |
| 676 | UniqueCPtr<const char[]> location_real(realpath(location.c_str(), nullptr)); |
| 677 | // Note that it's ok if we cannot get the real path. |
| 678 | if (location_real != nullptr) { |
| 679 | resolved_locations_it->second.emplace_back(location_real.get()); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | // Add the resolved locations to the tracked collection. |
| 685 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 686 | for (const auto& it : resolved_locations_map) { |
| 687 | AddTrackedLocationsToMap(it.first, it.second, &tracked_dex_base_locations_); |
| 688 | } |
| 689 | } |
| 690 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 691 | } // namespace art |