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