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/histogram-inl.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 21 | #include "base/macros.h" |
| 22 | #include "base/mutex.h" |
David Srbecky | 2182147 | 2019-07-29 15:10:31 +0100 | [diff] [blame] | 23 | #include "base/runtime_debug.h" |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 24 | #include "base/timing_logger.h" |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 25 | #include "handle.h" |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 26 | #include "jit/profile_saver_options.h" |
| 27 | #include "obj_ptr.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 28 | #include "thread_pool.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 32 | class ArtMethod; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 33 | class ClassLinker; |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 34 | class DexFile; |
Nicolas Geoffray | dc2fbb6 | 2019-04-11 22:55:50 +0100 | [diff] [blame] | 35 | class OatDexFile; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 36 | struct RuntimeArgumentMap; |
Vladimir Marko | 3a21e38 | 2016-09-02 12:38:38 +0100 | [diff] [blame] | 37 | union JValue; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 38 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 39 | namespace mirror { |
| 40 | class Object; |
| 41 | class Class; |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 42 | class ClassLoader; |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 43 | class DexCache; |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 44 | class String; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 45 | } // namespace mirror |
| 46 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 47 | namespace jit { |
| 48 | |
| 49 | class JitCodeCache; |
Nicolas Geoffray | 7f7539b | 2019-06-06 16:20:54 +0100 | [diff] [blame] | 50 | class JitMemoryRegion; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 51 | class JitOptions; |
| 52 | |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 53 | static constexpr int16_t kJitCheckForOSR = -1; |
| 54 | static constexpr int16_t kJitHotnessDisabled = -2; |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 55 | // At what priority to schedule jit threads. 9 is the lowest foreground priority on device. |
| 56 | // See android/os/Process.java. |
| 57 | static constexpr int kJitPoolThreadPthreadDefaultPriority = 9; |
David Srbecky | 81448a2 | 2019-08-01 14:29:53 +0100 | [diff] [blame] | 58 | // We check whether to jit-compile the method every Nth invoke. |
| 59 | // The tests often use threshold of 1000 (and thus 500 to start profiling). |
| 60 | static constexpr uint32_t kJitSamplesBatchSize = 512; // Must be power of 2. |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 61 | |
| 62 | class JitOptions { |
| 63 | public: |
| 64 | static JitOptions* CreateFromRuntimeArguments(const RuntimeArgumentMap& options); |
| 65 | |
| 66 | uint16_t GetCompileThreshold() const { |
| 67 | return compile_threshold_; |
| 68 | } |
| 69 | |
| 70 | uint16_t GetWarmupThreshold() const { |
| 71 | return warmup_threshold_; |
| 72 | } |
| 73 | |
| 74 | uint16_t GetOsrThreshold() const { |
| 75 | return osr_threshold_; |
| 76 | } |
| 77 | |
| 78 | uint16_t GetPriorityThreadWeight() const { |
| 79 | return priority_thread_weight_; |
| 80 | } |
| 81 | |
| 82 | uint16_t GetInvokeTransitionWeight() const { |
| 83 | return invoke_transition_weight_; |
| 84 | } |
| 85 | |
| 86 | size_t GetCodeCacheInitialCapacity() const { |
| 87 | return code_cache_initial_capacity_; |
| 88 | } |
| 89 | |
| 90 | size_t GetCodeCacheMaxCapacity() const { |
| 91 | return code_cache_max_capacity_; |
| 92 | } |
| 93 | |
| 94 | bool DumpJitInfoOnShutdown() const { |
| 95 | return dump_info_on_shutdown_; |
| 96 | } |
| 97 | |
| 98 | const ProfileSaverOptions& GetProfileSaverOptions() const { |
| 99 | return profile_saver_options_; |
| 100 | } |
| 101 | |
| 102 | bool GetSaveProfilingInfo() const { |
| 103 | return profile_saver_options_.IsEnabled(); |
| 104 | } |
| 105 | |
| 106 | int GetThreadPoolPthreadPriority() const { |
| 107 | return thread_pool_pthread_priority_; |
| 108 | } |
| 109 | |
| 110 | bool UseJitCompilation() const { |
| 111 | return use_jit_compilation_; |
| 112 | } |
| 113 | |
| 114 | void SetUseJitCompilation(bool b) { |
| 115 | use_jit_compilation_ = b; |
| 116 | } |
| 117 | |
| 118 | void SetSaveProfilingInfo(bool save_profiling_info) { |
| 119 | profile_saver_options_.SetEnabled(save_profiling_info); |
| 120 | } |
| 121 | |
| 122 | void SetWaitForJitNotificationsToSaveProfile(bool value) { |
| 123 | profile_saver_options_.SetWaitForJitNotificationsToSave(value); |
| 124 | } |
| 125 | |
| 126 | void SetProfileAOTCode(bool value) { |
| 127 | profile_saver_options_.SetProfileAOTCode(value); |
| 128 | } |
| 129 | |
| 130 | void SetJitAtFirstUse() { |
| 131 | use_jit_compilation_ = true; |
| 132 | compile_threshold_ = 0; |
| 133 | } |
| 134 | |
| 135 | private: |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 136 | // We add the sample in batches of size kJitSamplesBatchSize. |
| 137 | // This method rounds the threshold so that it is multiple of the batch size. |
| 138 | static uint32_t RoundUpThreshold(uint32_t threshold); |
| 139 | |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 140 | bool use_jit_compilation_; |
| 141 | size_t code_cache_initial_capacity_; |
| 142 | size_t code_cache_max_capacity_; |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 143 | uint32_t compile_threshold_; |
| 144 | uint32_t warmup_threshold_; |
| 145 | uint32_t osr_threshold_; |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 146 | uint16_t priority_thread_weight_; |
| 147 | uint16_t invoke_transition_weight_; |
| 148 | bool dump_info_on_shutdown_; |
| 149 | int thread_pool_pthread_priority_; |
| 150 | ProfileSaverOptions profile_saver_options_; |
| 151 | |
| 152 | JitOptions() |
| 153 | : use_jit_compilation_(false), |
| 154 | code_cache_initial_capacity_(0), |
| 155 | code_cache_max_capacity_(0), |
| 156 | compile_threshold_(0), |
| 157 | warmup_threshold_(0), |
| 158 | osr_threshold_(0), |
| 159 | priority_thread_weight_(0), |
| 160 | invoke_transition_weight_(0), |
| 161 | dump_info_on_shutdown_(false), |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 162 | thread_pool_pthread_priority_(kJitPoolThreadPthreadDefaultPriority) {} |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 163 | |
| 164 | DISALLOW_COPY_AND_ASSIGN(JitOptions); |
| 165 | }; |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 166 | |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame^] | 167 | // Implemented and provided by the compiler library. |
| 168 | class JitCompilerInterface { |
| 169 | public: |
| 170 | virtual ~JitCompilerInterface() {} |
| 171 | virtual bool CompileMethod( |
| 172 | Thread* self, JitMemoryRegion* region, ArtMethod* method, bool baseline, bool osr) |
| 173 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 174 | virtual void TypesLoaded(mirror::Class**, size_t count) |
| 175 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 176 | virtual bool GenerateDebugInfo(); |
| 177 | virtual void ParseCompilerOptions(); |
| 178 | }; |
| 179 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 180 | class Jit { |
| 181 | public: |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 182 | static constexpr size_t kDefaultPriorityThreadWeightRatio = 1000; |
Nicolas Geoffray | bd553eb | 2016-04-28 13:56:04 +0100 | [diff] [blame] | 183 | static constexpr size_t kDefaultInvokeTransitionWeightRatio = 500; |
buzbee | 42a09cb0 | 2017-02-01 09:08:31 -0800 | [diff] [blame] | 184 | // How frequently should the interpreter check to see if OSR compilation is ready. |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 185 | static constexpr int16_t kJitRecheckOSRThreshold = 101; // Prime number to avoid patterns. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 186 | |
David Srbecky | 2182147 | 2019-07-29 15:10:31 +0100 | [diff] [blame] | 187 | DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode); |
| 188 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 189 | virtual ~Jit(); |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 190 | |
| 191 | // Create JIT itself. |
| 192 | static Jit* Create(JitCodeCache* code_cache, JitOptions* options); |
| 193 | |
Nicolas Geoffray | d2f13ba | 2019-06-04 16:48:58 +0100 | [diff] [blame] | 194 | bool CompileMethod(ArtMethod* method, Thread* self, bool baseline, bool osr, bool prejit) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 195 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 196 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 197 | const JitCodeCache* GetCodeCache() const { |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 198 | return code_cache_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 199 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 200 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 201 | JitCodeCache* GetCodeCache() { |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 202 | return code_cache_; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 203 | } |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 204 | |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 205 | void CreateThreadPool(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 206 | void DeleteThreadPool(); |
Mathieu Chartier | 93c21ba | 2018-12-10 13:08:30 -0800 | [diff] [blame] | 207 | void WaitForWorkersToBeCreated(); |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 208 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 209 | // Dump interesting info: #methods compiled, code vs data size, compile / verify cumulative |
| 210 | // loggers. |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 211 | void DumpInfo(std::ostream& os) REQUIRES(!lock_); |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 212 | // Add a timing logger to cumulative_timings_. |
| 213 | void AddTimingLogger(const TimingLogger& logger); |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 214 | |
| 215 | void AddMemoryUsage(ArtMethod* method, size_t bytes) |
| 216 | REQUIRES(!lock_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 217 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 218 | |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 219 | uint16_t OSRMethodThreshold() const { |
| 220 | return options_->GetOsrThreshold(); |
Mathieu Chartier | a50f9cf | 2015-09-25 11:34:45 -0700 | [diff] [blame] | 221 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 222 | |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 223 | uint16_t HotMethodThreshold() const { |
| 224 | return options_->GetCompileThreshold(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 227 | uint16_t WarmMethodThreshold() const { |
| 228 | return options_->GetWarmupThreshold(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | uint16_t PriorityThreadWeight() const { |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 232 | return options_->GetPriorityThreadWeight(); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 235 | // Return whether we should do JIT compilation. Note this will returns false |
| 236 | // if we only need to save profile information and not compile methods. |
Nicolas Geoffray | 2fef66b | 2019-06-26 22:00:02 +0000 | [diff] [blame] | 237 | bool UseJitCompilation() const { |
| 238 | return options_->UseJitCompilation(); |
| 239 | } |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 240 | |
Calin Juravle | 138dbff | 2016-06-28 19:36:58 +0100 | [diff] [blame] | 241 | bool GetSaveProfilingInfo() const { |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 242 | return options_->GetSaveProfilingInfo(); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 245 | // Wait until there is no more pending compilation tasks. |
| 246 | void WaitForCompilationToFinish(Thread* self); |
| 247 | |
| 248 | // Profiling methods. |
| 249 | void MethodEntered(Thread* thread, ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 250 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 251 | |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 252 | ALWAYS_INLINE void AddSamples(Thread* self, |
| 253 | ArtMethod* method, |
| 254 | uint16_t samples, |
| 255 | bool with_backedges) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 256 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 257 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 258 | void InvokeVirtualOrInterface(ObjPtr<mirror::Object> this_object, |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 259 | ArtMethod* caller, |
| 260 | uint32_t dex_pc, |
| 261 | ArtMethod* callee) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 262 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 274fe4a | 2016-04-12 16:33:24 +0100 | [diff] [blame] | 263 | |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 264 | void NotifyInterpreterToCompiledCodeTransition(Thread* self, ArtMethod* caller) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 265 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 266 | AddSamples(self, caller, options_->GetInvokeTransitionWeight(), false); |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void NotifyCompiledCodeToInterpreterTransition(Thread* self, ArtMethod* callee) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 270 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 271 | AddSamples(self, callee, options_->GetInvokeTransitionWeight(), false); |
Nicolas Geoffray | 71cd50f | 2016-04-14 15:00:33 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 274 | // Starts the profile saver if the config options allow profile recording. |
| 275 | // The profile will be stored in the specified `filename` and will contain |
| 276 | // information collected from the given `code_paths` (a set of dex locations). |
Calin Juravle | c90bc92 | 2016-02-24 10:13:09 +0000 | [diff] [blame] | 277 | void StartProfileSaver(const std::string& filename, |
Calin Juravle | 77651c4 | 2017-03-03 18:04:02 -0800 | [diff] [blame] | 278 | const std::vector<std::string>& code_paths); |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 279 | void StopProfileSaver(); |
Calin Juravle | 31f2c15 | 2015-10-23 17:56:15 +0100 | [diff] [blame] | 280 | |
Calin Juravle | b8e6999 | 2016-03-09 15:37:48 +0000 | [diff] [blame] | 281 | void DumpForSigQuit(std::ostream& os) REQUIRES(!lock_); |
Nicolas Geoffray | aee2156 | 2015-12-15 16:39:44 +0000 | [diff] [blame] | 282 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 283 | static void NewTypeLoadedIfUsingJit(mirror::Class* type) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 284 | REQUIRES_SHARED(Locks::mutator_lock_); |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 285 | |
Tamas Berghammer | fffbee4 | 2016-01-15 13:09:34 +0000 | [diff] [blame] | 286 | // If debug info generation is turned on then write the type information for types already loaded |
| 287 | // into the specified class linker to the jit debug interface, |
| 288 | void DumpTypeInfoForLoadedTypes(ClassLinker* linker); |
| 289 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 290 | // 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] | 291 | bool JitAtFirstUse(); |
| 292 | |
Nicolas Geoffray | 3512244 | 2016-03-02 12:05:30 +0000 | [diff] [blame] | 293 | // Return whether we can invoke JIT code for `method`. |
| 294 | bool CanInvokeCompiledCode(ArtMethod* method); |
| 295 | |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 296 | // Return whether the runtime should use a priority thread weight when sampling. |
Vladimir Marko | a710d91 | 2017-09-12 14:56:07 +0100 | [diff] [blame] | 297 | static bool ShouldUsePriorityThreadWeight(Thread* self); |
Calin Juravle | b2771b4 | 2016-04-07 17:09:25 +0100 | [diff] [blame] | 298 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 299 | // If an OSR compiled version is available for `method`, |
| 300 | // and `dex_pc + dex_pc_offset` is an entry point of that compiled |
| 301 | // version, this method will jump to the compiled code, let it run, |
| 302 | // and return true afterwards. Return false otherwise. |
| 303 | static bool MaybeDoOnStackReplacement(Thread* thread, |
| 304 | ArtMethod* method, |
| 305 | uint32_t dex_pc, |
| 306 | int32_t dex_pc_offset, |
| 307 | JValue* result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 308 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 309 | |
Nicolas Geoffray | a7edd0d | 2018-11-07 03:18:16 +0000 | [diff] [blame] | 310 | // Load the compiler library. |
| 311 | static bool LoadCompilerLibrary(std::string* error_msg); |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 312 | |
Andreas Gampe | f149b3f | 2016-11-16 14:58:24 -0800 | [diff] [blame] | 313 | ThreadPool* GetThreadPool() const { |
| 314 | return thread_pool_.get(); |
| 315 | } |
| 316 | |
Nicolas Geoffray | 021c5f2 | 2016-12-16 11:22:05 +0000 | [diff] [blame] | 317 | // Stop the JIT by waiting for all current compilations and enqueued compilations to finish. |
| 318 | void Stop(); |
| 319 | |
| 320 | // Start JIT threads. |
| 321 | void Start(); |
| 322 | |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 323 | // Transition to a child state. |
Nicolas Geoffray | 0d54cfb | 2019-05-03 09:13:52 +0100 | [diff] [blame] | 324 | void PostForkChildAction(bool is_system_server, bool is_zygote); |
Nicolas Geoffray | ce9ed36 | 2018-11-29 03:19:28 +0000 | [diff] [blame] | 325 | |
| 326 | // Prepare for forking. |
| 327 | void PreZygoteFork(); |
| 328 | |
| 329 | // Adjust state after forking. |
| 330 | void PostZygoteFork(); |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 331 | |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 332 | // Compile methods from the given profile (.prof extension). If `add_to_queue` |
| 333 | // is true, methods in the profile are added to the JIT queue. Otherwise they are compiled |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 334 | // directly. |
Nicolas Geoffray | e32d24c | 2019-07-05 10:28:59 +0100 | [diff] [blame] | 335 | // Return the number of methods added to the queue. |
| 336 | uint32_t CompileMethodsFromProfile(Thread* self, |
| 337 | const std::vector<const DexFile*>& dex_files, |
| 338 | const std::string& profile_path, |
| 339 | Handle<mirror::ClassLoader> class_loader, |
| 340 | bool add_to_queue); |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 341 | |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 342 | // Compile methods from the given boot profile (.bprof extension). If `add_to_queue` |
| 343 | // is true, methods in the profile are added to the JIT queue. Otherwise they are compiled |
| 344 | // directly. |
| 345 | // Return the number of methods added to the queue. |
| 346 | uint32_t CompileMethodsFromBootProfile(Thread* self, |
| 347 | const std::vector<const DexFile*>& dex_files, |
| 348 | const std::string& profile_path, |
| 349 | Handle<mirror::ClassLoader> class_loader, |
| 350 | bool add_to_queue); |
| 351 | |
Nicolas Geoffray | 9ac09ee | 2019-05-08 23:38:27 +0100 | [diff] [blame] | 352 | // Register the dex files to the JIT. This is to perform any compilation/optimization |
| 353 | // at the point of loading the dex files. |
| 354 | void RegisterDexFiles(const std::vector<std::unique_ptr<const DexFile>>& dex_files, |
Nicolas Geoffray | 741a070 | 2019-06-10 11:18:11 +0100 | [diff] [blame] | 355 | jobject class_loader); |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 356 | |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 357 | // Called by the compiler to know whether it can directly encode the |
| 358 | // method/class/string. |
Nicolas Geoffray | 05b41c4 | 2019-06-28 12:46:33 +0100 | [diff] [blame] | 359 | bool CanEncodeMethod(ArtMethod* method, bool is_for_shared_region) const |
| 360 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | a48c3df | 2019-06-27 13:11:12 +0000 | [diff] [blame] | 361 | bool CanEncodeClass(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const |
| 362 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 363 | bool CanEncodeString(ObjPtr<mirror::String> string, bool is_for_shared_region) const |
| 364 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 365 | bool CanAssumeInitialized(ObjPtr<mirror::Class> cls, bool is_for_shared_region) const |
| 366 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 367 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 368 | private: |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 369 | Jit(JitCodeCache* code_cache, JitOptions* options); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 370 | |
Nicolas Geoffray | 0f8950a | 2019-07-24 13:22:11 +0100 | [diff] [blame] | 371 | // Compile an individual method listed in a profile. If `add_to_queue` is |
| 372 | // true and the method was resolved, return true. Otherwise return false. |
| 373 | bool CompileMethodFromProfile(Thread* self, |
| 374 | ClassLinker* linker, |
| 375 | uint32_t method_idx, |
| 376 | Handle<mirror::DexCache> dex_cache, |
| 377 | Handle<mirror::ClassLoader> class_loader, |
| 378 | bool add_to_queue) |
| 379 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 380 | |
David Srbecky | e3fc2d1 | 2018-11-30 13:41:14 +0000 | [diff] [blame] | 381 | // Compile the method if the number of samples passes a threshold. |
| 382 | // Returns false if we can not compile now - don't increment the counter and retry later. |
| 383 | bool MaybeCompileMethod(Thread* self, |
| 384 | ArtMethod* method, |
| 385 | uint32_t old_count, |
| 386 | uint32_t new_count, |
| 387 | bool with_backedges) |
| 388 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 389 | |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 390 | static bool BindCompilerMethods(std::string* error_msg); |
Mathieu Chartier | c1bc415 | 2016-03-24 17:22:52 -0700 | [diff] [blame] | 391 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 392 | // JIT compiler |
Mathieu Chartier | 72918ea | 2016-03-24 11:07:06 -0700 | [diff] [blame] | 393 | static void* jit_library_handle_; |
David Srbecky | 46b5353 | 2019-08-06 13:39:05 +0100 | [diff] [blame^] | 394 | static JitCompilerInterface* jit_compiler_; |
| 395 | static JitCompilerInterface* (*jit_load_)(void); |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 396 | template <typename T> static bool LoadSymbol(T*, const char* symbol, std::string* error_msg); |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 397 | |
Orion Hodson | ad28f5e | 2018-10-17 09:08:17 +0100 | [diff] [blame] | 398 | // JIT resources owned by runtime. |
| 399 | jit::JitCodeCache* const code_cache_; |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 400 | const JitOptions* const options_; |
| 401 | |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 402 | std::unique_ptr<ThreadPool> thread_pool_; |
Nicolas Geoffray | dc2fbb6 | 2019-04-11 22:55:50 +0100 | [diff] [blame] | 403 | std::vector<std::unique_ptr<OatDexFile>> type_lookup_tables_; |
Nicolas Geoffray | 47b9580 | 2018-05-16 15:42:17 +0100 | [diff] [blame] | 404 | |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 405 | // Performance monitoring. |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 406 | CumulativeLogger cumulative_timings_; |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 407 | Histogram<uint64_t> memory_use_ GUARDED_BY(lock_); |
| 408 | Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
Mathieu Chartier | a4885cb | 2015-03-09 15:38:54 -0700 | [diff] [blame] | 409 | |
Mathieu Chartier | 3130cdf | 2015-05-03 15:20:23 -0700 | [diff] [blame] | 410 | DISALLOW_COPY_AND_ASSIGN(Jit); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 411 | }; |
| 412 | |
Andreas Gampe | f149b3f | 2016-11-16 14:58:24 -0800 | [diff] [blame] | 413 | // Helper class to stop the JIT for a given scope. This will wait for the JIT to quiesce. |
| 414 | class ScopedJitSuspend { |
| 415 | public: |
| 416 | ScopedJitSuspend(); |
| 417 | ~ScopedJitSuspend(); |
| 418 | |
| 419 | private: |
| 420 | bool was_on_; |
| 421 | }; |
| 422 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 423 | } // namespace jit |
| 424 | } // namespace art |
| 425 | |
| 426 | #endif // ART_RUNTIME_JIT_JIT_H_ |