blob: 9c5e41fd13693c279cb8b29970786c8dbe311ff8 [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#ifndef ART_RUNTIME_JIT_PROFILE_SAVER_H_
18#define ART_RUNTIME_JIT_PROFILE_SAVER_H_
19
20#include "base/mutex.h"
21#include "jit_code_cache.h"
Calin Juravle33083d62017-01-18 15:29:12 -080022#include "profile_compilation_info.h"
Calin Juravle138dbff2016-06-28 19:36:58 +010023#include "profile_saver_options.h"
Calin Juravleb4eddd22016-01-13 15:52:33 -080024#include "safe_map.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000025
26namespace art {
27
28class ProfileSaver {
29 public:
Calin Juravleb4eddd22016-01-13 15:52:33 -080030 // Starts the profile saver thread if not already started.
31 // If the saver is already running it adds (output_filename, code_paths) to its tracked locations.
Calin Juravle138dbff2016-06-28 19:36:58 +010032 static void Start(const ProfileSaverOptions& options,
33 const std::string& output_filename,
Calin Juravle4d77b6a2015-12-01 18:38:09 +000034 jit::JitCodeCache* jit_code_cache,
Calin Juravlec90bc922016-02-24 10:13:09 +000035 const std::vector<std::string>& code_paths,
36 const std::string& foreign_dex_profile_path,
37 const std::string& app_data_dir)
Calin Juravle4d77b6a2015-12-01 18:38:09 +000038 REQUIRES(!Locks::profiler_lock_, !wait_lock_);
39
40 // Stops the profile saver thread.
41 // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock.
Calin Juravleb8e69992016-03-09 15:37:48 +000042 static void Stop(bool dump_info_)
Calin Juravle4d77b6a2015-12-01 18:38:09 +000043 REQUIRES(!Locks::profiler_lock_, !wait_lock_)
44 NO_THREAD_SAFETY_ANALYSIS;
45
46 // Returns true if the profile saver is started.
47 static bool IsStarted() REQUIRES(!Locks::profiler_lock_);
48
Calin Juravlec90bc922016-02-24 10:13:09 +000049 static void NotifyDexUse(const std::string& dex_location);
50
Calin Juravleb8e69992016-03-09 15:37:48 +000051 // If the profile saver is running, dumps statistics to the `os`. Otherwise it does nothing.
52 static void DumpInstanceInfo(std::ostream& os);
53
Calin Juravlea2638922016-04-29 16:44:11 +010054 // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock.
55 static void NotifyJitActivity()
56 REQUIRES(!Locks::profiler_lock_, !wait_lock_)
57 NO_THREAD_SAFETY_ANALYSIS;
58
Calin Juravleffc87072016-04-20 14:22:09 +010059 // Just for testing purpose.
60 static void ForceProcessProfiles();
61 static bool HasSeenMethod(const std::string& profile,
62 const DexFile* dex_file,
63 uint16_t method_idx);
64
Calin Juravle4d77b6a2015-12-01 18:38:09 +000065 private:
Calin Juravle138dbff2016-06-28 19:36:58 +010066 ProfileSaver(const ProfileSaverOptions& options,
67 const std::string& output_filename,
Calin Juravle4d77b6a2015-12-01 18:38:09 +000068 jit::JitCodeCache* jit_code_cache,
Calin Juravlec90bc922016-02-24 10:13:09 +000069 const std::vector<std::string>& code_paths,
70 const std::string& foreign_dex_profile_path,
71 const std::string& app_data_dir);
Calin Juravle4d77b6a2015-12-01 18:38:09 +000072
73 // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock.
74 static void* RunProfileSaverThread(void* arg)
75 REQUIRES(!Locks::profiler_lock_, !wait_lock_)
76 NO_THREAD_SAFETY_ANALYSIS;
77
78 // The run loop for the saver.
79 void Run() REQUIRES(!Locks::profiler_lock_, !wait_lock_);
80 // Processes the existing profiling info from the jit code cache and returns
81 // true if it needed to be saved to disk.
Calin Juravlea2638922016-04-29 16:44:11 +010082 bool ProcessProfilingInfo(uint16_t* new_methods)
Calin Juravleffc87072016-04-20 14:22:09 +010083 REQUIRES(!Locks::profiler_lock_)
84 REQUIRES(!Locks::mutator_lock_);
85
Calin Juravlea2638922016-04-29 16:44:11 +010086 void NotifyJitActivityInternal() REQUIRES(!wait_lock_);
87 void WakeUpSaver() REQUIRES(wait_lock_);
88
Calin Juravle4d77b6a2015-12-01 18:38:09 +000089 // Returns true if the saver is shutting down (ProfileSaver::Stop() has been called).
90 bool ShuttingDown(Thread* self) REQUIRES(!Locks::profiler_lock_);
91
Calin Juravleb4eddd22016-01-13 15:52:33 -080092 void AddTrackedLocations(const std::string& output_filename,
Calin Juravle20b7e3b2016-04-18 18:59:22 +010093 const std::string& app_data_dir,
Calin Juravleb4eddd22016-01-13 15:52:33 -080094 const std::vector<std::string>& code_paths)
95 REQUIRES(Locks::profiler_lock_);
96
Calin Juravle67265462016-03-18 16:23:40 +000097 // Retrieves the cached profile compilation info for the given profile file.
98 // If no entry exists, a new empty one will be created, added to the cache and
99 // then returned.
100 ProfileCompilationInfo* GetCachedProfiledInfo(const std::string& filename);
Mathieu Chartier27ed3a42016-05-18 08:51:52 -0700101 // Fetches the current resolved classes and methods from the ClassLinker and stores them in the
102 // profile_cache_ for later save.
103 void FetchAndCacheResolvedClassesAndMethods();
Calin Juravle67265462016-03-18 16:23:40 +0000104
Calin Juravleb8e69992016-03-09 15:37:48 +0000105 static bool MaybeRecordDexUseInternal(
Calin Juravlec90bc922016-02-24 10:13:09 +0000106 const std::string& dex_location,
107 const std::set<std::string>& tracked_locations,
108 const std::string& foreign_dex_profile_path,
Calin Juravle20b7e3b2016-04-18 18:59:22 +0100109 const std::set<std::string>& app_data_dirs);
Calin Juravlec90bc922016-02-24 10:13:09 +0000110
Calin Juravleb8e69992016-03-09 15:37:48 +0000111 void DumpInfo(std::ostream& os);
112
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000113 // The only instance of the saver.
114 static ProfileSaver* instance_ GUARDED_BY(Locks::profiler_lock_);
115 // Profile saver thread.
116 static pthread_t profiler_pthread_ GUARDED_BY(Locks::profiler_lock_);
117
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000118 jit::JitCodeCache* jit_code_cache_;
Calin Juravle20b7e3b2016-04-18 18:59:22 +0100119
120 // Collection of code paths that the profiles tracks.
121 // It maps profile locations to code paths (dex base locations).
Calin Juravleb4eddd22016-01-13 15:52:33 -0800122 SafeMap<std::string, std::set<std::string>> tracked_dex_base_locations_
123 GUARDED_BY(Locks::profiler_lock_);
Calin Juravle20b7e3b2016-04-18 18:59:22 +0100124 // The directory were the we should store the code paths.
Calin Juravlec90bc922016-02-24 10:13:09 +0000125 std::string foreign_dex_profile_path_;
Calin Juravle20b7e3b2016-04-18 18:59:22 +0100126
127 // A list of application directories, used to infer if a loaded dex belongs
128 // to the application or not. Multiple application data directories are possible when
129 // different apps share the same runtime.
130 std::set<std::string> app_data_dirs_ GUARDED_BY(Locks::profiler_lock_);
131
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000132 bool shutting_down_ GUARDED_BY(Locks::profiler_lock_);
Calin Juravle67265462016-03-18 16:23:40 +0000133 uint32_t last_save_number_of_methods_;
Calin Juravle698f4d12016-03-30 18:18:58 +0100134 uint32_t last_save_number_of_classes_;
Calin Juravlea2638922016-04-29 16:44:11 +0100135 uint64_t last_time_ns_saver_woke_up_ GUARDED_BY(wait_lock_);
136 uint32_t jit_activity_notifications_;
Calin Juravle67265462016-03-18 16:23:40 +0000137
138 // A local cache for the profile information. Maps each tracked file to its
139 // profile information. The size of this cache is usually very small and tops
140 // to just a few hundreds entries in the ProfileCompilationInfo objects.
141 // It helps avoiding unnecessary writes to disk.
142 SafeMap<std::string, ProfileCompilationInfo> profile_cache_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000143
144 // Save period condition support.
145 Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
146 ConditionVariable period_condition_ GUARDED_BY(wait_lock_);
147
Calin Juravleb8e69992016-03-09 15:37:48 +0000148 uint64_t total_bytes_written_;
149 uint64_t total_number_of_writes_;
150 uint64_t total_number_of_code_cache_queries_;
151 uint64_t total_number_of_skipped_writes_;
152 uint64_t total_number_of_failed_writes_;
Calin Juravle67265462016-03-18 16:23:40 +0000153 uint64_t total_ms_of_sleep_;
Calin Juravleb8e69992016-03-09 15:37:48 +0000154 uint64_t total_ns_of_work_;
155 uint64_t total_number_of_foreign_dex_marks_;
Calin Juravle67265462016-03-18 16:23:40 +0000156 // TODO(calin): replace with an actual size.
157 uint64_t max_number_of_profile_entries_cached_;
Calin Juravlea2638922016-04-29 16:44:11 +0100158 uint64_t total_number_of_hot_spikes_;
159 uint64_t total_number_of_wake_ups_;
Calin Juravleb8e69992016-03-09 15:37:48 +0000160
Calin Juravle138dbff2016-06-28 19:36:58 +0100161 const ProfileSaverOptions options_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000162 DISALLOW_COPY_AND_ASSIGN(ProfileSaver);
163};
164
165} // namespace art
166
167#endif // ART_RUNTIME_JIT_PROFILE_SAVER_H_