blob: 5a469e51b47dc2b8017ba1b5b650d4615936d0a1 [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
Calin Juravlec90bc922016-02-24 10:13:09 +000019#include <sys/types.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22
Calin Juravle4d77b6a2015-12-01 18:38:09 +000023#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070024#include "base/enums.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080025#include "base/systrace.h"
Calin Juravle050fb1b2016-03-25 17:17:09 +000026#include "base/time_utils.h"
27#include "compiler_filter.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000028#include "oat_file_manager.h"
Calin Juravle050fb1b2016-03-25 17:17:09 +000029#include "scoped_thread_state_change.h"
30
Calin Juravle4d77b6a2015-12-01 18:38:09 +000031
32namespace art {
33
Calin Juravle4d77b6a2015-12-01 18:38:09 +000034ProfileSaver* ProfileSaver::instance_ = nullptr;
35pthread_t ProfileSaver::profiler_pthread_ = 0U;
36
Calin Juravle138dbff2016-06-28 19:36:58 +010037ProfileSaver::ProfileSaver(const ProfileSaverOptions& options,
38 const std::string& output_filename,
Calin Juravle4d77b6a2015-12-01 18:38:09 +000039 jit::JitCodeCache* jit_code_cache,
Calin Juravlec90bc922016-02-24 10:13:09 +000040 const std::vector<std::string>& code_paths,
41 const std::string& foreign_dex_profile_path,
42 const std::string& app_data_dir)
Calin Juravleb4eddd22016-01-13 15:52:33 -080043 : jit_code_cache_(jit_code_cache),
Calin Juravlec90bc922016-02-24 10:13:09 +000044 foreign_dex_profile_path_(foreign_dex_profile_path),
Calin Juravle4d77b6a2015-12-01 18:38:09 +000045 shutting_down_(false),
Calin Juravle67265462016-03-18 16:23:40 +000046 last_save_number_of_methods_(0),
Calin Juravle698f4d12016-03-30 18:18:58 +010047 last_save_number_of_classes_(0),
Calin Juravlea2638922016-04-29 16:44:11 +010048 last_time_ns_saver_woke_up_(0),
49 jit_activity_notifications_(0),
Calin Juravle4d77b6a2015-12-01 18:38:09 +000050 wait_lock_("ProfileSaver wait lock"),
Calin Juravleb8e69992016-03-09 15:37:48 +000051 period_condition_("ProfileSaver period condition", wait_lock_),
52 total_bytes_written_(0),
53 total_number_of_writes_(0),
54 total_number_of_code_cache_queries_(0),
55 total_number_of_skipped_writes_(0),
56 total_number_of_failed_writes_(0),
Calin Juravle67265462016-03-18 16:23:40 +000057 total_ms_of_sleep_(0),
Calin Juravleb8e69992016-03-09 15:37:48 +000058 total_ns_of_work_(0),
Calin Juravle67265462016-03-18 16:23:40 +000059 total_number_of_foreign_dex_marks_(0),
Calin Juravlea2638922016-04-29 16:44:11 +010060 max_number_of_profile_entries_cached_(0),
61 total_number_of_hot_spikes_(0),
Calin Juravle138dbff2016-06-28 19:36:58 +010062 total_number_of_wake_ups_(0),
63 options_(options) {
64 DCHECK(options_.IsEnabled());
Calin Juravle20b7e3b2016-04-18 18:59:22 +010065 AddTrackedLocations(output_filename, app_data_dir, code_paths);
Calin Juravlec90bc922016-02-24 10:13:09 +000066 if (!app_data_dir.empty()) {
67 // The application directory is used to determine which dex files are owned by app.
68 // Since it could be a symlink (e.g. /data/data instead of /data/user/0), and we
69 // don't have control over how the dex files are actually loaded (symlink or canonical path),
70 // store it's canonical form to be sure we use the same base when comparing.
71 UniqueCPtr<const char[]> app_data_dir_real_path(realpath(app_data_dir.c_str(), nullptr));
72 if (app_data_dir_real_path != nullptr) {
Calin Juravle20b7e3b2016-04-18 18:59:22 +010073 app_data_dirs_.emplace(app_data_dir_real_path.get());
Calin Juravlec90bc922016-02-24 10:13:09 +000074 } else {
Calin Juravle20b7e3b2016-04-18 18:59:22 +010075 LOG(WARNING) << "Failed to get the real path for app dir: " << app_data_dir
Calin Juravlec90bc922016-02-24 10:13:09 +000076 << ". The app dir will not be used to determine which dex files belong to the app";
77 }
78 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +000079}
80
81void ProfileSaver::Run() {
Calin Juravle4d77b6a2015-12-01 18:38:09 +000082 Thread* self = Thread::Current();
83
Calin Juravlea2638922016-04-29 16:44:11 +010084 // Fetch the resolved classes for the app images after sleeping for
Calin Juravle138dbff2016-06-28 19:36:58 +010085 // options_.GetSaveResolvedClassesDelayMs().
Calin Juravlea2638922016-04-29 16:44:11 +010086 // TODO(calin) This only considers the case of the primary profile file.
87 // Anything that gets loaded in the same VM will not have their resolved
88 // classes save (unless they started before the initial saving was done).
89 {
90 MutexLock mu(self, wait_lock_);
Calin Juravle138dbff2016-06-28 19:36:58 +010091 const uint64_t end_time = NanoTime() + MsToNs(options_.GetSaveResolvedClassesDelayMs());
Mathieu Chartiera57305e2016-05-18 19:51:23 -070092 while (true) {
93 const uint64_t current_time = NanoTime();
94 if (current_time >= end_time) {
95 break;
96 }
97 period_condition_.TimedWait(self, NsToMs(end_time - current_time), 0);
98 }
Calin Juravle138dbff2016-06-28 19:36:58 +010099 total_ms_of_sleep_ += options_.GetSaveResolvedClassesDelayMs();
Calin Juravlea2638922016-04-29 16:44:11 +0100100 }
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700101 FetchAndCacheResolvedClassesAndMethods();
Calin Juravlea2638922016-04-29 16:44:11 +0100102
103 // Loop for the profiled methods.
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800104 while (!ShuttingDown(self)) {
Calin Juravlea2638922016-04-29 16:44:11 +0100105 uint64_t sleep_start = NanoTime();
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000106 {
Calin Juravle8fbea8e2016-05-18 15:49:36 -0700107 uint64_t sleep_time = 0;
108 {
109 MutexLock mu(self, wait_lock_);
110 period_condition_.Wait(self);
Calin Juravle5d04eb62016-05-25 18:09:53 +0100111 sleep_time = NanoTime() - sleep_start;
Calin Juravle8fbea8e2016-05-18 15:49:36 -0700112 }
113 // Check if the thread was woken up for shutdown.
114 if (ShuttingDown(self)) {
115 break;
116 }
Calin Juravlea2638922016-04-29 16:44:11 +0100117 total_number_of_wake_ups_++;
118 // We might have been woken up by a huge number of notifications to guarantee saving.
119 // If we didn't meet the minimum saving period go back to sleep (only if missed by
120 // a reasonable margin).
Calin Juravle138dbff2016-06-28 19:36:58 +0100121 uint64_t min_save_period_ns = MsToNs(options_.GetMinSavePeriodMs());
122 while (min_save_period_ns * 0.9 > sleep_time) {
Calin Juravle8fbea8e2016-05-18 15:49:36 -0700123 {
124 MutexLock mu(self, wait_lock_);
Calin Juravle138dbff2016-06-28 19:36:58 +0100125 period_condition_.TimedWait(self, NsToMs(min_save_period_ns - sleep_time), 0);
Calin Juravle5d04eb62016-05-25 18:09:53 +0100126 sleep_time = NanoTime() - sleep_start;
Calin Juravle8fbea8e2016-05-18 15:49:36 -0700127 }
128 // Check if the thread was woken up for shutdown.
129 if (ShuttingDown(self)) {
130 break;
131 }
Calin Juravlea2638922016-04-29 16:44:11 +0100132 total_number_of_wake_ups_++;
Calin Juravlea2638922016-04-29 16:44:11 +0100133 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000134 }
Calin Juravlea2638922016-04-29 16:44:11 +0100135 total_ms_of_sleep_ += NsToMs(NanoTime() - sleep_start);
136
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000137 if (ShuttingDown(self)) {
138 break;
139 }
140
Calin Juravlea2638922016-04-29 16:44:11 +0100141 uint16_t new_methods = 0;
142 uint64_t start_work = NanoTime();
143 bool profile_saved_to_disk = ProcessProfilingInfo(&new_methods);
144 // Update the notification counter based on result. Note that there might be contention on this
145 // but we don't care about to be 100% precise.
146 if (!profile_saved_to_disk) {
147 // If we didn't save to disk it may be because we didn't have enough new methods.
148 // Set the jit activity notifications to new_methods so we can wake up earlier if needed.
149 jit_activity_notifications_ = new_methods;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000150 }
Calin Juravlea2638922016-04-29 16:44:11 +0100151 total_ns_of_work_ += NanoTime() - start_work;
152 }
153}
Calin Juravleb8e69992016-03-09 15:37:48 +0000154
Calin Juravlea2638922016-04-29 16:44:11 +0100155void ProfileSaver::NotifyJitActivity() {
156 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
157 if (instance_ == nullptr || instance_->shutting_down_) {
158 return;
159 }
160 instance_->NotifyJitActivityInternal();
161}
162
163void ProfileSaver::WakeUpSaver() {
164 jit_activity_notifications_ = 0;
165 last_time_ns_saver_woke_up_ = NanoTime();
166 period_condition_.Signal(Thread::Current());
167}
168
169void ProfileSaver::NotifyJitActivityInternal() {
170 // Unlikely to overflow but if it happens,
171 // we would have waken up the saver long before that.
172 jit_activity_notifications_++;
173 // Note that we are not as precise as we could be here but we don't want to wake the saver
174 // every time we see a hot method.
Calin Juravle138dbff2016-06-28 19:36:58 +0100175 if (jit_activity_notifications_ > options_.GetMinNotificationBeforeWake()) {
Calin Juravlea2638922016-04-29 16:44:11 +0100176 MutexLock wait_mutex(Thread::Current(), wait_lock_);
Calin Juravle138dbff2016-06-28 19:36:58 +0100177 if ((NanoTime() - last_time_ns_saver_woke_up_) > MsToNs(options_.GetMinSavePeriodMs())) {
Calin Juravlea2638922016-04-29 16:44:11 +0100178 WakeUpSaver();
179 }
Calin Juravle138dbff2016-06-28 19:36:58 +0100180 } else if (jit_activity_notifications_ > options_.GetMaxNotificationBeforeWake()) {
Calin Juravlea2638922016-04-29 16:44:11 +0100181 // Make sure to wake up the saver if we see a spike in the number of notifications.
182 // This is a precaution to avoid "loosing" a big number of methods in case
183 // this is a spike with no jit after.
184 total_number_of_hot_spikes_++;
185 MutexLock wait_mutex(Thread::Current(), wait_lock_);
186 WakeUpSaver();
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000187 }
188}
189
Calin Juravle67265462016-03-18 16:23:40 +0000190ProfileCompilationInfo* ProfileSaver::GetCachedProfiledInfo(const std::string& filename) {
191 auto info_it = profile_cache_.find(filename);
192 if (info_it == profile_cache_.end()) {
193 info_it = profile_cache_.Put(filename, ProfileCompilationInfo());
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000194 }
Calin Juravle67265462016-03-18 16:23:40 +0000195 return &info_it->second;
196}
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000197
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700198// Get resolved methods that have a profile info or more than kStartupMethodSamples samples.
199// Excludes native methods and classes in the boot image.
200class GetMethodsVisitor : public ClassVisitor {
201 public:
Calin Juravle138dbff2016-06-28 19:36:58 +0100202 GetMethodsVisitor(std::vector<MethodReference>* methods, uint32_t startup_method_samples)
203 : methods_(methods),
204 startup_method_samples_(startup_method_samples) {}
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700205
206 virtual bool operator()(mirror::Class* klass) SHARED_REQUIRES(Locks::mutator_lock_) {
207 if (Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) {
208 return true;
209 }
Andreas Gampe542451c2016-07-26 09:02:02 -0700210 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700211 if (!method.IsNative()) {
Calin Juravle138dbff2016-06-28 19:36:58 +0100212 if (method.GetCounter() >= startup_method_samples_ ||
Andreas Gampe542451c2016-07-26 09:02:02 -0700213 method.GetProfilingInfo(kRuntimePointerSize) != nullptr) {
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700214 // Have samples, add to profile.
Andreas Gampe542451c2016-07-26 09:02:02 -0700215 const DexFile* dex_file =
216 method.GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetDexFile();
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700217 methods_->push_back(MethodReference(dex_file, method.GetDexMethodIndex()));
218 }
219 }
220 }
221 return true;
222 }
223
224 private:
225 std::vector<MethodReference>* const methods_;
Calin Juravle138dbff2016-06-28 19:36:58 +0100226 uint32_t startup_method_samples_;
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700227};
228
229void ProfileSaver::FetchAndCacheResolvedClassesAndMethods() {
Calin Juravle67265462016-03-18 16:23:40 +0000230 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravle67265462016-03-18 16:23:40 +0000231 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
232 std::set<DexCacheResolvedClasses> resolved_classes =
233 class_linker->GetResolvedClasses(/*ignore boot classes*/ true);
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700234
235 std::vector<MethodReference> methods;
236 {
237 ScopedTrace trace2("Get hot methods");
Calin Juravle138dbff2016-06-28 19:36:58 +0100238 GetMethodsVisitor visitor(&methods, options_.GetStartupMethodSamples());
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700239 ScopedObjectAccess soa(Thread::Current());
240 class_linker->VisitClasses(&visitor);
241 VLOG(profiler) << "Methods with samples greater than "
Calin Juravle138dbff2016-06-28 19:36:58 +0100242 << options_.GetStartupMethodSamples() << " = " << methods.size();
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700243 }
Calin Juravle67265462016-03-18 16:23:40 +0000244 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
245 uint64_t total_number_of_profile_entries_cached = 0;
Mathieu Chartierb384e5e2016-04-29 12:03:56 -0700246
Calin Juravle67265462016-03-18 16:23:40 +0000247 for (const auto& it : tracked_dex_base_locations_) {
Mathieu Chartierb384e5e2016-04-29 12:03:56 -0700248 std::set<DexCacheResolvedClasses> resolved_classes_for_location;
Calin Juravle67265462016-03-18 16:23:40 +0000249 const std::string& filename = it.first;
250 const std::set<std::string>& locations = it.second;
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700251 std::vector<MethodReference> methods_for_location;
252 for (const MethodReference& ref : methods) {
253 if (locations.find(ref.dex_file->GetBaseLocation()) != locations.end()) {
254 methods_for_location.push_back(ref);
255 }
256 }
Calin Juravle67265462016-03-18 16:23:40 +0000257 for (const DexCacheResolvedClasses& classes : resolved_classes) {
Mathieu Chartierb384e5e2016-04-29 12:03:56 -0700258 if (locations.find(classes.GetBaseLocation()) != locations.end()) {
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700259 VLOG(profiler) << "Added " << classes.GetClasses().size() << " classes for location "
260 << classes.GetBaseLocation() << " (" << classes.GetDexLocation() << ")";
Calin Juravle67265462016-03-18 16:23:40 +0000261 resolved_classes_for_location.insert(classes);
Mathieu Chartierb384e5e2016-04-29 12:03:56 -0700262 } else {
263 VLOG(profiler) << "Location not found " << classes.GetBaseLocation()
264 << " (" << classes.GetDexLocation() << ")";
Calin Juravle67265462016-03-18 16:23:40 +0000265 }
266 }
267 ProfileCompilationInfo* info = GetCachedProfiledInfo(filename);
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700268 info->AddMethodsAndClasses(methods_for_location, resolved_classes_for_location);
Calin Juravle67265462016-03-18 16:23:40 +0000269 total_number_of_profile_entries_cached += resolved_classes_for_location.size();
270 }
271 max_number_of_profile_entries_cached_ = std::max(
272 max_number_of_profile_entries_cached_,
273 total_number_of_profile_entries_cached);
274}
275
Calin Juravlea2638922016-04-29 16:44:11 +0100276bool ProfileSaver::ProcessProfilingInfo(uint16_t* new_methods) {
Calin Juravle67265462016-03-18 16:23:40 +0000277 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravleb4eddd22016-01-13 15:52:33 -0800278 SafeMap<std::string, std::set<std::string>> tracked_locations;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000279 {
Calin Juravleb4eddd22016-01-13 15:52:33 -0800280 // Make a copy so that we don't hold the lock while doing I/O.
281 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
282 tracked_locations = tracked_dex_base_locations_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000283 }
Calin Juravleb9c1b9b2016-03-17 17:07:52 +0000284
Calin Juravle67265462016-03-18 16:23:40 +0000285 bool profile_file_saved = false;
286 uint64_t total_number_of_profile_entries_cached = 0;
Calin Juravlea2638922016-04-29 16:44:11 +0100287 *new_methods = 0;
288
Calin Juravleb4eddd22016-01-13 15:52:33 -0800289 for (const auto& it : tracked_locations) {
290 if (ShuttingDown(Thread::Current())) {
291 return true;
292 }
293 const std::string& filename = it.first;
294 const std::set<std::string>& locations = it.second;
Calin Juravle99629622016-04-19 16:33:46 +0100295 std::vector<MethodReference> methods;
Calin Juravleb4eddd22016-01-13 15:52:33 -0800296 {
297 ScopedObjectAccess soa(Thread::Current());
Calin Juravle99629622016-04-19 16:33:46 +0100298 jit_code_cache_->GetProfiledMethods(locations, methods);
Calin Juravleb8e69992016-03-09 15:37:48 +0000299 total_number_of_code_cache_queries_++;
Calin Juravleb4eddd22016-01-13 15:52:33 -0800300 }
Calin Juravleb9c1b9b2016-03-17 17:07:52 +0000301
Calin Juravle67265462016-03-18 16:23:40 +0000302 ProfileCompilationInfo* cached_info = GetCachedProfiledInfo(filename);
303 cached_info->AddMethodsAndClasses(methods, std::set<DexCacheResolvedClasses>());
Calin Juravle698f4d12016-03-30 18:18:58 +0100304 int64_t delta_number_of_methods =
305 cached_info->GetNumberOfMethods() -
306 static_cast<int64_t>(last_save_number_of_methods_);
307 int64_t delta_number_of_classes =
308 cached_info->GetNumberOfResolvedClasses() -
309 static_cast<int64_t>(last_save_number_of_classes_);
Calin Juravle67265462016-03-18 16:23:40 +0000310
Calin Juravle138dbff2016-06-28 19:36:58 +0100311 if (delta_number_of_methods < options_.GetMinMethodsToSave() &&
312 delta_number_of_classes < options_.GetMinClassesToSave()) {
Calin Juravle698f4d12016-03-30 18:18:58 +0100313 VLOG(profiler) << "Not enough information to save to: " << filename
Calin Juravle138dbff2016-06-28 19:36:58 +0100314 << " Number of methods: " << delta_number_of_methods
315 << " Number of classes: " << delta_number_of_classes;
Calin Juravleb8e69992016-03-09 15:37:48 +0000316 total_number_of_skipped_writes_++;
Calin Juravle67265462016-03-18 16:23:40 +0000317 continue;
Calin Juravleb4eddd22016-01-13 15:52:33 -0800318 }
Calin Juravlea2638922016-04-29 16:44:11 +0100319 *new_methods = std::max(static_cast<uint16_t>(delta_number_of_methods), *new_methods);
Calin Juravleb8e69992016-03-09 15:37:48 +0000320 uint64_t bytes_written;
Calin Juravle5d1bd0a2016-03-24 20:33:22 +0000321 // Force the save. In case the profile data is corrupted or the the profile
322 // has the wrong version this will "fix" the file to the correct format.
323 if (cached_info->MergeAndSave(filename, &bytes_written, /*force*/ true)) {
Calin Juravle67265462016-03-18 16:23:40 +0000324 last_save_number_of_methods_ = cached_info->GetNumberOfMethods();
Calin Juravle698f4d12016-03-30 18:18:58 +0100325 last_save_number_of_classes_ = cached_info->GetNumberOfResolvedClasses();
Calin Juravle67265462016-03-18 16:23:40 +0000326 // Clear resolved classes. No need to store them around as
327 // they don't change after the first write.
328 cached_info->ClearResolvedClasses();
Calin Juravleb8e69992016-03-09 15:37:48 +0000329 if (bytes_written > 0) {
330 total_number_of_writes_++;
331 total_bytes_written_ += bytes_written;
Calin Juravle698f4d12016-03-30 18:18:58 +0100332 profile_file_saved = true;
Calin Juravle67265462016-03-18 16:23:40 +0000333 } else {
334 // At this point we could still have avoided the write.
335 // We load and merge the data from the file lazily at its first ever
336 // save attempt. So, whatever we are trying to save could already be
337 // in the file.
338 total_number_of_skipped_writes_++;
Calin Juravleb8e69992016-03-09 15:37:48 +0000339 }
Calin Juravle67265462016-03-18 16:23:40 +0000340 } else {
341 LOG(WARNING) << "Could not save profiling info to " << filename;
342 total_number_of_failed_writes_++;
Calin Juravleb4eddd22016-01-13 15:52:33 -0800343 }
Calin Juravle67265462016-03-18 16:23:40 +0000344 total_number_of_profile_entries_cached +=
345 cached_info->GetNumberOfMethods() +
346 cached_info->GetNumberOfResolvedClasses();
Calin Juravleb4eddd22016-01-13 15:52:33 -0800347 }
Calin Juravle67265462016-03-18 16:23:40 +0000348 max_number_of_profile_entries_cached_ = std::max(
349 max_number_of_profile_entries_cached_,
350 total_number_of_profile_entries_cached);
351 return profile_file_saved;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000352}
353
354void* ProfileSaver::RunProfileSaverThread(void* arg) {
355 Runtime* runtime = Runtime::Current();
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000356
Calin Juravlef39f0092016-04-28 12:59:33 +0100357 bool attached = runtime->AttachCurrentThread("Profile Saver",
358 /*as_daemon*/true,
359 runtime->GetSystemThreadGroup(),
360 /*create_peer*/true);
361 if (!attached) {
362 CHECK(runtime->IsShuttingDown(Thread::Current()));
363 return nullptr;
364 }
365
366 ProfileSaver* profile_saver = reinterpret_cast<ProfileSaver*>(arg);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000367 profile_saver->Run();
368
369 runtime->DetachCurrentThread();
370 VLOG(profiler) << "Profile saver shutdown";
371 return nullptr;
372}
373
Calin Juravle050fb1b2016-03-25 17:17:09 +0000374static bool ShouldProfileLocation(const std::string& location) {
Calin Juravle0b791272016-04-18 16:38:27 +0100375 OatFileManager& oat_manager = Runtime::Current()->GetOatFileManager();
376 const OatFile* oat_file = oat_manager.FindOpenedOatFileFromDexLocation(location);
Calin Juravle050fb1b2016-03-25 17:17:09 +0000377 if (oat_file == nullptr) {
378 // This can happen if we fallback to run code directly from the APK.
379 // Profile it with the hope that the background dexopt will get us back into
380 // a good state.
Calin Juravle0b791272016-04-18 16:38:27 +0100381 VLOG(profiler) << "Asked to profile a location without an oat file:" << location;
Calin Juravle050fb1b2016-03-25 17:17:09 +0000382 return true;
383 }
384 CompilerFilter::Filter filter = oat_file->GetCompilerFilter();
Calin Juravle65439332016-04-19 18:17:41 +0100385 if ((filter == CompilerFilter::kSpeed) || (filter == CompilerFilter::kEverything)) {
Calin Juravle0b791272016-04-18 16:38:27 +0100386 VLOG(profiler)
Calin Juravle65439332016-04-19 18:17:41 +0100387 << "Skip profiling oat file because it's already speed|everything compiled: "
388 << location << " oat location: " << oat_file->GetLocation();
Calin Juravle050fb1b2016-03-25 17:17:09 +0000389 return false;
390 }
391 return true;
392}
393
Calin Juravle138dbff2016-06-28 19:36:58 +0100394void ProfileSaver::Start(const ProfileSaverOptions& options,
395 const std::string& output_filename,
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000396 jit::JitCodeCache* jit_code_cache,
Calin Juravlec90bc922016-02-24 10:13:09 +0000397 const std::vector<std::string>& code_paths,
398 const std::string& foreign_dex_profile_path,
399 const std::string& app_data_dir) {
Calin Juravle138dbff2016-06-28 19:36:58 +0100400 DCHECK(options.IsEnabled());
401 DCHECK(Runtime::Current()->GetJit() != nullptr);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000402 DCHECK(!output_filename.empty());
403 DCHECK(jit_code_cache != nullptr);
404
Calin Juravle050fb1b2016-03-25 17:17:09 +0000405 std::vector<std::string> code_paths_to_profile;
406
407 for (const std::string& location : code_paths) {
408 if (ShouldProfileLocation(location)) {
409 code_paths_to_profile.push_back(location);
Calin Juravle050fb1b2016-03-25 17:17:09 +0000410 }
411 }
412 if (code_paths_to_profile.empty()) {
Calin Juravle0b791272016-04-18 16:38:27 +0100413 VLOG(profiler) << "No code paths should be profiled.";
Calin Juravle050fb1b2016-03-25 17:17:09 +0000414 return;
415 }
416
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000417 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000418 if (instance_ != nullptr) {
Calin Juravleb4eddd22016-01-13 15:52:33 -0800419 // If we already have an instance, make sure it uses the same jit_code_cache.
420 // This may be called multiple times via Runtime::registerAppInfo (e.g. for
421 // apps which share the same runtime).
422 DCHECK_EQ(instance_->jit_code_cache_, jit_code_cache);
423 // Add the code_paths to the tracked locations.
Calin Juravle20b7e3b2016-04-18 18:59:22 +0100424 instance_->AddTrackedLocations(output_filename, app_data_dir, code_paths_to_profile);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000425 return;
426 }
427
428 VLOG(profiler) << "Starting profile saver using output file: " << output_filename
Calin Juravle0b791272016-04-18 16:38:27 +0100429 << ". Tracking: " << Join(code_paths_to_profile, ':');
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000430
Calin Juravle138dbff2016-06-28 19:36:58 +0100431 instance_ = new ProfileSaver(options,
432 output_filename,
Calin Juravlec90bc922016-02-24 10:13:09 +0000433 jit_code_cache,
Calin Juravle0b791272016-04-18 16:38:27 +0100434 code_paths_to_profile,
Calin Juravlec90bc922016-02-24 10:13:09 +0000435 foreign_dex_profile_path,
436 app_data_dir);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000437
438 // Create a new thread which does the saving.
439 CHECK_PTHREAD_CALL(
440 pthread_create,
441 (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)),
442 "Profile saver thread");
443}
444
Calin Juravleb8e69992016-03-09 15:37:48 +0000445void ProfileSaver::Stop(bool dump_info) {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000446 ProfileSaver* profile_saver = nullptr;
447 pthread_t profiler_pthread = 0U;
448
449 {
450 MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
Calin Juravleb4eddd22016-01-13 15:52:33 -0800451 VLOG(profiler) << "Stopping profile saver thread";
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000452 profile_saver = instance_;
453 profiler_pthread = profiler_pthread_;
454 if (instance_ == nullptr) {
455 DCHECK(false) << "Tried to stop a profile saver which was not started";
456 return;
457 }
458 if (instance_->shutting_down_) {
459 DCHECK(false) << "Tried to stop the profile saver twice";
460 return;
461 }
462 instance_->shutting_down_ = true;
Calin Juravleb8e69992016-03-09 15:37:48 +0000463 if (dump_info) {
464 instance_->DumpInfo(LOG(INFO));
465 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000466 }
467
468 {
469 // Wake up the saver thread if it is sleeping to allow for a clean exit.
470 MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_);
471 profile_saver->period_condition_.Signal(Thread::Current());
472 }
473
474 // Wait for the saver thread to stop.
475 CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown");
476
477 {
478 MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
479 instance_ = nullptr;
480 profiler_pthread_ = 0U;
481 }
482 delete profile_saver;
483}
484
485bool ProfileSaver::ShuttingDown(Thread* self) {
486 MutexLock mu(self, *Locks::profiler_lock_);
487 return shutting_down_;
488}
489
490bool ProfileSaver::IsStarted() {
491 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
492 return instance_ != nullptr;
493}
494
Calin Juravleb4eddd22016-01-13 15:52:33 -0800495void ProfileSaver::AddTrackedLocations(const std::string& output_filename,
Calin Juravle20b7e3b2016-04-18 18:59:22 +0100496 const std::string& app_data_dir,
Calin Juravleb4eddd22016-01-13 15:52:33 -0800497 const std::vector<std::string>& code_paths) {
498 auto it = tracked_dex_base_locations_.find(output_filename);
499 if (it == tracked_dex_base_locations_.end()) {
500 tracked_dex_base_locations_.Put(output_filename,
501 std::set<std::string>(code_paths.begin(), code_paths.end()));
Calin Juravle20b7e3b2016-04-18 18:59:22 +0100502 app_data_dirs_.insert(app_data_dir);
Calin Juravleb4eddd22016-01-13 15:52:33 -0800503 } else {
504 it->second.insert(code_paths.begin(), code_paths.end());
505 }
506}
507
Calin Juravlec90bc922016-02-24 10:13:09 +0000508void ProfileSaver::NotifyDexUse(const std::string& dex_location) {
Calin Juravle050fb1b2016-03-25 17:17:09 +0000509 if (!ShouldProfileLocation(dex_location)) {
510 return;
511 }
Calin Juravlec90bc922016-02-24 10:13:09 +0000512 std::set<std::string> app_code_paths;
513 std::string foreign_dex_profile_path;
Calin Juravle20b7e3b2016-04-18 18:59:22 +0100514 std::set<std::string> app_data_dirs;
Calin Juravlec90bc922016-02-24 10:13:09 +0000515 {
516 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
Calin Juravleb8e69992016-03-09 15:37:48 +0000517 if (instance_ == nullptr) {
518 return;
519 }
Calin Juravlec90bc922016-02-24 10:13:09 +0000520 // Make a copy so that we don't hold the lock while doing I/O.
521 for (const auto& it : instance_->tracked_dex_base_locations_) {
522 app_code_paths.insert(it.second.begin(), it.second.end());
523 }
524 foreign_dex_profile_path = instance_->foreign_dex_profile_path_;
Calin Juravle20b7e3b2016-04-18 18:59:22 +0100525 app_data_dirs.insert(instance_->app_data_dirs_.begin(), instance_->app_data_dirs_.end());
Calin Juravlec90bc922016-02-24 10:13:09 +0000526 }
527
Calin Juravleb8e69992016-03-09 15:37:48 +0000528 bool mark_created = MaybeRecordDexUseInternal(dex_location,
529 app_code_paths,
530 foreign_dex_profile_path,
Calin Juravle20b7e3b2016-04-18 18:59:22 +0100531 app_data_dirs);
Calin Juravleb8e69992016-03-09 15:37:48 +0000532 if (mark_created) {
533 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
534 if (instance_ != nullptr) {
535 instance_->total_number_of_foreign_dex_marks_++;
536 }
537 }
Calin Juravlec90bc922016-02-24 10:13:09 +0000538}
539
Calin Juravleb8e69992016-03-09 15:37:48 +0000540bool ProfileSaver::MaybeRecordDexUseInternal(
Calin Juravlec90bc922016-02-24 10:13:09 +0000541 const std::string& dex_location,
542 const std::set<std::string>& app_code_paths,
543 const std::string& foreign_dex_profile_path,
Calin Juravle20b7e3b2016-04-18 18:59:22 +0100544 const std::set<std::string>& app_data_dirs) {
Calin Juravlef529e9b2016-03-08 12:52:52 +0000545 if (dex_location.empty()) {
546 LOG(WARNING) << "Asked to record foreign dex use with an empty dex location.";
Calin Juravleb8e69992016-03-09 15:37:48 +0000547 return false;
Calin Juravlef529e9b2016-03-08 12:52:52 +0000548 }
Calin Juravlec90bc922016-02-24 10:13:09 +0000549 if (foreign_dex_profile_path.empty()) {
550 LOG(WARNING) << "Asked to record foreign dex use without a valid profile path ";
Calin Juravleb8e69992016-03-09 15:37:48 +0000551 return false;
Calin Juravlec90bc922016-02-24 10:13:09 +0000552 }
553
554 UniqueCPtr<const char[]> dex_location_real_path(realpath(dex_location.c_str(), nullptr));
Calin Juravlef529e9b2016-03-08 12:52:52 +0000555 if (dex_location_real_path == nullptr) {
556 PLOG(WARNING) << "Could not get realpath for " << dex_location;
557 }
558 std::string dex_location_real_path_str((dex_location_real_path == nullptr)
559 ? dex_location.c_str()
560 : dex_location_real_path.get());
Calin Juravlec90bc922016-02-24 10:13:09 +0000561
Calin Juravle20b7e3b2016-04-18 18:59:22 +0100562 if (app_data_dirs.find(dex_location_real_path_str) != app_data_dirs.end()) {
Calin Juravlec90bc922016-02-24 10:13:09 +0000563 // The dex location is under the application folder. Nothing to record.
Calin Juravleb8e69992016-03-09 15:37:48 +0000564 return false;
Calin Juravlec90bc922016-02-24 10:13:09 +0000565 }
566
567 if (app_code_paths.find(dex_location) != app_code_paths.end()) {
568 // The dex location belongs to the application code paths. Nothing to record.
Calin Juravleb8e69992016-03-09 15:37:48 +0000569 return false;
Calin Juravlec90bc922016-02-24 10:13:09 +0000570 }
571 // Do another round of checks with the real paths.
572 // Note that we could cache all the real locations in the saver (since it's an expensive
573 // operation). However we expect that app_code_paths is small (usually 1 element), and
574 // NotifyDexUse is called just a few times in the app lifetime. So we make the compromise
575 // to save some bytes of memory usage.
576 for (const auto& app_code_location : app_code_paths) {
577 UniqueCPtr<const char[]> real_app_code_location(realpath(app_code_location.c_str(), nullptr));
Calin Juravlef529e9b2016-03-08 12:52:52 +0000578 if (real_app_code_location == nullptr) {
579 PLOG(WARNING) << "Could not get realpath for " << app_code_location;
580 }
581 std::string real_app_code_location_str((real_app_code_location == nullptr)
582 ? app_code_location.c_str()
583 : real_app_code_location.get());
Calin Juravlec90bc922016-02-24 10:13:09 +0000584 if (real_app_code_location_str == dex_location_real_path_str) {
585 // The dex location belongs to the application code paths. Nothing to record.
Calin Juravleb8e69992016-03-09 15:37:48 +0000586 return false;
Calin Juravlec90bc922016-02-24 10:13:09 +0000587 }
588 }
589
590 // For foreign dex files we record a flag on disk. PackageManager will (potentially) take this
591 // into account when deciding how to optimize the loaded dex file.
592 // The expected flag name is the canonical path of the apk where '/' is substituted to '@'.
593 // (it needs to be kept in sync with
594 // frameworks/base/services/core/java/com/android/server/pm/PackageDexOptimizer.java)
595 std::replace(dex_location_real_path_str.begin(), dex_location_real_path_str.end(), '/', '@');
596 std::string flag_path = foreign_dex_profile_path + "/" + dex_location_real_path_str;
Richard Uhlere18d6192016-05-10 14:01:18 -0700597 // We use O_RDONLY as the access mode because we must supply some access
598 // mode, and there is no access mode that means 'create but do not read' the
599 // file. We will not not actually read from the file.
600 int fd = TEMP_FAILURE_RETRY(open(flag_path.c_str(),
601 O_CREAT | O_RDONLY | O_EXCL | O_CLOEXEC | O_NOFOLLOW, 0));
Calin Juravlec90bc922016-02-24 10:13:09 +0000602 if (fd != -1) {
603 if (close(fd) != 0) {
604 PLOG(WARNING) << "Could not close file after flagging foreign dex use " << flag_path;
605 }
Calin Juravleb8e69992016-03-09 15:37:48 +0000606 return true;
Calin Juravlec90bc922016-02-24 10:13:09 +0000607 } else {
Richard Uhlere18d6192016-05-10 14:01:18 -0700608 if (errno != EEXIST && errno != EACCES) {
609 // Another app could have already created the file, and selinux may not
610 // allow the read access to the file implied by the call to open.
Calin Juravlec90bc922016-02-24 10:13:09 +0000611 PLOG(WARNING) << "Could not create foreign dex use mark " << flag_path;
Calin Juravleb8e69992016-03-09 15:37:48 +0000612 return false;
Calin Juravlec90bc922016-02-24 10:13:09 +0000613 }
Calin Juravleb8e69992016-03-09 15:37:48 +0000614 return true;
Calin Juravlec90bc922016-02-24 10:13:09 +0000615 }
616}
617
Calin Juravleb8e69992016-03-09 15:37:48 +0000618void ProfileSaver::DumpInstanceInfo(std::ostream& os) {
619 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
620 if (instance_ != nullptr) {
621 instance_->DumpInfo(os);
622 }
623}
624
625void ProfileSaver::DumpInfo(std::ostream& os) {
626 os << "ProfileSaver total_bytes_written=" << total_bytes_written_ << '\n'
627 << "ProfileSaver total_number_of_writes=" << total_number_of_writes_ << '\n'
Calin Juravle67265462016-03-18 16:23:40 +0000628 << "ProfileSaver total_number_of_code_cache_queries="
629 << total_number_of_code_cache_queries_ << '\n'
Calin Juravleb8e69992016-03-09 15:37:48 +0000630 << "ProfileSaver total_number_of_skipped_writes=" << total_number_of_skipped_writes_ << '\n'
631 << "ProfileSaver total_number_of_failed_writes=" << total_number_of_failed_writes_ << '\n'
Calin Juravle67265462016-03-18 16:23:40 +0000632 << "ProfileSaver total_ms_of_sleep=" << total_ms_of_sleep_ << '\n'
Calin Juravle050fb1b2016-03-25 17:17:09 +0000633 << "ProfileSaver total_ms_of_work=" << NsToMs(total_ns_of_work_) << '\n'
Calin Juravle67265462016-03-18 16:23:40 +0000634 << "ProfileSaver total_number_of_foreign_dex_marks="
635 << total_number_of_foreign_dex_marks_ << '\n'
636 << "ProfileSaver max_number_profile_entries_cached="
Calin Juravlea2638922016-04-29 16:44:11 +0100637 << max_number_of_profile_entries_cached_ << '\n'
638 << "ProfileSaver total_number_of_hot_spikes=" << total_number_of_hot_spikes_ << '\n'
639 << "ProfileSaver total_number_of_wake_ups=" << total_number_of_wake_ups_ << '\n';
Calin Juravleb8e69992016-03-09 15:37:48 +0000640}
641
Calin Juravleffc87072016-04-20 14:22:09 +0100642
643void ProfileSaver::ForceProcessProfiles() {
644 ProfileSaver* saver = nullptr;
645 {
646 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
647 saver = instance_;
648 }
649 // TODO(calin): this is not actually thread safe as the instance_ may have been deleted,
650 // but we only use this in testing when we now this won't happen.
651 // Refactor the way we handle the instance so that we don't end up in this situation.
652 if (saver != nullptr) {
Calin Juravlea2638922016-04-29 16:44:11 +0100653 uint16_t new_methods;
654 saver->ProcessProfilingInfo(&new_methods);
Calin Juravleffc87072016-04-20 14:22:09 +0100655 }
656}
657
658bool ProfileSaver::HasSeenMethod(const std::string& profile,
659 const DexFile* dex_file,
660 uint16_t method_idx) {
661 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
662 if (instance_ != nullptr) {
663 ProfileCompilationInfo* info = instance_->GetCachedProfiledInfo(profile);
664 if (info != nullptr) {
665 return info->ContainsMethod(MethodReference(dex_file, method_idx));
666 }
667 }
668 return false;
669}
670
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000671} // namespace art