Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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_JIT_H_ |
| 18 | #define ART_RUNTIME_JIT_JIT_H_ |
| 19 | |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 20 | #include "base/arena_allocator.h" |
| 21 | #include "base/histogram-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 22 | #include "base/macros.h" |
| 23 | #include "base/mutex.h" |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 24 | #include "base/timing_logger.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 25 | #include "object_callbacks.h" |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 26 | #include "offline_profiling_info.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 27 | #include "thread_pool.h" |
| 28 | |
| 29 | namespace art { |
| 30 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 31 | class ArtMethod; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 32 | struct RuntimeArgumentMap; |
| 33 | |
| 34 | namespace jit { |
| 35 | |
| 36 | class JitCodeCache; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 37 | class JitOptions; |
| 38 | |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 39 | static constexpr int16_t kJitCheckForOSR = -1; |
| 40 | static constexpr int16_t kJitHotnessDisabled = -2; |
| 41 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 42 | class Jit { |
| 43 | public: |
| 44 | static constexpr bool kStressMode = kIsDebugBuild; |
Nicolas Geoffray | 83f080a | 2016-03-08 16:50:21 +0000 | [diff] [blame] | 45 | static constexpr size_t kDefaultCompileThreshold = kStressMode ? 2 : 10000; |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 46 | static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000; |
| 47 | static constexpr size_t kDefaultTransitionRatio = 100; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 48 | |
| 49 | virtual ~Jit(); |
| 50 | static Jit* Create(JitOptions* options, std::string* error_msg); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 51 | bool CompileMethod(ArtMethod* method, Thread* self, bool osr) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 52 | SHARED_REQUIRES(Locks::mutator_lock_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 53 | void CreateThreadPool(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 54 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 55 | const JitCodeCache* GetCodeCache() const { |
| 56 | return code_cache_.get(); |
| 57 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 58 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 59 | JitCodeCache* GetCodeCache() { |
| 60 | return code_cache_.get(); |
| 61 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 62 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 63 | void DeleteThreadPool(); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 64 | // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative |
| 65 | // loggers. |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 66 | void DumpInfo(std::ostream& os) REQUIRES(!lock_); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 67 | // Add a timing logger to cumulative_timings_. |
| 68 | void AddTimingLogger(const TimingLogger& logger); |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 69 | |
| 70 | void AddMemoryUsage(ArtMethod* method, size_t bytes) |
| 71 | REQUIRES(!lock_) |
| 72 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 73 | |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 74 | size_t OSRMethodThreshold() const { |
| 75 | return osr_method_threshold_; |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 76 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 77 | |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 78 | size_t HotMethodThreshold() const { |
| 79 | return hot_method_threshold_; |
| 80 | } |
| 81 | |
| 82 | size_t WarmMethodThreshold() const { |
| 83 | return warm_method_threshold_; |
| 84 | } |
| 85 | |
| 86 | uint16_t PriorityThreadWeight() const { |
| 87 | return priority_thread_weight_; |
| 88 | } |
| 89 | |
| 90 | // Wait until there is no more pending compilation tasks. |
| 91 | void WaitForCompilationToFinish(Thread* self); |
| 92 | |
| 93 | // Profiling methods. |
| 94 | void MethodEntered(Thread* thread, ArtMethod* method) |
| 95 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 96 | |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 97 | void AddSamples(Thread* self, ArtMethod* method, uint16_t samples, bool with_backedges) |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 98 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 99 | |
| 100 | void InvokeVirtualOrInterface(Thread* thread, |
| 101 | mirror::Object* this_object, |
| 102 | ArtMethod* caller, |
| 103 | uint32_t dex_pc, |
| 104 | ArtMethod* callee) |
| 105 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 106 | |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 107 | void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller) |
| 108 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 109 | AddSamples(self, caller, transition_weight_, false); |
| 110 | } |
| 111 | |
| 112 | void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee) |
| 113 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 114 | AddSamples(self, callee, transition_weight_, false); |
| 115 | } |
| 116 | |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 117 | // Starts the profile saver if the config options allow profile recording. |
| 118 | // The profile will be stored in the specified `filename` and will contain |
| 119 | // information collected from the given `code_paths` (a set of dex locations). |
| 120 | // The `foreign_dex_profile_path` is the path where the saver will put the |
| 121 | // profile markers for loaded dex files which are not owned by the application. |
| 122 | // The `app_dir` is the application directory and is used to decide which |
| 123 | // dex files belong to the application. |
| 124 | void StartProfileSaver(const std::string& filename, |
| 125 | const std::vector<std::string>& code_paths, |
| 126 | const std::string& foreign_dex_profile_path, |
| 127 | const std::string& app_dir); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 128 | void StopProfileSaver(); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 129 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame^] | 130 | void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_); |
Nicolas Geoffray | aee2156 | 2015-12-15 16:39:44 +0000 | [diff] [blame] | 131 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 132 | static void NewTypeLoadedIfUsingJit(mirror::Class* type) |
| 133 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 134 | |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 135 | // If debug info generation is turned on then write the type information for types already loaded |
| 136 | // into the specified class linker to the jit debug interface, |
| 137 | void DumpTypeInfoForLoadedTypes(ClassLinker* linker); |
| 138 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 139 | // Return whether we should try to JIT compiled code as soon as an ArtMethod is invoked. |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 140 | bool JitAtFirstUse(); |
| 141 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 142 | // Return whether we can invoke JIT code for `method`. |
| 143 | bool CanInvokeCompiledCode(ArtMethod* method); |
| 144 | |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 145 | // Return whether the runtime should use a priority thread weight when sampling. |
| 146 | static bool ShouldUsePriorityThreadWeight(); |
| 147 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 148 | // If an OSR compiled version is available for `method`, |
| 149 | // and `dex_pc + dex_pc_offset` is an entry point of that compiled |
| 150 | // version, this method will jump to the compiled code, let it run, |
| 151 | // and return true afterwards. Return false otherwise. |
| 152 | static bool MaybeDoOnStackReplacement(Thread* thread, |
| 153 | ArtMethod* method, |
| 154 | uint32_t dex_pc, |
| 155 | int32_t dex_pc_offset, |
| 156 | JValue* result) |
| 157 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 158 | |
Mathieu Chartier | c1bc415 | 2016-03-24 17:22:52 -0700 | [diff] [blame] | 159 | static bool LoadCompilerLibrary(std::string* error_msg); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 160 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 161 | private: |
| 162 | Jit(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 163 | |
Mathieu Chartier | c1bc415 | 2016-03-24 17:22:52 -0700 | [diff] [blame] | 164 | static bool LoadCompiler(std::string* error_msg); |
| 165 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 166 | // JIT compiler |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 167 | static void* jit_library_handle_; |
| 168 | static void* jit_compiler_handle_; |
| 169 | static void* (*jit_load_)(bool*); |
| 170 | static void (*jit_unload_)(void*); |
| 171 | static bool (*jit_compile_method_)(void*, ArtMethod*, Thread*, bool); |
| 172 | static void (*jit_types_loaded_)(void*, mirror::Class**, size_t count); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 173 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 174 | // Performance monitoring. |
| 175 | bool dump_info_on_shutdown_; |
| 176 | CumulativeLogger cumulative_timings_; |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 177 | Histogram<uint64_t> memory_use_ GUARDED_BY(lock_); |
| 178 | Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 179 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 180 | std::unique_ptr<jit::JitCodeCache> code_cache_; |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 181 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 182 | bool save_profiling_info_; |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 183 | static bool generate_debug_info_; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 184 | uint16_t hot_method_threshold_; |
| 185 | uint16_t warm_method_threshold_; |
| 186 | uint16_t osr_method_threshold_; |
| 187 | uint16_t priority_thread_weight_; |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 188 | uint16_t transition_weight_; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 189 | std::unique_ptr<ThreadPool> thread_pool_; |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 190 | |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 191 | DISALLOW_COPY_AND_ASSIGN(Jit); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | class JitOptions { |
| 195 | public: |
| 196 | static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options); |
| 197 | size_t GetCompileThreshold() const { |
| 198 | return compile_threshold_; |
| 199 | } |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 200 | size_t GetWarmupThreshold() const { |
| 201 | return warmup_threshold_; |
| 202 | } |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 203 | size_t GetOsrThreshold() const { |
| 204 | return osr_threshold_; |
| 205 | } |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 206 | uint16_t GetPriorityThreadWeight() const { |
| 207 | return priority_thread_weight_; |
| 208 | } |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 209 | size_t GetCodeCacheInitialCapacity() const { |
| 210 | return code_cache_initial_capacity_; |
| 211 | } |
| 212 | size_t GetCodeCacheMaxCapacity() const { |
| 213 | return code_cache_max_capacity_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 214 | } |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 215 | bool DumpJitInfoOnShutdown() const { |
| 216 | return dump_info_on_shutdown_; |
| 217 | } |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 218 | bool GetSaveProfilingInfo() const { |
| 219 | return save_profiling_info_; |
| 220 | } |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 221 | bool UseJIT() const { |
| 222 | return use_jit_; |
| 223 | } |
| 224 | void SetUseJIT(bool b) { |
| 225 | use_jit_ = b; |
| 226 | } |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 227 | void SetSaveProfilingInfo(bool b) { |
| 228 | save_profiling_info_ = b; |
| 229 | } |
Siva Chandra | 05d2415 | 2016-01-05 17:43:17 -0800 | [diff] [blame] | 230 | void SetJitAtFirstUse() { |
| 231 | use_jit_ = true; |
| 232 | compile_threshold_ = 0; |
| 233 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 234 | |
| 235 | private: |
Mathieu Chartier | 455f67c | 2015-03-17 13:48:29 -0700 | [diff] [blame] | 236 | bool use_jit_; |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 237 | size_t code_cache_initial_capacity_; |
| 238 | size_t code_cache_max_capacity_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 239 | size_t compile_threshold_; |
Nicolas Geoffray | 5550ca8 | 2015-08-21 18:38:30 +0100 | [diff] [blame] | 240 | size_t warmup_threshold_; |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 241 | size_t osr_threshold_; |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 242 | uint16_t priority_thread_weight_; |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 243 | bool dump_info_on_shutdown_; |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 244 | bool save_profiling_info_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 245 | |
Nicolas Geoffray | 0a3be16 | 2015-11-18 11:15:22 +0000 | [diff] [blame] | 246 | JitOptions() |
| 247 | : use_jit_(false), |
| 248 | code_cache_initial_capacity_(0), |
| 249 | code_cache_max_capacity_(0), |
| 250 | compile_threshold_(0), |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 251 | dump_info_on_shutdown_(false), |
| 252 | save_profiling_info_(false) { } |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 253 | |
| 254 | DISALLOW_COPY_AND_ASSIGN(JitOptions); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | } // namespace jit |
| 258 | } // namespace art |
| 259 | |
| 260 | #endif // ART_RUNTIME_JIT_JIT_H_ |