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