Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 1 | /* |
| 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 Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 22 | #include "profile_compilation_info.h" |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 23 | #include "profile_saver_options.h" |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 24 | #include "safe_map.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | class ProfileSaver { |
| 29 | public: |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 30 | // 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 Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 32 | static void Start(const ProfileSaverOptions& options, |
| 33 | const std::string& output_filename, |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 34 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 35 | const std::vector<std::string>& code_paths, |
| 36 | const std::string& foreign_dex_profile_path, |
| 37 | const std::string& app_data_dir) |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 38 | 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 Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 42 | static void Stop(bool dump_info_) |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 43 | 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 Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 49 | static void NotifyDexUse(const std::string& dex_location); |
| 50 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 51 | // If the profile saver is running, dumps statistics to the `os`. Otherwise it does nothing. |
| 52 | static void DumpInstanceInfo(std::ostream& os); |
| 53 | |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 54 | // 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 Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 59 | // 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 Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 65 | private: |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 66 | ProfileSaver(const ProfileSaverOptions& options, |
| 67 | const std::string& output_filename, |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 68 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 69 | const std::vector<std::string>& code_paths, |
| 70 | const std::string& foreign_dex_profile_path, |
| 71 | const std::string& app_data_dir); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 72 | |
| 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 Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 82 | bool ProcessProfilingInfo(uint16_t* new_methods) |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 83 | REQUIRES(!Locks::profiler_lock_) |
| 84 | REQUIRES(!Locks::mutator_lock_); |
| 85 | |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 86 | void NotifyJitActivityInternal() REQUIRES(!wait_lock_); |
| 87 | void WakeUpSaver() REQUIRES(wait_lock_); |
| 88 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 89 | // Returns true if the saver is shutting down (ProfileSaver::Stop() has been called). |
| 90 | bool ShuttingDown(Thread* self) REQUIRES(!Locks::profiler_lock_); |
| 91 | |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 92 | void AddTrackedLocations(const std::string& output_filename, |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 93 | const std::string& app_data_dir, |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 94 | const std::vector<std::string>& code_paths) |
| 95 | REQUIRES(Locks::profiler_lock_); |
| 96 | |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 97 | // 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 Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 101 | // 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 Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 104 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 105 | static bool MaybeRecordDexUseInternal( |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 106 | const std::string& dex_location, |
| 107 | const std::set<std::string>& tracked_locations, |
| 108 | const std::string& foreign_dex_profile_path, |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 109 | const std::set<std::string>& app_data_dirs); |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 110 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 111 | void DumpInfo(std::ostream& os); |
| 112 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 113 | // 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 Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 118 | jit::JitCodeCache* jit_code_cache_; |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 119 | |
| 120 | // Collection of code paths that the profiles tracks. |
| 121 | // It maps profile locations to code paths (dex base locations). |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 122 | SafeMap<std::string, std::set<std::string>> tracked_dex_base_locations_ |
| 123 | GUARDED_BY(Locks::profiler_lock_); |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 124 | // The directory were the we should store the code paths. |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 125 | std::string foreign_dex_profile_path_; |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 126 | |
| 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 Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 132 | bool shutting_down_ GUARDED_BY(Locks::profiler_lock_); |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 133 | uint32_t last_save_number_of_methods_; |
Calin Juravle | 698f4d1 | 2016-03-30 18:18:58 +0100 | [diff] [blame] | 134 | uint32_t last_save_number_of_classes_; |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 135 | uint64_t last_time_ns_saver_woke_up_ GUARDED_BY(wait_lock_); |
| 136 | uint32_t jit_activity_notifications_; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 137 | |
| 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 Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 143 | |
| 144 | // Save period condition support. |
| 145 | Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 146 | ConditionVariable period_condition_ GUARDED_BY(wait_lock_); |
| 147 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 148 | 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 Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 153 | uint64_t total_ms_of_sleep_; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 154 | uint64_t total_ns_of_work_; |
| 155 | uint64_t total_number_of_foreign_dex_marks_; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 156 | // TODO(calin): replace with an actual size. |
| 157 | uint64_t max_number_of_profile_entries_cached_; |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 158 | uint64_t total_number_of_hot_spikes_; |
| 159 | uint64_t total_number_of_wake_ups_; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 160 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 161 | const ProfileSaverOptions options_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 162 | DISALLOW_COPY_AND_ASSIGN(ProfileSaver); |
| 163 | }; |
| 164 | |
| 165 | } // namespace art |
| 166 | |
| 167 | #endif // ART_RUNTIME_JIT_PROFILE_SAVER_H_ |