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