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