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