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