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