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" |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 22 | #include "method_reference.h" |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 23 | #include "profile_compilation_info.h" |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 24 | #include "profile_saver_options.h" |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 25 | #include "safe_map.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | class ProfileSaver { |
| 30 | public: |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 31 | // Starts the profile saver thread if not already started. |
| 32 | // 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] | 33 | static void Start(const ProfileSaverOptions& options, |
| 34 | const std::string& output_filename, |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 35 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 36 | const std::vector<std::string>& code_paths) |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 37 | REQUIRES(!Locks::profiler_lock_, !wait_lock_); |
| 38 | |
| 39 | // Stops the profile saver thread. |
| 40 | // 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] | 41 | static void Stop(bool dump_info_) |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 42 | REQUIRES(!Locks::profiler_lock_, !wait_lock_) |
| 43 | NO_THREAD_SAFETY_ANALYSIS; |
| 44 | |
| 45 | // Returns true if the profile saver is started. |
| 46 | static bool IsStarted() REQUIRES(!Locks::profiler_lock_); |
| 47 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 48 | // If the profile saver is running, dumps statistics to the `os`. Otherwise it does nothing. |
| 49 | static void DumpInstanceInfo(std::ostream& os); |
| 50 | |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 51 | // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock. |
| 52 | static void NotifyJitActivity() |
| 53 | REQUIRES(!Locks::profiler_lock_, !wait_lock_) |
| 54 | NO_THREAD_SAFETY_ANALYSIS; |
| 55 | |
Mathieu Chartier | 07ea07e | 2017-04-05 17:23:54 -0700 | [diff] [blame] | 56 | // For testing or manual purposes (SIGUSR1). |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 57 | static void ForceProcessProfiles(); |
Mathieu Chartier | 07ea07e | 2017-04-05 17:23:54 -0700 | [diff] [blame] | 58 | |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 59 | // Just for testing purposes. |
| 60 | static bool HasSeenMethod(const std::string& profile, bool hot, MethodReference ref); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 61 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 62 | private: |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 63 | ProfileSaver(const ProfileSaverOptions& options, |
| 64 | const std::string& output_filename, |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 65 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 66 | const std::vector<std::string>& code_paths); |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 67 | ~ProfileSaver(); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 68 | |
| 69 | // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock. |
| 70 | static void* RunProfileSaverThread(void* arg) |
| 71 | REQUIRES(!Locks::profiler_lock_, !wait_lock_) |
| 72 | NO_THREAD_SAFETY_ANALYSIS; |
| 73 | |
| 74 | // The run loop for the saver. |
| 75 | void Run() REQUIRES(!Locks::profiler_lock_, !wait_lock_); |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 76 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 77 | // Processes the existing profiling info from the jit code cache and returns |
| 78 | // true if it needed to be saved to disk. |
Calin Juravle | a345d31 | 2017-03-14 18:45:55 -0700 | [diff] [blame] | 79 | // If number_of_new_methods is not null, after the call it will contain the number of new methods |
| 80 | // written to disk. |
| 81 | // If force_save is true, the saver will ignore any constraints which limit IO (e.g. will write |
| 82 | // the profile to disk even if it's just one new method). |
| 83 | bool ProcessProfilingInfo(bool force_save, /*out*/uint16_t* number_of_new_methods) |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 84 | REQUIRES(!Locks::profiler_lock_) |
| 85 | REQUIRES(!Locks::mutator_lock_); |
| 86 | |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 87 | void NotifyJitActivityInternal() REQUIRES(!wait_lock_); |
| 88 | void WakeUpSaver() REQUIRES(wait_lock_); |
| 89 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 90 | // Returns true if the saver is shutting down (ProfileSaver::Stop() has been called). |
| 91 | bool ShuttingDown(Thread* self) REQUIRES(!Locks::profiler_lock_); |
| 92 | |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 93 | void AddTrackedLocations(const std::string& output_filename, |
| 94 | const std::vector<std::string>& code_paths) |
| 95 | REQUIRES(Locks::profiler_lock_); |
| 96 | |
Mathieu Chartier | 27ed3a4 | 2016-05-18 08:51:52 -0700 | [diff] [blame] | 97 | // Fetches the current resolved classes and methods from the ClassLinker and stores them in the |
| 98 | // profile_cache_ for later save. |
Mathieu Chartier | 06bed30 | 2017-07-13 13:23:18 -0700 | [diff] [blame] | 99 | void FetchAndCacheResolvedClassesAndMethods(bool startup); |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 100 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 101 | void DumpInfo(std::ostream& os); |
| 102 | |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 103 | // Resolve the realpath of the locations stored in tracked_dex_base_locations_to_be_resolved_ |
| 104 | // and put the result in tracked_dex_base_locations_. |
| 105 | void ResolveTrackedLocations() REQUIRES(!Locks::profiler_lock_); |
| 106 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 107 | // The only instance of the saver. |
| 108 | static ProfileSaver* instance_ GUARDED_BY(Locks::profiler_lock_); |
| 109 | // Profile saver thread. |
| 110 | static pthread_t profiler_pthread_ GUARDED_BY(Locks::profiler_lock_); |
| 111 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 112 | jit::JitCodeCache* jit_code_cache_; |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 113 | |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 114 | // Collection of code paths that the profiler tracks. |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 115 | // It maps profile locations to code paths (dex base locations). |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 116 | SafeMap<std::string, std::set<std::string>> tracked_dex_base_locations_ |
| 117 | GUARDED_BY(Locks::profiler_lock_); |
Calin Juravle | 20b7e3b | 2016-04-18 18:59:22 +0100 | [diff] [blame] | 118 | |
Calin Juravle | 8b5d9b6 | 2017-05-05 17:27:23 -0700 | [diff] [blame] | 119 | // Collection of code paths that the profiler tracks but may note have been resolved |
| 120 | // to their realpath. The resolution is done async to minimize the time it takes for |
| 121 | // someone to register a path. |
| 122 | SafeMap<std::string, std::set<std::string>> tracked_dex_base_locations_to_be_resolved_ |
| 123 | GUARDED_BY(Locks::profiler_lock_); |
| 124 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 125 | bool shutting_down_ GUARDED_BY(Locks::profiler_lock_); |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 126 | uint64_t last_time_ns_saver_woke_up_ GUARDED_BY(wait_lock_); |
| 127 | uint32_t jit_activity_notifications_; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 128 | |
| 129 | // A local cache for the profile information. Maps each tracked file to its |
Calin Juravle | dcab190 | 2017-05-12 19:18:47 -0700 | [diff] [blame] | 130 | // profile information. This is used to cache the startup classes so that |
| 131 | // we don't hammer the disk to save them right away. |
| 132 | // The size of this cache is usually very small and tops |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 133 | // to just a few hundreds entries in the ProfileCompilationInfo objects. |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 134 | SafeMap<std::string, ProfileCompilationInfo*> profile_cache_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 135 | |
| 136 | // Save period condition support. |
| 137 | Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 138 | ConditionVariable period_condition_ GUARDED_BY(wait_lock_); |
| 139 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 140 | uint64_t total_bytes_written_; |
| 141 | uint64_t total_number_of_writes_; |
| 142 | uint64_t total_number_of_code_cache_queries_; |
| 143 | uint64_t total_number_of_skipped_writes_; |
| 144 | uint64_t total_number_of_failed_writes_; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 145 | uint64_t total_ms_of_sleep_; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 146 | uint64_t total_ns_of_work_; |
Calin Juravle | 6726546 | 2016-03-18 16:23:40 +0000 | [diff] [blame] | 147 | // TODO(calin): replace with an actual size. |
| 148 | uint64_t max_number_of_profile_entries_cached_; |
Calin Juravle | a263892 | 2016-04-29 16:44:11 +0100 | [diff] [blame] | 149 | uint64_t total_number_of_hot_spikes_; |
| 150 | uint64_t total_number_of_wake_ups_; |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 151 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 152 | const ProfileSaverOptions options_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 153 | DISALLOW_COPY_AND_ASSIGN(ProfileSaver); |
| 154 | }; |
| 155 | |
| 156 | } // namespace art |
| 157 | |
| 158 | #endif // ART_RUNTIME_JIT_PROFILE_SAVER_H_ |