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