blob: 53f48644f2d053e4bede73c6c13400ae2fc2659c [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 Juravle86a9ebe2016-02-24 10:13:09 +000019#include <fcntl.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020#include <sys/resource.h>
21#include <sys/stat.h>
22#include <sys/types.h>
Calin Juravle86a9ebe2016-02-24 10:13:09 +000023
Andreas Gampe9186ced2016-12-12 14:28:21 -080024#include "android-base/strings.h"
25
Calin Juravle4d77b6a2015-12-01 18:38:09 +000026#include "art_method-inl.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070027#include "base/enums.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080028#include "base/logging.h" // For VLOG.
Mathieu Chartierfaf83202017-06-08 10:35:20 -070029#include "base/scoped_arena_containers.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070030#include "base/stl_util.h"
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080031#include "base/systrace.h"
Calin Juravle6044fa72016-03-25 17:17:09 +000032#include "base/time_utils.h"
Mathieu Chartier06bed302017-07-13 13:23:18 -070033#include "class_table-inl.h"
Calin Juravle6044fa72016-03-25 17:17:09 +000034#include "compiler_filter.h"
David Sehr9e734c72018-01-04 17:56:19 -080035#include "dex/dex_file_loader.h"
Mathieu Chartierfaf83202017-06-08 10:35:20 -070036#include "dex_reference_collection.h"
Mathieu Chartier39100372017-05-17 13:14:10 -070037#include "gc/collector_type.h"
38#include "gc/gc_cause.h"
39#include "gc/scoped_gc_critical_section.h"
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -070040#include "jit/profile_compilation_info.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000041#include "oat_file_manager.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070042#include "scoped_thread_state_change-inl.h"
Calin Juravle6044fa72016-03-25 17:17:09 +000043
Calin Juravle4d77b6a2015-12-01 18:38:09 +000044namespace art {
45
Calin Juravle4d77b6a2015-12-01 18:38:09 +000046ProfileSaver* ProfileSaver::instance_ = nullptr;
47pthread_t ProfileSaver::profiler_pthread_ = 0U;
48
Mathieu Chartier5494e5b2017-06-26 14:13:27 -070049// At what priority to schedule the saver threads. 9 is the lowest foreground priority on device.
50static constexpr int kProfileSaverPthreadPriority = 9;
51
52static void SetProfileSaverThreadPriority(pthread_t thread, int priority) {
53#if defined(ART_TARGET_ANDROID)
54 int result = setpriority(PRIO_PROCESS, pthread_gettid_np(thread), priority);
55 if (result != 0) {
56 LOG(ERROR) << "Failed to setpriority to :" << priority;
57 }
58#else
59 UNUSED(thread);
60 UNUSED(priority);
61#endif
62}
63
64static int GetDefaultThreadPriority() {
65#if defined(ART_TARGET_ANDROID)
66 pthread_attr_t attr;
67 sched_param param;
68 pthread_attr_init(&attr);
69 pthread_attr_getschedparam(&attr, &param);
70 return param.sched_priority;
71#else
72 return 0;
73#endif
74}
75
Calin Juravle138dbff2016-06-28 19:36:58 +010076ProfileSaver::ProfileSaver(const ProfileSaverOptions& options,
77 const std::string& output_filename,
Calin Juravle4d77b6a2015-12-01 18:38:09 +000078 jit::JitCodeCache* jit_code_cache,
Calin Juravle77651c42017-03-03 18:04:02 -080079 const std::vector<std::string>& code_paths)
Calin Juravleb4eddd22016-01-13 15:52:33 -080080 : jit_code_cache_(jit_code_cache),
Calin Juravle4d77b6a2015-12-01 18:38:09 +000081 shutting_down_(false),
Calin Juravle5fbb0fe2016-04-29 16:44:11 +010082 last_time_ns_saver_woke_up_(0),
83 jit_activity_notifications_(0),
Calin Juravle4d77b6a2015-12-01 18:38:09 +000084 wait_lock_("ProfileSaver wait lock"),
Calin Juravlec19c1c22016-03-09 15:37:48 +000085 period_condition_("ProfileSaver period condition", wait_lock_),
86 total_bytes_written_(0),
87 total_number_of_writes_(0),
88 total_number_of_code_cache_queries_(0),
89 total_number_of_skipped_writes_(0),
90 total_number_of_failed_writes_(0),
Calin Juravle85f7bf32016-03-18 16:23:40 +000091 total_ms_of_sleep_(0),
Calin Juravlec19c1c22016-03-09 15:37:48 +000092 total_ns_of_work_(0),
Calin Juravle5fbb0fe2016-04-29 16:44:11 +010093 max_number_of_profile_entries_cached_(0),
94 total_number_of_hot_spikes_(0),
Calin Juravle138dbff2016-06-28 19:36:58 +010095 total_number_of_wake_ups_(0),
96 options_(options) {
97 DCHECK(options_.IsEnabled());
Calin Juravle77651c42017-03-03 18:04:02 -080098 AddTrackedLocations(output_filename, code_paths);
Calin Juravle4d77b6a2015-12-01 18:38:09 +000099}
100
Calin Juravlecc3171a2017-05-19 16:47:53 -0700101ProfileSaver::~ProfileSaver() {
102 for (auto& it : profile_cache_) {
103 delete it.second;
104 }
105}
106
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000107void ProfileSaver::Run() {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000108 Thread* self = Thread::Current();
109
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100110 // Fetch the resolved classes for the app images after sleeping for
Calin Juravle138dbff2016-06-28 19:36:58 +0100111 // options_.GetSaveResolvedClassesDelayMs().
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100112 // TODO(calin) This only considers the case of the primary profile file.
113 // Anything that gets loaded in the same VM will not have their resolved
114 // classes save (unless they started before the initial saving was done).
115 {
116 MutexLock mu(self, wait_lock_);
Calin Juravle138dbff2016-06-28 19:36:58 +0100117 const uint64_t end_time = NanoTime() + MsToNs(options_.GetSaveResolvedClassesDelayMs());
Mathieu Chartier0ec065d2016-05-18 19:51:23 -0700118 while (true) {
119 const uint64_t current_time = NanoTime();
120 if (current_time >= end_time) {
121 break;
122 }
123 period_condition_.TimedWait(self, NsToMs(end_time - current_time), 0);
124 }
Calin Juravle138dbff2016-06-28 19:36:58 +0100125 total_ms_of_sleep_ += options_.GetSaveResolvedClassesDelayMs();
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100126 }
Mathieu Chartier06bed302017-07-13 13:23:18 -0700127 FetchAndCacheResolvedClassesAndMethods(/*startup*/ true);
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100128
129 // Loop for the profiled methods.
Mathieu Chartier8913fc12015-12-09 16:38:30 -0800130 while (!ShuttingDown(self)) {
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100131 uint64_t sleep_start = NanoTime();
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000132 {
Calin Juravle0233a412016-05-18 15:49:36 -0700133 uint64_t sleep_time = 0;
134 {
135 MutexLock mu(self, wait_lock_);
136 period_condition_.Wait(self);
Calin Juravledc85bd72016-05-25 18:09:53 +0100137 sleep_time = NanoTime() - sleep_start;
Calin Juravle0233a412016-05-18 15:49:36 -0700138 }
139 // Check if the thread was woken up for shutdown.
140 if (ShuttingDown(self)) {
141 break;
142 }
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100143 total_number_of_wake_ups_++;
144 // We might have been woken up by a huge number of notifications to guarantee saving.
145 // If we didn't meet the minimum saving period go back to sleep (only if missed by
146 // a reasonable margin).
Calin Juravle138dbff2016-06-28 19:36:58 +0100147 uint64_t min_save_period_ns = MsToNs(options_.GetMinSavePeriodMs());
148 while (min_save_period_ns * 0.9 > sleep_time) {
Calin Juravle0233a412016-05-18 15:49:36 -0700149 {
150 MutexLock mu(self, wait_lock_);
Calin Juravle138dbff2016-06-28 19:36:58 +0100151 period_condition_.TimedWait(self, NsToMs(min_save_period_ns - sleep_time), 0);
Calin Juravledc85bd72016-05-25 18:09:53 +0100152 sleep_time = NanoTime() - sleep_start;
Calin Juravle0233a412016-05-18 15:49:36 -0700153 }
154 // Check if the thread was woken up for shutdown.
155 if (ShuttingDown(self)) {
156 break;
157 }
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100158 total_number_of_wake_ups_++;
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100159 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000160 }
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100161 total_ms_of_sleep_ += NsToMs(NanoTime() - sleep_start);
162
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000163 if (ShuttingDown(self)) {
164 break;
165 }
166
Calin Juravlea345d312017-03-14 18:45:55 -0700167 uint16_t number_of_new_methods = 0;
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100168 uint64_t start_work = NanoTime();
Calin Juravlea345d312017-03-14 18:45:55 -0700169 bool profile_saved_to_disk = ProcessProfilingInfo(/*force_save*/false, &number_of_new_methods);
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100170 // Update the notification counter based on result. Note that there might be contention on this
171 // but we don't care about to be 100% precise.
172 if (!profile_saved_to_disk) {
173 // If we didn't save to disk it may be because we didn't have enough new methods.
Calin Juravlea345d312017-03-14 18:45:55 -0700174 // Set the jit activity notifications to number_of_new_methods so we can wake up earlier
175 // if needed.
176 jit_activity_notifications_ = number_of_new_methods;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000177 }
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100178 total_ns_of_work_ += NanoTime() - start_work;
179 }
180}
Calin Juravlec19c1c22016-03-09 15:37:48 +0000181
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100182void ProfileSaver::NotifyJitActivity() {
183 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
184 if (instance_ == nullptr || instance_->shutting_down_) {
185 return;
186 }
187 instance_->NotifyJitActivityInternal();
188}
189
190void ProfileSaver::WakeUpSaver() {
191 jit_activity_notifications_ = 0;
192 last_time_ns_saver_woke_up_ = NanoTime();
193 period_condition_.Signal(Thread::Current());
194}
195
196void ProfileSaver::NotifyJitActivityInternal() {
197 // Unlikely to overflow but if it happens,
198 // we would have waken up the saver long before that.
199 jit_activity_notifications_++;
200 // Note that we are not as precise as we could be here but we don't want to wake the saver
201 // every time we see a hot method.
Calin Juravle138dbff2016-06-28 19:36:58 +0100202 if (jit_activity_notifications_ > options_.GetMinNotificationBeforeWake()) {
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100203 MutexLock wait_mutex(Thread::Current(), wait_lock_);
Calin Juravle138dbff2016-06-28 19:36:58 +0100204 if ((NanoTime() - last_time_ns_saver_woke_up_) > MsToNs(options_.GetMinSavePeriodMs())) {
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100205 WakeUpSaver();
Serguei Katkov87de9cf2016-08-01 17:47:04 +0700206 } else if (jit_activity_notifications_ > options_.GetMaxNotificationBeforeWake()) {
207 // Make sure to wake up the saver if we see a spike in the number of notifications.
208 // This is a precaution to avoid losing a big number of methods in case
209 // this is a spike with no jit after.
210 total_number_of_hot_spikes_++;
211 WakeUpSaver();
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100212 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000213 }
214}
215
Mathieu Chartier5494e5b2017-06-26 14:13:27 -0700216class ScopedDefaultPriority {
217 public:
218 explicit ScopedDefaultPriority(pthread_t thread) : thread_(thread) {
219 SetProfileSaverThreadPriority(thread_, GetDefaultThreadPriority());
220 }
221
222 ~ScopedDefaultPriority() {
223 SetProfileSaverThreadPriority(thread_, kProfileSaverPthreadPriority);
224 }
225
226 private:
227 const pthread_t thread_;
228};
229
Mathieu Chartier06bed302017-07-13 13:23:18 -0700230// GetClassLoadersVisitor takes a snapshot of the class loaders and stores them in the out
231// class_loaders argument. Not affected by class unloading since there are no suspend points in
232// the caller.
233class GetClassLoadersVisitor : public ClassLoaderVisitor {
234 public:
235 explicit GetClassLoadersVisitor(VariableSizedHandleScope* hs,
236 std::vector<Handle<mirror::ClassLoader>>* class_loaders)
237 : hs_(hs),
238 class_loaders_(class_loaders) {}
239
240 void Visit(ObjPtr<mirror::ClassLoader> class_loader)
241 REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) OVERRIDE {
242 class_loaders_->push_back(hs_->NewHandle(class_loader));
243 }
244
245 private:
246 VariableSizedHandleScope* const hs_;
247 std::vector<Handle<mirror::ClassLoader>>* const class_loaders_;
248};
249
250// GetClassesVisitor takes a snapshot of the loaded classes that we may want to visit and stores
251// them in the out argument. Not affected by class unloading since there are no suspend points in
252// the caller.
253class GetClassesVisitor : public ClassVisitor {
254 public:
255 explicit GetClassesVisitor(bool profile_boot_class_path,
256 ScopedArenaVector<ObjPtr<mirror::Class>>* out)
257 : profile_boot_class_path_(profile_boot_class_path),
258 out_(out) {}
259
260 virtual bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) {
261 if (klass->IsProxyClass() ||
262 klass->IsArrayClass() ||
263 klass->IsPrimitive() ||
264 !klass->IsResolved() ||
265 klass->IsErroneousResolved() ||
266 (!profile_boot_class_path_ && klass->GetClassLoader() == nullptr)) {
267 return true;
268 }
269 out_->push_back(klass);
270 return true;
271 }
272
273 private:
274 const bool profile_boot_class_path_;
275 ScopedArenaVector<ObjPtr<mirror::Class>>* const out_;
276};
277
278using MethodReferenceCollection = DexReferenceCollection<uint16_t, ScopedArenaAllocatorAdapter>;
279using TypeReferenceCollection = DexReferenceCollection<dex::TypeIndex,
280 ScopedArenaAllocatorAdapter>;
281
282// Iterate over all of the loaded classes and visit each one. For each class, add it to the
283// resolved_classes out argument if startup is true.
284// Add methods to the hot_methods out argument if the number of samples is greater or equal to
285// hot_method_sample_threshold, add it to sampled_methods if it has at least one sample.
286static void SampleClassesAndExecutedMethods(pthread_t profiler_pthread,
287 bool profile_boot_class_path,
288 ScopedArenaAllocator* allocator,
289 uint32_t hot_method_sample_threshold,
290 bool startup,
291 TypeReferenceCollection* resolved_classes,
292 MethodReferenceCollection* hot_methods,
293 MethodReferenceCollection* sampled_methods) {
294 Thread* const self = Thread::Current();
295 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
296 // Restore profile saver thread priority during the GC critical section. This helps prevent
297 // priority inversions blocking the GC for long periods of time.
298 std::unique_ptr<ScopedDefaultPriority> sdp;
299 // Only restore default priority if we are the profile saver thread. Other threads that call this
300 // are threads calling Stop and the signal catcher (for SIGUSR1).
301 if (pthread_self() == profiler_pthread) {
302 sdp.reset(new ScopedDefaultPriority(profiler_pthread));
303 }
304
305 // Do ScopedGCCriticalSection before acquiring mutator lock to prevent the GC running and
306 // blocking threads during thread root flipping. Since the GC is a background thread, blocking it
307 // is not a problem.
308 ScopedObjectAccess soa(self);
309 gc::ScopedGCCriticalSection sgcs(self,
310 gc::kGcCauseProfileSaver,
311 gc::kCollectorTypeCriticalSection);
312 VariableSizedHandleScope hs(soa.Self());
313 std::vector<Handle<mirror::ClassLoader>> class_loaders;
314 if (profile_boot_class_path) {
315 // First add the boot class loader since visit classloaders doesn't visit it.
316 class_loaders.push_back(hs.NewHandle<mirror::ClassLoader>(nullptr));
317 }
318 GetClassLoadersVisitor class_loader_visitor(&hs, &class_loaders);
319 {
320 // Read the class loaders into a temporary array to prevent contention problems on the
321 // class_linker_classes_lock.
322 ScopedTrace trace2("Get class loaders");
323 ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_);
324 class_linker->VisitClassLoaders(&class_loader_visitor);
325 }
326 ScopedArenaVector<ObjPtr<mirror::Class>> classes(allocator->Adapter());
327 for (Handle<mirror::ClassLoader> class_loader : class_loaders) {
328 ClassTable* table = class_linker->ClassTableForClassLoader(class_loader.Get());
329 if (table == nullptr) {
330 // If the class loader has not loaded any classes, it may have a null table.
331 continue;
332 }
333 GetClassesVisitor get_classes_visitor(profile_boot_class_path, &classes);
334 {
335 // Collect the classes into a temporary array to prevent lock contention on the class
336 // table lock. We want to avoid blocking class loading in other threads as much as
337 // possible.
338 ScopedTrace trace3("Visiting class table");
339 table->Visit(get_classes_visitor);
340 }
341 for (ObjPtr<mirror::Class> klass : classes) {
342 if (startup) {
343 // We only record classes for the startup case. This may change in the future.
344 resolved_classes->AddReference(&klass->GetDexFile(), klass->GetDexTypeIndex());
345 }
346 // Visit all of the methods in the class to see which ones were executed.
347 for (ArtMethod& method : klass->GetMethods(kRuntimePointerSize)) {
348 if (!method.IsNative()) {
349 DCHECK(!method.IsProxyMethod());
350 const uint16_t counter = method.GetCounter();
351 // Mark startup methods as hot if they have more than hot_method_sample_threshold
352 // samples. This means they will get compiled by the compiler driver.
353 if (method.GetProfilingInfo(kRuntimePointerSize) != nullptr ||
Orion Hodsoncfcc9cf2017-09-29 15:07:27 +0100354 method.PreviouslyWarm() ||
Mathieu Chartier06bed302017-07-13 13:23:18 -0700355 counter >= hot_method_sample_threshold) {
356 hot_methods->AddReference(method.GetDexFile(), method.GetDexMethodIndex());
357 } else if (counter != 0) {
358 sampled_methods->AddReference(method.GetDexFile(), method.GetDexMethodIndex());
359 }
360 } else {
Vladimir Marko2196c652017-11-30 16:16:07 +0000361 // We do not record native methods. Once we AOT-compile the app, all native
362 // methods shall have their thunks compiled.
Mathieu Chartier06bed302017-07-13 13:23:18 -0700363 }
364 }
365 }
366 classes.clear();
367 }
368}
369
370void ProfileSaver::FetchAndCacheResolvedClassesAndMethods(bool startup) {
Calin Juravle85f7bf32016-03-18 16:23:40 +0000371 ScopedTrace trace(__PRETTY_FUNCTION__);
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700372 const uint64_t start_time = NanoTime();
Calin Juravle8b5d9b62017-05-05 17:27:23 -0700373
374 // Resolve any new registered locations.
375 ResolveTrackedLocations();
376
Mathieu Chartier39100372017-05-17 13:14:10 -0700377 Thread* const self = Thread::Current();
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700378 Runtime* const runtime = Runtime::Current();
379 ArenaStack stack(runtime->GetArenaPool());
380 ScopedArenaAllocator allocator(&stack);
381 MethodReferenceCollection hot_methods(allocator.Adapter(), allocator.Adapter());
Mathieu Chartier06bed302017-07-13 13:23:18 -0700382 MethodReferenceCollection sampled_methods(allocator.Adapter(), allocator.Adapter());
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700383 TypeReferenceCollection resolved_classes(allocator.Adapter(), allocator.Adapter());
Mathieu Chartier273d1102017-06-06 17:07:13 -0700384 const bool is_low_ram = Runtime::Current()->GetHeap()->IsLowMemoryMode();
Mathieu Chartier5494e5b2017-06-26 14:13:27 -0700385 pthread_t profiler_pthread;
Mathieu Chartierc600eaa2016-05-18 08:51:52 -0700386 {
Mathieu Chartier5494e5b2017-06-26 14:13:27 -0700387 MutexLock mu(self, *Locks::profiler_lock_);
388 profiler_pthread = profiler_pthread_;
389 }
Mathieu Chartier06bed302017-07-13 13:23:18 -0700390 const uint32_t hot_method_sample_threshold = startup ?
391 options_.GetHotStartupMethodSamples(is_low_ram) :
392 std::numeric_limits<uint32_t>::max();
393 SampleClassesAndExecutedMethods(profiler_pthread,
394 options_.GetProfileBootClassPath(),
395 &allocator,
396 hot_method_sample_threshold,
397 startup,
398 &resolved_classes,
399 &hot_methods,
400 &sampled_methods);
Mathieu Chartier39100372017-05-17 13:14:10 -0700401 MutexLock mu(self, *Locks::profiler_lock_);
Calin Juravle85f7bf32016-03-18 16:23:40 +0000402 uint64_t total_number_of_profile_entries_cached = 0;
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700403 using Hotness = ProfileCompilationInfo::MethodHotness;
Mathieu Chartier9275af62016-04-29 12:03:56 -0700404
Calin Juravle85f7bf32016-03-18 16:23:40 +0000405 for (const auto& it : tracked_dex_base_locations_) {
Mathieu Chartier9275af62016-04-29 12:03:56 -0700406 std::set<DexCacheResolvedClasses> resolved_classes_for_location;
Calin Juravle85f7bf32016-03-18 16:23:40 +0000407 const std::string& filename = it.first;
Mathieu Chartier06bed302017-07-13 13:23:18 -0700408 auto info_it = profile_cache_.find(filename);
409 if (info_it == profile_cache_.end()) {
410 info_it = profile_cache_.Put(
411 filename,
412 new ProfileCompilationInfo(Runtime::Current()->GetArenaPool()));
413 }
Calin Juravlecc3171a2017-05-19 16:47:53 -0700414 ProfileCompilationInfo* cached_info = info_it->second;
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700415
416 const std::set<std::string>& locations = it.second;
417 for (const auto& pair : hot_methods.GetMap()) {
418 const DexFile* const dex_file = pair.first;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700419 const std::string base_location = DexFileLoader::GetBaseLocation(dex_file->GetLocation());
420 if (locations.find(base_location) != locations.end()) {
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700421 const MethodReferenceCollection::IndexVector& indices = pair.second;
Mathieu Chartier06bed302017-07-13 13:23:18 -0700422 uint8_t flags = Hotness::kFlagHot;
423 flags |= startup ? Hotness::kFlagStartup : Hotness::kFlagPostStartup;
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700424 cached_info->AddMethodsForDex(
Mathieu Chartier06bed302017-07-13 13:23:18 -0700425 static_cast<Hotness::Flag>(flags),
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700426 dex_file,
427 indices.begin(),
428 indices.end());
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700429 }
430 }
Mathieu Chartier06bed302017-07-13 13:23:18 -0700431 for (const auto& pair : sampled_methods.GetMap()) {
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700432 const DexFile* const dex_file = pair.first;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700433 const std::string base_location = DexFileLoader::GetBaseLocation(dex_file->GetLocation());
434 if (locations.find(base_location) != locations.end()) {
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700435 const MethodReferenceCollection::IndexVector& indices = pair.second;
Mathieu Chartier06bed302017-07-13 13:23:18 -0700436 cached_info->AddMethodsForDex(startup ? Hotness::kFlagStartup : Hotness::kFlagPostStartup,
Mathieu Chartierdb40eac2017-06-09 18:34:11 -0700437 dex_file,
438 indices.begin(),
439 indices.end());
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700440 }
441 }
442 for (const auto& pair : resolved_classes.GetMap()) {
443 const DexFile* const dex_file = pair.first;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700444 const std::string base_location = DexFileLoader::GetBaseLocation(dex_file->GetLocation());
445 if (locations.find(base_location) != locations.end()) {
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700446 const TypeReferenceCollection::IndexVector& classes = pair.second;
447 VLOG(profiler) << "Added " << classes.size() << " classes for location "
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700448 << base_location
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700449 << " (" << dex_file->GetLocation() << ")";
450 cached_info->AddClassesForDex(dex_file, classes.begin(), classes.end());
451 } else {
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700452 VLOG(profiler) << "Location not found " << base_location
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700453 << " (" << dex_file->GetLocation() << ")";
454 }
455 }
Calin Juravle85f7bf32016-03-18 16:23:40 +0000456 total_number_of_profile_entries_cached += resolved_classes_for_location.size();
457 }
458 max_number_of_profile_entries_cached_ = std::max(
459 max_number_of_profile_entries_cached_,
460 total_number_of_profile_entries_cached);
Mathieu Chartierfaf83202017-06-08 10:35:20 -0700461 VLOG(profiler) << "Profile saver recorded " << hot_methods.NumReferences() << " hot methods and "
Mathieu Chartier06bed302017-07-13 13:23:18 -0700462 << sampled_methods.NumReferences() << " sampled methods with threshold "
463 << hot_method_sample_threshold << " in "
464 << PrettyDuration(NanoTime() - start_time);
Calin Juravle85f7bf32016-03-18 16:23:40 +0000465}
466
Calin Juravlea345d312017-03-14 18:45:55 -0700467bool ProfileSaver::ProcessProfilingInfo(bool force_save, /*out*/uint16_t* number_of_new_methods) {
Calin Juravle85f7bf32016-03-18 16:23:40 +0000468 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravle8b5d9b62017-05-05 17:27:23 -0700469
470 // Resolve any new registered locations.
471 ResolveTrackedLocations();
472
Calin Juravleb4eddd22016-01-13 15:52:33 -0800473 SafeMap<std::string, std::set<std::string>> tracked_locations;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000474 {
Calin Juravleb4eddd22016-01-13 15:52:33 -0800475 // Make a copy so that we don't hold the lock while doing I/O.
476 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
477 tracked_locations = tracked_dex_base_locations_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000478 }
Calin Juravlec15e5662016-03-17 17:07:52 +0000479
Calin Juravle85f7bf32016-03-18 16:23:40 +0000480 bool profile_file_saved = false;
Calin Juravlea345d312017-03-14 18:45:55 -0700481 if (number_of_new_methods != nullptr) {
482 *number_of_new_methods = 0;
483 }
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100484
Mathieu Chartier06bed302017-07-13 13:23:18 -0700485 // We only need to do this once, not once per dex location.
486 // TODO: Figure out a way to only do it when stuff has changed? It takes 30-50ms.
487 FetchAndCacheResolvedClassesAndMethods(/*startup*/ false);
488
Calin Juravleb4eddd22016-01-13 15:52:33 -0800489 for (const auto& it : tracked_locations) {
Calin Juravlea345d312017-03-14 18:45:55 -0700490 if (!force_save && ShuttingDown(Thread::Current())) {
491 // The ProfileSaver is in shutdown mode, meaning a stop request was made and
492 // we need to exit cleanly (by waiting for the saver thread to finish). Unless
493 // we have a request for a forced save, do not do any processing so that we
494 // speed up the exit.
Calin Juravleb4eddd22016-01-13 15:52:33 -0800495 return true;
496 }
497 const std::string& filename = it.first;
498 const std::set<std::string>& locations = it.second;
Calin Juravle940eb0c2017-01-30 19:30:44 -0800499 std::vector<ProfileMethodInfo> profile_methods;
Calin Juravleb4eddd22016-01-13 15:52:33 -0800500 {
501 ScopedObjectAccess soa(Thread::Current());
Calin Juravle940eb0c2017-01-30 19:30:44 -0800502 jit_code_cache_->GetProfiledMethods(locations, profile_methods);
Calin Juravlec19c1c22016-03-09 15:37:48 +0000503 total_number_of_code_cache_queries_++;
Calin Juravleb4eddd22016-01-13 15:52:33 -0800504 }
Calin Juravlecc3171a2017-05-19 16:47:53 -0700505 {
506 ProfileCompilationInfo info(Runtime::Current()->GetArenaPool());
507 if (!info.Load(filename, /*clear_if_invalid*/ true)) {
508 LOG(WARNING) << "Could not forcefully load profile " << filename;
509 continue;
510 }
511 uint64_t last_save_number_of_methods = info.GetNumberOfMethods();
512 uint64_t last_save_number_of_classes = info.GetNumberOfResolvedClasses();
Calin Juravlec15e5662016-03-17 17:07:52 +0000513
Calin Juravleee9cb412018-02-13 20:32:35 -0800514 info.AddMethods(profile_methods, ProfileCompilationInfo::MethodHotness::kFlagPostStartup);
Calin Juravlecc3171a2017-05-19 16:47:53 -0700515 auto profile_cache_it = profile_cache_.find(filename);
Calin Juravledcab1902017-05-12 19:18:47 -0700516 if (profile_cache_it != profile_cache_.end()) {
Calin Juravlecc3171a2017-05-19 16:47:53 -0700517 info.MergeWith(*(profile_cache_it->second));
Calin Juravledcab1902017-05-12 19:18:47 -0700518 }
Calin Juravlecc3171a2017-05-19 16:47:53 -0700519
520 int64_t delta_number_of_methods =
521 info.GetNumberOfMethods() - last_save_number_of_methods;
522 int64_t delta_number_of_classes =
523 info.GetNumberOfResolvedClasses() - last_save_number_of_classes;
524
525 if (!force_save &&
526 delta_number_of_methods < options_.GetMinMethodsToSave() &&
527 delta_number_of_classes < options_.GetMinClassesToSave()) {
528 VLOG(profiler) << "Not enough information to save to: " << filename
529 << " Number of methods: " << delta_number_of_methods
530 << " Number of classes: " << delta_number_of_classes;
Calin Juravle85f7bf32016-03-18 16:23:40 +0000531 total_number_of_skipped_writes_++;
Calin Juravlecc3171a2017-05-19 16:47:53 -0700532 continue;
Calin Juravlec19c1c22016-03-09 15:37:48 +0000533 }
Mathieu Chartier06bed302017-07-13 13:23:18 -0700534
Calin Juravlecc3171a2017-05-19 16:47:53 -0700535 if (number_of_new_methods != nullptr) {
536 *number_of_new_methods =
537 std::max(static_cast<uint16_t>(delta_number_of_methods),
538 *number_of_new_methods);
539 }
540 uint64_t bytes_written;
541 // Force the save. In case the profile data is corrupted or the the profile
542 // has the wrong version this will "fix" the file to the correct format.
543 if (info.Save(filename, &bytes_written)) {
544 // We managed to save the profile. Clear the cache stored during startup.
545 if (profile_cache_it != profile_cache_.end()) {
546 ProfileCompilationInfo *cached_info = profile_cache_it->second;
547 profile_cache_.erase(profile_cache_it);
548 delete cached_info;
549 }
550 if (bytes_written > 0) {
551 total_number_of_writes_++;
552 total_bytes_written_ += bytes_written;
553 profile_file_saved = true;
554 } else {
555 // At this point we could still have avoided the write.
556 // We load and merge the data from the file lazily at its first ever
557 // save attempt. So, whatever we are trying to save could already be
558 // in the file.
559 total_number_of_skipped_writes_++;
560 }
561 } else {
562 LOG(WARNING) << "Could not save profiling info to " << filename;
563 total_number_of_failed_writes_++;
564 }
Calin Juravleb4eddd22016-01-13 15:52:33 -0800565 }
Calin Juravleb4eddd22016-01-13 15:52:33 -0800566 }
Calin Juravledcab1902017-05-12 19:18:47 -0700567
Mathieu Chartier06bed302017-07-13 13:23:18 -0700568 // Trim the maps to madvise the pages used for profile info.
569 // It is unlikely we will need them again in the near feature.
570 Runtime::Current()->GetArenaPool()->TrimMaps();
571
Calin Juravle85f7bf32016-03-18 16:23:40 +0000572 return profile_file_saved;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000573}
574
575void* ProfileSaver::RunProfileSaverThread(void* arg) {
576 Runtime* runtime = Runtime::Current();
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000577
Calin Juravlee55fda12016-04-28 12:59:33 +0100578 bool attached = runtime->AttachCurrentThread("Profile Saver",
579 /*as_daemon*/true,
580 runtime->GetSystemThreadGroup(),
581 /*create_peer*/true);
582 if (!attached) {
583 CHECK(runtime->IsShuttingDown(Thread::Current()));
584 return nullptr;
585 }
586
587 ProfileSaver* profile_saver = reinterpret_cast<ProfileSaver*>(arg);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000588 profile_saver->Run();
589
590 runtime->DetachCurrentThread();
591 VLOG(profiler) << "Profile saver shutdown";
592 return nullptr;
593}
594
Calin Juravle6044fa72016-03-25 17:17:09 +0000595static bool ShouldProfileLocation(const std::string& location) {
Calin Juravle75064232016-04-18 16:38:27 +0100596 OatFileManager& oat_manager = Runtime::Current()->GetOatFileManager();
597 const OatFile* oat_file = oat_manager.FindOpenedOatFileFromDexLocation(location);
Calin Juravle6044fa72016-03-25 17:17:09 +0000598 if (oat_file == nullptr) {
599 // This can happen if we fallback to run code directly from the APK.
600 // Profile it with the hope that the background dexopt will get us back into
601 // a good state.
Calin Juravle75064232016-04-18 16:38:27 +0100602 VLOG(profiler) << "Asked to profile a location without an oat file:" << location;
Calin Juravle6044fa72016-03-25 17:17:09 +0000603 return true;
604 }
605 CompilerFilter::Filter filter = oat_file->GetCompilerFilter();
Calin Juravled19dc462016-04-19 18:17:41 +0100606 if ((filter == CompilerFilter::kSpeed) || (filter == CompilerFilter::kEverything)) {
Calin Juravle75064232016-04-18 16:38:27 +0100607 VLOG(profiler)
Calin Juravled19dc462016-04-19 18:17:41 +0100608 << "Skip profiling oat file because it's already speed|everything compiled: "
609 << location << " oat location: " << oat_file->GetLocation();
Calin Juravle6044fa72016-03-25 17:17:09 +0000610 return false;
611 }
612 return true;
613}
614
Calin Juravle138dbff2016-06-28 19:36:58 +0100615void ProfileSaver::Start(const ProfileSaverOptions& options,
616 const std::string& output_filename,
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000617 jit::JitCodeCache* jit_code_cache,
Calin Juravle77651c42017-03-03 18:04:02 -0800618 const std::vector<std::string>& code_paths) {
Mathieu Chartier885a7132017-06-10 14:35:11 -0700619 Runtime* const runtime = Runtime::Current();
Calin Juravle138dbff2016-06-28 19:36:58 +0100620 DCHECK(options.IsEnabled());
Mathieu Chartier885a7132017-06-10 14:35:11 -0700621 DCHECK(runtime->GetJit() != nullptr);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000622 DCHECK(!output_filename.empty());
623 DCHECK(jit_code_cache != nullptr);
624
Calin Juravle6044fa72016-03-25 17:17:09 +0000625 std::vector<std::string> code_paths_to_profile;
Calin Juravle6044fa72016-03-25 17:17:09 +0000626 for (const std::string& location : code_paths) {
627 if (ShouldProfileLocation(location)) {
628 code_paths_to_profile.push_back(location);
Calin Juravle6044fa72016-03-25 17:17:09 +0000629 }
630 }
Mathieu Chartier885a7132017-06-10 14:35:11 -0700631
632 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
633 // Support getting profile samples for the boot class path. This will be used to generate the boot
634 // image profile. The intention is to use this code to generate to boot image but not use it in
635 // production. b/37966211
636 if (options.GetProfileBootClassPath()) {
637 std::set<std::string> code_paths_keys;
638 for (const std::string& location : code_paths) {
639 code_paths_keys.insert(ProfileCompilationInfo::GetProfileDexFileKey(location));
640 }
641 for (const DexFile* dex_file : runtime->GetClassLinker()->GetBootClassPath()) {
642 // Don't check ShouldProfileLocation since the boot class path may be speed compiled.
643 const std::string& location = dex_file->GetLocation();
644 const std::string key = ProfileCompilationInfo::GetProfileDexFileKey(location);
645 VLOG(profiler) << "Registering boot dex file " << location;
646 if (code_paths_keys.find(key) != code_paths_keys.end()) {
647 LOG(WARNING) << "Boot class path location key conflicts with code path " << location;
648 } else if (instance_ == nullptr) {
649 // Only add the boot class path once since Start may be called multiple times for secondary
650 // dexes.
651 // We still do the collision check above. This handles any secondary dexes that conflict
652 // with the boot class path dex files.
653 code_paths_to_profile.push_back(location);
654 }
655 }
656 }
Calin Juravle6044fa72016-03-25 17:17:09 +0000657 if (code_paths_to_profile.empty()) {
Calin Juravle75064232016-04-18 16:38:27 +0100658 VLOG(profiler) << "No code paths should be profiled.";
Calin Juravle6044fa72016-03-25 17:17:09 +0000659 return;
660 }
661
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000662 if (instance_ != nullptr) {
Calin Juravleb4eddd22016-01-13 15:52:33 -0800663 // If we already have an instance, make sure it uses the same jit_code_cache.
664 // This may be called multiple times via Runtime::registerAppInfo (e.g. for
665 // apps which share the same runtime).
666 DCHECK_EQ(instance_->jit_code_cache_, jit_code_cache);
667 // Add the code_paths to the tracked locations.
Calin Juravle77651c42017-03-03 18:04:02 -0800668 instance_->AddTrackedLocations(output_filename, code_paths_to_profile);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000669 return;
670 }
671
672 VLOG(profiler) << "Starting profile saver using output file: " << output_filename
Andreas Gampe9186ced2016-12-12 14:28:21 -0800673 << ". Tracking: " << android::base::Join(code_paths_to_profile, ':');
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000674
Calin Juravle138dbff2016-06-28 19:36:58 +0100675 instance_ = new ProfileSaver(options,
676 output_filename,
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000677 jit_code_cache,
Calin Juravle77651c42017-03-03 18:04:02 -0800678 code_paths_to_profile);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000679
680 // Create a new thread which does the saving.
681 CHECK_PTHREAD_CALL(
682 pthread_create,
683 (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)),
684 "Profile saver thread");
Nicolas Geoffray23caed82017-04-21 14:30:18 +0100685
Mathieu Chartier5494e5b2017-06-26 14:13:27 -0700686 SetProfileSaverThreadPriority(profiler_pthread_, kProfileSaverPthreadPriority);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000687}
688
Calin Juravlec19c1c22016-03-09 15:37:48 +0000689void ProfileSaver::Stop(bool dump_info) {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000690 ProfileSaver* profile_saver = nullptr;
691 pthread_t profiler_pthread = 0U;
692
693 {
694 MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
Calin Juravleb4eddd22016-01-13 15:52:33 -0800695 VLOG(profiler) << "Stopping profile saver thread";
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000696 profile_saver = instance_;
697 profiler_pthread = profiler_pthread_;
698 if (instance_ == nullptr) {
699 DCHECK(false) << "Tried to stop a profile saver which was not started";
700 return;
701 }
702 if (instance_->shutting_down_) {
703 DCHECK(false) << "Tried to stop the profile saver twice";
704 return;
705 }
706 instance_->shutting_down_ = true;
707 }
708
709 {
710 // Wake up the saver thread if it is sleeping to allow for a clean exit.
711 MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_);
712 profile_saver->period_condition_.Signal(Thread::Current());
713 }
714
Mathieu Chartier06bed302017-07-13 13:23:18 -0700715 // Force save everything before destroying the thread since we want profiler_pthread_ to remain
716 // valid.
717 instance_->ProcessProfilingInfo(/*force_save*/true, /*number_of_new_methods*/nullptr);
718
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000719 // Wait for the saver thread to stop.
720 CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown");
721
722 {
723 MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
Calin Juravle9db22e82017-03-30 16:31:23 -0700724 if (dump_info) {
725 instance_->DumpInfo(LOG_STREAM(INFO));
726 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000727 instance_ = nullptr;
728 profiler_pthread_ = 0U;
729 }
730 delete profile_saver;
731}
732
733bool ProfileSaver::ShuttingDown(Thread* self) {
734 MutexLock mu(self, *Locks::profiler_lock_);
735 return shutting_down_;
736}
737
738bool ProfileSaver::IsStarted() {
739 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
740 return instance_ != nullptr;
741}
742
Calin Juravle8b5d9b62017-05-05 17:27:23 -0700743static void AddTrackedLocationsToMap(const std::string& output_filename,
744 const std::vector<std::string>& code_paths,
745 SafeMap<std::string, std::set<std::string>>* map) {
746 auto it = map->find(output_filename);
747 if (it == map->end()) {
748 map->Put(output_filename, std::set<std::string>(code_paths.begin(), code_paths.end()));
Calin Juravleb4eddd22016-01-13 15:52:33 -0800749 } else {
750 it->second.insert(code_paths.begin(), code_paths.end());
751 }
752}
753
Calin Juravle8b5d9b62017-05-05 17:27:23 -0700754void ProfileSaver::AddTrackedLocations(const std::string& output_filename,
755 const std::vector<std::string>& code_paths) {
756 // Add the code paths to the list of tracked location.
757 AddTrackedLocationsToMap(output_filename, code_paths, &tracked_dex_base_locations_);
758 // The code paths may contain symlinks which could fool the profiler.
759 // If the dex file is compiled with an absolute location but loaded with symlink
760 // the profiler could skip the dex due to location mismatch.
761 // To avoid this, we add the code paths to the temporary cache of 'to_be_resolved'
762 // locations. When the profiler thread executes we will resolve the paths to their
763 // real paths.
764 // Note that we delay taking the realpath to avoid spending more time than needed
765 // when registering location (as it is done during app launch).
766 AddTrackedLocationsToMap(output_filename,
767 code_paths,
768 &tracked_dex_base_locations_to_be_resolved_);
769}
770
Calin Juravlec19c1c22016-03-09 15:37:48 +0000771void ProfileSaver::DumpInstanceInfo(std::ostream& os) {
772 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
773 if (instance_ != nullptr) {
774 instance_->DumpInfo(os);
775 }
776}
777
778void ProfileSaver::DumpInfo(std::ostream& os) {
779 os << "ProfileSaver total_bytes_written=" << total_bytes_written_ << '\n'
780 << "ProfileSaver total_number_of_writes=" << total_number_of_writes_ << '\n'
Calin Juravle85f7bf32016-03-18 16:23:40 +0000781 << "ProfileSaver total_number_of_code_cache_queries="
782 << total_number_of_code_cache_queries_ << '\n'
Calin Juravlec19c1c22016-03-09 15:37:48 +0000783 << "ProfileSaver total_number_of_skipped_writes=" << total_number_of_skipped_writes_ << '\n'
784 << "ProfileSaver total_number_of_failed_writes=" << total_number_of_failed_writes_ << '\n'
Calin Juravle85f7bf32016-03-18 16:23:40 +0000785 << "ProfileSaver total_ms_of_sleep=" << total_ms_of_sleep_ << '\n'
Calin Juravle6044fa72016-03-25 17:17:09 +0000786 << "ProfileSaver total_ms_of_work=" << NsToMs(total_ns_of_work_) << '\n'
Calin Juravle85f7bf32016-03-18 16:23:40 +0000787 << "ProfileSaver max_number_profile_entries_cached="
Calin Juravle5fbb0fe2016-04-29 16:44:11 +0100788 << max_number_of_profile_entries_cached_ << '\n'
789 << "ProfileSaver total_number_of_hot_spikes=" << total_number_of_hot_spikes_ << '\n'
790 << "ProfileSaver total_number_of_wake_ups=" << total_number_of_wake_ups_ << '\n';
Calin Juravlec19c1c22016-03-09 15:37:48 +0000791}
792
Calin Juravlee5de54c2016-04-20 14:22:09 +0100793
794void ProfileSaver::ForceProcessProfiles() {
795 ProfileSaver* saver = nullptr;
796 {
797 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
798 saver = instance_;
799 }
800 // TODO(calin): this is not actually thread safe as the instance_ may have been deleted,
801 // but we only use this in testing when we now this won't happen.
802 // Refactor the way we handle the instance so that we don't end up in this situation.
803 if (saver != nullptr) {
Calin Juravlea345d312017-03-14 18:45:55 -0700804 saver->ProcessProfilingInfo(/*force_save*/true, /*number_of_new_methods*/nullptr);
Calin Juravlee5de54c2016-04-20 14:22:09 +0100805 }
806}
807
Mathieu Chartier06bed302017-07-13 13:23:18 -0700808bool ProfileSaver::HasSeenMethod(const std::string& profile, bool hot, MethodReference ref) {
Calin Juravlee5de54c2016-04-20 14:22:09 +0100809 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
810 if (instance_ != nullptr) {
Calin Juravlecc3171a2017-05-19 16:47:53 -0700811 ProfileCompilationInfo info(Runtime::Current()->GetArenaPool());
Calin Juravledcab1902017-05-12 19:18:47 -0700812 if (!info.Load(profile, /*clear_if_invalid*/false)) {
813 return false;
814 }
Mathieu Chartier06bed302017-07-13 13:23:18 -0700815 ProfileCompilationInfo::MethodHotness hotness = info.GetMethodHotness(ref);
Mathieu Chartier48a951b2017-07-13 17:04:02 -0700816 // Ignore hot parameter for now since it was causing test 595 to be flaky. TODO: Investigate.
817 // b/63635729
818 UNUSED(hot);
819 return hotness.IsInProfile();
Calin Juravlee5de54c2016-04-20 14:22:09 +0100820 }
821 return false;
822}
823
Calin Juravle8b5d9b62017-05-05 17:27:23 -0700824void ProfileSaver::ResolveTrackedLocations() {
825 SafeMap<std::string, std::set<std::string>> locations_to_be_resolved;
826 {
827 // Make a copy so that we don't hold the lock while doing I/O.
828 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
829 locations_to_be_resolved = tracked_dex_base_locations_to_be_resolved_;
830 tracked_dex_base_locations_to_be_resolved_.clear();
831 }
832
833 // Resolve the locations.
834 SafeMap<std::string, std::vector<std::string>> resolved_locations_map;
835 for (const auto& it : locations_to_be_resolved) {
836 const std::string& filename = it.first;
837 const std::set<std::string>& locations = it.second;
838 auto resolved_locations_it = resolved_locations_map.Put(
839 filename,
840 std::vector<std::string>(locations.size()));
841
842 for (const auto& location : locations) {
843 UniqueCPtr<const char[]> location_real(realpath(location.c_str(), nullptr));
844 // Note that it's ok if we cannot get the real path.
845 if (location_real != nullptr) {
846 resolved_locations_it->second.emplace_back(location_real.get());
847 }
848 }
849 }
850
851 // Add the resolved locations to the tracked collection.
852 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
853 for (const auto& it : resolved_locations_map) {
854 AddTrackedLocationsToMap(it.first, it.second, &tracked_dex_base_locations_);
855 }
856}
857
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000858} // namespace art