blob: bb3174666222cf850454199b55fe7c2370ec03e2 [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 <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"
Mathieu Chartierdabdc0f2016-03-04 14:58:03 -080024#include "base/systrace.h"
Calin Juravle6044fa72016-03-25 17:17:09 +000025#include "base/time_utils.h"
26#include "compiler_filter.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000027#include "oat_file_manager.h"
Calin Juravle6044fa72016-03-25 17:17:09 +000028#include "scoped_thread_state_change.h"
29
Calin Juravle4d77b6a2015-12-01 18:38:09 +000030
31namespace art {
32
Calin Juravle4d77b6a2015-12-01 18:38:09 +000033// TODO: read the constants from ProfileOptions,
34// Add a random delay each time we go to sleep so that we don't hammer the CPU
35// with all profile savers running at the same time.
Calin Juravle85f7bf32016-03-18 16:23:40 +000036static constexpr const uint64_t kRandomDelayMaxMs = 30 * 1000; // 30 seconds
Calin Juravle932a0512016-01-19 14:32:26 -080037static constexpr const uint64_t kMaxBackoffMs = 5 * 60 * 1000; // 5 minutes
Calin Juravle85f7bf32016-03-18 16:23:40 +000038static constexpr const uint64_t kSavePeriodMs = 20 * 1000; // 20 seconds
Calin Juravlec15e5662016-03-17 17:07:52 +000039static constexpr const uint64_t kSaveResolvedClassesDelayMs = 2 * 1000; // 2 seconds
Calin Juravle4d77b6a2015-12-01 18:38:09 +000040static constexpr const double kBackoffCoef = 1.5;
41
Calin Juravle85f7bf32016-03-18 16:23:40 +000042static constexpr const uint32_t kMinimumNumberOfMethodsToSave = 10;
43static constexpr const uint32_t kMinimumNumberOfClassesToSave = 10;
Calin Juravle4d77b6a2015-12-01 18:38:09 +000044
45ProfileSaver* ProfileSaver::instance_ = nullptr;
46pthread_t ProfileSaver::profiler_pthread_ = 0U;
47
48ProfileSaver::ProfileSaver(const std::string& output_filename,
49 jit::JitCodeCache* jit_code_cache,
Calin Juravle86a9ebe2016-02-24 10:13:09 +000050 const std::vector<std::string>& code_paths,
51 const std::string& foreign_dex_profile_path,
52 const std::string& app_data_dir)
Calin Juravleb4eddd22016-01-13 15:52:33 -080053 : jit_code_cache_(jit_code_cache),
Calin Juravle86a9ebe2016-02-24 10:13:09 +000054 foreign_dex_profile_path_(foreign_dex_profile_path),
Calin Juravle4d77b6a2015-12-01 18:38:09 +000055 shutting_down_(false),
Calin Juravle85f7bf32016-03-18 16:23:40 +000056 last_save_number_of_methods_(0),
57 last_save_number_of_classes__(0),
Calin Juravle4d77b6a2015-12-01 18:38:09 +000058 wait_lock_("ProfileSaver wait lock"),
Calin Juravlec19c1c22016-03-09 15:37:48 +000059 period_condition_("ProfileSaver period condition", wait_lock_),
60 total_bytes_written_(0),
61 total_number_of_writes_(0),
62 total_number_of_code_cache_queries_(0),
63 total_number_of_skipped_writes_(0),
64 total_number_of_failed_writes_(0),
Calin Juravle85f7bf32016-03-18 16:23:40 +000065 total_ms_of_sleep_(0),
Calin Juravlec19c1c22016-03-09 15:37:48 +000066 total_ns_of_work_(0),
Calin Juravle85f7bf32016-03-18 16:23:40 +000067 total_number_of_foreign_dex_marks_(0),
68 max_number_of_profile_entries_cached_(0) {
Calin Juravleb4eddd22016-01-13 15:52:33 -080069 AddTrackedLocations(output_filename, code_paths);
Calin Juravlec15e5662016-03-17 17:07:52 +000070 // We only need to save the resolved classes if the profile file is empty.
71 // Otherwise we must have already save them (we always do it during the first
72 // ever profile save).
Calin Juravle86a9ebe2016-02-24 10:13:09 +000073 app_data_dir_ = "";
74 if (!app_data_dir.empty()) {
75 // The application directory is used to determine which dex files are owned by app.
76 // Since it could be a symlink (e.g. /data/data instead of /data/user/0), and we
77 // don't have control over how the dex files are actually loaded (symlink or canonical path),
78 // store it's canonical form to be sure we use the same base when comparing.
79 UniqueCPtr<const char[]> app_data_dir_real_path(realpath(app_data_dir.c_str(), nullptr));
80 if (app_data_dir_real_path != nullptr) {
81 app_data_dir_.assign(app_data_dir_real_path.get());
82 } else {
83 LOG(WARNING) << "Failed to get the real path for app dir: " << app_data_dir_
84 << ". The app dir will not be used to determine which dex files belong to the app";
85 }
86 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +000087}
88
89void ProfileSaver::Run() {
90 srand(MicroTime() * getpid());
91 Thread* self = Thread::Current();
92
93 uint64_t save_period_ms = kSavePeriodMs;
94 VLOG(profiler) << "Save profiling information every " << save_period_ms << " ms";
Calin Juravle85f7bf32016-03-18 16:23:40 +000095 bool cache_resolved_classes = true;
Mathieu Chartier8913fc12015-12-09 16:38:30 -080096 while (!ShuttingDown(self)) {
97 uint64_t sleep_time_ms;
Calin Juravle85f7bf32016-03-18 16:23:40 +000098 if (cache_resolved_classes) {
Mathieu Chartier8913fc12015-12-09 16:38:30 -080099 // Sleep less long for the first iteration since we want to record loaded classes shortly
100 // after app launch.
Calin Juravlec15e5662016-03-17 17:07:52 +0000101 sleep_time_ms = kSaveResolvedClassesDelayMs;
Mathieu Chartier8913fc12015-12-09 16:38:30 -0800102 } else {
103 const uint64_t random_sleep_delay_ms = rand() % kRandomDelayMaxMs;
104 sleep_time_ms = save_period_ms + random_sleep_delay_ms;
105 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000106 {
107 MutexLock mu(self, wait_lock_);
108 period_condition_.TimedWait(self, sleep_time_ms, 0);
109 }
Calin Juravle85f7bf32016-03-18 16:23:40 +0000110 total_ms_of_sleep_ += sleep_time_ms;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000111 if (ShuttingDown(self)) {
112 break;
113 }
114
Calin Juravlec19c1c22016-03-09 15:37:48 +0000115 uint64_t start = NanoTime();
Calin Juravle85f7bf32016-03-18 16:23:40 +0000116 if (cache_resolved_classes) {
117 // TODO(calin) This only considers the case of the primary profile file.
118 // Anything that gets loaded in the same VM will not have their resolved
119 // classes save (unless they started before the initial saving was done).
120 FetchAndCacheResolvedClasses();
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000121 } else {
Calin Juravle85f7bf32016-03-18 16:23:40 +0000122 bool profile_saved_to_disk = ProcessProfilingInfo();
123 if (!profile_saved_to_disk && save_period_ms < kMaxBackoffMs) {
124 // If we don't need to save now it is less likely that we will need to do
125 // so in the future. Increase the time between saves according to the
126 // kBackoffCoef, but make it no larger than kMaxBackoffMs.
127 save_period_ms = static_cast<uint64_t>(kBackoffCoef * save_period_ms);
128 } else {
129 // Reset the period to the initial value as it's highly likely to JIT again.
130 save_period_ms = kSavePeriodMs;
131 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000132 }
Calin Juravle85f7bf32016-03-18 16:23:40 +0000133 cache_resolved_classes = false;
Calin Juravlec19c1c22016-03-09 15:37:48 +0000134
135 total_ns_of_work_ += (NanoTime() - start);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000136 }
137}
138
Calin Juravle85f7bf32016-03-18 16:23:40 +0000139ProfileCompilationInfo* ProfileSaver::GetCachedProfiledInfo(const std::string& filename) {
140 auto info_it = profile_cache_.find(filename);
141 if (info_it == profile_cache_.end()) {
142 info_it = profile_cache_.Put(filename, ProfileCompilationInfo());
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000143 }
Calin Juravle85f7bf32016-03-18 16:23:40 +0000144 return &info_it->second;
145}
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000146
Calin Juravle85f7bf32016-03-18 16:23:40 +0000147void ProfileSaver::FetchAndCacheResolvedClasses() {
148 ScopedTrace trace(__PRETTY_FUNCTION__);
149
150 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
151 std::set<DexCacheResolvedClasses> resolved_classes =
152 class_linker->GetResolvedClasses(/*ignore boot classes*/ true);
153 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
154 uint64_t total_number_of_profile_entries_cached = 0;
155 for (const auto& it : tracked_dex_base_locations_) {
156 std::set<DexCacheResolvedClasses> resolved_classes_for_location;
157 const std::string& filename = it.first;
158 const std::set<std::string>& locations = it.second;
159
160 for (const DexCacheResolvedClasses& classes : resolved_classes) {
161 if (locations.find(classes.GetDexLocation()) != locations.end()) {
162 resolved_classes_for_location.insert(classes);
163 }
164 }
165 ProfileCompilationInfo* info = GetCachedProfiledInfo(filename);
166 info->AddMethodsAndClasses(std::vector<ArtMethod*>(), resolved_classes_for_location);
167 total_number_of_profile_entries_cached += resolved_classes_for_location.size();
168 }
169 max_number_of_profile_entries_cached_ = std::max(
170 max_number_of_profile_entries_cached_,
171 total_number_of_profile_entries_cached);
172}
173
174bool ProfileSaver::ProcessProfilingInfo() {
175 ScopedTrace trace(__PRETTY_FUNCTION__);
Calin Juravleb4eddd22016-01-13 15:52:33 -0800176 SafeMap<std::string, std::set<std::string>> tracked_locations;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000177 {
Calin Juravleb4eddd22016-01-13 15:52:33 -0800178 // Make a copy so that we don't hold the lock while doing I/O.
179 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
180 tracked_locations = tracked_dex_base_locations_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000181 }
Calin Juravlec15e5662016-03-17 17:07:52 +0000182
Calin Juravle85f7bf32016-03-18 16:23:40 +0000183 bool profile_file_saved = false;
184 uint64_t total_number_of_profile_entries_cached = 0;
Calin Juravleb4eddd22016-01-13 15:52:33 -0800185 for (const auto& it : tracked_locations) {
186 if (ShuttingDown(Thread::Current())) {
187 return true;
188 }
189 const std::string& filename = it.first;
190 const std::set<std::string>& locations = it.second;
191 std::vector<ArtMethod*> methods;
192 {
193 ScopedObjectAccess soa(Thread::Current());
194 jit_code_cache_->GetCompiledArtMethods(locations, methods);
Calin Juravlec19c1c22016-03-09 15:37:48 +0000195 total_number_of_code_cache_queries_++;
Calin Juravleb4eddd22016-01-13 15:52:33 -0800196 }
Calin Juravlec15e5662016-03-17 17:07:52 +0000197
Calin Juravle85f7bf32016-03-18 16:23:40 +0000198 ProfileCompilationInfo* cached_info = GetCachedProfiledInfo(filename);
199 cached_info->AddMethodsAndClasses(methods, std::set<DexCacheResolvedClasses>());
200 uint32_t delta_number_of_methods =
201 cached_info->GetNumberOfMethods() - last_save_number_of_methods_;
202 uint32_t delta_number_of_classes =
203 cached_info->GetNumberOfResolvedClasses() - last_save_number_of_classes__;
204
205 if (delta_number_of_methods < kMinimumNumberOfMethodsToSave &&
206 delta_number_of_classes < kMinimumNumberOfClassesToSave) {
207 VLOG(profiler) << "Not enough information to save to: " << filename
208 << " Nr of methods: " << delta_number_of_methods
209 << " Nr of classes: " << delta_number_of_classes;
Calin Juravlec19c1c22016-03-09 15:37:48 +0000210 total_number_of_skipped_writes_++;
Calin Juravle85f7bf32016-03-18 16:23:40 +0000211 continue;
Calin Juravleb4eddd22016-01-13 15:52:33 -0800212 }
Calin Juravlec19c1c22016-03-09 15:37:48 +0000213 uint64_t bytes_written;
Calin Juravlefe297a92016-03-24 20:33:22 +0000214 // Force the save. In case the profile data is corrupted or the the profile
215 // has the wrong version this will "fix" the file to the correct format.
216 if (cached_info->MergeAndSave(filename, &bytes_written, /*force*/ true)) {
Calin Juravle85f7bf32016-03-18 16:23:40 +0000217 last_save_number_of_methods_ = cached_info->GetNumberOfMethods();
218 last_save_number_of_classes__ = cached_info->GetNumberOfResolvedClasses();
219 // Clear resolved classes. No need to store them around as
220 // they don't change after the first write.
221 cached_info->ClearResolvedClasses();
Calin Juravlec19c1c22016-03-09 15:37:48 +0000222 if (bytes_written > 0) {
223 total_number_of_writes_++;
224 total_bytes_written_ += bytes_written;
Calin Juravle85f7bf32016-03-18 16:23:40 +0000225 } else {
226 // At this point we could still have avoided the write.
227 // We load and merge the data from the file lazily at its first ever
228 // save attempt. So, whatever we are trying to save could already be
229 // in the file.
230 total_number_of_skipped_writes_++;
Calin Juravlec19c1c22016-03-09 15:37:48 +0000231 }
Calin Juravle85f7bf32016-03-18 16:23:40 +0000232 profile_file_saved = true;
233 } else {
234 LOG(WARNING) << "Could not save profiling info to " << filename;
235 total_number_of_failed_writes_++;
236 // TODO: (calin): if we failed because of bad profiling data or parsing
237 // errors we should clear the profile file.
Calin Juravleb4eddd22016-01-13 15:52:33 -0800238 }
Calin Juravle85f7bf32016-03-18 16:23:40 +0000239 total_number_of_profile_entries_cached +=
240 cached_info->GetNumberOfMethods() +
241 cached_info->GetNumberOfResolvedClasses();
Calin Juravleb4eddd22016-01-13 15:52:33 -0800242 }
Calin Juravle85f7bf32016-03-18 16:23:40 +0000243 max_number_of_profile_entries_cached_ = std::max(
244 max_number_of_profile_entries_cached_,
245 total_number_of_profile_entries_cached);
246 return profile_file_saved;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000247}
248
249void* ProfileSaver::RunProfileSaverThread(void* arg) {
250 Runtime* runtime = Runtime::Current();
251 ProfileSaver* profile_saver = reinterpret_cast<ProfileSaver*>(arg);
252
253 CHECK(runtime->AttachCurrentThread("Profile Saver",
254 /*as_daemon*/true,
255 runtime->GetSystemThreadGroup(),
256 /*create_peer*/true));
257 profile_saver->Run();
258
259 runtime->DetachCurrentThread();
260 VLOG(profiler) << "Profile saver shutdown";
261 return nullptr;
262}
263
Calin Juravle6044fa72016-03-25 17:17:09 +0000264static bool ShouldProfileLocation(const std::string& location) {
265 const OatFileManager& oat_manager = Runtime::Current()->GetOatFileManager();
266 const OatFile* oat_file = oat_manager.FindOpenedOatFileFromOatLocation(location);
267 if (oat_file == nullptr) {
268 // This can happen if we fallback to run code directly from the APK.
269 // Profile it with the hope that the background dexopt will get us back into
270 // a good state.
271 LOG(WARNING) << "Accepting to profile an un-opened oat file " << location;
272 return true;
273 }
274 CompilerFilter::Filter filter = oat_file->GetCompilerFilter();
275 if (filter == CompilerFilter::kSpeed || CompilerFilter::kEverything) {
276 return false;
277 }
278 return true;
279}
280
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000281void ProfileSaver::Start(const std::string& output_filename,
282 jit::JitCodeCache* jit_code_cache,
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000283 const std::vector<std::string>& code_paths,
284 const std::string& foreign_dex_profile_path,
285 const std::string& app_data_dir) {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000286 DCHECK(Runtime::Current()->UseJit());
287 DCHECK(!output_filename.empty());
288 DCHECK(jit_code_cache != nullptr);
289
Calin Juravle6044fa72016-03-25 17:17:09 +0000290 std::vector<std::string> code_paths_to_profile;
291
292 for (const std::string& location : code_paths) {
293 if (ShouldProfileLocation(location)) {
294 code_paths_to_profile.push_back(location);
295 } else {
296 VLOG(profiler)
297 << "Skip profiling oat file because it's already speed|everything compiled:"
298 << location;
299 }
300 }
301 if (code_paths_to_profile.empty()) {
302 return;
303 }
304
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000305 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000306 if (instance_ != nullptr) {
Calin Juravleb4eddd22016-01-13 15:52:33 -0800307 // If we already have an instance, make sure it uses the same jit_code_cache.
308 // This may be called multiple times via Runtime::registerAppInfo (e.g. for
309 // apps which share the same runtime).
310 DCHECK_EQ(instance_->jit_code_cache_, jit_code_cache);
311 // Add the code_paths to the tracked locations.
312 instance_->AddTrackedLocations(output_filename, code_paths);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000313 return;
314 }
315
316 VLOG(profiler) << "Starting profile saver using output file: " << output_filename
317 << ". Tracking: " << Join(code_paths, ':');
318
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000319 instance_ = new ProfileSaver(output_filename,
320 jit_code_cache,
321 code_paths,
322 foreign_dex_profile_path,
323 app_data_dir);
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000324
325 // Create a new thread which does the saving.
326 CHECK_PTHREAD_CALL(
327 pthread_create,
328 (&profiler_pthread_, nullptr, &RunProfileSaverThread, reinterpret_cast<void*>(instance_)),
329 "Profile saver thread");
330}
331
Calin Juravlec19c1c22016-03-09 15:37:48 +0000332void ProfileSaver::Stop(bool dump_info) {
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000333 ProfileSaver* profile_saver = nullptr;
334 pthread_t profiler_pthread = 0U;
335
336 {
337 MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
Calin Juravleb4eddd22016-01-13 15:52:33 -0800338 VLOG(profiler) << "Stopping profile saver thread";
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000339 profile_saver = instance_;
340 profiler_pthread = profiler_pthread_;
341 if (instance_ == nullptr) {
342 DCHECK(false) << "Tried to stop a profile saver which was not started";
343 return;
344 }
345 if (instance_->shutting_down_) {
346 DCHECK(false) << "Tried to stop the profile saver twice";
347 return;
348 }
349 instance_->shutting_down_ = true;
Calin Juravlec19c1c22016-03-09 15:37:48 +0000350 if (dump_info) {
351 instance_->DumpInfo(LOG(INFO));
352 }
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000353 }
354
355 {
356 // Wake up the saver thread if it is sleeping to allow for a clean exit.
357 MutexLock wait_mutex(Thread::Current(), profile_saver->wait_lock_);
358 profile_saver->period_condition_.Signal(Thread::Current());
359 }
360
361 // Wait for the saver thread to stop.
362 CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profile saver thread shutdown");
363
364 {
365 MutexLock profiler_mutex(Thread::Current(), *Locks::profiler_lock_);
366 instance_ = nullptr;
367 profiler_pthread_ = 0U;
368 }
369 delete profile_saver;
370}
371
372bool ProfileSaver::ShuttingDown(Thread* self) {
373 MutexLock mu(self, *Locks::profiler_lock_);
374 return shutting_down_;
375}
376
377bool ProfileSaver::IsStarted() {
378 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
379 return instance_ != nullptr;
380}
381
Calin Juravleb4eddd22016-01-13 15:52:33 -0800382void ProfileSaver::AddTrackedLocations(const std::string& output_filename,
383 const std::vector<std::string>& code_paths) {
384 auto it = tracked_dex_base_locations_.find(output_filename);
385 if (it == tracked_dex_base_locations_.end()) {
386 tracked_dex_base_locations_.Put(output_filename,
387 std::set<std::string>(code_paths.begin(), code_paths.end()));
388 } else {
389 it->second.insert(code_paths.begin(), code_paths.end());
390 }
391}
392
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000393void ProfileSaver::NotifyDexUse(const std::string& dex_location) {
Calin Juravle6044fa72016-03-25 17:17:09 +0000394 if (!ShouldProfileLocation(dex_location)) {
395 return;
396 }
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000397 std::set<std::string> app_code_paths;
398 std::string foreign_dex_profile_path;
399 std::string app_data_dir;
400 {
401 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
Calin Juravlec19c1c22016-03-09 15:37:48 +0000402 if (instance_ == nullptr) {
403 return;
404 }
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000405 // Make a copy so that we don't hold the lock while doing I/O.
406 for (const auto& it : instance_->tracked_dex_base_locations_) {
407 app_code_paths.insert(it.second.begin(), it.second.end());
408 }
409 foreign_dex_profile_path = instance_->foreign_dex_profile_path_;
410 app_data_dir = instance_->app_data_dir_;
411 }
412
Calin Juravlec19c1c22016-03-09 15:37:48 +0000413 bool mark_created = MaybeRecordDexUseInternal(dex_location,
414 app_code_paths,
415 foreign_dex_profile_path,
416 app_data_dir);
417 if (mark_created) {
418 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
419 if (instance_ != nullptr) {
420 instance_->total_number_of_foreign_dex_marks_++;
421 }
422 }
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000423}
424
Calin Juravlec19c1c22016-03-09 15:37:48 +0000425bool ProfileSaver::MaybeRecordDexUseInternal(
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000426 const std::string& dex_location,
427 const std::set<std::string>& app_code_paths,
428 const std::string& foreign_dex_profile_path,
429 const std::string& app_data_dir) {
Calin Juravle1fae45f2016-03-08 12:52:52 +0000430 if (dex_location.empty()) {
431 LOG(WARNING) << "Asked to record foreign dex use with an empty dex location.";
Calin Juravlec19c1c22016-03-09 15:37:48 +0000432 return false;
Calin Juravle1fae45f2016-03-08 12:52:52 +0000433 }
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000434 if (foreign_dex_profile_path.empty()) {
435 LOG(WARNING) << "Asked to record foreign dex use without a valid profile path ";
Calin Juravlec19c1c22016-03-09 15:37:48 +0000436 return false;
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000437 }
438
439 UniqueCPtr<const char[]> dex_location_real_path(realpath(dex_location.c_str(), nullptr));
Calin Juravle1fae45f2016-03-08 12:52:52 +0000440 if (dex_location_real_path == nullptr) {
441 PLOG(WARNING) << "Could not get realpath for " << dex_location;
442 }
443 std::string dex_location_real_path_str((dex_location_real_path == nullptr)
444 ? dex_location.c_str()
445 : dex_location_real_path.get());
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000446
447 if (dex_location_real_path_str.compare(0, app_data_dir.length(), app_data_dir) == 0) {
448 // The dex location is under the application folder. Nothing to record.
Calin Juravlec19c1c22016-03-09 15:37:48 +0000449 return false;
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000450 }
451
452 if (app_code_paths.find(dex_location) != app_code_paths.end()) {
453 // The dex location belongs to the application code paths. Nothing to record.
Calin Juravlec19c1c22016-03-09 15:37:48 +0000454 return false;
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000455 }
456 // Do another round of checks with the real paths.
457 // Note that we could cache all the real locations in the saver (since it's an expensive
458 // operation). However we expect that app_code_paths is small (usually 1 element), and
459 // NotifyDexUse is called just a few times in the app lifetime. So we make the compromise
460 // to save some bytes of memory usage.
461 for (const auto& app_code_location : app_code_paths) {
462 UniqueCPtr<const char[]> real_app_code_location(realpath(app_code_location.c_str(), nullptr));
Calin Juravle1fae45f2016-03-08 12:52:52 +0000463 if (real_app_code_location == nullptr) {
464 PLOG(WARNING) << "Could not get realpath for " << app_code_location;
465 }
466 std::string real_app_code_location_str((real_app_code_location == nullptr)
467 ? app_code_location.c_str()
468 : real_app_code_location.get());
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000469 if (real_app_code_location_str == dex_location_real_path_str) {
470 // The dex location belongs to the application code paths. Nothing to record.
Calin Juravlec19c1c22016-03-09 15:37:48 +0000471 return false;
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000472 }
473 }
474
475 // For foreign dex files we record a flag on disk. PackageManager will (potentially) take this
476 // into account when deciding how to optimize the loaded dex file.
477 // The expected flag name is the canonical path of the apk where '/' is substituted to '@'.
478 // (it needs to be kept in sync with
479 // frameworks/base/services/core/java/com/android/server/pm/PackageDexOptimizer.java)
480 std::replace(dex_location_real_path_str.begin(), dex_location_real_path_str.end(), '/', '@');
481 std::string flag_path = foreign_dex_profile_path + "/" + dex_location_real_path_str;
482 // No need to give any sort of access to flag_path. The system has enough permissions
483 // to test for its existence.
484 int fd = TEMP_FAILURE_RETRY(open(flag_path.c_str(), O_CREAT | O_EXCL, 0));
485 if (fd != -1) {
486 if (close(fd) != 0) {
487 PLOG(WARNING) << "Could not close file after flagging foreign dex use " << flag_path;
488 }
Calin Juravlec19c1c22016-03-09 15:37:48 +0000489 return true;
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000490 } else {
491 if (errno != EEXIST) {
492 // Another app could have already created the file.
493 PLOG(WARNING) << "Could not create foreign dex use mark " << flag_path;
Calin Juravlec19c1c22016-03-09 15:37:48 +0000494 return false;
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000495 }
Calin Juravlec19c1c22016-03-09 15:37:48 +0000496 return true;
Calin Juravle86a9ebe2016-02-24 10:13:09 +0000497 }
498}
499
Calin Juravlec19c1c22016-03-09 15:37:48 +0000500void ProfileSaver::DumpInstanceInfo(std::ostream& os) {
501 MutexLock mu(Thread::Current(), *Locks::profiler_lock_);
502 if (instance_ != nullptr) {
503 instance_->DumpInfo(os);
504 }
505}
506
507void ProfileSaver::DumpInfo(std::ostream& os) {
508 os << "ProfileSaver total_bytes_written=" << total_bytes_written_ << '\n'
509 << "ProfileSaver total_number_of_writes=" << total_number_of_writes_ << '\n'
Calin Juravle85f7bf32016-03-18 16:23:40 +0000510 << "ProfileSaver total_number_of_code_cache_queries="
511 << total_number_of_code_cache_queries_ << '\n'
Calin Juravlec19c1c22016-03-09 15:37:48 +0000512 << "ProfileSaver total_number_of_skipped_writes=" << total_number_of_skipped_writes_ << '\n'
513 << "ProfileSaver total_number_of_failed_writes=" << total_number_of_failed_writes_ << '\n'
Calin Juravle85f7bf32016-03-18 16:23:40 +0000514 << "ProfileSaver total_ms_of_sleep=" << total_ms_of_sleep_ << '\n'
Calin Juravle6044fa72016-03-25 17:17:09 +0000515 << "ProfileSaver total_ms_of_work=" << NsToMs(total_ns_of_work_) << '\n'
Calin Juravle85f7bf32016-03-18 16:23:40 +0000516 << "ProfileSaver total_number_of_foreign_dex_marks="
517 << total_number_of_foreign_dex_marks_ << '\n'
518 << "ProfileSaver max_number_profile_entries_cached="
519 << max_number_of_profile_entries_cached_ << '\n';
Calin Juravlec19c1c22016-03-09 15:37:48 +0000520}
521
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000522} // namespace art