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" |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 24 | #include "base/systrace.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 25 | #include "scoped_thread_state_change.h" |
| 26 | #include "oat_file_manager.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
Calin Juravle | 932a051 | 2016-01-19 14:32:26 -0800 | [diff] [blame] | 30 | // An arbitrary value to throttle save requests. Set to 2s for now. |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 31 | static constexpr const uint64_t kMilisecondsToNano = 1000000; |
Calin Juravle | 932a051 | 2016-01-19 14:32:26 -0800 | [diff] [blame] | 32 | static constexpr const uint64_t kMinimumTimeBetweenCodeCacheUpdatesNs = 2000 * kMilisecondsToNano; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 33 | |
| 34 | // TODO: read the constants from ProfileOptions, |
| 35 | // Add a random delay each time we go to sleep so that we don't hammer the CPU |
| 36 | // with all profile savers running at the same time. |
Calin Juravle | 815759a | 2016-03-14 17:32:49 +0000 | [diff] [blame] | 37 | static constexpr const uint64_t kRandomDelayMaxMs = 40 * 1000; // 40 seconds |
Calin Juravle | 932a051 | 2016-01-19 14:32:26 -0800 | [diff] [blame] | 38 | static constexpr const uint64_t kMaxBackoffMs = 5 * 60 * 1000; // 5 minutes |
Calin Juravle | 815759a | 2016-03-14 17:32:49 +0000 | [diff] [blame] | 39 | static constexpr const uint64_t kSavePeriodMs = 40 * 1000; // 40 seconds |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 40 | static constexpr const uint64_t kInitialDelayMs = 2 * 1000; // 2 seconds |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 41 | static constexpr const double kBackoffCoef = 1.5; |
| 42 | |
| 43 | static constexpr const uint32_t kMinimumNrOrMethodsToSave = 10; |
| 44 | |
| 45 | ProfileSaver* ProfileSaver::instance_ = nullptr; |
| 46 | pthread_t ProfileSaver::profiler_pthread_ = 0U; |
| 47 | |
| 48 | ProfileSaver::ProfileSaver(const std::string& output_filename, |
| 49 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 50 | const std::vector<std::string>& code_paths, |
| 51 | const std::string& foreign_dex_profile_path, |
| 52 | const std::string& app_data_dir) |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 53 | : jit_code_cache_(jit_code_cache), |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 54 | foreign_dex_profile_path_(foreign_dex_profile_path), |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 55 | code_cache_last_update_time_ns_(0), |
| 56 | shutting_down_(false), |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 57 | first_profile_(true), |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 58 | wait_lock_("ProfileSaver wait lock"), |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 59 | period_condition_("ProfileSaver period condition", wait_lock_), |
| 60 | total_bytes_written_(0), |
| 61 | total_number_of_writes_(0), |
| 62 | total_number_of_code_cache_queries_(0), |
| 63 | total_number_of_skipped_writes_(0), |
| 64 | total_number_of_failed_writes_(0), |
| 65 | total_ns_of_sleep_(0), |
| 66 | total_ns_of_work_(0), |
| 67 | total_number_of_foreign_dex_marks_(0) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 68 | AddTrackedLocations(output_filename, code_paths); |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 69 | app_data_dir_ = ""; |
| 70 | if (!app_data_dir.empty()) { |
| 71 | // The application directory is used to determine which dex files are owned by app. |
| 72 | // Since it could be a symlink (e.g. /data/data instead of /data/user/0), and we |
| 73 | // don't have control over how the dex files are actually loaded (symlink or canonical path), |
| 74 | // store it's canonical form to be sure we use the same base when comparing. |
| 75 | UniqueCPtr<const char[]> app_data_dir_real_path(realpath(app_data_dir.c_str(), nullptr)); |
| 76 | if (app_data_dir_real_path != nullptr) { |
| 77 | app_data_dir_.assign(app_data_dir_real_path.get()); |
| 78 | } else { |
| 79 | LOG(WARNING) << "Failed to get the real path for app dir: " << app_data_dir_ |
| 80 | << ". The app dir will not be used to determine which dex files belong to the app"; |
| 81 | } |
| 82 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void ProfileSaver::Run() { |
| 86 | srand(MicroTime() * getpid()); |
| 87 | Thread* self = Thread::Current(); |
| 88 | |
| 89 | uint64_t save_period_ms = kSavePeriodMs; |
| 90 | VLOG(profiler) << "Save profiling information every " << save_period_ms << " ms"; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 91 | |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 92 | bool first_iteration = true; |
| 93 | while (!ShuttingDown(self)) { |
| 94 | uint64_t sleep_time_ms; |
| 95 | if (first_iteration) { |
| 96 | // Sleep less long for the first iteration since we want to record loaded classes shortly |
| 97 | // after app launch. |
| 98 | sleep_time_ms = kInitialDelayMs; |
| 99 | } else { |
| 100 | const uint64_t random_sleep_delay_ms = rand() % kRandomDelayMaxMs; |
| 101 | sleep_time_ms = save_period_ms + random_sleep_delay_ms; |
| 102 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 103 | { |
| 104 | MutexLock mu(self, wait_lock_); |
| 105 | period_condition_.TimedWait(self, sleep_time_ms, 0); |
| 106 | } |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 107 | total_ns_of_sleep_ += sleep_time_ms; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 108 | if (ShuttingDown(self)) { |
| 109 | break; |
| 110 | } |
| 111 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 112 | uint64_t start = NanoTime(); |
| 113 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 114 | if (!ProcessProfilingInfo() && save_period_ms < kMaxBackoffMs) { |
| 115 | // If we don't need to save now it is less likely that we will need to do |
| 116 | // so in the future. Increase the time between saves according to the |
| 117 | // kBackoffCoef, but make it no larger than kMaxBackoffMs. |
| 118 | save_period_ms = static_cast<uint64_t>(kBackoffCoef * save_period_ms); |
| 119 | } else { |
| 120 | // Reset the period to the initial value as it's highly likely to JIT again. |
| 121 | save_period_ms = kSavePeriodMs; |
| 122 | } |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 123 | first_iteration = false; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 124 | |
| 125 | total_ns_of_work_ += (NanoTime() - start); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | bool ProfileSaver::ProcessProfilingInfo() { |
Mathieu Chartier | dabdc0f | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 130 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 131 | uint64_t last_update_time_ns = jit_code_cache_->GetLastUpdateTimeNs(); |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 132 | if (!first_profile_ && last_update_time_ns - code_cache_last_update_time_ns_ |
| 133 | < kMinimumTimeBetweenCodeCacheUpdatesNs) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 134 | VLOG(profiler) << "Not enough time has passed since the last code cache update." |
| 135 | << "Last update: " << last_update_time_ns |
| 136 | << " Last save: " << code_cache_last_update_time_ns_; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 137 | total_number_of_skipped_writes_++; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 141 | code_cache_last_update_time_ns_ = last_update_time_ns; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 142 | SafeMap<std::string, std::set<std::string>> tracked_locations; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 143 | { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 144 | // Make a copy so that we don't hold the lock while doing I/O. |
| 145 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 146 | tracked_locations = tracked_dex_base_locations_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 147 | } |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 148 | for (const auto& it : tracked_locations) { |
| 149 | if (ShuttingDown(Thread::Current())) { |
| 150 | return true; |
| 151 | } |
| 152 | const std::string& filename = it.first; |
| 153 | const std::set<std::string>& locations = it.second; |
| 154 | std::vector<ArtMethod*> methods; |
| 155 | { |
| 156 | ScopedObjectAccess soa(Thread::Current()); |
| 157 | jit_code_cache_->GetCompiledArtMethods(locations, methods); |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 158 | total_number_of_code_cache_queries_++; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 159 | } |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 160 | // Always save for the first one for loaded classes profile. |
| 161 | if (methods.size() < kMinimumNrOrMethodsToSave && !first_profile_) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 162 | VLOG(profiler) << "Not enough information to save to: " << filename |
| 163 | <<" Nr of methods: " << methods.size(); |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 164 | total_number_of_skipped_writes_++; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 165 | return false; |
| 166 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 167 | |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 168 | std::set<DexCacheResolvedClasses> resolved_classes; |
| 169 | if (first_profile_) { |
| 170 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 171 | resolved_classes = class_linker->GetResolvedClasses(/*ignore boot classes*/true); |
| 172 | } |
| 173 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 174 | uint64_t bytes_written; |
| 175 | if (!ProfileCompilationInfo::SaveProfilingInfo(filename, methods, |
| 176 | resolved_classes, |
| 177 | &bytes_written)) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 178 | LOG(WARNING) << "Could not save profiling info to " << filename; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 179 | total_number_of_failed_writes_++; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 180 | return false; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 181 | } else { |
| 182 | if (bytes_written > 0) { |
| 183 | total_number_of_writes_++; |
| 184 | total_bytes_written_ += bytes_written; |
| 185 | } |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 186 | } |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 187 | } |
Mathieu Chartier | 8913fc1 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 188 | first_profile_ = false; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
| 192 | void* ProfileSaver::RunProfileSaverThread(void* arg) { |
| 193 | Runtime* runtime = Runtime::Current(); |
| 194 | ProfileSaver* profile_saver = reinterpret_cast<ProfileSaver*>(arg); |
| 195 | |
| 196 | CHECK(runtime->AttachCurrentThread("Profile Saver", |
| 197 | /*as_daemon*/true, |
| 198 | runtime->GetSystemThreadGroup(), |
| 199 | /*create_peer*/true)); |
| 200 | profile_saver->Run(); |
| 201 | |
| 202 | runtime->DetachCurrentThread(); |
| 203 | VLOG(profiler) << "Profile saver shutdown"; |
| 204 | return nullptr; |
| 205 | } |
| 206 | |
| 207 | void ProfileSaver::Start(const std::string& output_filename, |
| 208 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 209 | const std::vector<std::string>& code_paths, |
| 210 | const std::string& foreign_dex_profile_path, |
| 211 | const std::string& app_data_dir) { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 212 | DCHECK(Runtime::Current()->UseJit()); |
| 213 | DCHECK(!output_filename.empty()); |
| 214 | DCHECK(jit_code_cache != nullptr); |
| 215 | |
| 216 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 217 | if (instance_ != nullptr) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 218 | // If we already have an instance, make sure it uses the same jit_code_cache. |
| 219 | // This may be called multiple times via Runtime::registerAppInfo (e.g. for |
| 220 | // apps which share the same runtime). |
| 221 | DCHECK_EQ(instance_->jit_code_cache_, jit_code_cache); |
| 222 | // Add the code_paths to the tracked locations. |
| 223 | instance_->AddTrackedLocations(output_filename, code_paths); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | |
| 227 | VLOG(profiler) << "Starting profile saver using output file: " << output_filename |
| 228 | << ". Tracking: " << Join(code_paths, ':'); |
| 229 | |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 230 | instance_ = new ProfileSaver(output_filename, |
| 231 | jit_code_cache, |
| 232 | code_paths, |
| 233 | foreign_dex_profile_path, |
| 234 | app_data_dir); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 235 | |
| 236 | // Create a new thread which does the saving. |
| 237 | CHECK_PTHREAD_CALL( |
| 238 | pthread_create, |
| 239 | (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)), |
| 240 | "Profile saver thread"); |
| 241 | } |
| 242 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 243 | void ProfileSaver::Stop(bool dump_info) { |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 244 | ProfileSaver* profile_saver = nullptr; |
| 245 | pthread_t profiler_pthread = 0U; |
| 246 | |
| 247 | { |
| 248 | MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 249 | VLOG(profiler) << "Stopping profile saver thread"; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 250 | profile_saver = instance_; |
| 251 | profiler_pthread = profiler_pthread_; |
| 252 | if (instance_ == nullptr) { |
| 253 | DCHECK(false) << "Tried to stop a profile saver which was not started"; |
| 254 | return; |
| 255 | } |
| 256 | if (instance_->shutting_down_) { |
| 257 | DCHECK(false) << "Tried to stop the profile saver twice"; |
| 258 | return; |
| 259 | } |
| 260 | instance_->shutting_down_ = true; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 261 | if (dump_info) { |
| 262 | instance_->DumpInfo(LOG(INFO)); |
| 263 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | { |
| 267 | // Wake up the saver thread if it is sleeping to allow for a clean exit. |
| 268 | MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_); |
| 269 | profile_saver->period_condition_.Signal(Thread::Current()); |
| 270 | } |
| 271 | |
| 272 | // Wait for the saver thread to stop. |
| 273 | CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown"); |
| 274 | |
| 275 | { |
| 276 | MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_); |
| 277 | instance_ = nullptr; |
| 278 | profiler_pthread_ = 0U; |
| 279 | } |
| 280 | delete profile_saver; |
| 281 | } |
| 282 | |
| 283 | bool ProfileSaver::ShuttingDown(Thread* self) { |
| 284 | MutexLock mu(self, *Locks::profiler_lock_); |
| 285 | return shutting_down_; |
| 286 | } |
| 287 | |
| 288 | bool ProfileSaver::IsStarted() { |
| 289 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 290 | return instance_ != nullptr; |
| 291 | } |
| 292 | |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 293 | void ProfileSaver::AddTrackedLocations(const std::string& output_filename, |
| 294 | const std::vector<std::string>& code_paths) { |
| 295 | auto it = tracked_dex_base_locations_.find(output_filename); |
| 296 | if (it == tracked_dex_base_locations_.end()) { |
| 297 | tracked_dex_base_locations_.Put(output_filename, |
| 298 | std::set<std::string>(code_paths.begin(), code_paths.end())); |
| 299 | } else { |
| 300 | it->second.insert(code_paths.begin(), code_paths.end()); |
| 301 | } |
| 302 | } |
| 303 | |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 304 | void ProfileSaver::NotifyDexUse(const std::string& dex_location) { |
| 305 | std::set<std::string> app_code_paths; |
| 306 | std::string foreign_dex_profile_path; |
| 307 | std::string app_data_dir; |
| 308 | { |
| 309 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 310 | if (instance_ == nullptr) { |
| 311 | return; |
| 312 | } |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 313 | // Make a copy so that we don't hold the lock while doing I/O. |
| 314 | for (const auto& it : instance_->tracked_dex_base_locations_) { |
| 315 | app_code_paths.insert(it.second.begin(), it.second.end()); |
| 316 | } |
| 317 | foreign_dex_profile_path = instance_->foreign_dex_profile_path_; |
| 318 | app_data_dir = instance_->app_data_dir_; |
| 319 | } |
| 320 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 321 | bool mark_created = MaybeRecordDexUseInternal(dex_location, |
| 322 | app_code_paths, |
| 323 | foreign_dex_profile_path, |
| 324 | app_data_dir); |
| 325 | if (mark_created) { |
| 326 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 327 | if (instance_ != nullptr) { |
| 328 | instance_->total_number_of_foreign_dex_marks_++; |
| 329 | } |
| 330 | } |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 333 | bool ProfileSaver::MaybeRecordDexUseInternal( |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 334 | const std::string& dex_location, |
| 335 | const std::set<std::string>& app_code_paths, |
| 336 | const std::string& foreign_dex_profile_path, |
| 337 | const std::string& app_data_dir) { |
Calin Juravle | 1fae45f | 2016-03-08 12:52:52 +0000 | [diff] [blame] | 338 | if (dex_location.empty()) { |
| 339 | LOG(WARNING) << "Asked to record foreign dex use with an empty dex location."; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 340 | return false; |
Calin Juravle | 1fae45f | 2016-03-08 12:52:52 +0000 | [diff] [blame] | 341 | } |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 342 | if (foreign_dex_profile_path.empty()) { |
| 343 | LOG(WARNING) << "Asked to record foreign dex use without a valid profile path "; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 344 | return false; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | UniqueCPtr<const char[]> dex_location_real_path(realpath(dex_location.c_str(), nullptr)); |
Calin Juravle | 1fae45f | 2016-03-08 12:52:52 +0000 | [diff] [blame] | 348 | if (dex_location_real_path == nullptr) { |
| 349 | PLOG(WARNING) << "Could not get realpath for " << dex_location; |
| 350 | } |
| 351 | std::string dex_location_real_path_str((dex_location_real_path == nullptr) |
| 352 | ? dex_location.c_str() |
| 353 | : dex_location_real_path.get()); |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 354 | |
| 355 | if (dex_location_real_path_str.compare(0, app_data_dir.length(), app_data_dir) == 0) { |
| 356 | // The dex location is under the application folder. Nothing to record. |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 357 | return false; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | if (app_code_paths.find(dex_location) != app_code_paths.end()) { |
| 361 | // The dex location belongs to the application code paths. Nothing to record. |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 362 | return false; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 363 | } |
| 364 | // Do another round of checks with the real paths. |
| 365 | // Note that we could cache all the real locations in the saver (since it's an expensive |
| 366 | // operation). However we expect that app_code_paths is small (usually 1 element), and |
| 367 | // NotifyDexUse is called just a few times in the app lifetime. So we make the compromise |
| 368 | // to save some bytes of memory usage. |
| 369 | for (const auto& app_code_location : app_code_paths) { |
| 370 | UniqueCPtr<const char[]> real_app_code_location(realpath(app_code_location.c_str(), nullptr)); |
Calin Juravle | 1fae45f | 2016-03-08 12:52:52 +0000 | [diff] [blame] | 371 | if (real_app_code_location == nullptr) { |
| 372 | PLOG(WARNING) << "Could not get realpath for " << app_code_location; |
| 373 | } |
| 374 | std::string real_app_code_location_str((real_app_code_location == nullptr) |
| 375 | ? app_code_location.c_str() |
| 376 | : real_app_code_location.get()); |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 377 | if (real_app_code_location_str == dex_location_real_path_str) { |
| 378 | // The dex location belongs to the application code paths. Nothing to record. |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 379 | return false; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | // For foreign dex files we record a flag on disk. PackageManager will (potentially) take this |
| 384 | // into account when deciding how to optimize the loaded dex file. |
| 385 | // The expected flag name is the canonical path of the apk where '/' is substituted to '@'. |
| 386 | // (it needs to be kept in sync with |
| 387 | // frameworks/base/services/core/java/com/android/server/pm/PackageDexOptimizer.java) |
| 388 | std::replace(dex_location_real_path_str.begin(), dex_location_real_path_str.end(), '/', '@'); |
| 389 | std::string flag_path = foreign_dex_profile_path + "/" + dex_location_real_path_str; |
| 390 | // No need to give any sort of access to flag_path. The system has enough permissions |
| 391 | // to test for its existence. |
| 392 | int fd = TEMP_FAILURE_RETRY(open(flag_path.c_str(), O_CREAT | O_EXCL, 0)); |
| 393 | if (fd != -1) { |
| 394 | if (close(fd) != 0) { |
| 395 | PLOG(WARNING) << "Could not close file after flagging foreign dex use " << flag_path; |
| 396 | } |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 397 | return true; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 398 | } else { |
| 399 | if (errno != EEXIST) { |
| 400 | // Another app could have already created the file. |
| 401 | PLOG(WARNING) << "Could not create foreign dex use mark " << flag_path; |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 402 | return false; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 403 | } |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 404 | return true; |
Calin Juravle | 86a9ebe | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Calin Juravle | c19c1c2 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 408 | void ProfileSaver::DumpInstanceInfo(std::ostream& os) { |
| 409 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 410 | if (instance_ != nullptr) { |
| 411 | instance_->DumpInfo(os); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | void ProfileSaver::DumpInfo(std::ostream& os) { |
| 416 | os << "ProfileSaver total_bytes_written=" << total_bytes_written_ << '\n' |
| 417 | << "ProfileSaver total_number_of_writes=" << total_number_of_writes_ << '\n' |
| 418 | << "ProfileSaver total_number_of_code_cache_queries=" << total_number_of_code_cache_queries_ << '\n' |
| 419 | << "ProfileSaver total_number_of_skipped_writes=" << total_number_of_skipped_writes_ << '\n' |
| 420 | << "ProfileSaver total_number_of_failed_writes=" << total_number_of_failed_writes_ << '\n' |
| 421 | << "ProfileSaver total_ms_of_sleep=" << (total_ns_of_sleep_ / kMilisecondsToNano) << '\n' |
| 422 | << "ProfileSaver total_ms_of_work=" << (total_ns_of_work_ / kMilisecondsToNano) << '\n' |
| 423 | << "ProfileSaver total_number_of_foreign_dex_marks=" << total_number_of_foreign_dex_marks_ << '\n'; |
| 424 | } |
| 425 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 426 | } // namespace art |