blob: bd581570edd85306f92f5c9d9c3a8a46ace96b97 [file] [log] [blame]
Calin Juravle4d77b6a2015-12-01 18:38:09 +00001/*
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 Chartier32ce2ad2016-03-04 14:58:03 -080020#include "base/systrace.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000021#include "scoped_thread_state_change.h"
22#include "oat_file_manager.h"
23
24namespace art {
25
Calin Juravle932a0512016-01-19 14:32:26 -080026// An arbitrary value to throttle save requests. Set to 2s for now.
Calin Juravle4d77b6a2015-12-01 18:38:09 +000027static constexpr const uint64_t kMilisecondsToNano = 1000000;
Calin Juravle932a0512016-01-19 14:32:26 -080028static constexpr const uint64_t kMinimumTimeBetweenCodeCacheUpdatesNs = 2000 * kMilisecondsToNano;
Calin Juravle4d77b6a2015-12-01 18:38:09 +000029
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 Juravle932a0512016-01-19 14:32:26 -080033static constexpr const uint64_t kRandomDelayMaxMs = 20 * 1000; // 20 seconds
34static constexpr const uint64_t kMaxBackoffMs = 5 * 60 * 1000; // 5 minutes
35static constexpr const uint64_t kSavePeriodMs = 10 * 1000; // 10 seconds
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080036static constexpr const uint64_t kInitialDelayMs = 2 * 1000; // 2 seconds
Calin Juravle4d77b6a2015-12-01 18:38:09 +000037static constexpr const double kBackoffCoef = 1.5;
38
39static constexpr const uint32_t kMinimumNrOrMethodsToSave = 10;
40
41ProfileSaver* ProfileSaver::instance_ = nullptr;
42pthread_t ProfileSaver::profiler_pthread_ = 0U;
43
44ProfileSaver::ProfileSaver(const std::string& output_filename,
45 jit::JitCodeCache* jit_code_cache,
46 const std::vector<std::string>& code_paths)
Calin Juravleb4eddd22016-01-13 15:52:33 -080047 : jit_code_cache_(jit_code_cache),
Calin Juravle4d77b6a2015-12-01 18:38:09 +000048 code_cache_last_update_time_ns_(0),
49 shutting_down_(false),
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080050 first_profile_(true),
Calin Juravle4d77b6a2015-12-01 18:38:09 +000051 wait_lock_("ProfileSaver wait lock"),
52 period_condition_("ProfileSaver period condition", wait_lock_) {
Calin Juravleb4eddd22016-01-13 15:52:33 -080053 AddTrackedLocations(output_filename, code_paths);
Calin Juravle4d77b6a2015-12-01 18:38:09 +000054}
55
56void 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 Juravle4d77b6a2015-12-01 18:38:09 +000062
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080063 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 Juravle4d77b6a2015-12-01 18:38:09 +000074 {
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 Chartierc5dd3192015-12-09 16:38:30 -080092 first_iteration = false;
Calin Juravle4d77b6a2015-12-01 18:38:09 +000093 }
94}
95
96bool ProfileSaver::ProcessProfilingInfo() {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080097 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravle4d77b6a2015-12-01 18:38:09 +000098 uint64_t last_update_time_ns = jit_code_cache_->GetLastUpdateTimeNs();
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080099 if (!first_profile_ && last_update_time_ns - code_cache_last_update_time_ns_
100 < kMinimumTimeBetweenCodeCacheUpdatesNs) {
Calin Juravle877fd962016-01-05 14:29:29 +0000101 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 Juravle4d77b6a2015-12-01 18:38:09 +0000104 return false;
105 }
106
107 uint64_t start = NanoTime();
108 code_cache_last_update_time_ns_ = last_update_time_ns;
Calin Juravleb4eddd22016-01-13 15:52:33 -0800109 SafeMap<std::string, std::set<std::string>> tracked_locations;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000110 {
Calin Juravleb4eddd22016-01-13 15:52:33 -0800111 // 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 Juravle4d77b6a2015-12-01 18:38:09 +0000114 }
Calin Juravleb4eddd22016-01-13 15:52:33 -0800115 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 Chartierc5dd3192015-12-09 16:38:30 -0800126 // Always save for the first one for loaded classes profile.
127 if (methods.size() < kMinimumNrOrMethodsToSave && !first_profile_) {
Calin Juravleb4eddd22016-01-13 15:52:33 -0800128 VLOG(profiler) << "Not enough information to save to: " << filename
129 <<" Nr of methods: " << methods.size();
130 return false;
131 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000132
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800133 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 Juravleb4eddd22016-01-13 15:52:33 -0800140 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 Chartierc5dd3192015-12-09 16:38:30 -0800146 first_profile_ = false;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000147 return true;
148}
149
150void* 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
165void 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 Juravle4d77b6a2015-12-01 18:38:09 +0000173 if (instance_ != nullptr) {
Calin Juravleb4eddd22016-01-13 15:52:33 -0800174 // 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 Juravle4d77b6a2015-12-01 18:38:09 +0000180 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
195void 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 Juravleb4eddd22016-01-13 15:52:33 -0800201 VLOG(profiler) << "Stopping profile saver thread";
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000202 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
232bool ProfileSaver::ShuttingDown(Thread* self) {
233 MutexLock mu(self, *Locks::profiler_lock_);
234 return shutting_down_;
235}
236
237bool ProfileSaver::IsStarted() {
238 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
239 return instance_ != nullptr;
240}
241
Calin Juravleb4eddd22016-01-13 15:52:33 -0800242void 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 Juravle4d77b6a2015-12-01 18:38:09 +0000253} // namespace art