blob: f3f5f95fb208021e908d886f0d3902cb1e6b947e [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
Calin Juravle932a0512016-01-19 14:32:26 -080025// An arbitrary value to throttle save requests. Set to 2s for now.
Calin Juravle4d77b6a2015-12-01 18:38:09 +000026static constexpr const uint64_t kMilisecondsToNano = 1000000;
Calin Juravle932a0512016-01-19 14:32:26 -080027static constexpr const uint64_t kMinimumTimeBetweenCodeCacheUpdatesNs = 2000 * kMilisecondsToNano;
Calin Juravle4d77b6a2015-12-01 18:38:09 +000028
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.
Calin Juravle932a0512016-01-19 14:32:26 -080032static constexpr const uint64_t kRandomDelayMaxMs = 20 * 1000; // 20 seconds
33static constexpr const uint64_t kMaxBackoffMs = 5 * 60 * 1000; // 5 minutes
34static constexpr const uint64_t kSavePeriodMs = 10 * 1000; // 10 seconds
Calin Juravle4d77b6a2015-12-01 18:38:09 +000035static 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)
45 : output_filename_(output_filename),
46 jit_code_cache_(jit_code_cache),
47 tracked_dex_base_locations_(code_paths.begin(), code_paths.end()),
48 code_cache_last_update_time_ns_(0),
49 shutting_down_(false),
50 wait_lock_("ProfileSaver wait lock"),
51 period_condition_("ProfileSaver period condition", wait_lock_) {
52}
53
54void ProfileSaver::Run() {
55 srand(MicroTime() * getpid());
56 Thread* self = Thread::Current();
57
58 uint64_t save_period_ms = kSavePeriodMs;
59 VLOG(profiler) << "Save profiling information every " << save_period_ms << " ms";
60 while (true) {
61 if (ShuttingDown(self)) {
62 break;
63 }
64
65 uint64_t random_sleep_delay_ms = rand() % kRandomDelayMaxMs;
66 uint64_t sleep_time_ms = save_period_ms + random_sleep_delay_ms;
67 {
68 MutexLock mu(self, wait_lock_);
69 period_condition_.TimedWait(self, sleep_time_ms, 0);
70 }
71
72 if (ShuttingDown(self)) {
73 break;
74 }
75
76 if (!ProcessProfilingInfo() && save_period_ms < kMaxBackoffMs) {
77 // If we don't need to save now it is less likely that we will need to do
78 // so in the future. Increase the time between saves according to the
79 // kBackoffCoef, but make it no larger than kMaxBackoffMs.
80 save_period_ms = static_cast<uint64_t>(kBackoffCoef * save_period_ms);
81 } else {
82 // Reset the period to the initial value as it's highly likely to JIT again.
83 save_period_ms = kSavePeriodMs;
84 }
85 }
86}
87
88bool ProfileSaver::ProcessProfilingInfo() {
Calin Juravle877fd962016-01-05 14:29:29 +000089 VLOG(profiler) << "Save profiling information to: " << output_filename_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +000090
91 uint64_t last_update_time_ns = jit_code_cache_->GetLastUpdateTimeNs();
92 if (last_update_time_ns - code_cache_last_update_time_ns_
Calin Juravle877fd962016-01-05 14:29:29 +000093 < kMinimumTimeBetweenCodeCacheUpdatesNs) {
94 VLOG(profiler) << "Not enough time has passed since the last code cache update."
95 << "Last update: " << last_update_time_ns
96 << " Last save: " << code_cache_last_update_time_ns_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +000097 return false;
98 }
99
100 uint64_t start = NanoTime();
101 code_cache_last_update_time_ns_ = last_update_time_ns;
102 std::vector<ArtMethod*> methods;
103 {
104 ScopedObjectAccess soa(Thread::Current());
105 jit_code_cache_->GetCompiledArtMethods(tracked_dex_base_locations_, methods);
106 }
107 if (methods.size() < kMinimumNrOrMethodsToSave) {
108 VLOG(profiler) << "Not enough information to save. Nr of methods: " << methods.size();
109 return false;
110 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000111
Calin Juravle998c2162015-12-21 15:39:33 +0200112 ProfileCompilationInfo::SaveProfilingInfo(output_filename_, methods);
113 VLOG(profiler) << "Profile process time: " << PrettyDuration(NanoTime() - start);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000114 return true;
115}
116
117void* ProfileSaver::RunProfileSaverThread(void* arg) {
118 Runtime* runtime = Runtime::Current();
119 ProfileSaver* profile_saver = reinterpret_cast<ProfileSaver*>(arg);
120
121 CHECK(runtime->AttachCurrentThread("Profile Saver",
122 /*as_daemon*/true,
123 runtime->GetSystemThreadGroup(),
124 /*create_peer*/true));
125 profile_saver->Run();
126
127 runtime->DetachCurrentThread();
128 VLOG(profiler) << "Profile saver shutdown";
129 return nullptr;
130}
131
132void ProfileSaver::Start(const std::string& output_filename,
133 jit::JitCodeCache* jit_code_cache,
134 const std::vector<std::string>& code_paths) {
135 DCHECK(Runtime::Current()->UseJit());
136 DCHECK(!output_filename.empty());
137 DCHECK(jit_code_cache != nullptr);
138
139 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
140 // Don't start two profile saver threads.
141 if (instance_ != nullptr) {
142 DCHECK(false) << "Tried to start two profile savers";
143 return;
144 }
145
146 VLOG(profiler) << "Starting profile saver using output file: " << output_filename
147 << ". Tracking: " << Join(code_paths, ':');
148
149 instance_ = new ProfileSaver(output_filename, jit_code_cache, code_paths);
150
151 // Create a new thread which does the saving.
152 CHECK_PTHREAD_CALL(
153 pthread_create,
154 (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)),
155 "Profile saver thread");
156}
157
158void ProfileSaver::Stop() {
159 ProfileSaver* profile_saver = nullptr;
160 pthread_t profiler_pthread = 0U;
161
162 {
163 MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
164 VLOG(profiler) << "Stopping profile saver thread for file: " << instance_->output_filename_;
165 profile_saver = instance_;
166 profiler_pthread = profiler_pthread_;
167 if (instance_ == nullptr) {
168 DCHECK(false) << "Tried to stop a profile saver which was not started";
169 return;
170 }
171 if (instance_->shutting_down_) {
172 DCHECK(false) << "Tried to stop the profile saver twice";
173 return;
174 }
175 instance_->shutting_down_ = true;
176 }
177
178 {
179 // Wake up the saver thread if it is sleeping to allow for a clean exit.
180 MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_);
181 profile_saver->period_condition_.Signal(Thread::Current());
182 }
183
184 // Wait for the saver thread to stop.
185 CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown");
186
187 {
188 MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
189 instance_ = nullptr;
190 profiler_pthread_ = 0U;
191 }
192 delete profile_saver;
193}
194
195bool ProfileSaver::ShuttingDown(Thread* self) {
196 MutexLock mu(self, *Locks::profiler_lock_);
197 return shutting_down_;
198}
199
200bool ProfileSaver::IsStarted() {
201 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
202 return instance_ != nullptr;
203}
204
205} // namespace art