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 | |
| 19 | #include "art_method-inl.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame^] | 20 | #include "base/systrace.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 21 | #include "scoped_thread_state_change.h" |
| 22 | #include "oat_file_manager.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
Calin Juravle | 932a051 | 2016-01-19 14:32:26 -0800 | [diff] [blame] | 26 | // An arbitrary value to throttle save requests. Set to 2s for now. |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 27 | static constexpr const uint64_t kMilisecondsToNano = 1000000; |
Calin Juravle | 932a051 | 2016-01-19 14:32:26 -0800 | [diff] [blame] | 28 | static constexpr const uint64_t kMinimumTimeBetweenCodeCacheUpdatesNs = 2000 * kMilisecondsToNano; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 29 | |
| 30 | // TODO: read the constants from ProfileOptions, |
| 31 | // Add a random delay each time we go to sleep so that we don't hammer the CPU |
| 32 | // with all profile savers running at the same time. |
Calin Juravle | 932a051 | 2016-01-19 14:32:26 -0800 | [diff] [blame] | 33 | static constexpr const uint64_t kRandomDelayMaxMs = 20 * 1000; // 20 seconds |
| 34 | static constexpr const uint64_t kMaxBackoffMs = 5 * 60 * 1000; // 5 minutes |
| 35 | static constexpr const uint64_t kSavePeriodMs = 10 * 1000; // 10 seconds |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 36 | static constexpr const uint64_t kInitialDelayMs = 2 * 1000; // 2 seconds |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 37 | static constexpr const double kBackoffCoef = 1.5; |
| 38 | |
| 39 | static constexpr const uint32_t kMinimumNrOrMethodsToSave = 10; |
| 40 | |
| 41 | ProfileSaver* ProfileSaver::instance_ = nullptr; |
| 42 | pthread_t ProfileSaver::profiler_pthread_ = 0U; |
| 43 | |
| 44 | ProfileSaver::ProfileSaver(const std::string& output_filename, |
| 45 | jit::JitCodeCache* jit_code_cache, |
| 46 | const std::vector<std::string>& code_paths) |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 47 | : jit_code_cache_(jit_code_cache), |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 48 | code_cache_last_update_time_ns_(0), |
| 49 | shutting_down_(false), |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 50 | first_profile_(true), |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 51 | wait_lock_("ProfileSaver wait lock"), |
| 52 | period_condition_("ProfileSaver period condition", wait_lock_) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 53 | AddTrackedLocations(output_filename, code_paths); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void ProfileSaver::Run() { |
| 57 | srand(MicroTime() * getpid()); |
| 58 | Thread* self = Thread::Current(); |
| 59 | |
| 60 | uint64_t save_period_ms = kSavePeriodMs; |
| 61 | VLOG(profiler) << "Save profiling information every " << save_period_ms << " ms"; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 62 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 63 | bool first_iteration = true; |
| 64 | while (!ShuttingDown(self)) { |
| 65 | uint64_t sleep_time_ms; |
| 66 | if (first_iteration) { |
| 67 | // Sleep less long for the first iteration since we want to record loaded classes shortly |
| 68 | // after app launch. |
| 69 | sleep_time_ms = kInitialDelayMs; |
| 70 | } else { |
| 71 | const uint64_t random_sleep_delay_ms = rand() % kRandomDelayMaxMs; |
| 72 | sleep_time_ms = save_period_ms + random_sleep_delay_ms; |
| 73 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 74 | { |
| 75 | MutexLock mu(self, wait_lock_); |
| 76 | period_condition_.TimedWait(self, sleep_time_ms, 0); |
| 77 | } |
| 78 | |
| 79 | if (ShuttingDown(self)) { |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | if (!ProcessProfilingInfo() && save_period_ms < kMaxBackoffMs) { |
| 84 | // If we don't need to save now it is less likely that we will need to do |
| 85 | // so in the future. Increase the time between saves according to the |
| 86 | // kBackoffCoef, but make it no larger than kMaxBackoffMs. |
| 87 | save_period_ms = static_cast<uint64_t>(kBackoffCoef * save_period_ms); |
| 88 | } else { |
| 89 | // Reset the period to the initial value as it's highly likely to JIT again. |
| 90 | save_period_ms = kSavePeriodMs; |
| 91 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 92 | first_iteration = false; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | bool ProfileSaver::ProcessProfilingInfo() { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame^] | 97 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 98 | uint64_t last_update_time_ns = jit_code_cache_->GetLastUpdateTimeNs(); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 99 | if (!first_profile_ && last_update_time_ns - code_cache_last_update_time_ns_ |
| 100 | < kMinimumTimeBetweenCodeCacheUpdatesNs) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 101 | VLOG(profiler) << "Not enough time has passed since the last code cache update." |
| 102 | << "Last update: " << last_update_time_ns |
| 103 | << " Last save: " << code_cache_last_update_time_ns_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | |
| 107 | uint64_t start = NanoTime(); |
| 108 | code_cache_last_update_time_ns_ = last_update_time_ns; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 109 | SafeMap<std::string, std::set<std::string>> tracked_locations; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 110 | { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 111 | // Make a copy so that we don't hold the lock while doing I/O. |
| 112 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 113 | tracked_locations = tracked_dex_base_locations_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 114 | } |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 115 | for (const auto& it : tracked_locations) { |
| 116 | if (ShuttingDown(Thread::Current())) { |
| 117 | return true; |
| 118 | } |
| 119 | const std::string& filename = it.first; |
| 120 | const std::set<std::string>& locations = it.second; |
| 121 | std::vector<ArtMethod*> methods; |
| 122 | { |
| 123 | ScopedObjectAccess soa(Thread::Current()); |
| 124 | jit_code_cache_->GetCompiledArtMethods(locations, methods); |
| 125 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 126 | // Always save for the first one for loaded classes profile. |
| 127 | if (methods.size() < kMinimumNrOrMethodsToSave && !first_profile_) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 128 | VLOG(profiler) << "Not enough information to save to: " << filename |
| 129 | <<" Nr of methods: " << methods.size(); |
| 130 | return false; |
| 131 | } |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 132 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 133 | std::set<DexCacheResolvedClasses> resolved_classes; |
| 134 | if (first_profile_) { |
| 135 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 136 | resolved_classes = class_linker->GetResolvedClasses(/*ignore boot classes*/true); |
| 137 | } |
| 138 | |
| 139 | if (!ProfileCompilationInfo::SaveProfilingInfo(filename, methods, resolved_classes)) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 140 | LOG(WARNING) << "Could not save profiling info to " << filename; |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | VLOG(profiler) << "Profile process time: " << PrettyDuration(NanoTime() - start); |
| 145 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 146 | first_profile_ = false; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | |
| 150 | void* ProfileSaver::RunProfileSaverThread(void* arg) { |
| 151 | Runtime* runtime = Runtime::Current(); |
| 152 | ProfileSaver* profile_saver = reinterpret_cast<ProfileSaver*>(arg); |
| 153 | |
| 154 | CHECK(runtime->AttachCurrentThread("Profile Saver", |
| 155 | /*as_daemon*/true, |
| 156 | runtime->GetSystemThreadGroup(), |
| 157 | /*create_peer*/true)); |
| 158 | profile_saver->Run(); |
| 159 | |
| 160 | runtime->DetachCurrentThread(); |
| 161 | VLOG(profiler) << "Profile saver shutdown"; |
| 162 | return nullptr; |
| 163 | } |
| 164 | |
| 165 | void ProfileSaver::Start(const std::string& output_filename, |
| 166 | jit::JitCodeCache* jit_code_cache, |
| 167 | const std::vector<std::string>& code_paths) { |
| 168 | DCHECK(Runtime::Current()->UseJit()); |
| 169 | DCHECK(!output_filename.empty()); |
| 170 | DCHECK(jit_code_cache != nullptr); |
| 171 | |
| 172 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 173 | if (instance_ != nullptr) { |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 174 | // If we already have an instance, make sure it uses the same jit_code_cache. |
| 175 | // This may be called multiple times via Runtime::registerAppInfo (e.g. for |
| 176 | // apps which share the same runtime). |
| 177 | DCHECK_EQ(instance_->jit_code_cache_, jit_code_cache); |
| 178 | // Add the code_paths to the tracked locations. |
| 179 | instance_->AddTrackedLocations(output_filename, code_paths); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 180 | return; |
| 181 | } |
| 182 | |
| 183 | VLOG(profiler) << "Starting profile saver using output file: " << output_filename |
| 184 | << ". Tracking: " << Join(code_paths, ':'); |
| 185 | |
| 186 | instance_ = new ProfileSaver(output_filename, jit_code_cache, code_paths); |
| 187 | |
| 188 | // Create a new thread which does the saving. |
| 189 | CHECK_PTHREAD_CALL( |
| 190 | pthread_create, |
| 191 | (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)), |
| 192 | "Profile saver thread"); |
| 193 | } |
| 194 | |
| 195 | void ProfileSaver::Stop() { |
| 196 | ProfileSaver* profile_saver = nullptr; |
| 197 | pthread_t profiler_pthread = 0U; |
| 198 | |
| 199 | { |
| 200 | MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_); |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 201 | VLOG(profiler) << "Stopping profile saver thread"; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 202 | profile_saver = instance_; |
| 203 | profiler_pthread = profiler_pthread_; |
| 204 | if (instance_ == nullptr) { |
| 205 | DCHECK(false) << "Tried to stop a profile saver which was not started"; |
| 206 | return; |
| 207 | } |
| 208 | if (instance_->shutting_down_) { |
| 209 | DCHECK(false) << "Tried to stop the profile saver twice"; |
| 210 | return; |
| 211 | } |
| 212 | instance_->shutting_down_ = true; |
| 213 | } |
| 214 | |
| 215 | { |
| 216 | // Wake up the saver thread if it is sleeping to allow for a clean exit. |
| 217 | MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_); |
| 218 | profile_saver->period_condition_.Signal(Thread::Current()); |
| 219 | } |
| 220 | |
| 221 | // Wait for the saver thread to stop. |
| 222 | CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown"); |
| 223 | |
| 224 | { |
| 225 | MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_); |
| 226 | instance_ = nullptr; |
| 227 | profiler_pthread_ = 0U; |
| 228 | } |
| 229 | delete profile_saver; |
| 230 | } |
| 231 | |
| 232 | bool ProfileSaver::ShuttingDown(Thread* self) { |
| 233 | MutexLock mu(self, *Locks::profiler_lock_); |
| 234 | return shutting_down_; |
| 235 | } |
| 236 | |
| 237 | bool ProfileSaver::IsStarted() { |
| 238 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 239 | return instance_ != nullptr; |
| 240 | } |
| 241 | |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 242 | void ProfileSaver::AddTrackedLocations(const std::string& output_filename, |
| 243 | const std::vector<std::string>& code_paths) { |
| 244 | auto it = tracked_dex_base_locations_.find(output_filename); |
| 245 | if (it == tracked_dex_base_locations_.end()) { |
| 246 | tracked_dex_base_locations_.Put(output_filename, |
| 247 | std::set<std::string>(code_paths.begin(), code_paths.end())); |
| 248 | } else { |
| 249 | it->second.insert(code_paths.begin(), code_paths.end()); |
| 250 | } |
| 251 | } |
| 252 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 253 | } // namespace art |