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" |
| 22 | #include "offline_profiling_info.h" |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 23 | #include "safe_map.h" |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class ProfileSaver { |
| 28 | public: |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 29 | // Starts the profile saver thread if not already started. |
| 30 | // If the saver is already running it adds (output_filename, code_paths) to its tracked locations. |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 31 | static void Start(const std::string& output_filename, |
| 32 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 33 | const std::vector<std::string>& code_paths, |
| 34 | const std::string& foreign_dex_profile_path, |
| 35 | const std::string& app_data_dir) |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 36 | REQUIRES(!Locks::profiler_lock_, !wait_lock_); |
| 37 | |
| 38 | // Stops the profile saver thread. |
| 39 | // 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^] | 40 | static void Stop(bool dump_info_) |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 41 | REQUIRES(!Locks::profiler_lock_, !wait_lock_) |
| 42 | NO_THREAD_SAFETY_ANALYSIS; |
| 43 | |
| 44 | // Returns true if the profile saver is started. |
| 45 | static bool IsStarted() REQUIRES(!Locks::profiler_lock_); |
| 46 | |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 47 | static void NotifyDexUse(const std::string& dex_location); |
| 48 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 49 | // If the profile saver is running, dumps statistics to the `os`. Otherwise it does nothing. |
| 50 | static void DumpInstanceInfo(std::ostream& os); |
| 51 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 52 | private: |
| 53 | ProfileSaver(const std::string& output_filename, |
| 54 | jit::JitCodeCache* jit_code_cache, |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 55 | const std::vector<std::string>& code_paths, |
| 56 | const std::string& foreign_dex_profile_path, |
| 57 | const std::string& app_data_dir); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 58 | |
| 59 | // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock. |
| 60 | static void* RunProfileSaverThread(void* arg) |
| 61 | REQUIRES(!Locks::profiler_lock_, !wait_lock_) |
| 62 | NO_THREAD_SAFETY_ANALYSIS; |
| 63 | |
| 64 | // The run loop for the saver. |
| 65 | void Run() REQUIRES(!Locks::profiler_lock_, !wait_lock_); |
| 66 | // Processes the existing profiling info from the jit code cache and returns |
| 67 | // true if it needed to be saved to disk. |
| 68 | bool ProcessProfilingInfo(); |
| 69 | // Returns true if the saver is shutting down (ProfileSaver::Stop() has been called). |
| 70 | bool ShuttingDown(Thread* self) REQUIRES(!Locks::profiler_lock_); |
| 71 | |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 72 | void AddTrackedLocations(const std::string& output_filename, |
| 73 | const std::vector<std::string>& code_paths) |
| 74 | REQUIRES(Locks::profiler_lock_); |
| 75 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 76 | static bool MaybeRecordDexUseInternal( |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 77 | const std::string& dex_location, |
| 78 | const std::set<std::string>& tracked_locations, |
| 79 | const std::string& foreign_dex_profile_path, |
| 80 | const std::string& app_data_dir); |
| 81 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 82 | void DumpInfo(std::ostream& os); |
| 83 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 84 | // The only instance of the saver. |
| 85 | static ProfileSaver* instance_ GUARDED_BY(Locks::profiler_lock_); |
| 86 | // Profile saver thread. |
| 87 | static pthread_t profiler_pthread_ GUARDED_BY(Locks::profiler_lock_); |
| 88 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 89 | jit::JitCodeCache* jit_code_cache_; |
Calin Juravle | b4eddd2 | 2016-01-13 15:52:33 -0800 | [diff] [blame] | 90 | SafeMap<std::string, std::set<std::string>> tracked_dex_base_locations_ |
| 91 | GUARDED_BY(Locks::profiler_lock_); |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 92 | std::string foreign_dex_profile_path_; |
| 93 | std::string app_data_dir_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 94 | uint64_t code_cache_last_update_time_ns_; |
| 95 | bool shutting_down_ GUARDED_BY(Locks::profiler_lock_); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 96 | bool first_profile_ = true; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 97 | |
| 98 | // Save period condition support. |
| 99 | Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 100 | ConditionVariable period_condition_ GUARDED_BY(wait_lock_); |
| 101 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 102 | uint64_t total_bytes_written_; |
| 103 | uint64_t total_number_of_writes_; |
| 104 | uint64_t total_number_of_code_cache_queries_; |
| 105 | uint64_t total_number_of_skipped_writes_; |
| 106 | uint64_t total_number_of_failed_writes_; |
| 107 | uint64_t total_ns_of_sleep_; |
| 108 | uint64_t total_ns_of_work_; |
| 109 | uint64_t total_number_of_foreign_dex_marks_; |
| 110 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 111 | DISALLOW_COPY_AND_ASSIGN(ProfileSaver); |
| 112 | }; |
| 113 | |
| 114 | } // namespace art |
| 115 | |
| 116 | #endif // ART_RUNTIME_JIT_PROFILE_SAVER_H_ |